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 );
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;
89 for my $bookseller (@bookseller) {
90 if ( $bookseller->{'id'} eq $data->{'booksellerid'} ) {
91 $bookseller->{'selected'} = 1;
95 for my $bookseller (@bookseller) {
96 if ( $bookseller->{'id'} eq $booksellerid ) {
97 $bookseller->{'selected'} = 1;
102 contractnumber => $data->{'contractnumber'},
103 contractname => $data->{'contractname'},
104 contractdescription => $data->{'contractdescription'},
105 contractstartdate => format_date( $data->{'contractstartdate'} ),
106 contractenddate => format_date( $data->{'contractenddate'} ),
107 booksellerloop => \@bookseller,
108 booksellerid => $data->{'booksellerid'},
109 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
112 # END $OP eq ADD_FORM
114 #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
116 elsif ( $op eq 'add_validate' ) {
117 ## Please see file perltidy.ERR
118 $template->param( add_validate => 1 );
119 my $is_a_modif = $input->param("is_a_modif");
120 my $dbh = C4::Context->dbh;
122 my $sth = $dbh->prepare(
123 "UPDATE aqcontract SET contractstartdate=?,
126 contractdescription=?,
127 booksellerid=? WHERE contractnumber=?"
130 format_date_in_iso( $input->param('contractstartdate') ),
131 format_date_in_iso( $input->param('contractenddate') ),
132 $input->param('contractname'),
133 $input->param('contractdescription'),
134 $input->param('booksellerid'),
135 $input->param('contractnumber')
139 my $sth = $dbh->prepare("INSERT INTO aqcontract (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
141 $input->param('contractname'),
142 $input->param('contractdescription'),
143 $input->param('booksellerid'),
144 format_date_in_iso( $input->param('contractstartdate') ),
145 format_date_in_iso( $input->param('contractenddate') )
149 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
152 # END $OP eq ADD_VALIDATE
154 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
156 elsif ( $op eq 'delete_confirm' ) {
157 $template->param( delete_confirm => 1 );
159 my $dbh = C4::Context->dbh;
160 my $sth = $dbh->prepare("select contractnumber,contractstartdate,contractenddate,
161 contractname,contractdescription,booksellerid
162 from aqcontract where contractnumber=?");
163 $sth->execute($contractnumber);
164 my $data = $sth->fetchrow_hashref;
167 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
168 my $sth2 = $dbh->prepare($query);
170 my $result = $sth2->fetchrow;
171 my $booksellername = $result;
174 contractnumber => $data->{'contractnumber'},
175 contractname => $data->{'contractname'},
176 contractdescription => $data->{'contractdescription'},
177 contractstartdate => format_date( $data->{'contractstartdate'} ),
178 contractenddate => format_date( $data->{'contractenddate'} ),
179 booksellerid => $data->{'booksellerid'},
180 booksellername => $booksellername,
183 # END $OP eq DELETE_CONFIRM
185 #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
187 elsif ( $op eq 'delete_confirmed' ) {
188 $template->param( delete_confirmed => 1 );
189 my $dbh = C4::Context->dbh;
190 my $contractnumber = $input->param('contractnumber');
191 my $sth = $dbh->prepare("delete from aqcontract where contractnumber=?");
192 $sth->execute($contractnumber);
194 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
197 # END $OP eq DELETE_CONFIRMED
198 # DEFAULT: Builds a list of contracts and displays them
200 $template->param(else => 1);
202 my ($count,$results)=StringSearch($searchfield);
204 for (my $i=0; $i < $count; $i++){
205 if ( ($input->param('booksellerid') && $results->[$i]{'booksellerid'} == $input->param('booksellerid')) || ! $input->param('booksellerid') ) {
206 my %row = (contractnumber => $results->[$i]{'contractnumber'},
207 contractname => $results->[$i]{'contractname'},
208 contractdescription => $results->[$i]{'contractdescription'},
209 contractstartdate => format_date($results->[$i]{'contractstartdate'}),
210 contractenddate => format_date($results->[$i]{'contractenddate'}),
211 booksellerid => $results->[$i]{'booksellerid'},
224 for my $contract (@loop) {
225 my $dbh = C4::Context->dbh;
226 my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $contract->{'booksellerid'}";
227 my $sth =$dbh->prepare($query);
229 my $result=$sth->fetchrow;
230 $contract->{'booksellername'}=$result;
232 $template->param(loop => \@loop);
233 } #---- END $OP eq DEFAULT
234 output_html_with_http_headers $input, $cookie, $template->output;