Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / reports / catalogue_stats.pl
blobb0a72b00949f403fd36680f4f7836c2bde990a35
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Reports;
29 use C4::Circulation;
30 use C4::Biblio qw/GetMarcSubfieldStructureFromKohaField/;
32 use Koha::DateUtils;
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =cut
42 our $debug = 0;
43 my $input = new CGI;
44 my $fullreportname = "reports/catalogue_stats.tt";
45 my $do_it = $input->param('do_it');
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my $cellvalue = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
49 my @filters = $input->multi_param("Filter");
50 my $cotedigits = $input->param("cotedigits");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 our $sep = $input->param("sep");
54 $sep = "\t" if ($sep eq 'tabulation');
55 my $item_itype;
56 if(C4::Context->preference('item-level_itypes')) {
57 $item_itype = "items\.itype"
58 } else {
59 $item_itype = "itemtype";
61 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
62 $line = "copyrightdate";
64 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
65 $column = "copyrightdate";
68 my ($template, $borrowernumber, $cookie)
69 = get_template_and_user({template_name => $fullreportname,
70 query => $input,
71 type => "intranet",
72 authnotrequired => 0,
73 flagsrequired => {reports => '*'},
74 debug => 1,
75 });
76 $template->param(do_it => $do_it);
77 if ($do_it) {
78 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
79 if ( $output eq "screen" ) {
80 $template->param( mainloop => $results );
81 output_html_with_http_headers $input, $cookie, $template->output;
82 exit;
83 } else {
84 print $input->header(
85 -type => 'text/csv',
86 -encoding => 'utf-8',
87 -attachment => "$basename.csv",
88 -name => "$basename.csv"
90 my $cols = @$results[0]->{loopcol};
91 my $lines = @$results[0]->{looprow};
92 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
93 foreach my $col (@$cols) {
94 print $col->{coltitle} . $sep;
96 print "Total\n";
97 foreach my $line (@$lines) {
98 my $x = $line->{loopcell};
99 print $line->{rowtitle} . $sep;
100 foreach my $cell (@$x) {
101 print $cell->{value} . $sep;
103 print $line->{totalrow};
104 print "\n";
106 print "TOTAL";
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col (@$cols) {
109 print $sep. $col->{totalcol};
111 print $sep. @$results[0]->{total};
112 exit;
114 } else {
115 my $dbh = C4::Context->dbh;
116 my @values;
117 my %labels;
118 my $count=0;
119 my $req;
120 my @select;
122 my $itemtypes = GetItemTypes( style => 'array' );
124 my $authvals = GetKohaAuthorisedValues("items.ccode");
125 my @authvals;
126 foreach ( sort { $authvals->{$a} cmp $authvals->{$b} || $a cmp $b } keys %$authvals ) {
127 push @authvals, { code => $_, description => $authvals->{$_} };
130 my $locations = GetKohaAuthorisedValues("items.location");
131 my @locations;
132 foreach ( sort keys %$locations ) {
133 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
136 foreach my $kohafield (qw(items.notforloan items.materials)) {
137 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
138 if($subfield_structure) {
139 my $avlist;
140 my $avcategory = $subfield_structure->{authorised_value};
141 if($avcategory) {
142 $avlist = GetAuthorisedValues($avcategory);
144 my $kf = $kohafield;
145 $kf =~ s/^items\.//;
146 $template->param(
147 $kf => 1,
148 $kf."_label" => $subfield_structure->{liblibrarian},
149 $kf."_avlist" => $avlist
154 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
156 $template->param(
157 itemtypes => $itemtypes,
158 locationloop => \@locations,
159 authvals => \@authvals,
160 CGIextChoice => \@mime,
161 CGIsepChoice => GetDelimiterChoices,
162 item_itype => $item_itype,
166 output_html_with_http_headers $input, $cookie, $template->output;
168 ## End of Main Body
171 sub calculate {
172 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
173 my @mainloop;
174 my @loopfooter;
175 my @loopcol;
176 my @loopline;
177 my @looprow;
178 my %globalline;
179 my $grantotal = 0;
180 my $barcodelike = @$filters[16];
181 my $barcodefilter = @$filters[17];
182 my $not;
183 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
185 my $dbh = C4::Context->dbh;
187 # if barcodefilter is empty set as %
188 if ($barcodefilter) {
190 # Check if barcodefilter is "like" or "not like"
191 if ( !$barcodelike ) {
192 $not = "not";
195 # Change * to %
196 $barcodefilter =~ s/\*/%/g;
199 # Filters
200 # Checking filters
202 my @loopfilter;
203 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
204 my %cell;
205 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
206 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
207 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
209 $cell{filter} .= @$filters[$i];
210 $cell{crit} .=
211 ( $i == 0 ) ? "Item CallNumber From"
212 : ( $i == 1 ) ? "Item CallNumber To"
213 : ( $i == 2 ) ? "Item type"
214 : ( $i == 3 ) ? "Publisher"
215 : ( $i == 4 ) ? "Publication year From"
216 : ( $i == 5 ) ? "Publication year To"
217 : ( $i == 6 ) ? "Library"
218 : ( $i == 7 ) ? "Shelving Location"
219 : ( $i == 8 ) ? "Collection Code"
220 : ( $i == 9 ) ? "Status"
221 : ( $i == 10 ) ? "Materials"
222 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
223 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
224 : ( $i == 13 ) ? "Date acquired (item) from"
225 : ( $i == 14 ) ? "Date acquired (item) to"
226 : ( $i == 15 ) ? "Date deleted (item) from"
227 : ( $i == 16 ) ? "Date deleted (item) to"
228 : '';
230 push @loopfilter, \%cell;
234 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
235 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
236 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
237 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
239 my @linefilter;
240 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
241 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
242 if ( C4::Context->preference('item-level_itypes') ) {
243 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
244 } else {
245 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
247 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
248 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
249 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
251 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
252 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
253 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
254 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
255 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
256 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
257 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
258 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
259 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
261 my @colfilter;
262 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
263 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
264 if ( C4::Context->preference('item-level_itypes') ) {
265 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
266 } else {
267 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
269 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
270 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
271 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
272 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
273 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
274 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
275 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
276 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
277 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
278 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
279 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
280 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
282 # 1st, loop rows.
283 my $origline = $line;
284 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
285 my $linefield;
286 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
287 $linefield = "left($line,$cotedigits)";
288 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
289 $linefield = "DATE($line)";
290 } else {
291 $linefield = $line;
294 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
295 LEFT JOIN biblioitems USING (biblioitemnumber)
296 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
297 WHERE 1 ";
298 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
299 if (@linefilter) {
300 if ( $linefilter[1] ) {
301 $strsth .= " AND $line >= ? ";
302 $strsth .= " AND $line <= ? ";
303 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
304 $linefilter[0] =~ s/\*/%/g;
305 $strsth .= " AND $line LIKE ? ";
308 $strsth .= " ORDER BY $linefield";
309 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
311 my $sth = $dbh->prepare($strsth);
312 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
313 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
314 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
315 $sth->execute( $linefilter[0], $linefilter[1] );
316 } elsif ( $barcodefilter and $linefilter[0] ) {
317 $sth->execute( $barcodefilter, $linefilter[0] );
318 } elsif ( $linefilter[0] ) {
319 $sth->execute($linefilter[0]);
320 } elsif ($barcodefilter) {
321 $sth->execute($barcodefilter);
322 } else {
323 $sth->execute();
325 my $rowauthvals = GetKohaAuthorisedValues($origline);
326 while ( my ($celvalue) = $sth->fetchrow ) {
327 my %cell;
328 if (defined $celvalue and $celvalue ne '') {
329 if($rowauthvals and $rowauthvals->{$celvalue}) {
330 $cell{rowtitle} = $rowauthvals->{$celvalue};
331 } else {
332 $cell{rowtitle} = $celvalue;
334 $cell{value} = $celvalue;
336 else {
337 $cell{rowtitle} = "NULL";
338 $cell{value} = "zzEMPTY";
340 $cell{totalrow} = 0;
341 push @loopline, \%cell;
344 # 2nd, loop cols.
345 my $origcolumn = $column;
346 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
347 my $colfield;
348 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
349 $colfield = "left($column,$cotedigits)";
350 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
351 $colfield = "DATE($column)";
352 } else {
353 $colfield = $column;
356 my $strsth2 = "
357 SELECT distinctrow $colfield
358 FROM $itemstable
359 LEFT JOIN biblioitems
360 USING (biblioitemnumber)
361 LEFT JOIN biblio
362 ON (biblioitems.biblionumber = biblio.biblionumber)
363 WHERE 1 ";
364 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
366 if ( (@colfilter) and ( $colfilter[1] ) ) {
367 $strsth2 .= " AND $column >= ? AND $column <= ?";
368 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
369 $colfilter[0] =~ s/\*/%/g;
370 $strsth2 .= " AND $column LIKE ? ";
372 $strsth2 .= " ORDER BY $colfield";
373 $debug and print STDERR "SQL: $strsth2";
374 my $sth2 = $dbh->prepare($strsth2);
375 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
376 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
377 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
378 $sth2->execute( $colfilter[0], $colfilter[1] );
379 } elsif ( $barcodefilter && $colfilter[0] ) {
380 $sth2->execute( $barcodefilter , $colfilter[0] );
381 } elsif ( $colfilter[0]) {
382 $sth2->execute( $colfilter[0] );
383 } elsif ($barcodefilter) {
384 $sth2->execute($barcodefilter);
385 } else {
386 $sth2->execute();
388 my $colauthvals = GetKohaAuthorisedValues($origcolumn);
389 while ( my ($celvalue) = $sth2->fetchrow ) {
390 my %cell;
391 if (defined $celvalue and $celvalue ne '') {
392 if($colauthvals and $colauthvals->{$celvalue}) {
393 $cell{coltitle} = $colauthvals->{$celvalue};
394 } else {
395 $cell{coltitle} = $celvalue;
397 $cell{value} = $celvalue;
399 else {
400 $cell{coltitle} = "NULL";
401 $cell{value} = "zzEMPTY";
403 $cell{totalcol} = 0;
404 push @loopcol, \%cell;
407 my $i = 0;
408 my @totalcol;
409 my $hilighted = -1;
411 #Initialization of cell values.....
412 my %table;
414 foreach my $row (@loopline) {
415 foreach my $col (@loopcol) {
416 $table{ $row->{value} }->{ $col->{value} } = 0;
418 $table{ $row->{value} }->{totalrow} = 0;
421 # preparing calculation
422 my $select_cellvalue = " COUNT(*) ";
423 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
424 my $strcalc = "
425 SELECT $linefield, $colfield, $select_cellvalue
426 FROM $itemstable
427 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
428 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
429 WHERE 1 ";
431 my @sqlargs;
433 if ($barcodefilter) {
434 $strcalc .= "AND barcode $not like ? ";
435 push @sqlargs, $barcodefilter;
438 if ( @$filters[0] ) {
439 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
440 @$filters[0] =~ s/\*/%/g;
441 push @sqlargs, @$filters[0];
444 if ( @$filters[1] ) {
445 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
446 @$filters[1] =~ s/\*/%/g;
447 push @sqlargs, @$filters[1];
450 if ( @$filters[2] ) {
451 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
452 @$filters[2] =~ s/\*/%/g;
453 push @sqlargs, @$filters[2];
456 if ( @$filters[3] ) {
457 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
458 @$filters[3] =~ s/\*/%/g;
459 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
460 push @sqlargs, @$filters[3];
462 if ( @$filters[4] ) {
463 $strcalc .= " AND " .
464 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
465 . "> ? ";
466 @$filters[4] =~ s/\*/%/g;
467 push @sqlargs, @$filters[4];
469 if ( @$filters[5] ) {
470 @$filters[5] =~ s/\*/%/g;
471 $strcalc .= " AND " .
472 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
473 . "< ? ";
474 push @sqlargs, @$filters[5];
476 if ( @$filters[6] ) {
477 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
478 @$filters[6] =~ s/\*/%/g;
479 push @sqlargs, @$filters[6];
481 if ( @$filters[7] ) {
482 $strcalc .= " AND $itemstable.location LIKE ? ";
483 @$filters[7] =~ s/\*/%/g;
484 push @sqlargs, @$filters[7];
486 if ( @$filters[8] ) {
487 $strcalc .= " AND $itemstable.ccode LIKE ? ";
488 @$filters[8] =~ s/\*/%/g;
489 push @sqlargs, @$filters[8];
491 if ( defined @$filters[9] and @$filters[9] ne '' ) {
492 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
493 @$filters[9] =~ s/\*/%/g;
494 push @sqlargs, @$filters[9];
496 if ( defined @$filters[10] and @$filters[10] ne '' ) {
497 $strcalc .= " AND $itemstable.materials LIKE ? ";
498 @$filters[10] =~ s/\*/%/g;
499 push @sqlargs, @$filters[10];
501 if ( @$filters[13] ) {
502 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
503 @$filters[13] =~ s/\*/%/g;
504 push @sqlargs, @$filters[13];
506 if ( @$filters[14] ) {
507 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
508 @$filters[14] =~ s/\*/%/g;
509 push @sqlargs, @$filters[14];
511 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
512 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
513 @$filters[15] =~ s/\*/%/g;
514 push @sqlargs, @$filters[15];
516 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
517 @$filters[16] =~ s/\*/%/g;
518 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
519 push @sqlargs, @$filters[16];
521 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
522 $debug and warn "SQL: $strcalc";
523 my $dbcalc = $dbh->prepare($strcalc);
524 $dbcalc->execute(@sqlargs);
526 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
528 $col = "zzEMPTY" if ( !defined($col) );
529 $row = "zzEMPTY" if ( !defined($row) );
531 $table{$row}->{$col} += $value;
532 $table{$row}->{totalrow} += $value;
533 $grantotal += $value;
536 foreach my $row ( @loopline ) {
537 my @loopcell;
539 #@loopcol ensures the order for columns is common with column titles
540 # and the number matches the number of columns
541 foreach my $col (@loopcol) {
542 my $value = $table{$row->{value}}->{ $col->{value} };
543 push @loopcell, { value => $value };
545 push @looprow,
546 { 'rowtitle' => $row->{rowtitle},
547 'value' => $row->{value},
548 'loopcell' => \@loopcell,
549 'hilighted' => ( $hilighted *= -1 > 0 ),
550 'totalrow' => $table{$row->{value}}->{totalrow}
554 foreach my $col (@loopcol) {
555 my $total = 0;
556 foreach my $row (@looprow) {
557 $total += $table{ $row->{value} }->{ $col->{value} };
560 push @loopfooter, { 'totalcol' => $total };
563 # the header of the table
564 $globalline{loopfilter} = \@loopfilter;
566 # the core of the table
567 $globalline{looprow} = \@looprow;
568 $globalline{loopcol} = \@loopcol;
570 # the foot (totals by borrower type)
571 $globalline{loopfooter} = \@loopfooter;
572 $globalline{total} = $grantotal;
573 $globalline{line} = $line;
574 $globalline{column} = $column;
575 push @mainloop, \%globalline;
576 return \@mainloop;