Bug Fixing :
[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 C4::Branch; # GetBranches
43 use List::Util qw/min/;
44
45 use C4::Date;
46 use C4::Auth;
47 use C4::Acquisition;
48 use C4::Context;
49 use C4::Output;
50 use C4::Interface::CGI::Output;
51 use C4::Koha;
52
53 my $input = new CGI;
54 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
55 my $bookfundid=$input->param('bookfundid');
56 my $aqbudgetid=$input->param('aqbudgetid');
57 my $branchcodeid=$input->param('branchcode');
58 my $pagesize = 20;
59 my $op = $input->param('op');
60
61 my ($template, $borrowernumber, $cookie)
62     = get_template_and_user(
63         {template_name => "admin/aqbudget.tmpl",
64          query => $input,
65          type => "intranet",
66          authnotrequired => 0,
67          flagsrequired => {parameters => 1},
68          debug => 1,
69      }
70     );
71
72 $template->param(
73     action => $script_name,
74     DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
75     script_name => $script_name,
76     $op || 'else' => 1,
77 );
78
79 my $dbh = C4::Context->dbh;
80 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
81 $sthtemp->execute($borrowernumber);
82 my ($flags, $homebranch)=$sthtemp->fetchrow;
83
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or  modify a record
86 if ($op eq 'add_form') {
87     my ($query, $dataaqbudget, $dataaqbookfund, $sth);
88     my $dbh = C4::Context->dbh;
89
90     #---- if primkey exists, it's a modify action, so read values to modify...
91     if ($aqbudgetid) {
92         $query = '
93 SELECT aqbudgetid,
94        bookfundname,
95        aqbookfund.bookfundid,
96        startdate,
97        enddate,
98        budgetamount,
99        aqbudget.branchcode
100   FROM aqbudget
101     INNER JOIN aqbookfund ON (aqbudget.bookfundid = aqbookfund.bookfundid AND
102       aqbudget.branchcode = aqbookfund.branchcode)
103   WHERE aqbudgetid = ?
104 ';
105         $sth=$dbh->prepare($query);
106         $sth->execute($aqbudgetid);
107         $dataaqbudget=$sth->fetchrow_hashref;
108         $sth->finish;
109     }
110
111     $query = '
112 SELECT aqbookfund.branchcode,
113        branches.branchname,
114        aqbookfund.bookfundname
115   FROM aqbookfund
116     LEFT JOIN branches ON aqbookfund.branchcode = branches.branchcode
117   WHERE bookfundid = ? AND aqbookfund.branchcode=?
118 ';
119     $sth=$dbh->prepare($query);
120     $sth->execute(
121         defined $aqbudgetid ? $dataaqbudget->{bookfundid} : $bookfundid,
122         $branchcodeid
123     );
124     $dataaqbookfund=$sth->fetchrow_hashref;
125     $sth->finish;
126
127     if (defined $aqbudgetid) {
128         $template->param(
129             bookfundid => $dataaqbudget->{'bookfundid'},
130             branchcode => $dataaqbudget->{'branchcode'},
131             bookfundname => $dataaqbudget->{'bookfundname'}
132         );
133     }
134     else {
135         $template->param(
136             bookfundid => $bookfundid,
137             branchcode => $dataaqbookfund->{'branchcode'},
138             bookfundname => $dataaqbookfund->{bookfundname},
139         );
140     }
141
142     # Available branches
143     my @branches = ();
144
145     $query = '
146 SELECT branchcode,
147        branchname
148   FROM branches
149   ORDER BY branchname
150 ';
151     $sth=$dbh->prepare($query);
152     $sth->execute();
153     while (my $row = $sth->fetchrow_hashref) {
154         my $branch = $row;
155
156         if (defined $dataaqbookfund->{branchcode}) {
157             $branch->{selected} =
158                 $dataaqbookfund->{branchcode} eq $row->{branchcode} ? 1 : 0;
159         }
160         elsif (defined $aqbudgetid) {
161             $branch->{selected} =
162                 $dataaqbudget->{branchcode} eq $row->{branchcode} ? 1 : 0;
163         }
164
165         push @branches, $branch;
166     }
167     $sth->finish;
168
169     $template->param(
170         dateformat => display_date_format(),
171         aqbudgetid => $dataaqbudget->{'aqbudgetid'},
172         startdate => format_date($dataaqbudget->{'startdate'}),
173         enddate => format_date($dataaqbudget->{'enddate'}),
174         budgetamount => $dataaqbudget->{'budgetamount'},
175         branches => \@branches,
176     );
177
178     if (defined $dataaqbookfund->{branchcode}) {
179         $template->param(
180             disable_branchselection => 1,
181             branch => $dataaqbookfund->{branchcode},
182         );
183     }
184                                                                                                         # END $OP eq ADD_FORM
185 ################## ADD_VALIDATE ##################################
186 # called by add_form, used to insert/modify data in DB
187 } elsif ($op eq 'add_validate') {
188     my ($query, $sth);
189
190     if (defined $aqbudgetid) {
191         $query = '
192 UPDATE aqbudget
193   SET bookfundid = ?,
194       startdate = ?,
195       enddate = ?,
196       budgetamount = ?,
197       branchcode = ?
198   WHERE aqbudgetid = ?
199 ';
200         $sth=$dbh->prepare($query);
201         $sth->execute(
202             $input->param('bookfundid'),
203             format_date_in_iso($input->param('startdate')),
204             format_date_in_iso($input->param('enddate')),
205             $input->param('budgetamount'),
206             $input->param('branch') || '',
207             $aqbudgetid,
208         );
209         $sth->finish;
210     }
211     else {
212         $query = '
213 INSERT
214   INTO aqbudget
215   (bookfundid, startdate, enddate, budgetamount, branchcode)
216   VALUES
217   (?, ?, ?, ?, ?)
218 ';
219         $sth=$dbh->prepare($query);
220         $sth->execute(
221             $input->param('bookfundid'),
222             format_date_in_iso($input->param('startdate')),
223             format_date_in_iso($input->param('enddate')),
224             $input->param('budgetamount'),
225             $input->param('branch') || '',
226         );
227         $sth->finish;
228     }
229
230     $input->redirect("aqbudget.pl");
231
232 # END $OP eq ADD_VALIDATE
233 ################## DELETE_CONFIRM ##################################
234 # called by default form, used to confirm deletion of data in DB
235 } elsif ($op eq 'delete_confirm') {
236         my $dbh = C4::Context->dbh;
237         my $sth=$dbh->prepare("select aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudgetid=?");
238         $sth->execute($aqbudgetid);
239         my $data=$sth->fetchrow_hashref;
240         $sth->finish;
241         $template->param(bookfundid => $bookfundid);
242         $template->param(aqbudgetid => $data->{'aqbudgetid'});
243         $template->param(startdate => format_date($data->{'startdate'}));
244         $template->param(enddate => format_date($data->{'enddate'}));
245         $template->param(budgetamount => $data->{'budgetamount'});
246                                                                                                         # END $OP eq DELETE_CONFIRM
247 ################## DELETE_CONFIRMED ##################################
248 # called by delete_confirm, used to effectively confirm deletion of data in DB
249 } elsif ($op eq 'delete_confirmed') {
250         my $dbh = C4::Context->dbh;
251         my $aqbudgetid=uc($input->param('aqbudgetid'));
252         my $sth=$dbh->prepare("delete from aqbudget where aqbudgetid=?");
253         $sth->execute($aqbudgetid);
254         $sth->finish;
255          print $input->redirect("aqbookfund.pl");
256          return;
257                                                                                                         # END $OP eq DELETE_CONFIRMED
258 ################## DEFAULT ##################################
259 } else { # DEFAULT
260     my ($query, $sth);
261
262     # create a look-up table for bookfund names from bookfund ids,
263     # instead of having on query per budget
264     my %bookfundname_of = ();
265     $query = '
266 SELECT bookfundid, bookfundname
267   FROM aqbookfund
268 ';
269     $sth=$dbh->prepare($query);
270     $sth->execute;
271     while (my $row = $sth->fetchrow_hashref) {
272         $bookfundname_of{ $row->{bookfundid} } = $row->{bookfundname};
273     }
274     $sth->finish;
275
276     # filters
277     my $branches = GetBranches();
278     my @branchloop;
279     foreach my $branchcode (sort keys %{$branches}) {
280         my $row = {
281             code => $branchcode,
282             name => $branches->{$branchcode}->{branchname},
283         };
284
285         if (defined $input->param('filter_branchcode')
286             and $input->param('filter_branchcode') eq $branchcode) {
287             $row->{selected} = 1;
288         }
289
290         push @branchloop, $row;
291     }
292
293     my @bookfundids_loop;
294     $query = '
295 SELECT bookfundid
296   FROM aqbookfund
297 ';
298     $sth = $dbh->prepare($query);
299     $sth->execute();
300     while (my $row = $sth->fetchrow_hashref) {
301         if (defined $input->param('filter_bookfundid')
302             and $input->param('filter_bookfundid') eq $row->{bookfundid}) {
303             $row->{selected} = 1;
304         }
305
306         push @bookfundids_loop, $row;
307     }
308     $sth->finish;
309
310     $template->param(
311         filter_bookfundids => \@bookfundids_loop,
312         filter_branches => \@branchloop,
313         filter_amount => $input->param('filter_amount') || undef,
314         filter_startdate => $input->param('filter_startdate') || undef,
315         filter_enddate => $input->param('filter_enddate') || undef,
316     );
317
318     my %sign_label_of = (
319         '=' => 'equal',
320         '>=' => 'superior',
321         '<=' => 'inferior',
322     );
323
324     foreach my $field (qw/startdate enddate amount/) {
325         my $param = 'filter_'.$field.'_sign';
326
327         foreach my $sign (keys %sign_label_of) {
328             if ($input->param($param) eq $sign) {
329                 $template->param(
330                     $param.'_'.$sign_label_of{$sign}.'_selected' => 1,
331                 );
332             }
333         }
334     }
335
336     # Search all available budgets
337     $query = '
338 SELECT aqbudgetid,
339        bookfundid,
340        startdate,
341        enddate,
342        budgetamount,
343        branchcode
344   FROM aqbudget
345   WHERE 1 = 1';
346
347     my @bindings;
348
349     if ($input->param('filter_bookfundid')) {
350         $query.= '
351     AND bookfundid = ?
352 ';
353         push @bindings, $input->param('filter_bookfundid');
354     }
355     if ($input->param('filter_branchcode')) {
356         $query.= '
357     AND branchcode = ?
358 ';
359         push @bindings, $input->param('filter_branchcode');
360     }
361     if ($input->param('filter_startdate')) {
362         $query.= '
363     AND startdate '.$input->param('filter_startdate_sign').' ?
364 ';
365         push @bindings, format_date_in_iso($input->param('filter_startdate'));
366     }
367     if ($input->param('filter_enddate')) {
368         $query.= '
369     AND enddate '.$input->param('filter_enddate_sign').' ?
370 ';
371         push @bindings, format_date_in_iso($input->param('filter_enddate'));
372     }
373     if ($input->param('filter_amount')) {
374         $query.= '
375     AND budgetamount '.$input->param('filter_amount_sign').' ?
376 ';
377         # the amount must be a quantity, with 2 digits after the decimal
378         # separator
379         $input->param('filter_amount') =~ m{(\d* (?:\.\d{,2})? )}xms;
380         my ($amount) = $1;
381         push @bindings, $amount;
382     }
383
384     $query.= '
385   ORDER BY bookfundid, aqbudgetid
386 ';
387     $sth = $dbh->prepare($query);
388     $sth->execute(@bindings);
389     my @results;
390     while (my $row = $sth->fetchrow_hashref){
391         push @results, $row;
392     }
393     $sth->finish;
394
395     # filter budgets depending on the pagination
396     my $page = $input->param('page') || 1;
397     my $first = ($page - 1) * $pagesize;
398
399     # if we are on the last page, the number of the last word to display
400     # must not exceed the length of the results array
401     my $last = min(
402         $first + $pagesize - 1,
403         scalar @results - 1,
404     );
405
406     my $toggle = 0;
407     my @loop;
408     foreach my $result (@results[$first .. $last]) {
409         push(
410             @loop,
411             {
412                 %{$result},
413                 toggle => $toggle++%2,
414                 bookfundname => $bookfundname_of{ $result->{'bookfundid'} },
415                 branchname => $branches->{ $result->{branchcode} }->{branchname},
416                 startdate => format_date($result->{startdate}),
417                 enddate => format_date($result->{enddate}),
418             }
419         );
420     }
421
422     $template->param(
423         budget => \@loop,
424         pagination_bar => pagination_bar(
425             $script_name,
426             getnbpages(scalar @results, $pagesize),
427             $page,
428             'page'
429         )
430     );
431 } #---- END $OP eq DEFAULT
432 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
433                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
434                 IntranetNav => C4::Context->preference("IntranetNav"),
435                 );
436 output_html_with_http_headers $input, $cookie, $template->output;
437