Bug 28804: (bug 25026 follow-up) Handle SQL errors in reports
[srvgit] / reports / issues_stats.pl
index 8f86fdb..3eb27fb 100755 (executable)
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
-use Date::Manip;
 
-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::Circulation;
-use C4::Reports;
-use C4::Members;
+use C4::Koha qw( GetAuthorisedValues );
+use C4::Output qw( output_html_with_http_headers );
+use C4::Reports qw( GetDelimiterChoices );
 
 use Koha::AuthorisedValues;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::ItemTypes;
 use Koha::Patron::Attribute::Types;
 
@@ -46,7 +42,6 @@ Plugin that shows circulation stats
 
 =cut
 
-# my $debug = 1;       # override for now.
 my $input = CGI->new;
 my $fullreportname = "reports/issues_stats.tt";
 my $do_it    = $input->param('do_it');
@@ -79,7 +74,6 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
        query => $input,
        type => "intranet",
        flagsrequired => {reports => '*'},
-       debug => 0,
 });
 our $sep     = $input->param("sep") // ';';
 $sep = "\t" if ($sep eq 'tabulation');
@@ -88,7 +82,7 @@ $template->param(do_it => $do_it,
 
 our $itemtypes = Koha::ItemTypes->search_with_localization->unblessed;
 
-our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+our @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
 
 our $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
 our $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
@@ -246,7 +240,6 @@ sub calculate {
     push @loopfilter, { crit => "Select Month", filter => $monthsel } if ($monthsel);
 
     my @linefilter;
-    $debug and warn "filtres " . join "|", @$filters;
     my ( $colsource, $linesource ) = ('', '');
     $linefilter[1] = @$filters[1] if ( $line =~ /datetime/ );
     $linefilter[0] =
@@ -338,7 +331,6 @@ sub calculate {
         $strsth .= " AND $line LIKE ? ";
     }
     $strsth .= " group by $linefield order by $lineorder ";
-    $debug and warn $strsth;
     push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth };
     my $sth = $dbh->prepare($strsth);
     if ( (@linefilter) and ($linefilter[0]) and ($linefilter[1]) ) {
@@ -428,7 +420,6 @@ sub calculate {
     }
 
     $strsth2 .= " group by $colfield order by $colorder ";
-    $debug and warn $strsth2;
     push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth2 };
     my $sth2 = $dbh->prepare($strsth2);
     if ( (@colfilter) and ($colfilter[0]) and ($colfilter[1]) ) {
@@ -471,7 +462,6 @@ sub calculate {
     my %table;
     foreach my $row (@loopline) {
         foreach my $col (@loopcol) {
-            $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} )  ";
             table_set(\%table, $row->{rowtitle}, $col->{coltitle}, 0);
         }
         table_set(\%table, $row->{rowtitle}, 'totalrow', 0);
@@ -569,13 +559,11 @@ sub calculate {
         $strcalc .= " $colorder ";
     }
 
-    ($debug) and warn $strcalc;
     my $dbcalc = $dbh->prepare($strcalc);
     push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strcalc };
     $dbcalc->execute;
     my ( $emptycol, $emptyrow );
     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
-        ($debug) and warn "filling table $row / $col / $value ";
         unless ( defined $col ) {
             $emptycol = 1;
         }
@@ -609,7 +597,6 @@ sub calculate {
         my $total = 0;
         foreach my $row (@looprow) {
             $total += table_get(\%table, $row->{rowtitle}, $col->{coltitle}) || 0;
-            $debug and warn "value added " . table_get(\%table, $row->{rowtitle}, $col->{coltitle}) . "for line " . $row->{rowtitle};
         }
         push @loopfooter, { 'totalcol' => $total };
     }