0d9a8583dc6098313a05c0c8c7f5cabc4b82d50a
[koha.git] / admin / aqcontract.pl
1 #!/usr/bin/perl
2
3 #script to administer the contract table
4 #written 02/09/2008 by john.soros@biblibre.com
5
6 # Copyright 2008-2009 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use warnings;
25 use CGI;
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Bookseller qw/GetBookSellerFromId/;
31
32 sub StringSearch  {
33     my ($searchstring)=@_;
34     my $dbh = C4::Context->dbh;
35     $searchstring=~ s/\'/\\\'/g;
36     my @data=split(' ',$searchstring);
37     $data[0]='' unless $data[0];
38     my $sth=$dbh->prepare("Select * from aqcontract where (contractdescription like ? or contractname like ?) order by contractnumber");
39     $sth->execute("%$data[0]%","%$data[0]%");
40     my @results;
41     while (my $row=$sth->fetchrow_hashref){
42         push(@results,$row);
43     }
44     $sth->finish;
45     return (scalar(@results),\@results);
46 }
47
48 my $input          = new CGI;
49 my $searchfield    = $input->param('searchfield') || '';
50 my $script_name    = "/cgi-bin/koha/admin/aqcontract.pl";
51 my $contractnumber = $input->param('contractnumber');
52 my $booksellerid   = $input->param('booksellerid');
53 my $op             = $input->param('op') || '';
54
55 my @bookseller = GetBookSellerFromId($booksellerid);
56
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
58     {   template_name   => "admin/aqcontract.tmpl",
59         query           => $input,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { acquisition => 'contracts_manage' },
63         debug           => 1,
64     }
65 );
66
67 $template->param(
68     script_name    => $script_name,
69     contractnumber => $contractnumber,
70     searchfield    => $searchfield, 
71     booksellerid   => $booksellerid,
72     booksellername => $bookseller[0]->{name},
73     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
74 );
75
76 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
77 if ( $op eq 'add_form' ) {
78     $template->param( add_form => 1 );
79     my $data;
80
81     #---- if primkey exists, it's a modify action, so read values to modify...
82     if ($contractnumber) {
83         my $dbh = C4::Context->dbh;
84         my $sth = $dbh->prepare("select * from aqcontract where contractnumber=?");
85         $sth->execute($contractnumber);
86         $data = $sth->fetchrow_hashref;
87         $sth->finish;
88     }
89
90     $template->param(
91         contractnumber           => $data->{'contractnumber'},
92         contractname             => $data->{'contractname'},
93         contractdescription      => $data->{'contractdescription'},
94         contractstartdate        => format_date( $data->{'contractstartdate'} ),
95         contractenddate          => format_date( $data->{'contractenddate'} ),
96         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
97     );
98
99     # END $OP eq ADD_FORM
100 }
101 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
102 elsif ( $op eq 'add_validate' ) {
103 ## Please see file perltidy.ERR
104     $template->param( add_validate => 1 );
105     my $is_a_modif = $input->param("is_a_modif");
106     my $dbh        = C4::Context->dbh;
107     if ($is_a_modif) {
108         my $sth = $dbh->prepare(
109             "UPDATE aqcontract SET contractstartdate=?,
110             contractenddate=?,
111             contractname=?,
112             contractdescription=?,
113             booksellerid=? WHERE contractnumber=?"
114         );
115         $sth->execute(
116             format_date_in_iso( $input->param('contractstartdate') ),
117             format_date_in_iso( $input->param('contractenddate') ),
118             $input->param('contractname'),
119             $input->param('contractdescription'),
120             $input->param('booksellerid'),
121             $input->param('contractnumber')
122         );
123         $sth->finish;
124     } else {
125         my $sth = $dbh->prepare("INSERT INTO aqcontract  (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
126         $sth->execute(
127             $input->param('contractname'),
128             $input->param('contractdescription'),
129             $input->param('booksellerid'),
130             format_date_in_iso( $input->param('contractstartdate') ),
131             format_date_in_iso( $input->param('contractenddate') )
132         );
133         $sth->finish;
134     }
135     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
136     exit;
137
138     # END $OP eq ADD_VALIDATE
139 }
140 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
141 elsif ( $op eq 'delete_confirm' ) {
142     $template->param( delete_confirm => 1 );
143
144     my $dbh = C4::Context->dbh;
145     my $sth = $dbh->prepare("select contractnumber,contractstartdate,contractenddate,
146                                 contractname,contractdescription,booksellerid 
147                             from aqcontract where contractnumber=?");
148     $sth->execute($contractnumber);
149     my $data = $sth->fetchrow_hashref;
150     $sth->finish;
151
152     my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
153     my $sth2  = $dbh->prepare($query);
154     $sth2->execute;
155     my $result         = $sth2->fetchrow;
156     my $booksellername = $result;
157
158     $template->param(
159         contractnumber      => $data->{'contractnumber'},
160         contractname        => $data->{'contractname'},
161         contractdescription => $data->{'contractdescription'},
162         contractstartdate   => format_date( $data->{'contractstartdate'} ),
163         contractenddate     => format_date( $data->{'contractenddate'} ),
164         booksellerid        => $data->{'booksellerid'},
165         booksellername      => $booksellername,
166     );
167
168     # END $OP eq DELETE_CONFIRM
169 }
170 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
171 elsif ( $op eq 'delete_confirmed' ) {
172     $template->param( delete_confirmed => 1 );
173     my $dbh            = C4::Context->dbh;
174     my $contractnumber = $input->param('contractnumber');
175     my $sth            = $dbh->prepare("delete from aqcontract where contractnumber=?");
176     $sth->execute($contractnumber);
177     $sth->finish;
178     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
179     exit;
180
181     # END $OP eq DELETE_CONFIRMED
182 }
183 # DEFAULT: Builds a list of contracts and displays them
184 else {
185     $template->param(else => 1);
186     my @loop;
187     my ($count,$results)=StringSearch($searchfield);
188     for (my $i=0; $i < $count; $i++){
189         if ( ($input->param('booksellerid') && $results->[$i]{'booksellerid'} == $input->param('booksellerid')) || ! $input->param('booksellerid') ) {
190             push @loop, {
191                 contractnumber => $results->[$i]{'contractnumber'},
192                 contractname => $results->[$i]{'contractname'},
193                 contractdescription => $results->[$i]{'contractdescription'},
194                 contractstartdate => format_date($results->[$i]{'contractstartdate'}),
195                 contractenddate => format_date($results->[$i]{'contractenddate'}),
196                 booksellerid => $results->[$i]{'booksellerid'},
197             };
198         }
199     }
200     for my $contract (@loop) {
201         my $dbh = C4::Context->dbh;
202         my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $contract->{'booksellerid'}";
203         my $sth =$dbh->prepare($query);
204         $sth->execute;
205         my $result=$sth->fetchrow;
206         $contract->{'booksellername'}=$result;
207     }
208     $template->param(loop => \@loop);
209 } #---- END $OP eq DEFAULT
210 output_html_with_http_headers $input, $cookie, $template->output;