[MT1234] Fixed aqcontract breadcrumb
[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     my $booksellername;
81
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;
88         $sth->finish;
89
90         for ( @bookseller ) {
91             $booksellername = $$_{name} if $$_{id} eq $$data{booksellerid};
92         }
93     } else {
94         for ( @bookseller ) {
95             $booksellername = $$_{name} if $$_{id} eq $booksellerid;
96         }
97     }
98
99     $template->param(
100         contractnumber           => $data->{'contractnumber'},
101         contractname             => $data->{'contractname'},
102         contractdescription      => $data->{'contractdescription'},
103         contractstartdate        => format_date( $data->{'contractstartdate'} ),
104         contractenddate          => format_date( $data->{'contractenddate'} ),
105         booksellername           => $booksellername,
106         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
107     );
108
109     # END $OP eq ADD_FORM
110
111     #ADD_VALIDATE: called by add_form, used to insert/modify data in DB
112 }
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;
118   if ($is_a_modif) {
119       my $sth = $dbh->prepare(
120           "UPDATE aqcontract SET contractstartdate=?,
121                 contractenddate=?,
122                 contractname=?,
123                 contractdescription=?,
124                 booksellerid=? WHERE contractnumber=?"
125       );
126       $sth->execute(
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')
133       );
134       $sth->finish;
135   } else {
136       my $sth = $dbh->prepare("INSERT INTO aqcontract  (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)");
137       $sth->execute(
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') )
143       );
144       $sth->finish;
145   }
146   print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
147   exit;
148
149   # END $OP eq ADD_VALIDATE
150
151 #DELETE_CONFIRM: called by default form, used to confirm deletion of data in DB
152 }
153 elsif ( $op eq 'delete_confirm' ) {
154     $template->param( delete_confirm => 1 );
155
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;
162     $sth->finish;
163
164     my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}";
165     my $sth2  = $dbh->prepare($query);
166     $sth2->execute;
167     my $result         = $sth2->fetchrow;
168     my $booksellername = $result;
169
170     $template->param(
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,
178     );
179
180     # END $OP eq DELETE_CONFIRM
181
182     #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB
183 }
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);
190     $sth->finish;
191     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqcontract.pl?booksellerid=$booksellerid\"></html>";
192     exit;
193
194     # END $OP eq DELETE_CONFIRMED
195     # DEFAULT: Builds a list of contracts and displays them
196 } else {
197     $template->param(else => 1);
198     my @loop;
199     my ($count,$results)=StringSearch($searchfield);
200     my $toggle = 0;
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'},
209                     toggle => $toggle );
210             push @loop, \%row;
211             if ( $toggle eq 0 )
212             {
213                 $toggle = 1;
214             }
215             else
216             {
217                 $toggle = 0;
218             }
219         }
220     }
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);
225         $sth->execute;
226         my $result=$sth->fetchrow;
227         $contract->{'booksellername'}=$result;
228     }
229     $template->param(loop => \@loop);
230 } #---- END $OP eq DEFAULT
231 output_html_with_http_headers $input, $cookie, $template->output;