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