Bug 6934: fixed encoding issue
[koha.git] / reports / issues_by_borrower_category.plugin
old mode 100644 (file)
new mode 100755 (executable)
index 868fcbc..1c2d1d1
@@ -1,32 +1,33 @@
 #!/usr/bin/perl
 
-# $Id$
 
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 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 C4::Auth;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Context;
-use HTML::Template;
 use C4::Search;
 use C4::Output;
 use C4::Koha;
+use C4::Members;
+
+use Koha::Patron::Categories;
 
 =head1 NAME
 
@@ -61,27 +62,13 @@ the hashes are then translated to hash / arrays to be returned to manager.pl & s
 =cut
 
 sub set_parameters {
-       my ($template) = @_;
-       my $dbh = C4::Context->dbh;
-       my $branches=getbranches();
-       my @branches;
-       my @select_branch;
-       my %select_branches;
-       push @select_branch,"";
-       $select_branches{""} = "";
-       foreach my $branch (keys %$branches) {
-               push @select_branch, $branch;
-               $select_branches{$branch} = $branches->{$branch}->{'branchname'};
-       }
-       my $CGIbranch=CGI::scrolling_list( -name     => 'value',
-                               -id => 'value',
-                               -values   => \@select_branch,
-                               -labels   => \%select_branches,
-                               -size     => 1,
-                               -multiple => 0 );
-       $template->param(CGIbranch => $CGIbranch);
-       return $template;
+    my ($template) = @_;
+
+    my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
+    $template->param( patron_categories => $patron_categories );
+    return $template;
 }
+
 sub calculate {
        my ($parameters) = @_;
        my @results =();
@@ -100,75 +87,132 @@ sub calculate {
                $itemtypes{$itemtype}->{total} = 0;
        }
 # now, parse each category. Before filling the result array, fill it with 0 to have every itemtype column.
-       my $sth = $dbh->prepare("SELECT itemtype, count( * )
+       my $strsth="SELECT itemtype, count( * )
                                FROM issues, borrowers, biblioitems, items
                                WHERE issues.borrowernumber = borrowers.borrowernumber 
                                        AND items.itemnumber = issues.itemnumber 
                                        AND biblioitems.biblionumber = items.biblionumber 
-                                       AND borrowers.categorycode = ?
-                               GROUP BY biblioitems.itemtype");
+                                       AND borrowers.categorycode = ?";
+       $strsth.= " AND borrowers.branchcode = ".$dbh->quote($branch) if ($branch);
+       $strsth .= " GROUP BY biblioitems.itemtype";
+       my $sth = $dbh->prepare($strsth);
        my $sthcategories = $dbh->prepare("select categorycode,description from categories");
        $sthcategories->execute;
        my %borrowertype;
        my @categorycodeloop;
-       while (my ($categorycode,$description) = $sthcategories->fetchrow) {
-               $borrowertype{$categorycode}->{description} = $description;
-               $borrowertype{$categorycode}->{total} = 0;
-               my %categorycode;
-               $categorycode{categorycode} = $description;
-               push @categorycodeloop,\%categorycode;
-               foreach my $itemtype (keys %itemtypes) {
-                       $itemtypes{$itemtype}->{results}->{$categorycode} = 0;
-               }
-               $sth->execute($categorycode);
-               while (my ($itemtype, $total) = $sth->fetchrow) {
-                       $itemtypes{$itemtype}->{results}->{$categorycode} = $total;
-                       $borrowertype{$categorycode}->{total} += $total;
-                       $itemtypes{$itemtype}->{total} += $total;
-               }
-       }
-       my $grantotal = 0;
-# build the result
+       my $categorycode;
+       my $description;
+       my $borrower_categorycode =0;
        my @mainloop;
        my @itemtypeloop;
        my @loopborrowertype;
+       my @loopborrowertotal;
        my %globalline;
        my $hilighted=-1;
-       foreach my $itemtype (keys %itemtypes) {
-               my @loopitemtype;
-               foreach my $categorycode (keys %{$itemtypes{$itemtype}->{results}}) {
-                       my %cell;
-                       $cell{issues} = $itemtypes{$itemtype}->{results}->{$categorycode};
-                       push @loopitemtype,\%cell;
+       my $grantotal =0;
+       #If no Borrower-category selected....
+       # Print all 
+       if (!$borrower_category) {
+               while ( ($categorycode,$description) = $sthcategories->fetchrow) {
+                       $borrowertype{$categorycode}->{description} = $description;
+                       $borrowertype{$categorycode}->{total} = 0;
+                       my %categorycode;
+                       $categorycode{categorycode} = $description;
+                       push @categorycodeloop,\%categorycode;
+                       foreach my $itemtype (keys %itemtypes) {
+                               $itemtypes{$itemtype}->{results}->{$categorycode} = 0;
+                       }
+                       $sth->execute($categorycode);
+                       while (my ($itemtype, $total) = $sth->fetchrow) {
+                               $itemtypes{$itemtype}->{results}->{$categorycode} = $total;
+                               $borrowertype{$categorycode}->{total} += $total;
+                               $itemtypes{$itemtype}->{total} += $total;
+                               $grantotal += $total;
+                       }
+               }
+               # build the result
+               foreach my $itemtype (keys %itemtypes) {
+                       my @loopitemtype;
+                       $sthcategories->execute;
+                       while (($categorycode,$description) =  $sthcategories->fetchrow ) {
+                               my %cell;
+                               $cell{issues} = $itemtypes{$itemtype}->{results}->{$categorycode};
+                               #printf stderr "%s      ",$categorycode;
+                               push @loopitemtype,\%cell;
+                       }
+                       #printf stderr "\n";
+                       my %line;
+                       $line{loopitemtype} = \@loopitemtype;
+                       if ($itemtypes{$itemtype}->{description}) {
+                               $line{itemtype} = $itemtypes{$itemtype}->{description};
+                       } else {
+                               $line{itemtype} = "$itemtype (no entry in itemtype table)";
+                       }
+                       $line{hilighted} = 1 if $hilighted eq 1;
+                       $line{totalitemtype} = $itemtypes{$itemtype}->{total};
+                       $hilighted = -$hilighted;
+                       push @loopborrowertype, \%line;
+               }
+               $sthcategories->execute;
+               while (($categorycode,$description) =  $sthcategories->fetchrow ) {
+                       my %line;
+                       $line{issues} = $borrowertype{$categorycode}->{total};
+                       push @loopborrowertotal, \%line;
+               }
+       } else {
+               # A Borrower_category has been selected
+               # extracting corresponding data
+               $borrowertype{$categorycode}->{description} = $borrower_category;
+               $borrowertype{$categorycode}->{total} = 0;
+               while (($categorycode,$description) = $sthcategories->fetchrow) {
+                       if ($description =~ /$borrower_category/ ) {
+                               $borrower_categorycode = $categorycode;
+                               my %cc;
+                               $cc{categorycode} = $description;
+                               push @categorycodeloop,\%cc;
+                               foreach my $itemtype (keys %itemtypes) {
+                                       $itemtypes{$itemtype}->{results}->{$categorycode} = 0;
+                               }
+                               $sth->execute($categorycode);
+                               while (my ($itemtype, $total) = $sth->fetchrow) {
+                                       $itemtypes{$itemtype}->{results}->{$categorycode} = $total;
+                                       $borrowertype{$categorycode}->{total} += $total;
+                                       $itemtypes{$itemtype}->{total} += $total;
+                                       $grantotal +=$total;
+                               }
+                       }
                }
-               my %line;
-               $line{loopitemtype} = \@loopitemtype;
-               if ($itemtypes{$itemtype}->{description}) {
-                       $line{itemtype} = $itemtypes{$itemtype}->{description};
-               } else {
-                       $line{itemtype} = "$itemtype (no entry in itemtype table)";
+               # build the result
+               foreach my $itemtype (keys %itemtypes) {
+                       my @loopitemtype;
+                       my %cell;
+                       $cell{issues}=$itemtypes{$itemtype}->{results}->{$borrower_categorycode};
+                       push @loopitemtype, \%cell;
+                       my %line;
+                       $line{loopitemtype} = \@loopitemtype;
+                       if ($itemtypes{$itemtype}->{description}) {
+                               $line{itemtype} = $itemtypes{$itemtype}->{description};
+                       } else {
+                               $line{itemtype} = "$itemtype (no entry in itemtype table)";
+                       }
+                       $line{hilighted} = 1 if $hilighted eq 1;
+                       $line{totalitemtype} = $itemtypes{$itemtype}->{total};
+                       $hilighted = -$hilighted;
+                       push @loopborrowertype, \%line;
                }
-               $line{hilighted} = 1 if $hilighted eq 1;
-               $line{totalitemtype} = $itemtypes{$itemtype}->{total};
-               $hilighted = -$hilighted;
-               push @loopborrowertype, \%line;
+               my %cell;
+               $cell{issues} = $borrowertype{$borrower_categorycode}->{total};
+               push @loopborrowertotal, \%cell;
        }
        # the header of the table
        $globalline{loopborrowertype} = \@loopborrowertype;
        # the core of the table
        $globalline{categorycodeloop} = \@categorycodeloop;
        # the foot (totals by borrower type)
-       my @loopborrowertotal;
-       foreach my $categorycode (keys %borrowertype) {
-               my %line;
-               $line{issues} = $borrowertype{$categorycode}->{total};
-               push @loopborrowertotal, \%line;
-       }
        $globalline{loopborrowertotal} = \@loopborrowertotal;
-#      $globalline{total} = $grantotal;
-#      $globalline{borrower_category} = $borrower_category;
+       $globalline{grantotal}= $grantotal;
        push @mainloop,\%globalline;
        return \@mainloop;
 }
 
-1;
\ No newline at end of file
+1;