Savannah seems out of synch.Code cleaning removal of Date::Manip
[koha.git] / admin / aqbudget.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use List::Util qw/min/;
43 use C4::Date;
44 use C4::Auth;
45 use C4::Acquisition;
46 use C4::Context;
47 use C4::Interface::CGI::Output;
48 use C4::Search;
49 use C4::Koha;
50 use C4::Output;
51
52 my $input = new CGI;
53 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
54 my $bookfundid=$input->param('bookfundid');
55 my $aqbudgetid=$input->param('aqbudgetid');
56 my $pagesize = 20;
57 my $op = $input->param('op');
58
59 my ($template, $borrowernumber, $cookie)
60     = get_template_and_user(
61         {template_name => "admin/aqbudget.tmpl",
62          query => $input,
63          type => "intranet",
64          authnotrequired => 0,
65          flagsrequired => {parameters => 1},
66          debug => 1,
67      }
68     );
69
70 $template->param(
71     action => $script_name,
72     DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
73     script_name => $script_name,
74     $op || 'else' => 1,
75 );
76
77 my $dbh = C4::Context->dbh;
78 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
79 $sthtemp->execute($borrowernumber);
80 my ($flags, $homebranch)=$sthtemp->fetchrow;
81
82 ################## ADD_FORM ##################################
83 # called by default. Used to create form to add or  modify a record
84 if ($op eq 'add_form') {
85     my ($query, $dataaqbudget, $dataaqbookfund, $sth);
86     my $dbh = C4::Context->dbh;
87
88     #---- if primkey exists, it's a modify action, so read values to modify...
89     if ($aqbudgetid) {
90         $query = '
91 SELECT aqbudgetid,
92        bookfundname,
93        aqbookfund.bookfundid,
94        startdate,
95        enddate,
96        budgetamount,
97        aqbudget.branchcode
98   FROM aqbudget
99     INNER JOIN aqbookfund ON aqbudget.bookfundid = aqbookfund.bookfundid
100   WHERE aqbudgetid = ?
101 ';
102         $sth=$dbh->prepare($query);
103         $sth->execute($aqbudgetid);
104         $dataaqbudget=$sth->fetchrow_hashref;
105         $sth->finish;
106     }
107
108     $query = '
109 SELECT aqbookfund.branchcode,
110        branches.branchname,
111        aqbookfund.bookfundname
112   FROM aqbookfund
113     LEFT JOIN branches ON aqbookfund.branchcode = branches.branchcode
114   WHERE bookfundid = ?
115 ';
116     $sth=$dbh->prepare($query);
117     $sth->execute(
118         defined $aqbudgetid ? $dataaqbudget->{bookfundid} : $bookfundid,
119     );
120     $dataaqbookfund=$sth->fetchrow_hashref;
121     $sth->finish;
122
123     if (defined $aqbudgetid) {
124         $template->param(
125             bookfundid => $dataaqbudget->{'bookfundid'},
126             bookfundname => $dataaqbudget->{'bookfundname'}
127         );
128     }
129     else {
130         $template->param(
131             bookfundid => $bookfundid,
132             bookfundname => $dataaqbookfund->{bookfundname},
133         );
134     }
135
136     # Available branches
137     my @branches = ();
138
139     $query = '
140 SELECT branchcode,
141        branchname
142   FROM branches
143   ORDER BY branchname
144 ';
145     $sth=$dbh->prepare($query);
146     $sth->execute();
147     while (my $row = $sth->fetchrow_hashref) {
148         my $branch = $row;
149
150         if (defined $dataaqbookfund->{branchcode}) {
151             $branch->{selected} =
152                 $dataaqbookfund->{branchcode} eq $row->{branchcode} ? 1 : 0;
153         }
154         elsif (defined $aqbudgetid) {
155             $branch->{selected} =
156                 $dataaqbudget->{branchcode} eq $row->{branchcode} ? 1 : 0;
157         }
158
159         push @branches, $branch;
160     }
161     $sth->finish;
162
163     $template->param(
164         dateformat => display_date_format(),
165         aqbudgetid => $dataaqbudget->{'aqbudgetid'},
166         startdate => format_date($dataaqbudget->{'startdate'}),
167         enddate => format_date($dataaqbudget->{'enddate'}),
168         budgetamount => $dataaqbudget->{'budgetamount'},
169         branches => \@branches,
170     );
171
172     if (defined $dataaqbookfund->{branchcode}) {
173         $template->param(
174             disable_branchselection => 1,
175             branch => $dataaqbookfund->{branchcode},
176         );
177     }
178                                                                                                         # END $OP eq ADD_FORM
179 ################## ADD_VALIDATE ##################################
180 # called by add_form, used to insert/modify data in DB
181 } elsif ($op eq 'add_validate') {
182     my ($query, $sth);
183
184     if (defined $aqbudgetid) {
185         $query = '
186 UPDATE aqbudget
187   SET bookfundid = ?,
188       startdate = ?,
189       enddate = ?,
190       budgetamount = ?,
191       branchcode = ?
192   WHERE aqbudgetid = ?
193 ';
194         $sth=$dbh->prepare($query);
195         $sth->execute(
196             $input->param('bookfundid'),
197             format_date_in_iso($input->param('startdate')),
198             format_date_in_iso($input->param('enddate')),
199             $input->param('budgetamount'),
200             $input->param('branch') || undef,
201             $aqbudgetid,
202         );
203         $sth->finish;
204     }
205     else {
206         $query = '
207 INSERT
208   INTO aqbudget
209   (bookfundid, startdate, enddate, budgetamount, branchcode)
210   VALUES
211   (?, ?, ?, ?, ?)
212 ';
213         $sth=$dbh->prepare($query);
214         $sth->execute(
215             $input->param('bookfundid'),
216             format_date_in_iso($input->param('startdate')),
217             format_date_in_iso($input->param('enddate')),
218             $input->param('budgetamount'),
219             $input->param('branch') || undef,
220         );
221         $sth->finish;
222     }
223
224     $input->redirect("aqbudget.pl");
225
226 # END $OP eq ADD_VALIDATE
227 ################## DELETE_CONFIRM ##################################
228 # called by default form, used to confirm deletion of data in DB
229 } elsif ($op eq 'delete_confirm') {
230         my $dbh = C4::Context->dbh;
231         my $sth=$dbh->prepare("select aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudgetid=?");
232         $sth->execute($aqbudgetid);
233         my $data=$sth->fetchrow_hashref;
234         $sth->finish;
235         $template->param(bookfundid => $bookfundid);
236         $template->param(aqbudgetid => $data->{'aqbudgetid'});
237         $template->param(startdate => format_date($data->{'startdate'}));
238         $template->param(enddate => format_date($data->{'enddate'}));
239         $template->param(budgetamount => $data->{'budgetamount'});
240                                                                                                         # END $OP eq DELETE_CONFIRM
241 ################## DELETE_CONFIRMED ##################################
242 # called by delete_confirm, used to effectively confirm deletion of data in DB
243 } elsif ($op eq 'delete_confirmed') {
244         my $dbh = C4::Context->dbh;
245         my $aqbudgetid=uc($input->param('aqbudgetid'));
246         my $sth=$dbh->prepare("delete from aqbudget where aqbudgetid=?");
247         $sth->execute($aqbudgetid);
248         $sth->finish;
249          print $input->redirect("aqbookfund.pl");
250          return;
251                                                                                                         # END $OP eq DELETE_CONFIRMED
252 ################## DEFAULT ##################################
253 } else { # DEFAULT
254     my ($query, $sth);
255
256     # create a look-up table for bookfund names from bookfund ids,
257     # instead of having on query per budget
258     my %bookfundname_of = ();
259     $query = '
260 SELECT bookfundid, bookfundname
261   FROM aqbookfund
262 ';
263     $sth=$dbh->prepare($query);
264     $sth->execute;
265     while (my $row = $sth->fetchrow_hashref) {
266         $bookfundname_of{ $row->{bookfundid} } = $row->{bookfundname};
267     }
268     $sth->finish;
269
270     # filters
271     my $branches = GetBranches();
272     my @branchloop;
273     foreach my $branchcode (sort keys %{$branches}) {
274         my $row = {
275             code => $branchcode,
276             name => $branches->{$branchcode}->{branchname},
277         };
278
279         if (defined $input->param('filter_branchcode')
280             and $input->param('filter_branchcode') eq $branchcode) {
281             $row->{selected} = 1;
282         }
283
284         push @branchloop, $row;
285     }
286
287     my @bookfundids_loop;
288     $query = '
289 SELECT bookfundid
290   FROM aqbookfund
291 ';
292     $sth = $dbh->prepare($query);
293     $sth->execute();
294     while (my $row = $sth->fetchrow_hashref) {
295         if (defined $input->param('filter_bookfundid')
296             and $input->param('filter_bookfundid') eq $row->{bookfundid}) {
297             $row->{selected} = 1;
298         }
299
300         push @bookfundids_loop, $row;
301     }
302     $sth->finish;
303
304     $template->param(
305         filter_bookfundids => \@bookfundids_loop,
306         filter_branches => \@branchloop,
307         filter_amount => $input->param('filter_amount') || undef,
308         filter_startdate => $input->param('filter_startdate') || undef,
309         filter_enddate => $input->param('filter_enddate') || undef,
310     );
311
312     my %sign_label_of = (
313         '=' => 'equal',
314         '>=' => 'superior',
315         '<=' => 'inferior',
316     );
317
318     foreach my $field (qw/startdate enddate amount/) {
319         my $param = 'filter_'.$field.'_sign';
320
321         foreach my $sign (keys %sign_label_of) {
322             if ($input->param($param) eq $sign) {
323                 $template->param(
324                     $param.'_'.$sign_label_of{$sign}.'_selected' => 1,
325                 );
326             }
327         }
328     }
329
330     # Search all available budgets
331     $query = '
332 SELECT aqbudgetid,
333        bookfundid,
334        startdate,
335        enddate,
336        budgetamount,
337        branchcode
338   FROM aqbudget
339   WHERE 1 = 1';
340
341     my @bindings;
342
343     if ($input->param('filter_bookfundid')) {
344         $query.= '
345     AND bookfundid = ?
346 ';
347         push @bindings, $input->param('filter_bookfundid');
348     }
349     if ($input->param('filter_branchcode')) {
350         $query.= '
351     AND branchcode = ?
352 ';
353         push @bindings, $input->param('filter_branchcode');
354     }
355     if ($input->param('filter_startdate')) {
356         $query.= '
357     AND startdate '.$input->param('filter_startdate_sign').' ?
358 ';
359         push @bindings, format_date_in_iso($input->param('filter_startdate'));
360     }
361     if ($input->param('filter_enddate')) {
362         $query.= '
363     AND enddate '.$input->param('filter_enddate_sign').' ?
364 ';
365         push @bindings, format_date_in_iso($input->param('filter_enddate'));
366     }
367     if ($input->param('filter_amount')) {
368         $query.= '
369     AND budgetamount '.$input->param('filter_amount_sign').' ?
370 ';
371         # the amount must be a quantity, with 2 digits after the decimal
372         # separator
373         $input->param('filter_amount') =~ m{(\d* (?:\.\d{,2})? )}xms;
374         my ($amount) = $1;
375         push @bindings, $amount;
376     }
377
378     $query.= '
379   ORDER BY bookfundid, aqbudgetid
380 ';
381     $sth = $dbh->prepare($query);
382     $sth->execute(@bindings);
383     my @results;
384     while (my $row = $sth->fetchrow_hashref){
385         push @results, $row;
386     }
387     $sth->finish;
388
389     # filter budgets depending on the pagination
390     my $page = $input->param('page') || 1;
391     my $first = ($page - 1) * $pagesize;
392
393     # if we are on the last page, the number of the last word to display
394     # must not exceed the length of the results array
395     my $last = min(
396         $first + $pagesize - 1,
397         scalar @results - 1,
398     );
399
400     my $toggle = 0;
401     my @loop;
402     foreach my $result (@results[$first .. $last]) {
403         push(
404             @loop,
405             {
406                 %{$result},
407                 toggle => $toggle++%2,
408                 bookfundname => $bookfundname_of{ $result->{'bookfundid'} },
409                 branchname => $branches->{ $result->{branchcode} }->{branchname},
410                 startdate => format_date($result->{startdate}),
411                 enddate => format_date($result->{enddate}),
412             }
413         );
414     }
415
416     $template->param(
417         budget => \@loop,
418         pagination_bar => pagination_bar(
419             $script_name,
420             getnbpages(scalar @results, $pagesize),
421             $page,
422             'page'
423         )
424     );
425 } #---- END $OP eq DEFAULT
426 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
427                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
428                 IntranetNav => C4::Context->preference("IntranetNav"),
429                 );
430 output_html_with_http_headers $input, $cookie, $template->output;
431