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
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.
30 use C4
::Branch
; # GetBranches
34 plugin that shows a table with issues for categories and borrower
38 this result is quite complex to build...
39 the 2D array contains :
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 :
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
66 my $dbh = C4
::Context
->dbh;
67 my $branches=GetBranches
();
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',
79 -values => \
@select_branch,
80 -labels
=> \
%select_branches,
83 $template->param(CGIbranch
=> $CGIbranch);
85 my ($codes,$labels)=GetborCatFromCatType
(undef,undef);
87 foreach my $thisborcat (sort keys %$labels) {
88 push @borcatloop, {value
=> $thisborcat,
89 description
=> $labels->{$thisborcat},
92 $template->param(loopcategories
=> \
@borcatloop);
96 my ($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");
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;
125 my @categorycodeloop;
128 my $borrower_categorycode =0;
131 my @loopborrowertype;
132 my @loopborrowertotal;
136 #If no Borrower-category selected....
138 if (!$borrower_category) {
139 while ( ($categorycode,$description) = $sthcategories->fetchrow) {
140 $borrowertype{$categorycode}->{description
} = $description;
141 $borrowertype{$categorycode}->{total
} = 0;
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;
157 foreach my $itemtype (keys %itemtypes) {
159 $sthcategories->execute;
160 while (($categorycode,$description) = $sthcategories->fetchrow ) {
162 $cell{issues
} = $itemtypes{$itemtype}->{results
}->{$categorycode};
163 #printf stderr "%s ",$categorycode;
164 push @loopitemtype,\
%cell;
168 $line{loopitemtype
} = \
@loopitemtype;
169 if ($itemtypes{$itemtype}->{description
}) {
170 $line{itemtype
} = $itemtypes{$itemtype}->{description
};
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 ) {
182 $line{issues
} = $borrowertype{$categorycode}->{total
};
183 push @loopborrowertotal, \
%line;
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;
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;
209 foreach my $itemtype (keys %itemtypes) {
212 $cell{issues
}=$itemtypes{$itemtype}->{results
}->{$borrower_categorycode};
213 push @loopitemtype, \
%cell;
215 $line{loopitemtype
} = \
@loopitemtype;
216 if ($itemtypes{$itemtype}->{description
}) {
217 $line{itemtype
} = $itemtypes{$itemtype}->{description
};
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;
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;