Bug 2616: Remove unnecessary 'use HTML::Template' calls
[koha.git] / reports / itemtypes.plugin
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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.
20
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::Branch; # GetBranches
29 =head1
30
31 =cut
32
33 sub set_parameters {
34         my ($template) = @_;
35         my $dbh = C4::Context->dbh;
36         my $branches=GetBranches();
37         my @branches;
38         my @select_branch;
39         my %select_branches;
40         push @select_branch,"";
41         $select_branches{""} = "";
42         foreach my $branch (keys %$branches) {
43                 push @select_branch, $branch;
44                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
45         }
46         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
47                                 -id => 'value',
48                                 -values   => \@select_branch,
49                                 -labels   => \%select_branches,
50                                 -size     => 1,
51                                 -multiple => 0 );
52         $template->param(CGIbranch => $CGIbranch);
53         return $template;
54 }
55 sub calculate {
56         my ($parameters) = @_;
57         my @results =();
58         my $branch = @$parameters[0];
59         my $dbh = C4::Context->dbh;
60         my $sth;
61         if ($branch) {
62                 if (C4::Context->preference('item-level_itypes')) {
63                 $sth = $dbh->prepare("
64         SELECT description, items.itype as itemtype, COUNT(*) AS total 
65                         FROM itemtypes,items         
66                 WHERE items.itype=itemtypes.itemtype         
67                 AND items.holdingbranch=?            
68                 GROUP BY  items.itype");
69
70                 }
71                 else {
72                 $sth = $dbh->prepare("
73                 SELECT description, biblioitems.itemtype, COUNT(*) AS total 
74                         FROM itemtypes, biblioitems, items 
75                 WHERE biblioitems.itemtype=itemtypes.itemtype 
76                 AND items.biblioitemnumber=biblioitems.biblioitemnumber
77                 AND items.holdingbranch=?
78                         GROUP BY  biblioitems.itemtype");
79                 }
80                 $sth->execute($branch);
81         } else {
82                 if (C4::Context->preference('item-level_itypes')) {
83                 $sth = $dbh->prepare("
84                 SELECT description,items.itype AS itemtype, COUNT(*) AS total 
85                         FROM itemtypes,items
86                 WHERE items.itype=itemtypes.itemtype
87                         GROUP BY items.itype");
88                 }
89                 else {
90                 $sth = $dbh->prepare("SELECT description, biblioitems.itemtype, COUNT(*) AS total
91                         FROM itemtypes, biblioitems,items 
92                 WHERE biblioitems.itemtype=itemtypes.itemtype 
93                 AND biblioitems.biblioitemnumber = items.biblioitemnumber
94                         GROUP BY biblioitems.itemtype");
95                 }
96                 $sth->execute;
97         }
98         my ($description,$biblioitems,$total);
99         my $grantotal = 0;
100         my $count = 0;
101         while (($description,$biblioitems,$total) = $sth->fetchrow) {
102                 my %line;
103                 if($count % 2){
104                         $line{toggle} = 1;
105                         } else {
106                                 $line{toggle} = 0;
107                         }
108                 $line{itemtype} = $description;
109                 $line{count} = $total;
110                 $grantotal += $total;
111                 push @results,\%line;
112                 $count ++;
113         }
114         my @mainloop;
115         my %globalline;
116         $globalline{loopitemtype} = \@results;
117         $globalline{total} = $grantotal;
118         $globalline{branch} = $branch;
119         push @mainloop,\%globalline;
120         return \@mainloop;
121 }
122
123 1;