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