Bug 14932: Do not call can_edit_subscription with an empty value
[koha.git] / reports / catalogue_out.pl
blob73595f4b082d013fd8ee7fcd35f2b557ae044a20
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Context;
26 use C4::Debug;
27 use C4::Output;
28 use C4::Koha; # GetItemTypes
29 # use Date::Manip; # TODO: add not borrowed since date X criteria
30 use Data::Dumper;
32 =head1 catalogue_out
34 Report that shows unborrowed items.
36 =cut
38 my $input = new CGI;
39 my $do_it = $input->param('do_it');
40 my $limit = $input->param("Limit") || 10;
41 my $column = $input->param("Criteria");
42 my @filters = $input->multi_param("Filter");
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46 template_name => "reports/catalogue_out.tt",
47 query => $input,
48 type => "intranet",
49 authnotrequired => 0,
50 flagsrequired => { reports => 'execute_reports' },
51 debug => 1,
55 $template->param( do_it => $do_it );
56 if ($do_it) {
57 my $results = calculate( $limit, $column, \@filters );
58 $template->param( mainloop => $results );
59 output_html_with_http_headers $input, $cookie, $template->output;
60 exit; # in either case, exit after do_it
63 # Displaying choices (i.e., not do_it)
64 my $itemtypes = GetItemTypes();
65 my @itemtypeloop;
66 foreach (
67 sort { $itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description} }
68 keys %$itemtypes
71 push @itemtypeloop,
73 value => $_,
74 description => $itemtypes->{$_}->{translated_description},
78 $template->param(
79 itemtypeloop => \@itemtypeloop,
81 output_html_with_http_headers $input, $cookie, $template->output;
83 sub calculate {
84 my ( $limit, $column, $filters ) = @_;
85 my @loopline;
86 my @looprow;
87 my %globalline;
88 my %columns = ();
89 my $dbh = C4::Context->dbh;
91 # Filters
92 # Checking filters
94 my @loopfilter;
95 for ( my $i = 0 ; $i <= 6 ; $i++ ) {
96 if ( @$filters[$i] ) {
97 my %cell = ( filter => @$filters[$i] );
98 if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
99 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
101 $cell{crit} = "Branch" if ( $i == 0 );
102 $cell{crit} = "Doc Type" if ( $i == 1 );
103 push @loopfilter, \%cell;
106 push @loopfilter, { crit => 'limit', filter => $limit } if ($limit);
107 if ($column) {
108 push @loopfilter, { crit => 'by', filter => $column };
109 my $tablename = ( $column =~ /branchcode/ ) ? 'branches' : 'items';
110 $column =
111 ( $column =~ /branchcode/ or $column =~ /itype/ )
112 ? "$tablename.$column"
113 : $column;
114 my $strsth2 =
115 ( $tablename eq 'branches' )
116 ? "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column "
117 : "SELECT $column as coltitle, count(*) AS coltitle_count FROM $tablename ";
118 if ( $tablename eq 'branches' ) {
119 my $f = @$filters[0];
120 $f =~ s/\*/%/g;
121 $strsth2 .= " AND $column LIKE '$f' ";
123 $strsth2 .= " GROUP BY $column ORDER BY $column "; # needed for count
124 push @loopfilter, { crit => 'SQL', sql => 1, filter => $strsth2 };
125 $debug and warn "catalogue_out SQL: " . $strsth2;
126 my $sth2 = $dbh->prepare($strsth2);
127 $sth2->execute;
129 while ( my ( $celvalue, $count ) = $sth2->fetchrow ) {
130 ($celvalue) or $celvalue = 'UNKNOWN';
131 $columns{$celvalue} = $count;
135 my %tables = ( map { $_ => [] } keys %columns );
137 # preparing calculation
138 my @exe_args = ();
139 my $query = "
140 SELECT items.barcode as barcode,
141 items.homebranch as branch,
142 items.itemcallnumber as itemcallnumber,
143 biblio.title as title,
144 biblio.biblionumber as biblionumber,
145 biblio.author as author";
146 ($column) and $query .= ",\n$column as col ";
147 $query .= "
148 FROM items
149 LEFT JOIN biblio USING (biblionumber)
150 LEFT JOIN issues USING (itemnumber)
151 LEFT JOIN old_issues USING (itemnumber)
152 WHERE issues.itemnumber IS NULL
153 AND old_issues.itemnumber IS NULL
155 if ( $filters->[0] ) {
156 $filters->[0] =~ s/\*/%/g;
157 push @exe_args, $filters->[0];
158 $query .= " AND items.homebranch LIKE ?";
160 if ( $filters->[1] ) {
161 $filters->[1] =~ s/\*/%/g;
162 push @exe_args, $filters->[1];
163 $query .= " AND items.itype LIKE ?";
165 if ($column) {
166 $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";
167 # placeholder handled below
169 else {
170 $query .= " GROUP BY items.itemnumber ";
172 $query .= " ORDER BY items.itemcallnumber DESC, barcode";
173 $query .= " LIMIT 0,$limit" if ($limit);
174 $debug and warn "SQL : $query";
176 # warn "SQL : $query";
177 push @loopfilter, { crit => 'SQL', sql => 1, filter => $query };
178 my $dbcalc = $dbh->prepare($query);
180 if ($column) {
181 foreach ( sort keys %columns ) {
182 # execute(@exe_args,$_) would fail when the array is empty
183 # but @more_exe_args will work
184 my (@more_exe_args) = @exe_args;
185 push @more_exe_args, $_;
186 $dbcalc->execute(@more_exe_args)
187 or die "Query execute(@more_exe_args) failed: $query";
188 while ( my $data = $dbcalc->fetchrow_hashref ) {
189 my $col = $data->{col} || 'NULL';
190 $tables{$col} or $tables{$col} = [];
191 push @{ $tables{$col} }, $data;
195 else {
196 ( scalar @exe_args ) ? $dbcalc->execute(@exe_args) : $dbcalc->execute;
197 while ( my $data = $dbcalc->fetchrow_hashref ) {
198 my $col = $data->{col} || 'NULL';
199 $tables{$col} or $tables{$col} = [];
200 push @{ $tables{$col} }, $data;
204 foreach my $tablename ( sort keys %tables ) {
205 my (@temptable);
206 my $i = 0;
207 foreach my $cell ( @{ $tables{$tablename} } ) {
208 if ( 0 == $i++ and $debug ) {
209 my $dump = Dumper($cell);
210 $dump =~ s/\n/ /gs;
211 $dump =~ s/\s+/ /gs;
212 print STDERR "first cell for $tablename: $dump";
214 push @temptable, $cell;
216 my $count = scalar(@temptable);
217 my $allitems = $columns{$tablename};
218 $globalline{total_looptable_count} += $count;
219 $globalline{total_coltitle_count} += $allitems;
220 push @{ $globalline{looptables} },
222 looprow => \@temptable,
223 coltitle => $tablename,
224 coltitle_count => $allitems,
225 looptable_count => $count,
226 looptable_first => ($count) ? $temptable[0]->{itemcallnumber} : '',
227 looptable_last => ($count) ? $temptable[-1]->{itemcallnumber} : '',
231 # the header of the table
232 $globalline{loopfilter} = \@loopfilter;
233 $globalline{limit} = $limit;
234 $globalline{column} = $column;
235 return [ ( \%globalline ) ]; # reference to array of reference to hash
239 __END__