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