Bug 13914 : Fix UNION in SQL request for reserves statistics
[koha.git] / reports / reserves_stats.pl
index 067b815..a444bc5 100755 (executable)
@@ -26,22 +26,23 @@ use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Debug;
 use C4::Context;
-use C4::Branch; # GetBranches
 use C4::Koha;
 use C4::Output;
 use C4::Reports;
 use C4::Members;
 use Koha::DateUtils;
+use Koha::Libraries;
+use Koha::Patron::Categories;
 use List::MoreUtils qw/any/;
 use YAML;
 
 =head1 NAME
 
-plugin that shows circulation stats
+    reports/reserve_stats.pl
 
 =head1 DESCRIPTION
 
-=over 2
+    Plugin that shows circulation stats
 
 =cut
 
@@ -80,7 +81,7 @@ $template->param(do_it => $do_it,
 );
 
 my $itemtypes = GetItemTypes();
-my $categoryloop = GetBorrowercategoryList;
+my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
 
 my $ccodes    = GetKohaAuthorisedValues("items.ccode");
 my $locations = GetKohaAuthorisedValues("items.location");
@@ -158,11 +159,10 @@ my $CGIextChoice = ( 'CSV' ); # FIXME translation
 my $CGIsepChoice=GetDelimiterChoices;
  
 $template->param(
-       categoryloop => $categoryloop,
+    categoryloop => \@patron_categories,
        itemtypeloop => \@itemtypeloop,
        locationloop => \@locations,
           ccodeloop => \@ccodes,
-         branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
        hassort1=> $hassort1,
        hassort2=> $hassort2,
        Bsort1 => $Bsort1,
@@ -221,7 +221,7 @@ sub calculate {
                                ($process == 3) ? "(COUNT(DISTINCT reserves.itemnumber)) calculation"      : 
                                ($process == 4) ? "(COUNT(DISTINCT reserves.biblionumber)) calculation"    : '*';
        $strcalc .= "
-        FROM reserves
+        FROM (select * from reserves union select * from old_reserves) reserves
         LEFT JOIN borrowers USING (borrowernumber)
        ";
        $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
@@ -266,14 +266,11 @@ sub calculate {
        $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
        $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
        $strcalc .= " GROUP BY line, col )";
-       my $strcalc_old=$strcalc;
-       $strcalc_old=~s/reserves/old_reserves/g;
-       $strcalc.=qq{ UNION $strcalc_old ORDER BY line, col};
        ($debug) and print STDERR $strcalc;
        my $dbcalc = $dbh->prepare($strcalc);
        push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strcalc};
        @sqlparams=(@sqlparams,@sqlorparams);
-       $dbcalc->execute(@sqlparams,@sqlparams);
+       $dbcalc->execute(@sqlparams);
        my ($emptycol,$emptyrow); 
        my $data = $dbcalc->fetchall_hashref([qw(line col)]);
        my %cols_hash;
@@ -337,7 +334,7 @@ sub display_value {
         ( $crit =~ /ccode/ )         ? $ccodes->{$value}
       : ( $crit =~ /location/ )      ? $locations->{$value}
       : ( $crit =~ /itemtype/ )      ? $itemtypes->{$value}->{description}
-      : ( $crit =~ /branch/ )        ? GetBranchName($value)
+      : ( $crit =~ /branch/ )        ? Koha::Libraries->find($value)->branchname
       : ( $crit =~ /reservestatus/ ) ? reservestatushuman($value)
       :                                $value;    # default fallback
     if ($crit =~ /sort1/) {
@@ -353,9 +350,9 @@ sub display_value {
         }
     }
     elsif ( $crit =~ /category/ ) {
-        foreach (@$categoryloop) {
-            ( $value eq $_->{categorycode} ) or next;
-            $display_value = $_->{description} and last;
+        foreach my $patron_category ( @patron_categories ) {
+            ( $value eq $patron_category->categorycode ) or next;
+            $display_value = $patron_category->description and last;
         }
     }
     return $display_value;