[replace previous] fix for 3612 (bookseller improvements)
[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 my @bookseller = GetBookSellerFromId("$booksellerid");
55
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     name           => $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         for my $bookseller (@bookseller) {
90             if ( $bookseller->{'id'} eq $data->{'booksellerid'} ) {
91                 $bookseller->{'selected'} = 1;
92             }
93         }
94     } else {
95         for my $bookseller (@bookseller) {
96             if ( $bookseller->{'id'} eq $booksellerid ) {
97                 $bookseller->{'selected'} = 1;
98             }
99         }
100     }
101     $template->param(
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(),
110     );
111
112     # END $OP eq ADD_FORM
113
114     #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
115 }
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;
121   if ($is_a_modif) {
122       my $sth = $dbh->prepare(
123           "UPDATE aqcontract SET contractstartdate=?,
124                 contractenddate=?,
125                 contractname=?,
126                 contractdescription=?,
127                 booksellerid=? WHERE contractnumber=?"
128       );
129       $sth->execute(
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')
136       );
137       $sth->finish;
138   } else {
139       my $sth = $dbh->prepare("INSERT INTO aqcontract  (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
140       $sth->execute(
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') )
146       );
147       $sth->finish;
148   }
149   print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
150   exit;
151
152   # END $OP eq ADD_VALIDATE
153
154 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
155 }
156 elsif ( $op eq 'delete_confirm' ) {
157     $template->param( delete_confirm => 1 );
158
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;
165     $sth->finish;
166
167     my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
168     my $sth2  = $dbh->prepare($query);
169     $sth2->execute;
170     my $result         = $sth2->fetchrow;
171     my $booksellername = $result;
172
173     $template->param(
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,
181     );
182
183     # END $OP eq DELETE_CONFIRM
184
185     #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
186 }
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);
193     $sth->finish;
194     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
195     exit;
196
197     # END $OP eq DELETE_CONFIRMED
198     # DEFAULT: Builds a list of contracts and displays them
199 } else {
200     $template->param(else => 1);
201     my @loop;
202     my ($count,$results)=StringSearch($searchfield);
203     my $toggle = 0;
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'},
212                     toggle => $toggle );
213             push @loop, \%row;
214             if ( $toggle eq 0 )
215             {
216                 $toggle = 1;
217             }
218             else
219             {
220                 $toggle = 0;
221             }
222         }
223     }
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);
228         $sth->execute;
229         my $result=$sth->fetchrow;
230         $contract->{'booksellername'}=$result;
231     }
232     $template->param(loop => \@loop);
233 } #---- END $OP eq DEFAULT
234 output_html_with_http_headers $input, $cookie, $template->output;