+ if (defined $input->param('filter_branchcode')
+ and $input->param('filter_branchcode') eq $branchcode) {
+ $row->{selected} = 1;
+ }
+ push @branchloop, $row;
+ }
+
+ my @bookfundids_loop;
+ $query = '
+SELECT bookfundid
+ FROM aqbookfund
+';
+ $sth = $dbh->prepare($query);
+ $sth->execute();
+ while (my $row = $sth->fetchrow_hashref) {
+ if (defined $input->param('filter_bookfundid')
+ and $input->param('filter_bookfundid') eq $row->{bookfundid}) {
+ $row->{selected} = 1;
+ }
+ push @bookfundids_loop, $row;
+ }
+
+ $template->param(
+ filter_bookfundids => \@bookfundids_loop,
+ filter_branches => \@branchloop,
+ filter_amount => $input->param('filter_amount') || undef,
+ filter_startdate => $input->param('filter_startdate') || undef,
+ filter_enddate => $input->param('filter_enddate') || undef,
+ );
+
+ my %sign_label_of = (
+ '=' => 'equal',
+ '>=' => 'superior',
+ '<=' => 'inferior',
+ );
+
+ foreach my $field (qw/startdate enddate amount/) {
+ my $param = 'filter_'.$field.'_sign';
+
+ foreach my $sign (keys %sign_label_of) {
+ if ($input->param($param) eq $sign) {
+ $template->param(
+ $param.'_'.$sign_label_of{$sign}.'_selected' => 1,
+ );
+ }
+ }
+ }
+
+ # Search all available budgets
+ $query = '
+SELECT aqbudgetid,
+ bookfundid,
+ startdate,
+ enddate,
+ budgetamount,
+ branchcode
+ FROM aqbudget
+ WHERE 1 = 1';
+
+ my @bindings;
+
+ if ($input->param('filter_bookfundid')) {
+ $query.= '
+ AND bookfundid = ?
+';
+ push @bindings, $input->param('filter_bookfundid');
+ }
+ if ($input->param('filter_branchcode')) {
+ $query.= '
+ AND branchcode = ?
+';
+ push @bindings, $input->param('filter_branchcode');
+ }
+ if ($input->param('filter_startdate')) {
+ $query.= '
+ AND startdate '.$input->param('filter_startdate_sign').' ?
+';
+ push @bindings, format_date_in_iso($input->param('filter_startdate'));
+ }
+ if ($input->param('filter_enddate')) {
+ $query.= '
+ AND enddate '.$input->param('filter_enddate_sign').' ?
+';
+ push @bindings, format_date_in_iso($input->param('filter_enddate'));
+ }
+ if ($input->param('filter_amount')) {
+ $query.= '
+ AND budgetamount '.$input->param('filter_amount_sign').' ?
+';
+ # the amount must be a quantity, with 2 digits after the decimal
+ # separator
+ $input->param('filter_amount') =~ m{(\d* (?:\.\d{,2})? )}xms;
+ my ($amount) = $1;
+ push @bindings, $amount;
+ }
+
+ $query.= '
+ ORDER BY bookfundid, aqbudgetid
+';
+ $sth = $dbh->prepare($query);
+ $sth->execute(@bindings);
+ my @results;
+ while (my $row = $sth->fetchrow_hashref){
+ push @results, $row;
+ }
+
+ # filter budgets depending on the pagination
+ my $page = $input->param('page') || 1;
+ my $first = ($page - 1) * $pagesize;
+
+ # if we are on the last page, the number of the last word to display
+ # must not exceed the length of the results array
+ my $last = min(
+ $first + $pagesize - 1,
+ scalar @results - 1,
+ );
+
+ my @loop;
+ foreach my $result (@results[$first .. $last]) {
+ push @loop, {
+ %{$result},
+ bookfundname => $bookfundname_of{ $result->{'bookfundid'} },
+ branchname => $branches->{ $result->{branchcode} }->{branchname},
+ startdate => format_date($result->{startdate}),
+ enddate => format_date($result->{enddate}),
+ };
+ }
+
+ $template->param(
+ budget => \@loop,
+ pagination_bar => pagination_bar(
+ $script_name,
+ getnbpages(scalar @results, $pagesize),
+ $page,
+ 'page'
+ )
+ );
+} #---- END $OP eq DEFAULT