Bug 10694: (follow-up) fix various issues
[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 $userbranch = '';
36     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
37         $userbranch = C4::Context->userenv->{'branch'};
38     }
39     $template->param( branchloop => GetBranchesLoop($userbranch) );
40     return $template;
41 }
42
43 sub calculate {
44         my ($parameters) = @_;
45         my @results =();
46         my $branch = @$parameters[0];
47         my $dbh = C4::Context->dbh;
48         my $sth;
49         if ($branch) {
50                 if (C4::Context->preference('item-level_itypes')) {
51                 $sth = $dbh->prepare("
52         SELECT description, items.itype as itemtype, COUNT(*) AS total 
53                         FROM itemtypes,items         
54                 WHERE items.itype=itemtypes.itemtype         
55                 AND items.holdingbranch=?            
56                 GROUP BY items.itype
57                 ORDER BY itemtypes.description");
58
59                 }
60                 else {
61                 $sth = $dbh->prepare("
62                 SELECT description, biblioitems.itemtype, COUNT(*) AS total 
63                         FROM itemtypes, biblioitems, items 
64                 WHERE biblioitems.itemtype=itemtypes.itemtype 
65                 AND items.biblioitemnumber=biblioitems.biblioitemnumber
66                 AND items.holdingbranch=?
67                         GROUP BY  biblioitems.itemtype
68                         ORDER BY itemtypes.description");
69                 }
70                 $sth->execute($branch);
71         } else {
72                 if (C4::Context->preference('item-level_itypes')) {
73                 $sth = $dbh->prepare("
74                 SELECT description,items.itype AS itemtype, COUNT(*) AS total 
75                         FROM itemtypes,items
76                 WHERE items.itype=itemtypes.itemtype
77                         GROUP BY items.itype
78                         ORDER BY itemtypes.description");
79                 }
80                 else {
81                 $sth = $dbh->prepare("SELECT description, biblioitems.itemtype, COUNT(*) AS total
82                         FROM itemtypes, biblioitems,items 
83                 WHERE biblioitems.itemtype=itemtypes.itemtype 
84                 AND biblioitems.biblioitemnumber = items.biblioitemnumber
85                         GROUP BY biblioitems.itemtype
86                         ORDER BY itemtypes.description");
87                 }
88                 $sth->execute;
89         }
90         my ($description,$biblioitems,$total);
91         my $grantotal = 0;
92         my $count = 0;
93         while (($description,$biblioitems,$total) = $sth->fetchrow) {
94                 my %line;
95                 if($count % 2){
96                         $line{toggle} = 1;
97                         } else {
98                                 $line{toggle} = 0;
99                         }
100                 $line{itemtype} = $description;
101                 $line{count} = $total;
102                 $grantotal += $total;
103                 push @results,\%line;
104                 $count ++;
105         }
106         my @mainloop;
107         my %globalline;
108         $globalline{loopitemtype} = \@results;
109         $globalline{total} = $grantotal;
110         $globalline{branch} = $branch;
111         $globalline{branchname} = GetBranchName($branch);
112         push @mainloop,\%globalline;
113         return \@mainloop;
114 }
115
116 1;