Bug 18898 - Some permissions for Reports can be bypassed
[koha.git] / reports / reserves_stats.pl
index c20a676..09bfd0c 100755 (executable)
 # Koha; if not, write to the Free Software Foundation, Inc., 
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+use Modern::Perl;
 
-use strict;
-use warnings;
-
-use CGI;
+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 C4::Dates qw/format_date format_date_in_iso/;
-use C4::Category;
+use Koha::AuthorisedValues;
+use Koha::DateUtils;
+use Koha::ItemTypes;
+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 reserve stats
 
 =cut
 
 # my $debug = 1;       # override for now.
 my $input = new CGI;
-my $fullreportname = "reports/reserves_stats.tmpl";
+my $fullreportname = "reports/reserves_stats.tt";
 my $do_it    = $input->param('do_it');
 my $line     = $input->param("Line");
 my $column   = $input->param("Column");
-my $podsp    = $input->param("DisplayBy");
-my $type     = $input->param("PeriodTypeSel");
-my $daysel   = $input->param("PeriodDaySel");
-my $monthsel = $input->param("PeriodMonthSel");
 my $calc     = $input->param("Cellvalue");
 my $output   = $input->param("output");
 my $basename = $input->param("basename");
-my $mime     = $input->param("MIME");
 my $hash_params = $input->Vars;
 my $filter_hashref;
 foreach my $filter (grep {$_ =~/^filter/} keys %$hash_params){
@@ -78,15 +73,12 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
 our $sep     = $input->param("sep") || '';
 $sep = "\t" if ($sep eq 'tabulation');
 $template->param(do_it => $do_it,
-       DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
 );
 
-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");
-my $authvalue = GetKohaAuthorisedValues("items.authvalue");
+my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
+my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
 
 my $Bsort1 = GetAuthorisedValues("Bsort1");
 my $Bsort2 = GetAuthorisedValues("Bsort2");
@@ -138,11 +130,7 @@ my @values;
 my %labels;
 my %select;
 
-# create itemtype arrayref for <select>.
-my @itemtypeloop;
-for my $itype ( sort {$itemtypes->{$a}->{description} cmp $itemtypes->{$b}->{description}} keys(%$itemtypes)) {
-       push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{description} } ;
-}
+my $itemtypes = Koha::ItemTypes->search_with_localization;
 
     # location list
 my @locations;
@@ -156,23 +144,14 @@ foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
 }
 
 # various
-my @mime = (C4::Context->preference("MIME"));
-
-my $CGIextChoice=CGI::scrolling_list(
-       -name     => 'MIME',
-       -id       => 'MIME',
-       -values   => \@mime,
-       -size     => 1,
-       -multiple => 0 );
-    
+my $CGIextChoice = ( 'CSV' ); # FIXME translation
 my $CGIsepChoice=GetDelimiterChoices;
  
 $template->param(
-       categoryloop => $categoryloop,
-       itemtypeloop => \@itemtypeloop,
+    categoryloop => \@patron_categories,
+    itemtypes => $itemtypes,
        locationloop => \@locations,
           ccodeloop => \@ccodes,
-         branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
        hassort1=> $hassort1,
        hassort2=> $hassort2,
        Bsort1 => $Bsort1,
@@ -199,9 +178,10 @@ sub calculate {
     my @loopfilter;
     foreach my $filter ( keys %$filters_hashref ) {
         $filters_hashref->{$filter} =~ s/\*/%/;
-        $filters_hashref->{$filter} =
-          format_date_in_iso( $filters_hashref->{$filter} )
-          if ( $filter =~ /date/ );
+        if ( $filter =~ /date/ ) {
+            $filters_hashref->{$filter} =
+                eval { output_pref( { dt => dt_from_string( $filters_hashref->{$filter} ), dateonly => 1, dateformat => 'iso' }); };
+        }
     }
 
     #display
@@ -210,7 +190,7 @@ sub calculate {
             crit   => $_,
             filter => (
                 $_ =~ /date/
-                ? format_date( $filters_hashref->{$_} )
+                ? eval { output_pref( { dt => dt_from_string( $filters_hashref->{$_} ), dateonly => 1 }); }
                 : $filters_hashref->{$_}
             )
         }
@@ -230,7 +210,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 "
@@ -275,14 +255,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;
@@ -334,19 +311,17 @@ sub calculate {
        return [(\%globalline)];
 }
 
-sub null_to_zzempty ($) {
-       my $string = shift;
-       defined($string)    or  return 'zzEMPTY';
-       ($string eq "NULL") and return 'zzEMPTY';
-       return $string;         # else return the valid value
-}
 sub display_value {
     my ( $crit, $value ) = @_;
+    my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
+    my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
+    my $Bsort1 = GetAuthorisedValues("Bsort1");
+    my $Bsort2 = GetAuthorisedValues("Bsort2");
     my $display_value =
         ( $crit =~ /ccode/ )         ? $ccodes->{$value}
       : ( $crit =~ /location/ )      ? $locations->{$value}
-      : ( $crit =~ /itemtype/ )      ? $itemtypes->{$value}->{description}
-      : ( $crit =~ /branch/ )        ? GetBranchName($value)
+      : ( $crit =~ /itemtype/ )      ? Koha::ItemTypes->find( $value )->translated_description
+      : ( $crit =~ /branch/ )        ? Koha::Libraries->find($value)->branchname
       : ( $crit =~ /reservestatus/ ) ? reservestatushuman($value)
       :                                $value;    # default fallback
     if ($crit =~ /sort1/) {
@@ -362,13 +337,15 @@ sub display_value {
         }
     }
     elsif ( $crit =~ /category/ ) {
-        foreach (@$categoryloop) {
-            ( $value eq $_->{categorycode} ) or next;
-            $display_value = $_->{description} and last;
+        my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+        foreach my $patron_category ( @patron_categories ) {
+            ( $value eq $patron_category->categorycode ) or next;
+            $display_value = $patron_category->description and last;
         }
     }
     return $display_value;
 }
+
 sub reservestatushuman{
        my ($val)=@_;
        my %hashhuman=(
@@ -381,6 +358,7 @@ sub reservestatushuman{
        );
        $hashhuman{$val};
 }
+
 sub changeifreservestatus{
        my ($val)=@_;
        ($val=~/reservestatus/
@@ -400,4 +378,3 @@ sub changeifreservestatus{
                                        end }
                :$val);
 }
-1;