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)
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.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
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
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
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
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.
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
43 use List::Util qw/min/;
50 use C4::Interface::CGI::Output;
54 my $dbh = C4::Context->dbh;
56 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
57 my $bookfundid=$input->param('bookfundid');
59 my $op = $input->param('op') || '';
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user(
63 {template_name => "admin/aqbookfund.tmpl",
67 flagsrequired => {parameters => 1, management => 1},
74 script_name => $script_name,
76 ); # we show only the TMPL_VAR names $op
79 $template->param(script_name => $script_name,
80 else => 1); # we show only the TMPL_VAR names $op
82 $template->param(action => $script_name);
86 # my %select_branches;
88 my $branches = getbranches;
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...
105 my $sth=$dbh->prepare($query);
106 $sth->execute($bookfundid);
107 $dataaqbookfund = $sth->fetchrow_hashref;
111 $header = "Modify book fund";
112 $template->param('header-is-modify-p' => 1);
114 $header = "Add book fund";
115 $template->param('header-is-add-p' => 1);
117 $template->param('use-header-flags-p' => 1);
118 $template->param(header => $header); # NOTE deprecated
123 $template->param(add_or_modify => $add_or_modify);
124 $template->param(bookfundid =>$bookfundid);
125 $template->param(bookfundname =>$dataaqbookfund->{'bookfundname'});
128 foreach my $branchcode (sort keys %{$branches}) {
130 branchcode => $branchcode,
131 branchname => $branches->{$branchcode}->{branchname},
134 if (defined $bookfundid
135 and defined $dataaqbookfund->{branchcode}
136 and $dataaqbookfund->{branchcode} eq $branchcode) {
137 $row->{selected} = 1;
140 push @branchloop, $row;
143 $template->param(branches => \@branchloop);
145 } # END $OP eq ADD_FORM
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');
155 SELECT COUNT(*) AS counter
159 $sth=$dbh->prepare($query);
160 $sth->execute($bookfundid);
161 my $data = $sth->fetchrow_hashref;
163 my $bookfund_already_exists = $data->{counter} > 0 ? 1 : 0;
165 if ($bookfund_already_exists) {
168 SET bookfundname = ?,
172 $sth=$dbh->prepare($query);
174 $input->param('bookfundname'),
175 $input->param('branchcode') || undef,
180 # budgets depending on a bookfund must have the same branchcode
181 # if the bookfund branchcode is set
182 if (defined $input->param('branchcode')) {
187 $sth=$dbh->prepare($query);
188 $sth->execute($input->param('branchcode'));
196 (bookfundid, bookfundname, branchcode)
200 $sth=$dbh->prepare($query);
203 $input->param('bookfundname'),
204 $input->param('branchcode') || undef,
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;
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);
228 $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
229 $sth->execute($bookfundid);
231 # END $OP eq DELETE_CONFIRMED
232 ################## DEFAULT ##################################
236 $template->param(scriptname => $script_name);
240 foreach my $branchcode (sort keys %{$branches}) {
243 name => $branches->{$branchcode}->{branchname},
246 if (defined $input->param('filter_branchcode')
247 and $input->param('filter_branchcode') eq $branchcode) {
248 $row->{selected} = 1;
251 push @branchloop, $row;
254 my @bookfundids_loop;
259 $sth = $dbh->prepare($query);
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;
267 push @bookfundids_loop, $row;
272 filter_bookfundids => \@bookfundids_loop,
273 filter_branches => \@branchloop,
274 filter_bookfundname => $input->param('filter_bookfundname') || undef,
277 # searching the bookfunds corresponding to our filtering rules
287 if ($input->param('filter')) {
288 if ($input->param('filter_bookfundid')) {
292 push @bindings, $input->param('filter_bookfundid');
294 if ($input->param('filter_bookfundname')) {
296 AND bookfundname like ?
298 push @bindings, '%'.$input->param('filter_bookfundname').'%';
300 if ($input->param('filter_branchcode')) {
304 push @bindings, $input->param('filter_branchcode');
311 $sth = $dbh->prepare($query);
312 $sth->execute(@bindings);
314 while (my $row = $sth->fetchrow_hashref) {
318 # does the book funds have budgets?
325 $sth = $dbh->prepare($query);
328 while (my $row = $sth->fetchrow_hashref) {
329 $nb_budgets_of{ $row->{bookfundid} } = $row->{counter};
332 # pagination informations
333 my $page = $input->param('page') || 1;
336 my $first = ($page - 1) * $pagesize;
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
341 $first + $pagesize - 1,
342 scalar(@results) - 1,
346 foreach my $result (@results[$first .. $last]) {
351 toggle => $toggle++%2,
353 $branches->{ $result->{branchcode} }->{branchname},
354 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
361 pagination_bar => pagination_bar(
363 getnbpages(scalar @results, $pagesize),
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"),
373 output_html_with_http_headers $input, $cookie, $template->output;