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