Fix unimarc 010 field plugin
[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/GetBookSeller/;
31
32 sub StringSearch  {
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]%");
39     my @results;
40     while (my $row=$sth->fetchrow_hashref){
41         push(@results,$row);
42     }
43     $sth->finish;
44     return (scalar(@results),\@results);
45 }
46
47 my $input          = new CGI;
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');
52
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {   template_name   => "admin/aqcontract.tmpl",
55         query           => $input,
56         type            => "intranet",
57         authnotrequired => 0,
58         flagsrequired   => { acquisition => 'contracts_manage' },
59         debug           => 1,
60     }
61 );
62
63 $template->param(
64     script_name    => $script_name,
65     contractnumber => $contractnumber,
66     searchfield    => $searchfield
67 );
68
69
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 );
73     my $data;
74     my @booksellerloop = GetBookSeller("");
75
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;
82         $sth->finish;
83
84         for my $bookseller (@booksellerloop) {
85             if ( $bookseller->{'id'} eq $data->{'booksellerid'} ) {
86                 $bookseller->{'selected'} = 1;
87             }
88         }
89     }
90     $template->param(
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(),
99     );
100
101     # END $OP eq ADD_FORM
102
103     #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
104 }
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;
110   if ($is_a_modif) {
111       my $sth = $dbh->prepare(
112           "UPDATE aqcontract SET contractstartdate=?,
113                 contractenddate=?,
114                 contractname=?,
115                 contractdescription=?,
116                 booksellerid=? WHERE contractnumber=?"
117       );
118       $sth->execute(
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')
125       );
126       $sth->finish;
127   } else {
128       my $sth = $dbh->prepare("INSERT INTO aqcontract  (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
129       $sth->execute(
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') )
135       );
136       $sth->finish;
137   }
138   print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl\"></html>";
139   exit;
140
141   # END $OP eq ADD_VALIDATE
142
143 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
144 }
145 elsif ( $op eq 'delete_confirm' ) {
146     $template->param( delete_confirm => 1 );
147
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;
154     $sth->finish;
155
156     my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
157     my $sth2  = $dbh->prepare($query);
158     $sth2->execute;
159     my $result         = $sth2->fetchrow;
160     my $booksellername = $result;
161
162     $template->param(
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,
170     );
171
172     # END $OP eq DELETE_CONFIRM
173
174     #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
175 }
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);
182     $sth->finish;
183     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl\"></html>";
184     exit;
185
186     # END $OP eq DELETE_CONFIRMED
187     # DEFAULT: Builds a list of contracts and displays them
188 } else {
189     $template->param(else => 1);
190     my @loop;
191     my ($count,$results)=StringSearch($searchfield);
192     my $toggle = 0;
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'},
201                     toggle => $toggle );
202             push @loop, \%row;
203             if ( $toggle eq 0 )
204             {
205                 $toggle = 1;
206             }
207             else
208             {
209                 $toggle = 0;
210             }
211         }
212     }
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);
217         $sth->execute;
218         my $result=$sth->fetchrow;
219         $contract->{'booksellername'}=$result;
220     }
221     $template->param(loop => \@loop);
222 } #---- END $OP eq DEFAULT
223 output_html_with_http_headers $input, $cookie, $template->output;