Bug 17047: add a dedicated page for Mana setup
[srvgit] / reports / issues_stats.pl
index 15cb8a6..3c1ba1f 100755 (executable)
@@ -17,8 +17,7 @@
 # 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 Date::Manip;
@@ -34,6 +33,7 @@ use C4::Members;
 
 use Koha::AuthorisedValues;
 use Koha::DateUtils;
+use Koha::ItemTypes;
 use C4::Members::AttributeTypes;
 
 =head1 NAME
@@ -87,11 +87,12 @@ $sep = "\t" if ($sep eq 'tabulation');
 $template->param(do_it => $do_it,
 );
 
-our $itemtypes = GetItemTypes();
+our $itemtypes = Koha::ItemTypes->search_with_localization->unblessed;
+
 our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
 
-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'] } ) };
+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'] } ) };
 
 our $Bsort1 = GetAuthorisedValues("Bsort1");
 our $Bsort2 = GetAuthorisedValues("Bsort2");
@@ -135,6 +136,12 @@ if ($do_it) {
             print map { $_->{value} . $sep } @$x;
             print $line->{totalrow}, "\n";
         }
+
+        # footer
+        print "TOTAL";
+        $cols = @$results[0]->{loopfooter};
+        print map {$sep.$_->{totalcol}} @$cols;
+        print $sep.@$results[0]->{total};
     }
     exit;
 }
@@ -145,12 +152,6 @@ 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} } ;
-}
-
     # location list
 my @locations;
 foreach (sort keys %$locations) {
@@ -181,7 +182,7 @@ foreach my $attribute_type (@attribute_types) {
 
 $template->param(
     categoryloop => \@patron_categories,
-    itemtypeloop => \@itemtypeloop,
+    itemtypes    => $itemtypes,
     locationloop => \@locations,
     ccodeloop    => \@ccodes,
     hassort1     => $hassort1,
@@ -320,7 +321,7 @@ sub calculate {
           ( $linesource eq 'items' )
           ? " LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
           : " LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
-        $strsth .= " WHERE $line is not null ";
+        $strsth .= " WHERE $line is not null AND $line != '' ";
     }
 
     if ( $line =~ /datetime/ ) {
@@ -352,12 +353,13 @@ sub calculate {
         $sth->execute;
     }
 
+    my $itemtypes_map = { map { $_->{itemtype} => $_ } @{ $itemtypes } };
     while ( my ($celvalue) = $sth->fetchrow ) {
         my %cell = ( rowtitle => $celvalue, totalrow => 0 );    # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display
         $cell{rowtitle_display} =
             ( $line =~ /ccode/ )    ? $ccodes->{$celvalue}
           : ( $line =~ /location/ ) ? $locations->{$celvalue}
-          : ( $line =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description}
+          : ( $line =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
           :                           $celvalue;                               # default fallback
         if ( $line =~ /sort1/ ) {
             foreach (@$Bsort1) {
@@ -408,7 +410,7 @@ sub calculate {
           ( $colsource eq 'items' )
           ? "LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
           : "LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
-        $strsth2 .= " WHERE $column IS NOT NULL ";
+        $strsth2 .= " WHERE $column IS NOT NULL AND $column != '' ";
     }
 
     if ( $column =~ /datetime/ ) {
@@ -446,7 +448,7 @@ sub calculate {
         $cell{coltitle_display} =
             ( $column =~ /ccode/ )    ? $ccodes->{$celvalue}
           : ( $column =~ /location/ ) ? $locations->{$celvalue}
-          : ( $column =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description}
+          : ( $column =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
           :                             $celvalue;                               # default fallback
         if ( $column =~ /sort1/ ) {
             foreach (@$Bsort1) {
@@ -480,14 +482,14 @@ sub calculate {
     # preparing calculation
     my $strcalc = "SELECT ";
     if($line_attribute_type) {
-        $strcalc .= "attribute_$line_attribute_type.attribute AS line_attribute, ";
+        $strcalc .= "TRIM(attribute_$line_attribute_type.attribute) AS line_attribute, ";
     } else {
-        $strcalc .= "$linefield, ";
+        $strcalc .= "TRIM($linefield), ";
     }
     if($column_attribute_type) {
-        $strcalc .= "attribute_$column_attribute_type.attribute AS column_attribute, ";
+        $strcalc .= "TRIM(attribute_$column_attribute_type.attribute) AS column_attribute, ";
     } else {
-        $strcalc .= "$colfield, ";
+        $strcalc .= "TRIM($colfield), ";
     }
     $strcalc .=
         ( $process == 1 ) ? " COUNT(*) "
@@ -642,19 +644,25 @@ sub null_to_zzempty {
 sub table_set {
     my ($table, $row, $col, $val) = @_;
 
-    $table->{ null_to_zzempty(lc($row)) }->{ null_to_zzempty(lc($col)) } = $val;
+    $row = $row // '';
+    $col = $col // '';
+    $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } = $val;
 }
 
 sub table_get {
     my ($table, $row, $col) = @_;
 
-    return $table->{ null_to_zzempty(lc($row)) }->{ null_to_zzempty(lc($col)) };
+    $row = $row // '';
+    $col = $col // '';
+    return $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) };
 }
 
 sub table_inc {
     my ($table, $row, $col, $inc) = @_;
 
-    $table->{ null_to_zzempty(lc($row // '')) }->{ null_to_zzempty(lc($col // '')) } += $inc;
+    $row = $row // '';
+    $col = $col // '';
+    $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } += $inc;
 }
 
 1;