Bug 6934: fix errors pointed out by the QA script
[koha.git] / reports / issues_stats.pl
index 0df9942..648efe1 100755 (executable)
@@ -1,59 +1,61 @@
 #!/usr/bin/perl
 
-
 # Copyright 2000-2002 Katipo Communications
 #
 # 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; FIXME - Bug 2505
+use warnings;
 
-use CGI;
+use CGI qw ( -utf8 );
 use Date::Manip;
 
 use C4::Auth;
 use C4::Debug;
 use C4::Context;
-use C4::Branch; # GetBranches
 use C4::Koha;
 use C4::Output;
 use C4::Circulation;
 use C4::Reports;
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Members;
 
+use Koha::AuthorisedValues;
+use Koha::DateUtils;
+
 =head1 NAME
 
-plugin that shows circulation stats
+reports/issues_stats.pl
 
 =head1 DESCRIPTION
 
-=over 2
+Plugin that shows circulation stats
 
 =cut
 
 # my $debug = 1;       # override for now.
-my $input = new CGI;
+my $input = CGI->new;
 my $fullreportname = "reports/issues_stats.tt";
 my $do_it    = $input->param('do_it');
 my $line     = $input->param("Line");
 my $column   = $input->param("Column");
-my @filters  = $input->param("Filter");
-$filters[0]=format_date_in_iso($filters[0]);
-$filters[1]=format_date_in_iso($filters[1]);
+my @filters  = $input->multi_param("Filter");
+$filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
+    if ( $filters[0] );
+$filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
+    if ( $filters[1] );
 my $podsp    = $input->param("DisplayBy");
 my $type     = $input->param("PeriodTypeSel");
 my $daysel   = $input->param("PeriodDaySel");
@@ -69,19 +71,19 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
        flagsrequired => {reports => '*'},
        debug => 0,
 });
-our $sep     = $input->param("sep");
+our $sep     = $input->param("sep") // '';
 $sep = "\t" if ($sep eq 'tabulation');
 $template->param(do_it => $do_it,
 );
 
-my $itemtypes = GetItemTypes();
-my $categoryloop = GetBorrowercategoryList;
+our $itemtypes = GetItemTypes();
+our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
 
-my $ccodes    = GetKohaAuthorisedValues("items.ccode");
-my $locations = GetKohaAuthorisedValues("items.location");
+my $locations = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
+my $ccodes = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
 
-my $Bsort1 = GetAuthorisedValues("Bsort1");
-my $Bsort2 = GetAuthorisedValues("Bsort2");
+our $Bsort1 = GetAuthorisedValues("Bsort1");
+our $Bsort2 = GetAuthorisedValues("Bsort2");
 my ($hassort1,$hassort2);
 $hassort1=1 if $Bsort1;
 $hassort2=1 if $Bsort2;
@@ -132,8 +134,8 @@ 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} } ;
+for my $itype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description}} keys(%$itemtypes)) {
+    push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{translated_description} } ;
 }
 
     # location list
@@ -147,21 +149,14 @@ foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
        push @ccodes, { code => $_, description => $ccodes->{$_} };
 }
 
-my $CGIextChoice=CGI::scrolling_list(
-       -name     => 'MIME',
-       -id       => 'MIME',
-       -values   => ['CSV'], # FIXME translation
-       -size     => 1,
-       -multiple => 0 );
-    
+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,
@@ -196,7 +191,8 @@ sub calculate {
         if ($i>=2) {
                $cell{filter} = @$filters[$i];
         } else {
-               $cell{filter} = format_date(@$filters[$i]);
+                $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
+                   if ( @$filters[$i] );
                }
                $cell{crit} = 
             ( $i == 0 )  ? "Period From"
@@ -223,8 +219,8 @@ sub calculate {
        push @loopfilter,{crit=>"Select Month",filter=>$monthsel} if ($monthsel);
 
        my @linefilter;
-       $debug and warn "filtres ". join "|", @filters;
-       my ($colsource, $linesource);
+       $debug and warn "filtres ". join "|", @$filters;
+       my ($colsource, $linesource) = ('', '');
        $linefilter[1] = @$filters[1] if ($line =~ /datetime/);
        $linefilter[0] = 
            ( $line =~ /datetime/ ) ? @$filters[0]
@@ -268,7 +264,7 @@ sub calculate {
                $linefield = ($dsp == 1) ? "  dayname($line)" :
                                         ($dsp == 2) ? "monthname($line)" :
                                         ($dsp == 3) ? "     Year($line)" :
-                                       'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through C4::Dates
+                                         'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::DateUtils
        } else {
                $linefield = $line;
        }
@@ -326,12 +322,12 @@ sub calculate {
                                ($celvalue eq $_->{authorised_value}) or next;
                                $cell{rowtitle_display} = $_->{lib} and last;
                        }
-               } elsif ($line =~ /category/) {
-                       foreach (@$categoryloop) {
-                               ($celvalue eq $_->{categorycode}) or next;
-                               $cell{rowtitle_display} = $_->{description} and last;
-                       }
-               }
+        } elsif ($line =~ /category/) {
+            foreach my $patron_category ( @patron_categories ) {
+                ($celvalue eq $patron_category->categorycode) or next;
+                $cell{rowtitle_display} = $patron_category->description and last;
+            }
+        }
                push @loopline, \%cell;
        }
 
@@ -343,7 +339,7 @@ sub calculate {
                $colfield = ($dsp == 1) ? "  dayname($column)" :
                                        ($dsp == 2) ? "monthname($column)" :
                                        ($dsp == 3) ? "     Year($column)" :
-                                       'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through C4::Dates
+                                        'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::DateUtils
        } else {
                $colfield = $column;
        }
@@ -400,12 +396,12 @@ sub calculate {
                                ($celvalue eq $_->{authorised_value}) or next;
                                $cell{coltitle_display} = $_->{lib} and last;
                        }
-               } elsif ($column =~ /category/) {
-                       foreach (@$categoryloop) {
-                               ($celvalue eq $_->{categorycode}) or next;
-                               $cell{coltitle_display} = $_->{description} and last;
-                       }
-               }
+        } elsif ($column =~ /category/) {
+            foreach my $patron_category ( @patron_categories ) {
+                ($celvalue eq $patron_category->categorycode) or next;
+                $cell{coltitle_display} = $patron_category->description and last;
+            }
+        }
                push @loopcol, \%cell;
        }