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>.
27 # use Date::Manip; # TODO: add not borrowed since date X criteria
32 Report that shows unborrowed items.
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");
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "reports/catalogue_out.tt",
47 flagsrequired => { reports => 'execute_reports' },
52 $template->param( do_it => $do_it );
54 my $results = calculate( $limit, $column, \@filters );
55 $template->param( mainloop => $results );
56 output_html_with_http_headers $input, $cookie, $template->output;
57 exit; # in either case, exit after do_it
60 my $itemtypes = Koha::ItemTypes->search_with_localization;
62 itemtypes => $itemtypes,
64 output_html_with_http_headers $input, $cookie, $template->output;
67 my ( $limit, $column, $filters ) = @_;
72 my $dbh = C4::Context->dbh;
78 for ( my $i = 0 ; $i <= 6 ; $i++ ) {
79 if ( @$filters[$i] ) {
80 my %cell = ( filter => @$filters[$i] );
81 if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
82 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
84 $cell{crit} = "Branch" if ( $i == 0 );
85 $cell{crit} = "Doc Type" if ( $i == 1 );
86 push @loopfilter, \%cell;
89 push @loopfilter, { crit => 'limit', filter => $limit } if ($limit);
91 push @loopfilter, { crit => 'by', filter => $column };
92 my $tablename = ( $column =~ /branchcode/ ) ? 'branches' : 'items';
94 ( $column =~ /branchcode/ or $column =~ /itype/ )
95 ? "$tablename.$column"
98 ( $tablename eq 'branches' )
99 ? "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column "
100 : "SELECT $column as coltitle, count(*) AS coltitle_count FROM $tablename ";
101 if ( $tablename eq 'branches' ) {
102 my $f = @$filters[0];
104 $strsth2 .= " AND $column LIKE '$f' ";
106 $strsth2 .= " GROUP BY $column ORDER BY $column "; # needed for count
107 push @loopfilter, { crit => 'SQL', sql => 1, filter => $strsth2 };
108 $debug and warn "catalogue_out SQL: " . $strsth2;
109 my $sth2 = $dbh->prepare($strsth2);
112 while ( my ( $celvalue, $count ) = $sth2->fetchrow ) {
113 ($celvalue) or $celvalue = 'UNKNOWN';
114 $columns{$celvalue} = $count;
118 my %tables = ( map { $_ => [] } keys %columns );
120 # preparing calculation
123 SELECT items.barcode as barcode,
124 items.homebranch as branch,
125 items.itemcallnumber as itemcallnumber,
126 biblio.title as title,
127 biblio.biblionumber as biblionumber,
128 biblio.author as author";
129 ($column) and $query .= ",\n$column as col ";
132 LEFT JOIN biblio USING (biblionumber)
133 LEFT JOIN issues USING (itemnumber)
134 LEFT JOIN old_issues USING (itemnumber)
135 WHERE issues.itemnumber IS NULL
136 AND old_issues.itemnumber IS NULL
138 if ( $filters->[0] ) {
139 $filters->[0] =~ s/\*/%/g;
140 push @exe_args, $filters->[0];
141 $query .= " AND items.homebranch LIKE ?";
143 if ( $filters->[1] ) {
144 $filters->[1] =~ s/\*/%/g;
145 push @exe_args, $filters->[1];
146 $query .= " AND items.itype LIKE ?";
149 $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";
150 # placeholder handled below
153 $query .= " GROUP BY items.itemnumber ";
155 $query .= " ORDER BY items.itemcallnumber DESC, barcode";
156 $query .= " LIMIT 0,$limit" if ($limit);
157 $debug and warn "SQL : $query";
159 # warn "SQL : $query";
160 push @loopfilter, { crit => 'SQL', sql => 1, filter => $query };
161 my $dbcalc = $dbh->prepare($query);
164 foreach ( sort keys %columns ) {
165 # execute(@exe_args,$_) would fail when the array is empty
166 # but @more_exe_args will work
167 my (@more_exe_args) = @exe_args;
168 push @more_exe_args, $_;
169 $dbcalc->execute(@more_exe_args)
170 or die "Query execute(@more_exe_args) failed: $query";
171 while ( my $data = $dbcalc->fetchrow_hashref ) {
172 my $col = $data->{col} || 'NULL';
173 $tables{$col} or $tables{$col} = [];
174 push @{ $tables{$col} }, $data;
179 ( scalar @exe_args ) ? $dbcalc->execute(@exe_args) : $dbcalc->execute;
180 while ( my $data = $dbcalc->fetchrow_hashref ) {
181 my $col = $data->{col} || 'NULL';
182 $tables{$col} or $tables{$col} = [];
183 push @{ $tables{$col} }, $data;
187 foreach my $tablename ( sort keys %tables ) {
190 foreach my $cell ( @{ $tables{$tablename} } ) {
191 if ( 0 == $i++ and $debug ) {
192 my $dump = Dumper($cell);
195 print STDERR "first cell for $tablename: $dump";
197 push @temptable, $cell;
199 my $count = scalar(@temptable);
200 my $allitems = $columns{$tablename};
201 $globalline{total_looptable_count} += $count;
202 $globalline{total_coltitle_count} += $allitems;
203 push @{ $globalline{looptables} },
205 looprow => \@temptable,
206 coltitle => $tablename,
207 coltitle_count => $allitems,
208 looptable_count => $count,
209 looptable_first => ($count) ? $temptable[0]->{itemcallnumber} : '',
210 looptable_last => ($count) ? $temptable[-1]->{itemcallnumber} : '',
214 # the header of the table
215 $globalline{loopfilter} = \@loopfilter;
216 $globalline{limit} = $limit;
217 $globalline{column} = $column;
218 return [ ( \%globalline ) ]; # reference to array of reference to hash