3 #script to administer the contract table
4 #written 02/09/2008 by john.soros@biblibre.com
6 # Copyright 2008-2009 BibLibre SARL
8 # This file is part of Koha.
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
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.
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
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Bookseller qw/GetBookSellerFromId/;
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]%");
41 while (my $row=$sth->fetchrow_hashref){
45 return (scalar(@results),\@results);
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') || '';
55 my @bookseller = GetBookSellerFromId($booksellerid);
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
58 { template_name => "admin/aqcontract.tmpl",
62 flagsrequired => { acquisition => 'contracts_manage' },
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(),
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 );
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;
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(),
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;
108 my $sth = $dbh->prepare(
109 "UPDATE aqcontract SET contractstartdate=?,
112 contractdescription=?,
113 booksellerid=? WHERE contractnumber=?"
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')
125 my $sth = $dbh->prepare("INSERT INTO aqcontract (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
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') )
135 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
138 # END $OP eq ADD_VALIDATE
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 );
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;
152 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
153 my $sth2 = $dbh->prepare($query);
155 my $result = $sth2->fetchrow;
156 my $booksellername = $result;
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,
168 # END $OP eq DELETE_CONFIRM
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);
178 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
181 # END $OP eq DELETE_CONFIRMED
183 # DEFAULT: Builds a list of contracts and displays them
185 $template->param(else => 1);
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') ) {
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'},
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);
205 my $result=$sth->fetchrow;
206 $contract->{'booksellername'}=$result;
208 $template->param(loop => \@loop);
209 } #---- END $OP eq DEFAULT
210 output_html_with_http_headers $input, $cookie, $template->output;