Call to Bookfund Added.
[koha.git] / admin / aqbookfund.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::Auth;
46 use C4::Koha;
47 use C4::Context;
48 use C4::Acquisition;
49 use C4::Output;
50 use C4::Interface::CGI::Output;
51 use C4::Search;
52 use C4::Date;
53
54 my $dbh = C4::Context->dbh;
55 my $input = new CGI;
56 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
57 my $bookfundid=$input->param('bookfundid');
58 my $pagesize = 10;
59 my $op = $input->param('op') || '';
60
61 my ($template, $borrowernumber, $cookie)
62     = get_template_and_user(
63         {template_name => "admin/aqbookfund.tmpl",
64          query => $input,
65          type => "intranet",
66          authnotrequired => 0,
67          flagsrequired => {parameters => 1, management => 1},
68          debug => 1,
69      }
70     );
71
72 if ($op) {
73     $template->param(
74         script_name => $script_name,
75         $op => 1,
76     ); # we show only the TMPL_VAR names $op
77 }
78 else {
79     $template->param(script_name => $script_name,
80                 else              => 1); # we show only the TMPL_VAR names $op
81 }
82 $template->param(action => $script_name);
83
84 # my @branches;
85 # my @select_branch;
86 # my %select_branches;
87
88 my $branches = GetBranches;
89
90 ################## ADD_FORM ##################################
91 # called by default. Used to create form to add or  modify a record
92 if ($op eq 'add_form') {
93         #---- if primkey exists, it's a modify action, so read values to modify...
94         my $dataaqbookfund;
95         my $header;
96         if ($bookfundid) {
97                 my $query = '
98 SELECT bookfundid,
99        bookfundname,
100        bookfundgroup,
101        branchcode
102   FROM aqbookfund
103   WHERE bookfundid = ?
104 ';
105                 my $sth=$dbh->prepare($query);
106                 $sth->execute($bookfundid);
107                 $dataaqbookfund = $sth->fetchrow_hashref;
108                 $sth->finish;
109             }
110         if ($bookfundid) {
111             $header = "Modify book fund";
112             $template->param('header-is-modify-p' => 1);
113         } else {
114             $header = "Add book fund";
115             $template->param('header-is-add-p' => 1);
116         }
117         $template->param('use-header-flags-p' => 1);
118         $template->param(header => $header); # NOTE deprecated
119         my $add_or_modify=0;
120         if ($bookfundid) {
121             $add_or_modify=1;
122         }
123         $template->param(add_or_modify => $add_or_modify);
124         $template->param(bookfundid =>$bookfundid);
125         $template->param(bookfundname =>$dataaqbookfund->{'bookfundname'});
126
127         my @branchloop;
128         foreach my $branchcode (sort keys %{$branches}) {
129             my $row = {
130                 branchcode => $branchcode,
131                 branchname => $branches->{$branchcode}->{branchname},
132             };
133
134             if (defined $bookfundid
135                 and defined $dataaqbookfund->{branchcode}
136                 and $dataaqbookfund->{branchcode} eq $branchcode) {
137                 $row->{selected} = 1;
138             }
139
140             push @branchloop, $row;
141         }
142
143         $template->param(branches => \@branchloop);
144
145 } # END $OP eq ADD_FORM
146
147 ################## ADD_VALIDATE ##################################
148 # called by add_form, used to insert/modify data in DB
149 elsif ($op eq 'add_validate') {
150         my $bookfundid = uc $input->param('bookfundid');
151
152         my ($query, $sth);
153
154         $query = '
155 SELECT COUNT(*) AS counter
156   FROM aqbookfund
157   WHERE bookfundid = ?
158 ';
159         $sth=$dbh->prepare($query);
160         $sth->execute($bookfundid);
161         my $data = $sth->fetchrow_hashref;
162         $sth->finish;
163         my $bookfund_already_exists = $data->{counter} > 0 ? 1 : 0;
164
165         if ($bookfund_already_exists) {
166             $query = '
167 UPDATE aqbookfund
168   SET bookfundname = ?,
169       branchcode = ?
170   WHERE bookfundid = ?
171 ';
172             $sth=$dbh->prepare($query);
173             $sth->execute(
174                 $input->param('bookfundname'),
175                 $input->param('branchcode') || undef,
176                 $bookfundid,
177             );
178             $sth->finish;
179
180             # budgets depending on a bookfund must have the same branchcode
181             # if the bookfund branchcode is set
182             if (defined $input->param('branchcode')) {
183                 $query = '
184 UPDATE aqbudget
185   SET branchcode = ?
186 ';
187                 $sth=$dbh->prepare($query);
188                 $sth->execute($input->param('branchcode'));
189                 $sth->finish;
190             }
191         }
192         else {
193             $query = '
194 INSERT
195   INTO aqbookfund
196   (bookfundid, bookfundname, branchcode)
197   VALUES
198   (?, ?, ?)
199 ';
200             $sth=$dbh->prepare($query);
201             $sth->execute(
202                 $bookfundid,
203                 $input->param('bookfundname'),
204                 $input->param('branchcode') || undef,
205             );
206             $sth->finish;
207         }
208
209         $input->redirect('aqbookfund.pl');
210                                                                                 # END $OP eq ADD_VALIDATE
211 ################## DELETE_CONFIRM ##################################
212 # called by default form, used to confirm deletion of data in DB
213 } elsif ($op eq 'delete_confirm') {
214         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
215         $sth->execute($bookfundid);
216         my $data=$sth->fetchrow_hashref;
217         $sth->finish;
218         $template->param(bookfundid => $bookfundid);
219         $template->param(bookfundname => $data->{'bookfundname'});
220                                                                                                         # END $OP eq DELETE_CONFIRM
221 ################## DELETE_CONFIRMED ##################################
222 # called by delete_confirm, used to effectively confirm deletion of data in DB
223 } elsif ($op eq 'delete_confirmed') {
224         my $bookfundid=uc($input->param('bookfundid'));
225         my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
226         $sth->execute($bookfundid);
227         $sth->finish;
228         $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
229         $sth->execute($bookfundid);
230         $sth->finish;
231                                                                                                         # END $OP eq DELETE_CONFIRMED
232 ################## DEFAULT ##################################
233 } else { # DEFAULT
234     my ($query, $sth);
235
236     $template->param(scriptname => $script_name);
237
238     # filters
239     my @branchloop;
240     foreach my $branchcode (sort keys %{$branches}) {
241         my $row = {
242             code => $branchcode,
243             name => $branches->{$branchcode}->{branchname},
244         };
245
246         if (defined $input->param('filter_branchcode')
247             and $input->param('filter_branchcode') eq $branchcode) {
248             $row->{selected} = 1;
249         }
250
251         push @branchloop, $row;
252     }
253
254     my @bookfundids_loop;
255     $query = '
256 SELECT bookfundid
257   FROM aqbookfund
258 ';
259     $sth = $dbh->prepare($query);
260     $sth->execute();
261     while (my $row = $sth->fetchrow_hashref) {
262         if (defined $input->param('filter_bookfundid')
263             and $input->param('filter_bookfundid') eq $row->{bookfundid}) {
264             $row->{selected} = 1;
265         }
266
267         push @bookfundids_loop, $row;
268     }
269     $sth->finish;
270
271     $template->param(
272         filter_bookfundids => \@bookfundids_loop,
273         filter_branches => \@branchloop,
274         filter_bookfundname => $input->param('filter_bookfundname') || undef,
275     );
276
277     # searching the bookfunds corresponding to our filtering rules
278     my @bindings;
279
280     $query = '
281 SELECT bookfundid,
282        bookfundname,
283        bookfundgroup,
284        branchcode
285   FROM aqbookfund
286   WHERE 1 = 1';
287     if ($input->param('filter')) {
288         if ($input->param('filter_bookfundid')) {
289             $query.= '
290     AND bookfundid = ?
291 ';
292             push @bindings, $input->param('filter_bookfundid');
293         }
294         if ($input->param('filter_bookfundname')) {
295             $query.= '
296     AND bookfundname like ?
297 ';
298             push @bindings, '%'.$input->param('filter_bookfundname').'%';
299         }
300         if ($input->param('filter_branchcode')) {
301             $query.= '
302     AND branchcode = ?
303 ';
304             push @bindings, $input->param('filter_branchcode');
305         }
306     }
307     $query.= '
308   ORDER BY bookfundid
309 ';
310
311     $sth = $dbh->prepare($query);
312     $sth->execute(@bindings);
313     my @results;
314     while (my $row = $sth->fetchrow_hashref) {
315         push @results, $row;
316     }
317
318     # does the book funds have budgets?
319     $query = '
320 SELECT bookfundid,
321        COUNT(*) AS counter
322   FROM aqbudget
323   GROUP BY bookfundid
324 ';
325     $sth = $dbh->prepare($query);
326     $sth->execute();
327     my %nb_budgets_of;
328     while (my $row = $sth->fetchrow_hashref) {
329         $nb_budgets_of{ $row->{bookfundid} } = $row->{counter};
330     }
331
332     # pagination informations
333     my $page = $input->param('page') || 1;
334     my @loop;
335
336     my $first = ($page - 1) * $pagesize;
337
338     # if we are on the last page, the number of the last word to display
339     # must not exceed the length of the results array
340     my $last = min(
341         $first + $pagesize - 1,
342         scalar(@results) - 1,
343     );
344
345     my $toggle = 0;
346     foreach my $result (@results[$first .. $last]) {
347         push(
348             @loop,
349             {
350                 %{$result},
351                 toggle => $toggle++%2,
352                 branchname =>
353                     $branches->{ $result->{branchcode} }->{branchname},
354                 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
355             }
356         );
357     }
358
359     $template->param(
360         bookfund => \@loop,
361         pagination_bar => pagination_bar(
362             $script_name,
363             getnbpages(scalar @results, $pagesize),
364             $page,
365             'page'
366         )
367     );
368 } #---- END $OP eq DEFAULT
369 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
370                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
371                 IntranetNav => C4::Context->preference("IntranetNav"),
372                 );
373 output_html_with_http_headers $input, $cookie, $template->output;