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
42 use List::Util qw/min/;
47 use C4::Interface::CGI::Output;
53 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
54 my $bookfundid=$input->param('bookfundid');
55 my $aqbudgetid=$input->param('aqbudgetid');
57 my $op = $input->param('op');
59 my ($template, $borrowernumber, $cookie)
60 = get_template_and_user(
61 {template_name => "admin/aqbudget.tmpl",
65 flagsrequired => {parameters => 1},
71 action => $script_name,
72 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
73 script_name => $script_name,
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;
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;
88 #---- if primkey exists, it's a modify action, so read values to modify...
93 aqbookfund.bookfundid,
99 INNER JOIN aqbookfund ON aqbudget.bookfundid = aqbookfund.bookfundid
102 $sth=$dbh->prepare($query);
103 $sth->execute($aqbudgetid);
104 $dataaqbudget=$sth->fetchrow_hashref;
109 SELECT aqbookfund.branchcode,
111 aqbookfund.bookfundname
113 LEFT JOIN branches ON aqbookfund.branchcode = branches.branchcode
116 $sth=$dbh->prepare($query);
118 defined $aqbudgetid ? $dataaqbudget->{bookfundid} : $bookfundid,
120 $dataaqbookfund=$sth->fetchrow_hashref;
123 if (defined $aqbudgetid) {
125 bookfundid => $dataaqbudget->{'bookfundid'},
126 bookfundname => $dataaqbudget->{'bookfundname'}
131 bookfundid => $bookfundid,
132 bookfundname => $dataaqbookfund->{bookfundname},
145 $sth=$dbh->prepare($query);
147 while (my $row = $sth->fetchrow_hashref) {
150 if (defined $dataaqbookfund->{branchcode}) {
151 $branch->{selected} =
152 $dataaqbookfund->{branchcode} eq $row->{branchcode} ? 1 : 0;
154 elsif (defined $aqbudgetid) {
155 $branch->{selected} =
156 $dataaqbudget->{branchcode} eq $row->{branchcode} ? 1 : 0;
159 push @branches, $branch;
164 dateformat => display_date_format(),
165 aqbudgetid => $dataaqbudget->{'aqbudgetid'},
166 startdate => format_date($dataaqbudget->{'startdate'}),
167 enddate => format_date($dataaqbudget->{'enddate'}),
168 budgetamount => $dataaqbudget->{'budgetamount'},
169 branches => \@branches,
172 if (defined $dataaqbookfund->{branchcode}) {
174 disable_branchselection => 1,
175 branch => $dataaqbookfund->{branchcode},
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') {
184 if (defined $aqbudgetid) {
194 $sth=$dbh->prepare($query);
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') || undef,
209 (bookfundid, startdate, enddate, budgetamount, branchcode)
213 $sth=$dbh->prepare($query);
215 $input->param('bookfundid'),
216 format_date_in_iso($input->param('startdate')),
217 format_date_in_iso($input->param('enddate')),
218 $input->param('budgetamount'),
219 $input->param('branch') || undef,
224 $input->redirect("aqbudget.pl");
226 # END $OP eq ADD_VALIDATE
227 ################## DELETE_CONFIRM ##################################
228 # called by default form, used to confirm deletion of data in DB
229 } elsif ($op eq 'delete_confirm') {
230 my $dbh = C4::Context->dbh;
231 my $sth=$dbh->prepare("select aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudgetid=?");
232 $sth->execute($aqbudgetid);
233 my $data=$sth->fetchrow_hashref;
235 $template->param(bookfundid => $bookfundid);
236 $template->param(aqbudgetid => $data->{'aqbudgetid'});
237 $template->param(startdate => format_date($data->{'startdate'}));
238 $template->param(enddate => format_date($data->{'enddate'}));
239 $template->param(budgetamount => $data->{'budgetamount'});
240 # END $OP eq DELETE_CONFIRM
241 ################## DELETE_CONFIRMED ##################################
242 # called by delete_confirm, used to effectively confirm deletion of data in DB
243 } elsif ($op eq 'delete_confirmed') {
244 my $dbh = C4::Context->dbh;
245 my $aqbudgetid=uc($input->param('aqbudgetid'));
246 my $sth=$dbh->prepare("delete from aqbudget where aqbudgetid=?");
247 $sth->execute($aqbudgetid);
249 print $input->redirect("aqbookfund.pl");
251 # END $OP eq DELETE_CONFIRMED
252 ################## DEFAULT ##################################
256 # create a look-up table for bookfund names from bookfund ids,
257 # instead of having on query per budget
258 my %bookfundname_of = ();
260 SELECT bookfundid, bookfundname
263 $sth=$dbh->prepare($query);
265 while (my $row = $sth->fetchrow_hashref) {
266 $bookfundname_of{ $row->{bookfundid} } = $row->{bookfundname};
271 my $branches = GetBranches();
273 foreach my $branchcode (sort keys %{$branches}) {
276 name => $branches->{$branchcode}->{branchname},
279 if (defined $input->param('filter_branchcode')
280 and $input->param('filter_branchcode') eq $branchcode) {
281 $row->{selected} = 1;
284 push @branchloop, $row;
287 my @bookfundids_loop;
292 $sth = $dbh->prepare($query);
294 while (my $row = $sth->fetchrow_hashref) {
295 if (defined $input->param('filter_bookfundid')
296 and $input->param('filter_bookfundid') eq $row->{bookfundid}) {
297 $row->{selected} = 1;
300 push @bookfundids_loop, $row;
305 filter_bookfundids => \@bookfundids_loop,
306 filter_branches => \@branchloop,
307 filter_amount => $input->param('filter_amount') || undef,
308 filter_startdate => $input->param('filter_startdate') || undef,
309 filter_enddate => $input->param('filter_enddate') || undef,
312 my %sign_label_of = (
318 foreach my $field (qw/startdate enddate amount/) {
319 my $param = 'filter_'.$field.'_sign';
321 foreach my $sign (keys %sign_label_of) {
322 if ($input->param($param) eq $sign) {
324 $param.'_'.$sign_label_of{$sign}.'_selected' => 1,
330 # Search all available budgets
343 if ($input->param('filter_bookfundid')) {
347 push @bindings, $input->param('filter_bookfundid');
349 if ($input->param('filter_branchcode')) {
353 push @bindings, $input->param('filter_branchcode');
355 if ($input->param('filter_startdate')) {
357 AND startdate '.$input->param('filter_startdate_sign').' ?
359 push @bindings, format_date_in_iso($input->param('filter_startdate'));
361 if ($input->param('filter_enddate')) {
363 AND enddate '.$input->param('filter_enddate_sign').' ?
365 push @bindings, format_date_in_iso($input->param('filter_enddate'));
367 if ($input->param('filter_amount')) {
369 AND budgetamount '.$input->param('filter_amount_sign').' ?
371 # the amount must be a quantity, with 2 digits after the decimal
373 $input->param('filter_amount') =~ m{(\d* (?:\.\d{,2})? )}xms;
375 push @bindings, $amount;
379 ORDER BY bookfundid, aqbudgetid
381 $sth = $dbh->prepare($query);
382 $sth->execute(@bindings);
384 while (my $row = $sth->fetchrow_hashref){
389 # filter budgets depending on the pagination
390 my $page = $input->param('page') || 1;
391 my $first = ($page - 1) * $pagesize;
393 # if we are on the last page, the number of the last word to display
394 # must not exceed the length of the results array
396 $first + $pagesize - 1,
402 foreach my $result (@results[$first .. $last]) {
407 toggle => $toggle++%2,
408 bookfundname => $bookfundname_of{ $result->{'bookfundid'} },
409 branchname => $branches->{ $result->{branchcode} }->{branchname},
410 startdate => format_date($result->{startdate}),
411 enddate => format_date($result->{enddate}),
418 pagination_bar => pagination_bar(
420 getnbpages(scalar @results, $pagesize),
425 } #---- END $OP eq DEFAULT
426 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
427 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
428 IntranetNav => C4::Context->preference("IntranetNav"),
430 output_html_with_http_headers $input, $cookie, $template->output;