Bug 19950: Update popup window templates to use Bootstrap grid: Serials
[koha.git] / reports / catalogue_stats.pl
blobb7bd98a02444977c4432a207b211b6aa1d35ef52
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 Modern::Perl;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Reports;
28 use C4::Circulation;
29 use C4::Biblio qw/GetMarcSubfieldStructureFromKohaField/;
31 use Koha::AuthorisedValues;
32 use Koha::DateUtils;
33 use Koha::ItemTypes;
35 =head1 NAME
37 plugin that shows a stats on borrowers
39 =head1 DESCRIPTION
41 =cut
43 our $debug = 0;
44 my $input = new CGI;
45 my $fullreportname = "reports/catalogue_stats.tt";
46 my $do_it = $input->param('do_it');
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my $cellvalue = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
50 my @filters = $input->multi_param("Filter");
51 my $cotedigits = $input->param("cotedigits");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 our $sep = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 my $item_itype;
57 if(C4::Context->preference('item-level_itypes')) {
58 $item_itype = "items\.itype"
59 } else {
60 $item_itype = "itemtype";
62 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
63 $line = "copyrightdate";
65 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
66 $column = "copyrightdate";
69 my ($template, $borrowernumber, $cookie)
70 = get_template_and_user({template_name => $fullreportname,
71 query => $input,
72 type => "intranet",
73 authnotrequired => 0,
74 flagsrequired => {reports => '*'},
75 debug => 1,
76 });
77 $template->param(do_it => $do_it);
78 if ($do_it) {
79 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
80 if ( $output eq "screen" ) {
81 $template->param( mainloop => $results );
82 output_html_with_http_headers $input, $cookie, $template->output;
83 exit;
84 } else {
85 print $input->header(
86 -type => 'text/csv',
87 -encoding => 'utf-8',
88 -attachment => "$basename.csv",
89 -name => "$basename.csv"
91 my $cols = @$results[0]->{loopcol};
92 my $lines = @$results[0]->{looprow};
93 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
94 foreach my $col (@$cols) {
95 print $col->{coltitle} . $sep;
97 print "Total\n";
98 foreach my $line (@$lines) {
99 my $x = $line->{loopcell};
100 print $line->{rowtitle} . $sep;
101 foreach my $cell (@$x) {
102 print $cell->{value} . $sep;
104 print $line->{totalrow};
105 print "\n";
107 print "TOTAL";
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col (@$cols) {
110 print $sep. $col->{totalcol};
112 print $sep. @$results[0]->{total};
113 exit;
115 } else {
116 my $dbh = C4::Context->dbh;
117 my @values;
118 my %labels;
119 my $count=0;
120 my $req;
121 my @select;
123 my $itemtypes = Koha::ItemTypes->search_with_localization;
125 my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
126 my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
128 foreach my $kohafield (qw(items.notforloan items.materials)) {
129 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
130 if($subfield_structure) {
131 my $avlist;
132 my $avcategory = $subfield_structure->{authorised_value};
133 if($avcategory) {
134 $avlist = GetAuthorisedValues($avcategory);
136 my $kf = $kohafield;
137 $kf =~ s/^items\.//;
138 $template->param(
139 $kf => 1,
140 $kf."_label" => $subfield_structure->{liblibrarian},
141 $kf."_avlist" => $avlist
146 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
148 $template->param(
149 itemtypes => $itemtypes,
150 locationloop => \@locations,
151 authvals => \@authvals,
152 CGIextChoice => \@mime,
153 CGIsepChoice => GetDelimiterChoices,
154 item_itype => $item_itype,
158 output_html_with_http_headers $input, $cookie, $template->output;
160 ## End of Main Body
163 sub calculate {
164 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
165 my @mainloop;
166 my @loopfooter;
167 my @loopcol;
168 my @loopline;
169 my @looprow;
170 my %globalline;
171 my $grantotal = 0;
172 my $barcodelike = @$filters[16];
173 my $barcodefilter = @$filters[17];
174 my $not;
175 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
177 my $dbh = C4::Context->dbh;
179 # if barcodefilter is empty set as %
180 if ($barcodefilter) {
182 # Check if barcodefilter is "like" or "not like"
183 if ( !$barcodelike ) {
184 $not = "not";
187 # Change * to %
188 $barcodefilter =~ s/\*/%/g;
191 # Filters
192 # Checking filters
194 my @loopfilter;
195 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
196 my %cell;
197 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
198 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
199 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
201 $cell{filter} .= @$filters[$i];
202 $cell{crit} .=
203 ( $i == 0 ) ? "Item CallNumber From"
204 : ( $i == 1 ) ? "Item CallNumber To"
205 : ( $i == 2 ) ? "Item type"
206 : ( $i == 3 ) ? "Publisher"
207 : ( $i == 4 ) ? "Publication year From"
208 : ( $i == 5 ) ? "Publication year To"
209 : ( $i == 6 ) ? "Library"
210 : ( $i == 7 ) ? "Shelving Location"
211 : ( $i == 8 ) ? "Collection Code"
212 : ( $i == 9 ) ? "Status"
213 : ( $i == 10 ) ? "Materials"
214 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
215 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
216 : ( $i == 13 ) ? "Date acquired (item) from"
217 : ( $i == 14 ) ? "Date acquired (item) to"
218 : ( $i == 15 ) ? "Date deleted (item) from"
219 : ( $i == 16 ) ? "Date deleted (item) to"
220 : '';
222 push @loopfilter, \%cell;
226 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
227 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
228 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
229 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
231 my @linefilter;
232 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
233 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
234 if ( C4::Context->preference('item-level_itypes') ) {
235 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
236 } else {
237 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
239 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
240 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
241 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
243 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
244 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
245 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
246 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
247 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
248 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
249 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
250 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
251 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
253 my @colfilter;
254 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
255 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
256 if ( C4::Context->preference('item-level_itypes') ) {
257 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
258 } else {
259 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
261 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
262 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
263 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
264 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
265 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
266 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
267 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
268 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
269 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
270 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
271 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
272 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
274 # 1st, loop rows.
275 my $origline = $line;
276 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
277 my $linefield;
278 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
279 $linefield = "left($line,$cotedigits)";
280 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
281 $linefield = "DATE($line)";
282 } else {
283 $linefield = $line;
286 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
287 LEFT JOIN biblioitems USING (biblioitemnumber)
288 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
289 WHERE 1 ";
290 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
291 if (@linefilter) {
292 if ( $linefilter[1] ) {
293 $strsth .= " AND $line >= ? ";
294 $strsth .= " AND $line <= ? ";
295 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
296 $linefilter[0] =~ s/\*/%/g;
297 $strsth .= " AND $line LIKE ? ";
300 $strsth .= " ORDER BY $linefield";
301 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
303 my $sth = $dbh->prepare($strsth);
304 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
305 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
306 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
307 $sth->execute( $linefilter[0], $linefilter[1] );
308 } elsif ( $barcodefilter and $linefilter[0] ) {
309 $sth->execute( $barcodefilter, $linefilter[0] );
310 } elsif ( $linefilter[0] ) {
311 $sth->execute($linefilter[0]);
312 } elsif ($barcodefilter) {
313 $sth->execute($barcodefilter);
314 } else {
315 $sth->execute();
317 my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
318 while ( my ($celvalue) = $sth->fetchrow ) {
319 my %cell;
320 if (defined $celvalue and $celvalue ne '') {
321 if($rowauthvals and $rowauthvals->{$celvalue}) {
322 $cell{rowtitle} = $rowauthvals->{$celvalue};
323 } else {
324 $cell{rowtitle} = $celvalue;
326 $cell{value} = $celvalue;
328 else {
329 $cell{rowtitle} = "NULL";
330 $cell{value} = "zzEMPTY";
332 $cell{totalrow} = 0;
333 push @loopline, \%cell;
336 # 2nd, loop cols.
337 my $origcolumn = $column;
338 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
339 my $colfield;
340 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
341 $colfield = "left($column,$cotedigits)";
342 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
343 $colfield = "DATE($column)";
344 } else {
345 $colfield = $column;
348 my $strsth2 = "
349 SELECT distinctrow $colfield
350 FROM $itemstable
351 LEFT JOIN biblioitems
352 USING (biblioitemnumber)
353 LEFT JOIN biblio
354 ON (biblioitems.biblionumber = biblio.biblionumber)
355 WHERE 1 ";
356 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
358 if ( (@colfilter) and ( $colfilter[1] ) ) {
359 $strsth2 .= " AND $column >= ? AND $column <= ?";
360 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
361 $colfilter[0] =~ s/\*/%/g;
362 $strsth2 .= " AND $column LIKE ? ";
364 $strsth2 .= " ORDER BY $colfield";
365 $debug and print STDERR "SQL: $strsth2";
366 my $sth2 = $dbh->prepare($strsth2);
367 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
368 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
369 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
370 $sth2->execute( $colfilter[0], $colfilter[1] );
371 } elsif ( $barcodefilter && $colfilter[0] ) {
372 $sth2->execute( $barcodefilter , $colfilter[0] );
373 } elsif ( $colfilter[0]) {
374 $sth2->execute( $colfilter[0] );
375 } elsif ($barcodefilter) {
376 $sth2->execute($barcodefilter);
377 } else {
378 $sth2->execute();
380 my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
381 while ( my ($celvalue) = $sth2->fetchrow ) {
382 my %cell;
383 if (defined $celvalue and $celvalue ne '') {
384 if($colauthvals and $colauthvals->{$celvalue}) {
385 $cell{coltitle} = $colauthvals->{$celvalue};
386 } else {
387 $cell{coltitle} = $celvalue;
389 $cell{value} = $celvalue;
391 else {
392 $cell{coltitle} = "NULL";
393 $cell{value} = "zzEMPTY";
395 $cell{totalcol} = 0;
396 push @loopcol, \%cell;
399 my $i = 0;
400 my @totalcol;
401 my $hilighted = -1;
403 #Initialization of cell values.....
404 my %table;
406 foreach my $row (@loopline) {
407 foreach my $col (@loopcol) {
408 $table{ $row->{value} }->{ $col->{value} } = 0;
410 $table{ $row->{value} }->{totalrow} = 0;
413 # preparing calculation
414 my $select_cellvalue = " COUNT(*) ";
415 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
416 my $strcalc = "
417 SELECT $linefield, $colfield, $select_cellvalue
418 FROM $itemstable
419 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
420 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
421 WHERE 1 ";
423 my @sqlargs;
425 if ($barcodefilter) {
426 $strcalc .= "AND barcode $not like ? ";
427 push @sqlargs, $barcodefilter;
430 if ( @$filters[0] ) {
431 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
432 @$filters[0] =~ s/\*/%/g;
433 push @sqlargs, @$filters[0];
436 if ( @$filters[1] ) {
437 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
438 @$filters[1] =~ s/\*/%/g;
439 push @sqlargs, @$filters[1];
442 if ( @$filters[2] ) {
443 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
444 @$filters[2] =~ s/\*/%/g;
445 push @sqlargs, @$filters[2];
448 if ( @$filters[3] ) {
449 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
450 @$filters[3] =~ s/\*/%/g;
451 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
452 push @sqlargs, @$filters[3];
454 if ( @$filters[4] ) {
455 $strcalc .= " AND " .
456 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
457 . "> ? ";
458 @$filters[4] =~ s/\*/%/g;
459 push @sqlargs, @$filters[4];
461 if ( @$filters[5] ) {
462 @$filters[5] =~ s/\*/%/g;
463 $strcalc .= " AND " .
464 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
465 . "< ? ";
466 push @sqlargs, @$filters[5];
468 if ( @$filters[6] ) {
469 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
470 @$filters[6] =~ s/\*/%/g;
471 push @sqlargs, @$filters[6];
473 if ( @$filters[7] ) {
474 $strcalc .= " AND $itemstable.location LIKE ? ";
475 @$filters[7] =~ s/\*/%/g;
476 push @sqlargs, @$filters[7];
478 if ( @$filters[8] ) {
479 $strcalc .= " AND $itemstable.ccode LIKE ? ";
480 @$filters[8] =~ s/\*/%/g;
481 push @sqlargs, @$filters[8];
483 if ( defined @$filters[9] and @$filters[9] ne '' ) {
484 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
485 @$filters[9] =~ s/\*/%/g;
486 push @sqlargs, @$filters[9];
488 if ( defined @$filters[10] and @$filters[10] ne '' ) {
489 $strcalc .= " AND $itemstable.materials LIKE ? ";
490 @$filters[10] =~ s/\*/%/g;
491 push @sqlargs, @$filters[10];
493 if ( @$filters[13] ) {
494 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
495 @$filters[13] =~ s/\*/%/g;
496 push @sqlargs, @$filters[13];
498 if ( @$filters[14] ) {
499 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
500 @$filters[14] =~ s/\*/%/g;
501 push @sqlargs, @$filters[14];
503 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
504 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
505 @$filters[15] =~ s/\*/%/g;
506 push @sqlargs, @$filters[15];
508 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
509 @$filters[16] =~ s/\*/%/g;
510 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
511 push @sqlargs, @$filters[16];
513 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
514 $debug and warn "SQL: $strcalc";
515 my $dbcalc = $dbh->prepare($strcalc);
516 $dbcalc->execute(@sqlargs);
518 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
520 $col = "zzEMPTY" if ( !defined($col) );
521 $row = "zzEMPTY" if ( !defined($row) );
523 $table{$row}->{$col} += $value;
524 $table{$row}->{totalrow} += $value;
525 $grantotal += $value;
528 foreach my $row ( @loopline ) {
529 my @loopcell;
531 #@loopcol ensures the order for columns is common with column titles
532 # and the number matches the number of columns
533 foreach my $col (@loopcol) {
534 my $value = $table{$row->{value}}->{ $col->{value} };
535 push @loopcell, { value => $value };
537 push @looprow,
538 { 'rowtitle' => $row->{rowtitle},
539 'value' => $row->{value},
540 'loopcell' => \@loopcell,
541 'hilighted' => ( $hilighted *= -1 > 0 ),
542 'totalrow' => $table{$row->{value}}->{totalrow}
546 foreach my $col (@loopcol) {
547 my $total = 0;
548 foreach my $row (@looprow) {
549 $total += $table{ $row->{value} }->{ $col->{value} };
552 push @loopfooter, { 'totalcol' => $total };
555 # the header of the table
556 $globalline{loopfilter} = \@loopfilter;
558 # the core of the table
559 $globalline{looprow} = \@looprow;
560 $globalline{loopcol} = \@loopcol;
562 # the foot (totals by borrower type)
563 $globalline{loopfooter} = \@loopfooter;
564 $globalline{total} = $grantotal;
565 $globalline{line} = $line;
566 $globalline{column} = $column;
567 push @mainloop, \%globalline;
568 return \@mainloop;