Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / reports / catalogue_out.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22
23 use C4::Auth;
24 use C4::Context;
25 use C4::Debug;
26 use C4::Output;
27 # use Date::Manip;  # TODO: add not borrowed since date X criteria
28 use Data::Dumper;
29
30 =head1 catalogue_out
31
32 Report that shows unborrowed items.
33
34 =cut
35
36 my $input   = CGI->new;
37 my $do_it   = $input->param('do_it');
38 my $limit   = $input->param("Limit") || 10;
39 my $column  = $input->param("Criteria");
40 my @filters = $input->multi_param("Filter");
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "reports/catalogue_out.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { reports => 'execute_reports' },
48     }
49 );
50
51 $template->param( do_it => $do_it );
52 if ($do_it) {
53     my $results = calculate( $limit, $column, \@filters );
54     $template->param( mainloop => $results );
55     output_html_with_http_headers $input, $cookie, $template->output;
56     exit;    # in either case, exit after do_it
57 }
58
59 my $itemtypes = Koha::ItemTypes->search_with_localization;
60 $template->param(
61     itemtypes => $itemtypes,
62 );
63 output_html_with_http_headers $input, $cookie, $template->output;
64
65 sub calculate {
66     my ( $limit, $column, $filters ) = @_;
67     my %globalline;
68     my %columns = ();
69     my $dbh     = C4::Context->dbh;
70
71     # Filters
72     # Checking filters
73     #
74     my @loopfilter;
75     for ( my $i = 0 ; $i <= 6 ; $i++ ) {
76         if ( @$filters[$i] ) {
77             my %cell = ( filter => @$filters[$i] );
78             if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
79                 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
80             }
81             $cell{crit} = "Branch"   if ( $i == 0 );
82             $cell{crit} = "Doc Type" if ( $i == 1 );
83             push @loopfilter, \%cell;
84         }
85     }
86     push @loopfilter, { crit => 'limit', filter => $limit } if ($limit);
87     if ($column) {
88         push @loopfilter, { crit => 'by', filter => $column };
89         my $tablename = ( $column =~ /branchcode/ ) ? 'branches' : 'items';
90         $column =
91           ( $column =~ /branchcode/ or $column =~ /itype/ )
92           ? "$tablename.$column"
93           : $column;
94         my $strsth2 =
95           ( $tablename eq 'branches' )
96           ? "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column "
97           : "SELECT $column as coltitle, count(*)                AS coltitle_count FROM $tablename ";
98         if ( $tablename eq 'branches' ) {
99             my $f = @$filters[0];
100             $f =~ s/\*/%/g;
101             $strsth2 .= " AND $column LIKE '$f' ";
102         }
103         $strsth2 .= " GROUP BY $column ORDER BY $column ";    # needed for count
104         push @loopfilter, { crit => 'SQL', sql => 1, filter => $strsth2 };
105         $debug and warn "catalogue_out SQL: " . $strsth2;
106         my $sth2 = $dbh->prepare($strsth2);
107         $sth2->execute;
108
109         while ( my ( $celvalue, $count ) = $sth2->fetchrow ) {
110             ($celvalue) or $celvalue = 'UNKNOWN';
111             $columns{$celvalue} = $count;
112         }
113     }
114
115     my %tables = ( map { $_ => [] } keys %columns );
116
117     # preparing calculation
118     my @exe_args = ();
119     my $query    = "
120         SELECT items.barcode        as barcode,
121                items.homebranch     as branch,
122                items.itemcallnumber as itemcallnumber,
123                biblio.title         as title,
124                biblio.biblionumber  as biblionumber,
125                biblio.author        as author";
126     ($column) and $query .= ",\n$column as col ";
127     $query .= "
128         FROM items
129         LEFT JOIN biblio      USING (biblionumber)
130         LEFT JOIN     issues  USING (itemnumber)
131         LEFT JOIN old_issues  USING (itemnumber)
132           WHERE       issues.itemnumber IS NULL
133            AND    old_issues.itemnumber IS NULL
134         ";
135     if ( $filters->[0] ) {
136         $filters->[0] =~ s/\*/%/g;
137         push @exe_args, $filters->[0];
138         $query .= " AND items.homebranch LIKE ?";
139     }
140     if ( $filters->[1] ) {
141         $filters->[1] =~ s/\*/%/g;
142         push @exe_args, $filters->[1];
143         $query .= " AND items.itype      LIKE ?";
144     }
145     if ($column) {
146         $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";
147         # placeholder handled below
148     }
149     else {
150         $query .= " GROUP BY items.itemnumber ";
151     }
152     $query .= " ORDER BY items.itemcallnumber DESC, barcode";
153     $query .= " LIMIT 0,$limit" if ($limit);
154     $debug and warn "SQL : $query";
155
156     # warn "SQL : $query";
157     push @loopfilter, { crit => 'SQL', sql => 1, filter => $query };
158     my $dbcalc = $dbh->prepare($query);
159
160     if ($column) {
161         foreach ( sort keys %columns ) {
162             # execute(@exe_args,$_) would fail when the array is empty
163             # but @more_exe_args will work
164             my (@more_exe_args) = @exe_args;
165             push @more_exe_args, $_;
166             $dbcalc->execute(@more_exe_args)
167               or die "Query execute(@more_exe_args) failed: $query";
168             while ( my $data = $dbcalc->fetchrow_hashref ) {
169                 my $col = $data->{col} || 'NULL';
170                 $tables{$col} or $tables{$col} = [];
171                 push @{ $tables{$col} }, $data;
172             }
173         }
174     }
175     else {
176         ( scalar @exe_args ) ? $dbcalc->execute(@exe_args) : $dbcalc->execute;
177         while ( my $data = $dbcalc->fetchrow_hashref ) {
178             my $col = $data->{col} || 'NULL';
179             $tables{$col} or $tables{$col} = [];
180             push @{ $tables{$col} }, $data;
181         }
182     }
183
184     foreach my $tablename ( sort keys %tables ) {
185         my (@temptable);
186         my $i = 0;
187         foreach my $cell ( @{ $tables{$tablename} } ) {
188             if ( 0 == $i++ and $debug ) {
189                 my $dump = Dumper($cell);
190                 $dump =~ s/\n/ /gs;
191                 $dump =~ s/\s+/ /gs;
192                 print STDERR "first cell for $tablename: $dump";
193             }
194             push @temptable, $cell;
195         }
196         my $count    = scalar(@temptable);
197         my $allitems = $columns{$tablename};
198         $globalline{total_looptable_count} += $count;
199         $globalline{total_coltitle_count}  += $allitems;
200         push @{ $globalline{looptables} },
201           {
202             looprow         => \@temptable,
203             coltitle        => $tablename,
204             coltitle_count  => $allitems,
205             looptable_count => $count,
206             looptable_first => ($count) ? $temptable[0]->{itemcallnumber} : '',
207             looptable_last  => ($count) ? $temptable[-1]->{itemcallnumber} : '',
208           };
209     }
210
211     # the header of the table
212     $globalline{loopfilter} = \@loopfilter;
213     $globalline{limit}      = $limit;
214     $globalline{column}     = $column;
215     return [ ( \%globalline ) ];    # reference to array of reference to hash
216 }
217
218 1;
219 __END__
220