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