Fix for Bug 3431 - Catalog by itemtype report pulling from holdingbranch
[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 $default;
39         my @select_branch;
40         my %select_branches;
41         push @select_branch,"";
42         $select_branches{""} = "";
43         for my $branch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
44                 push @select_branch, $branch;
45                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
46                 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
47         }
48         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
49                                 -id => 'value',
50                                 -values   => \@select_branch,
51                                 -labels   => \%select_branches,
52                                 -size     => 1,
53                                 -multiple => 0,
54                                 -default => $default, );
55         $template->param(CGIbranch => $CGIbranch);
56         return $template;
57 }
58 sub calculate {
59         my ($parameters) = @_;
60         my @results =();
61         my $branch = @$parameters[0];
62         my $dbh = C4::Context->dbh;
63         my $sth;
64         if ($branch) {
65                 if (C4::Context->preference('item-level_itypes')) {
66                 $sth = $dbh->prepare("
67         SELECT description, items.itype as itemtype, COUNT(*) AS total 
68                         FROM itemtypes,items         
69                 WHERE items.itype=itemtypes.itemtype         
70                 AND items.holdingbranch=?            
71                 GROUP BY items.itype
72                 ORDER BY itemtypes.description");
73
74                 }
75                 else {
76                 $sth = $dbh->prepare("
77                 SELECT description, biblioitems.itemtype, COUNT(*) AS total 
78                         FROM itemtypes, biblioitems, items 
79                 WHERE biblioitems.itemtype=itemtypes.itemtype 
80                 AND items.biblioitemnumber=biblioitems.biblioitemnumber
81                 AND items.holdingbranch=?
82                         GROUP BY  biblioitems.itemtype
83                         ORDER BY itemtypes.description");
84                 }
85                 $sth->execute($branch);
86         } else {
87                 if (C4::Context->preference('item-level_itypes')) {
88                 $sth = $dbh->prepare("
89                 SELECT description,items.itype AS itemtype, COUNT(*) AS total 
90                         FROM itemtypes,items
91                 WHERE items.itype=itemtypes.itemtype
92                         GROUP BY items.itype
93                         ORDER BY itemtypes.description");
94                 }
95                 else {
96                 $sth = $dbh->prepare("SELECT description, biblioitems.itemtype, COUNT(*) AS total
97                         FROM itemtypes, biblioitems,items 
98                 WHERE biblioitems.itemtype=itemtypes.itemtype 
99                 AND biblioitems.biblioitemnumber = items.biblioitemnumber
100                         GROUP BY biblioitems.itemtype
101                         ORDER BY itemtypes.description");
102                 }
103                 $sth->execute;
104         }
105         my ($description,$biblioitems,$total);
106         my $grantotal = 0;
107         my $count = 0;
108         while (($description,$biblioitems,$total) = $sth->fetchrow) {
109                 my %line;
110                 if($count % 2){
111                         $line{toggle} = 1;
112                         } else {
113                                 $line{toggle} = 0;
114                         }
115                 $line{itemtype} = $description;
116                 $line{count} = $total;
117                 $grantotal += $total;
118                 push @results,\%line;
119                 $count ++;
120         }
121         my @mainloop;
122         my %globalline;
123         $globalline{loopitemtype} = \@results;
124         $globalline{total} = $grantotal;
125         $globalline{branch} = $branch;
126         $globalline{branchname} = GetBranchName($branch);
127         push @mainloop,\%globalline;
128         return \@mainloop;
129 }
130
131 1;