Bug 29387: Stringify exceptions for other background job modules
[koha-ffzg.git] / reports / reserves_stats.pl
index 79dd391..cb63a6e 100755 (executable)
@@ -4,63 +4,54 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
-use C4::Auth;
-use C4::Debug;
+use C4::Auth qw( get_template_and_user );
 use C4::Context;
-use C4::Koha;
-use C4::Output;
-use C4::Reports;
+use C4::Koha qw( GetAuthorisedValues );
+use C4::Output qw( output_html_with_http_headers );
+use C4::Reports qw( GetDelimiterChoices );
 use C4::Members;
 use Koha::AuthorisedValues;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::ItemTypes;
 use Koha::Libraries;
 use Koha::Patron::Categories;
-use List::MoreUtils qw/any/;
-use YAML;
+use List::MoreUtils qw( any );
 
 =head1 NAME
 
-    reports/reserve_stats.pl
+reports/reserve_stats.pl
 
 =head1 DESCRIPTION
 
-    Plugin that shows circulation stats
+Plugin that shows reserve stats
 
 =cut
 
-# my $debug = 1;       # override for now.
-my $input = new CGI;
+my $input = CGI->new;
 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){
@@ -72,17 +63,14 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
        template_name => $fullreportname,
        query => $input,
        type => "intranet",
-       authnotrequired => 0,
        flagsrequired => {reports => '*'},
-       debug => 0,
 });
 our $sep     = $input->param("sep") || '';
 $sep = "\t" if ($sep eq 'tabulation');
 $template->param(do_it => $do_it,
 );
 
-my $itemtypes = GetItemTypes();
-my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
 
 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'] } ) };
@@ -133,15 +121,8 @@ if ($do_it) {
 }
 
 my $dbh = C4::Context->dbh;
-my @values;
-my %labels;
-my %select;
-
-# create itemtype arrayref for <select>.
-my @itemtypeloop;
-for my $itype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description}} keys(%$itemtypes)) {
-       push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{translated_description} } ;
-}
+
+my $itemtypes = Koha::ItemTypes->search_with_localization;
 
     # location list
 my @locations;
@@ -160,7 +141,7 @@ my $CGIsepChoice=GetDelimiterChoices;
  
 $template->param(
     categoryloop => \@patron_categories,
-       itemtypeloop => \@itemtypeloop,
+    itemtypes => $itemtypes,
        locationloop => \@locations,
           ccodeloop => \@ccodes,
        hassort1=> $hassort1,
@@ -233,7 +214,6 @@ sub calculate {
        my @sqlorparams;
        my @sqlor;
        my @sqlwhere;
-       ($debug) and print STDERR Dump($filters_hashref);
        foreach my $filter (keys %$filters_hashref){
                my $string;
                my $stringfield=$filter;
@@ -266,12 +246,10 @@ sub calculate {
        $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
        $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
        $strcalc .= " GROUP 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);
-       my ($emptycol,$emptyrow); 
        my $data = $dbcalc->fetchall_hashref([qw(line col)]);
        my %cols_hash;
        foreach my $row (keys %$data){
@@ -303,7 +281,6 @@ sub calculate {
                my $total = 0;
                foreach my $row (@loopline) {
                        $total += $data->{$row}{$col}{calculation} if $data->{$row}{$col}{calculation};
-                       $debug and warn "value added ".$$data{$row}{$col}{calculation}. "for line ".$row;
                }
                push @loopfooter, {'totalcol' => $total};
                push @loopcol, {'coltitle' => $col,
@@ -322,23 +299,16 @@ 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 $itemtypes = GetItemTypes();
     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 =~ /itemtype/ )      ? Koha::ItemTypes->find( $value )->translated_description
       : ( $crit =~ /branch/ )        ? Koha::Libraries->find($value)->branchname
       : ( $crit =~ /reservestatus/ ) ? reservestatushuman($value)
       :                                $value;    # default fallback
@@ -355,7 +325,7 @@ sub display_value {
         }
     }
     elsif ( $crit =~ /category/ ) {
-        my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+        my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
         foreach my $patron_category ( @patron_categories ) {
             ( $value eq $patron_category->categorycode ) or next;
             $display_value = $patron_category->description and last;
@@ -363,6 +333,7 @@ sub display_value {
     }
     return $display_value;
 }
+
 sub reservestatushuman{
        my ($val)=@_;
        my %hashhuman=(
@@ -375,6 +346,7 @@ sub reservestatushuman{
        );
        $hashhuman{$val};
 }
+
 sub changeifreservestatus{
        my ($val)=@_;
        ($val=~/reservestatus/
@@ -394,4 +366,3 @@ sub changeifreservestatus{
                                        end }
                :$val);
 }
-1;