Bug 8062 [SIGNED-OFF] Cart email broken for non english templates
[koha.git] / reports / issues_by_borrower_category.plugin
blob825a492ebb4237daea645a7be6a9c70e18bee8f7
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Search;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Members;
30 use C4::Branch; # GetBranches
32 =head1 NAME
34 plugin that shows a table with issues for categories and borrower
36 =head1 DESCRIPTION
38 this result is quite complex to build...
39 the 2D array contains :
40 * item types on lines
41 * borrowers types on rows
43 If no issues are done, the array must be filled by 0 anyway.
44 So, the script works as this :
45 1- parse the itemtype table to get itemtype descriptions and set itemtype total to 0
46 2- for each borrower category :
47 ** create an array with total = 0 for each itemtype defined in 1
48 ** calculate the total for each itemtype (SQL request)
49 The big hash has the following structure :
50 $itemtypes{itemtype}
51 ->{results}
52 ->{borrowercategorycode} => the total of issues for each cell of the table.
53 ->{total} => the total for the itemtype
54 ->{description} => the itemtype description
56 the borrowertype hash contains description and total for each borrowercategory.
58 the hashes are then translated to hash / arrays to be returned to manager.pl & send to the template
60 =over2
62 =cut
64 sub set_parameters {
65 my ($template) = @_;
66 my $dbh = C4::Context->dbh;
67 my $branches=GetBranches();
68 my @branches;
69 my @select_branch;
70 my %select_branches;
71 push @select_branch,"";
72 $select_branches{""} = "";
73 foreach my $branch (keys %$branches) {
74 push @select_branch, $branch;
75 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
77 my $CGIbranch=CGI::scrolling_list( -name => 'value',
78 -id => 'branch',
79 -values => \@select_branch,
80 -labels => \%select_branches,
81 -size => 1,
82 -multiple => 0 );
83 $template->param(CGIbranch => $CGIbranch);
85 my ($codes,$labels)=GetborCatFromCatType(undef,undef);
86 my @borcatloop;
87 foreach my $thisborcat (sort keys %$labels) {
88 push @borcatloop, {value => $thisborcat,
89 description => $labels->{$thisborcat},
92 $template->param(loopcategories => \@borcatloop);
93 return $template;
95 sub calculate {
96 my ($parameters) = @_;
97 my @results =();
98 # extract parameters
99 my $borrower_category = @$parameters[0];
100 my $branch = @$parameters[1];
101 my $dbh = C4::Context->dbh;
102 # build the SQL query & execute it
104 # 1st, loop every itemtypes.
105 my $sth = $dbh->prepare("select itemtype,description from itemtypes");
106 $sth->execute;
107 my %itemtypes;
108 while (my ($itemtype,$description) = $sth->fetchrow) {
109 $itemtypes{$itemtype}->{description} = $description;
110 $itemtypes{$itemtype}->{total} = 0;
112 # now, parse each category. Before filling the result array, fill it with 0 to have every itemtype column.
113 my $strsth="SELECT itemtype, count( * )
114 FROM issues, borrowers, biblioitems, items
115 WHERE issues.borrowernumber = borrowers.borrowernumber
116 AND items.itemnumber = issues.itemnumber
117 AND biblioitems.biblionumber = items.biblionumber
118 AND borrowers.categorycode = ?";
119 $strsth.= " AND borrowers.branchcode = ".$dbh->quote($branch) if ($branch);
120 $strsth .= " GROUP BY biblioitems.itemtype";
121 my $sth = $dbh->prepare($strsth);
122 my $sthcategories = $dbh->prepare("select categorycode,description from categories");
123 $sthcategories->execute;
124 my %borrowertype;
125 my @categorycodeloop;
126 my $categorycode;
127 my $description;
128 my $borrower_categorycode =0;
129 my @mainloop;
130 my @itemtypeloop;
131 my @loopborrowertype;
132 my @loopborrowertotal;
133 my %globalline;
134 my $hilighted=-1;
135 my $grantotal =0;
136 #If no Borrower-category selected....
137 # Print all
138 if (!$borrower_category) {
139 while ( ($categorycode,$description) = $sthcategories->fetchrow) {
140 $borrowertype{$categorycode}->{description} = $description;
141 $borrowertype{$categorycode}->{total} = 0;
142 my %categorycode;
143 $categorycode{categorycode} = $description;
144 push @categorycodeloop,\%categorycode;
145 foreach my $itemtype (keys %itemtypes) {
146 $itemtypes{$itemtype}->{results}->{$categorycode} = 0;
148 $sth->execute($categorycode);
149 while (my ($itemtype, $total) = $sth->fetchrow) {
150 $itemtypes{$itemtype}->{results}->{$categorycode} = $total;
151 $borrowertype{$categorycode}->{total} += $total;
152 $itemtypes{$itemtype}->{total} += $total;
153 $grantotal += $total;
156 # build the result
157 foreach my $itemtype (keys %itemtypes) {
158 my @loopitemtype;
159 $sthcategories->execute;
160 while (($categorycode,$description) = $sthcategories->fetchrow ) {
161 my %cell;
162 $cell{issues} = $itemtypes{$itemtype}->{results}->{$categorycode};
163 #printf stderr "%s ",$categorycode;
164 push @loopitemtype,\%cell;
166 #printf stderr "\n";
167 my %line;
168 $line{loopitemtype} = \@loopitemtype;
169 if ($itemtypes{$itemtype}->{description}) {
170 $line{itemtype} = $itemtypes{$itemtype}->{description};
171 } else {
172 $line{itemtype} = "$itemtype (no entry in itemtype table)";
174 $line{hilighted} = 1 if $hilighted eq 1;
175 $line{totalitemtype} = $itemtypes{$itemtype}->{total};
176 $hilighted = -$hilighted;
177 push @loopborrowertype, \%line;
179 $sthcategories->execute;
180 while (($categorycode,$description) = $sthcategories->fetchrow ) {
181 my %line;
182 $line{issues} = $borrowertype{$categorycode}->{total};
183 push @loopborrowertotal, \%line;
185 } else {
186 # A Borrower_category has been selected
187 # extracting corresponding data
188 $borrowertype{$categorycode}->{description} = $borrower_category;
189 $borrowertype{$categorycode}->{total} = 0;
190 while (($categorycode,$description) = $sthcategories->fetchrow) {
191 if ($description =~ /$borrower_category/ ) {
192 $borrower_categorycode = $categorycode;
193 my %cc;
194 $cc{categorycode} = $description;
195 push @categorycodeloop,\%cc;
196 foreach my $itemtype (keys %itemtypes) {
197 $itemtypes{$itemtype}->{results}->{$categorycode} = 0;
199 $sth->execute($categorycode);
200 while (my ($itemtype, $total) = $sth->fetchrow) {
201 $itemtypes{$itemtype}->{results}->{$categorycode} = $total;
202 $borrowertype{$categorycode}->{total} += $total;
203 $itemtypes{$itemtype}->{total} += $total;
204 $grantotal +=$total;
208 # build the result
209 foreach my $itemtype (keys %itemtypes) {
210 my @loopitemtype;
211 my %cell;
212 $cell{issues}=$itemtypes{$itemtype}->{results}->{$borrower_categorycode};
213 push @loopitemtype, \%cell;
214 my %line;
215 $line{loopitemtype} = \@loopitemtype;
216 if ($itemtypes{$itemtype}->{description}) {
217 $line{itemtype} = $itemtypes{$itemtype}->{description};
218 } else {
219 $line{itemtype} = "$itemtype (no entry in itemtype table)";
221 $line{hilighted} = 1 if $hilighted eq 1;
222 $line{totalitemtype} = $itemtypes{$itemtype}->{total};
223 $hilighted = -$hilighted;
224 push @loopborrowertype, \%line;
226 my %cell;
227 $cell{issues} = $borrowertype{$borrower_categorycode}->{total};
228 push @loopborrowertotal, \%cell;
230 # the header of the table
231 $globalline{loopborrowertype} = \@loopborrowertype;
232 # the core of the table
233 $globalline{categorycodeloop} = \@categorycodeloop;
234 # the foot (totals by borrower type)
235 $globalline{loopborrowertotal} = \@loopborrowertotal;
236 $globalline{grantotal}= $grantotal;
237 push @mainloop,\%globalline;
238 return \@mainloop;