-} elsif ($op eq 'delete_confirmed') {
- my $dbh = C4::Context->dbh;
- my $bookfundid=uc($input->param('bookfundid'));
- my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
- $sth->execute($bookfundid);
- $sth->finish;
- $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
- $sth->execute($bookfundid);
- $sth->finish;
- # END $OP eq DELETE_CONFIRMED
-################## DEFAULT ##################################
-} else { # DEFAULT
- $template->param(scriptname => $script_name);
- if ($searchfield ne '') {
- $template->param(search => 1);
- $template->param(searchfield => $searchfield);
- }
- my $env;
- my ($count,$results)=StringSearch($env,$searchfield,'web');
- my $toggle="white";
- my @loop_data =();
- my $dbh = C4::Context->dbh;
- my $sth2 = $dbh->prepare("Select aqbudgetid,startdate,enddate,budgetamount from aqbudget where bookfundid = ? order by bookfundid");
- for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
- my %row_data;
- $row_data{bookfundid} =$results->[$i]{'bookfundid'};
- $row_data{bookfundname} = $results->[$i]{'bookfundname'};
- $sth2->execute($row_data{bookfundid});
- my @budget_loop;
- while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
- my %budgetrow_data;
- $budgetrow_data{aqbudgetid} = $aqbudgetid;
- $budgetrow_data{startdate} = $startdate;
- $budgetrow_data{enddate} = $enddate;
- $budgetrow_data{budgetamount} = $budgetamount;
- push @budget_loop,\%budgetrow_data;
- }
- $row_data{budget} = \@budget_loop;
- push @loop_data,\%row_data;
- }
- $template->param(bookfund => \@loop_data);
-} #---- END $OP eq DEFAULT
+elsif ($op eq 'delete_confirmed') {
+ DelBookFund($bookfundid, $branchcodeid);
+}
+else { # DEFAULT
+ my ($sth);
+ # filters
+ my @branchloop;
+ foreach my $branchcode (sort keys %{$branches}) {
+ my $row = {
+ code => $branchcode,
+ name => $branches->{$branchcode}->{branchname},
+ };
+ if (defined $input->param('filter_branchcode')
+ and $input->param('filter_branchcode') eq $branchcode) {
+ $row->{selected} = 1;
+ }
+ push @branchloop, $row;
+ }
+
+ my @bookfundids_loop;
+ $sth = GetBookFundsId();
+
+ 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_bookfundname => $input->param('filter_bookfundname') || undef,
+ );
+
+ # searching the bookfunds corresponding to our filtering rules
+ my @results = SearchBookFund(
+ $input->param('filter'),
+ $input->param('filter_bookfundid'),
+ $input->param('filter_bookfundname'),
+ $input->param('filter_branchcode'),
+ );
+
+ # does the book funds have budgets?
+ my @loop_id;
+ $sth = GetBookFundsId();
+ while (my $row = $sth->fetchrow){
+ push @loop_id, $row;
+ }
+
+ my ($id,%nb_budgets_of);
+ foreach $id (@loop_id){
+ my $number = Countbookfund($id);
+ $nb_budgets_of{$id} = $number;
+ }
+
+ # pagination informations
+ my $page = $input->param('page') || 1;
+ my @loop;
+
+ 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,
+ );
+
+ foreach my $result (@results[$first .. $last]) {
+ push @loop, {
+ %{$result},
+ branchname => $branches->{ $result->{branchcode} }->{branchname},
+ has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
+ };
+ }
+
+ $template->param(
+ bookfund => \@loop,
+ pagination_bar => pagination_bar(
+ $script_name,
+ getnbpages(scalar @results, $pagesize),
+ $page,
+ 'page'
+ )
+ );
+}