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') || '';
54 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 name => $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 );
82 #---- if primkey exists, it's a modify action, so read values to modify...
83 if ($contractnumber) {
84 my $dbh = C4::Context->dbh;
85 my $sth = $dbh->prepare("select * from aqcontract where contractnumber=?");
86 $sth->execute($contractnumber);
87 $data = $sth->fetchrow_hashref;
91 $booksellername = $$_{name} if $$_{id} eq $$data{booksellerid};
95 $booksellername = $$_{name} if $$_{id} eq $booksellerid;
99 contractnumber => $data->{'contractnumber'},
100 contractname => $data->{'contractname'},
101 contractdescription => $data->{'contractdescription'},
102 contractstartdate => format_date( $data->{'contractstartdate'} ),
103 contractenddate => format_date( $data->{'contractenddate'} ),
104 booksellername => $booksellername,
105 booksellerid => $data->{'booksellerid'},
106 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
109 # END $OP eq ADD_FORM
111 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
113 elsif ( $op eq 'add_validate' ) {
114 ## Please see file perltidy.ERR
115 $template->param( add_validate => 1 );
116 my $is_a_modif = $input->param("is_a_modif");
117 my $dbh = C4::Context->dbh;
119 my $sth = $dbh->prepare(
120 "UPDATE aqcontract SET contractstartdate=?,
123 contractdescription=?,
124 booksellerid=? WHERE contractnumber=?"
127 format_date_in_iso( $input->param('contractstartdate') ),
128 format_date_in_iso( $input->param('contractenddate') ),
129 $input->param('contractname'),
130 $input->param('contractdescription'),
131 $input->param('booksellerid'),
132 $input->param('contractnumber')
136 my $sth = $dbh->prepare("INSERT INTO aqcontract (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
138 $input->param('contractname'),
139 $input->param('contractdescription'),
140 $input->param('booksellerid'),
141 format_date_in_iso( $input->param('contractstartdate') ),
142 format_date_in_iso( $input->param('contractenddate') )
146 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
149 # END $OP eq ADD_VALIDATE
151 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
153 elsif ( $op eq 'delete_confirm' ) {
154 $template->param( delete_confirm => 1 );
156 my $dbh = C4::Context->dbh;
157 my $sth = $dbh->prepare("select contractnumber,contractstartdate,contractenddate,
158 contractname,contractdescription,booksellerid
159 from aqcontract where contractnumber=?");
160 $sth->execute($contractnumber);
161 my $data = $sth->fetchrow_hashref;
164 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
165 my $sth2 = $dbh->prepare($query);
167 my $result = $sth2->fetchrow;
168 my $booksellername = $result;
171 contractnumber => $data->{'contractnumber'},
172 contractname => $data->{'contractname'},
173 contractdescription => $data->{'contractdescription'},
174 contractstartdate => format_date( $data->{'contractstartdate'} ),
175 contractenddate => format_date( $data->{'contractenddate'} ),
176 booksellerid => $data->{'booksellerid'},
177 booksellername => $booksellername,
180 # END $OP eq DELETE_CONFIRM
182 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
184 elsif ( $op eq 'delete_confirmed' ) {
185 $template->param( delete_confirmed => 1 );
186 my $dbh = C4::Context->dbh;
187 my $contractnumber = $input->param('contractnumber');
188 my $sth = $dbh->prepare("delete from aqcontract where contractnumber=?");
189 $sth->execute($contractnumber);
191 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
194 # END $OP eq DELETE_CONFIRMED
195 # DEFAULT: Builds a list of contracts and displays them
197 $template->param(else => 1);
199 my ($count,$results)=StringSearch($searchfield);
201 for (my $i=0; $i < $count; $i++){
202 if ( ($input->param('booksellerid') && $results->[$i]{'booksellerid'} == $input->param('booksellerid')) || ! $input->param('booksellerid') ) {
203 my %row = (contractnumber => $results->[$i]{'contractnumber'},
204 contractname => $results->[$i]{'contractname'},
205 contractdescription => $results->[$i]{'contractdescription'},
206 contractstartdate => format_date($results->[$i]{'contractstartdate'}),
207 contractenddate => format_date($results->[$i]{'contractenddate'}),
208 booksellerid => $results->[$i]{'booksellerid'},
221 for my $contract (@loop) {
222 my $dbh = C4::Context->dbh;
223 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $contract->{'booksellerid'}";
224 my $sth =$dbh->prepare($query);
226 my $result=$sth->fetchrow;
227 $contract->{'booksellername'}=$result;
229 $template->param(loop => \@loop);
230 } #---- END $OP eq DEFAULT
231 output_html_with_http_headers $input, $cookie, $template->output;