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/GetBookSeller/;
33 my ($searchstring)=@_;
34 my $dbh = C4::Context->dbh;
35 $searchstring=~ s/\'/\\\'/g;
36 my @data=split(' ',$searchstring);
37 my $sth=$dbh->prepare("Select * from aqcontract where (contractdescription like ? or contractname like ?) order by contractnumber");
38 $sth->execute("%$data[0]%","%$data[0]%");
40 while (my $row=$sth->fetchrow_hashref){
44 return (scalar(@results),\@results);
48 my $searchfield = $input->param('searchfield');
49 my $script_name = "/cgi-bin/koha/admin/aqcontract.pl";
50 my $contractnumber = $input->param('contractnumber');
51 my $op = $input->param('op');
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54 { template_name => "admin/aqcontract.tmpl",
58 flagsrequired => { acquisition => 'contracts_manage' },
64 script_name => $script_name,
65 contractnumber => $contractnumber,
66 searchfield => $searchfield
70 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or modify a record
71 if ( $op eq 'add_form' ) {
72 $template->param( add_form => 1 );
74 my @booksellerloop = GetBookSeller("");
76 #---- if primkey exists, it's a modify action, so read values to modify...
77 if ($contractnumber) {
78 my $dbh = C4::Context->dbh;
79 my $sth = $dbh->prepare("select * from aqcontract where contractnumber=?");
80 $sth->execute($contractnumber);
81 $data = $sth->fetchrow_hashref;
84 for my $bookseller (@booksellerloop) {
85 if ( $bookseller->{'id'} eq $data->{'booksellerid'} ) {
86 $bookseller->{'selected'} = 1;
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 booksellerloop => \@booksellerloop,
97 booksellerid => $data->{'booksellerid'},
98 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
101 # END $OP eq ADD_FORM
103 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
105 elsif ( $op eq 'add_validate' ) {
106 ## Please see file perltidy.ERR
107 $template->param( add_validate => 1 );
108 my $is_a_modif = $input->param("is_a_modif");
109 my $dbh = C4::Context->dbh;
111 my $sth = $dbh->prepare(
112 "UPDATE aqcontract SET contractstartdate=?,
115 contractdescription=?,
116 booksellerid=? WHERE contractnumber=?"
119 format_date_in_iso( $input->param('contractstartdate') ),
120 format_date_in_iso( $input->param('contractenddate') ),
121 $input->param('contractname'),
122 $input->param('contractdescription'),
123 $input->param('booksellerid'),
124 $input->param('contractnumber')
128 my $sth = $dbh->prepare("INSERT INTO aqcontract (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
130 $input->param('contractname'),
131 $input->param('contractdescription'),
132 $input->param('booksellerid'),
133 format_date_in_iso( $input->param('contractstartdate') ),
134 format_date_in_iso( $input->param('contractenddate') )
138 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl\"></html>";
141 # END $OP eq ADD_VALIDATE
143 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
145 elsif ( $op eq 'delete_confirm' ) {
146 $template->param( delete_confirm => 1 );
148 my $dbh = C4::Context->dbh;
149 my $sth = $dbh->prepare("select contractnumber,contractstartdate,contractenddate,
150 contractname,contractdescription,booksellerid
151 from aqcontract where contractnumber=?");
152 $sth->execute($contractnumber);
153 my $data = $sth->fetchrow_hashref;
156 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
157 my $sth2 = $dbh->prepare($query);
159 my $result = $sth2->fetchrow;
160 my $booksellername = $result;
163 contractnumber => $data->{'contractnumber'},
164 contractname => $data->{'contractname'},
165 contractdescription => $data->{'contractdescription'},
166 contractstartdate => format_date( $data->{'contractstartdate'} ),
167 contractenddate => format_date( $data->{'contractenddate'} ),
168 booksellerid => $data->{'booksellerid'},
169 booksellername => $booksellername,
172 # END $OP eq DELETE_CONFIRM
174 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
176 elsif ( $op eq 'delete_confirmed' ) {
177 $template->param( delete_confirmed => 1 );
178 my $dbh = C4::Context->dbh;
179 my $contractnumber = $input->param('contractnumber');
180 my $sth = $dbh->prepare("delete from aqcontract where contractnumber=?");
181 $sth->execute($contractnumber);
183 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl\"></html>";
186 # END $OP eq DELETE_CONFIRMED
187 # DEFAULT: Builds a list of contracts and displays them
189 $template->param(else => 1);
191 my ($count,$results)=StringSearch($searchfield);
193 for (my $i=0; $i < $count; $i++){
194 if ( ($input->param('booksellerid') && $results->[$i]{'booksellerid'} == $input->param('booksellerid')) || ! $input->param('booksellerid') ) {
195 my %row = (contractnumber => $results->[$i]{'contractnumber'},
196 contractname => $results->[$i]{'contractname'},
197 contractdescription => $results->[$i]{'contractdescription'},
198 contractstartdate => format_date($results->[$i]{'contractstartdate'}),
199 contractenddate => format_date($results->[$i]{'contractenddate'}),
200 booksellerid => $results->[$i]{'booksellerid'},
213 for my $contract (@loop) {
214 my $dbh = C4::Context->dbh;
215 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $contract->{'booksellerid'}";
216 my $sth =$dbh->prepare($query);
218 my $result=$sth->fetchrow;
219 $contract->{'booksellername'}=$result;
221 $template->param(loop => \@loop);
222 } #---- END $OP eq DEFAULT
223 output_html_with_http_headers $input, $cookie, $template->output;