Bug 24322: Simplify condition to separate elements
[koha-ffzg.git] / reports / issues_stats.pl
index 5c7370e..8f86fdb 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,7 +33,8 @@ use C4::Members;
 
 use Koha::AuthorisedValues;
 use Koha::DateUtils;
-use C4::Members::AttributeTypes;
+use Koha::ItemTypes;
+use Koha::Patron::Attribute::Types;
 
 =head1 NAME
 
@@ -78,7 +78,6 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
        template_name => $fullreportname,
        query => $input,
        type => "intranet",
-       authnotrequired => 0,
        flagsrequired => {reports => '*'},
        debug => 0,
 });
@@ -87,11 +86,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,21 +135,18 @@ 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;
 }
 
 
 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} } ;
-}
 
     # location list
 my @locations;
@@ -165,9 +162,11 @@ foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
 my $CGIextChoice = ( 'CSV' ); # FIXME translation
 my $CGIsepChoice=GetDelimiterChoices;
 
-my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes(1);
+my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
+my $attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
 my %attribute_types_by_class;
-foreach my $attribute_type (@attribute_types) {
+while ( my ( $attribute_type ) = $attribute_types->next ) {
+    $attribute_type = $attribute_type->unblessed;
     if ($attribute_type->{authorised_value_category}) {
         my $authorised_values = C4::Koha::GetAuthorisedValues(
             $attribute_type->{authorised_value_category});
@@ -181,7 +180,7 @@ foreach my $attribute_type (@attribute_types) {
 
 $template->param(
     categoryloop => \@patron_categories,
-    itemtypeloop => \@itemtypeloop,
+    itemtypes    => $itemtypes,
     locationloop => \@locations,
     ccodeloop    => \@ccodes,
     hassort1     => $hassort1,
@@ -352,12 +351,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) {
@@ -446,7 +446,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) {
@@ -521,7 +521,7 @@ sub calculate {
         or ( $colsource eq 'items' ) || @$filters[5] || @$filters[6] || @$filters[7] || @$filters[8] || @$filters[9] || @$filters[10] || @$filters[11] || @$filters[12] || @$filters[13] );
 
     $strcalc .= "WHERE 1=1 ";
-    @$filters = map { defined($_) and s/\*/%/g; $_ } @$filters;
+    @$filters = map { my $f = $_; defined($f) and $f =~ s/\*/%/g; $f } @$filters;
     $strcalc .= " AND statistics.datetime >= '" . @$filters[0] . "'"       if ( @$filters[0] );
     $strcalc .= " AND statistics.datetime <= '" . @$filters[1] . " 23:59:59'"       if ( @$filters[1] );
     $strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] . "'" if ( @$filters[2] );