Bug 25728: Use a svc script instead of the REST API endpoint
[koha.git] / reports / catalogue_stats.pl
blob536a76b266ae065464ff3d57138f0bf040863263
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 $count=0;
119 my $itemtypes = Koha::ItemTypes->search_with_localization;
121 my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
122 my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
124 foreach my $kohafield (qw(items.notforloan items.materials)) {
125 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
126 if($subfield_structure) {
127 my $avlist;
128 my $avcategory = $subfield_structure->{authorised_value};
129 if($avcategory) {
130 $avlist = GetAuthorisedValues($avcategory);
132 my $kf = $kohafield;
133 $kf =~ s/^items\.//;
134 $template->param(
135 $kf => 1,
136 $kf."_label" => $subfield_structure->{liblibrarian},
137 $kf."_avlist" => $avlist
142 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
144 $template->param(
145 itemtypes => $itemtypes,
146 locationloop => \@locations,
147 authvals => \@authvals,
148 CGIextChoice => \@mime,
149 CGIsepChoice => GetDelimiterChoices,
150 item_itype => $item_itype,
154 output_html_with_http_headers $input, $cookie, $template->output;
156 ## End of Main Body
159 sub calculate {
160 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
161 my @mainloop;
162 my @loopfooter;
163 my @loopcol;
164 my @loopline;
165 my @looprow;
166 my %globalline;
167 my $grantotal = 0;
168 my $barcodelike = @$filters[16];
169 my $barcodefilter = @$filters[17];
170 my $not;
171 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
173 my $dbh = C4::Context->dbh;
175 # if barcodefilter is empty set as %
176 if ($barcodefilter) {
178 # Check if barcodefilter is "like" or "not like"
179 if ( !$barcodelike ) {
180 $not = "not";
183 # Change * to %
184 $barcodefilter =~ s/\*/%/g;
187 # Filters
188 # Checking filters
190 my @loopfilter;
191 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
192 my %cell;
193 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
194 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
195 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
197 $cell{filter} .= @$filters[$i];
198 $cell{crit} .=
199 ( $i == 0 ) ? "Item CallNumber From"
200 : ( $i == 1 ) ? "Item CallNumber To"
201 : ( $i == 2 ) ? "Item type"
202 : ( $i == 3 ) ? "Publisher"
203 : ( $i == 4 ) ? "Publication year From"
204 : ( $i == 5 ) ? "Publication year To"
205 : ( $i == 6 ) ? "Library"
206 : ( $i == 7 ) ? "Shelving Location"
207 : ( $i == 8 ) ? "Collection Code"
208 : ( $i == 9 ) ? "Status"
209 : ( $i == 10 ) ? "Materials"
210 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
211 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
212 : ( $i == 13 ) ? "Date acquired (item) from"
213 : ( $i == 14 ) ? "Date acquired (item) to"
214 : ( $i == 15 ) ? "Date deleted (item) from"
215 : ( $i == 16 ) ? "Date deleted (item) to"
216 : '';
218 push @loopfilter, \%cell;
222 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
223 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
224 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
225 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
227 my @linefilter;
228 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
229 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
230 if ( C4::Context->preference('item-level_itypes') ) {
231 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
232 } else {
233 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
235 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
236 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
237 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
239 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
240 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
241 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
242 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
243 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
244 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
245 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
246 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
247 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
249 my @colfilter;
250 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
251 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
252 if ( C4::Context->preference('item-level_itypes') ) {
253 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
254 } else {
255 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
257 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
258 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
259 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
260 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
261 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
262 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
263 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
264 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
265 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
266 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
267 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
268 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
270 # 1st, loop rows.
271 my $origline = $line;
272 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
273 my $linefield;
274 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
275 $linefield = "left($line,$cotedigits)";
276 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
277 $linefield = "DATE($line)";
278 } else {
279 $linefield = $line;
282 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
283 LEFT JOIN biblioitems USING (biblioitemnumber)
284 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
285 WHERE 1 ";
286 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
287 if (@linefilter) {
288 if ( $linefilter[1] ) {
289 $strsth .= " AND $line >= ? ";
290 $strsth .= " AND $line <= ? ";
291 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
292 $linefilter[0] =~ s/\*/%/g;
293 $strsth .= " AND $line LIKE ? ";
296 $strsth .= " ORDER BY $linefield";
297 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
299 my $sth = $dbh->prepare($strsth);
300 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
301 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
302 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
303 $sth->execute( $linefilter[0], $linefilter[1] );
304 } elsif ( $barcodefilter and $linefilter[0] ) {
305 $sth->execute( $barcodefilter, $linefilter[0] );
306 } elsif ( $linefilter[0] ) {
307 $sth->execute($linefilter[0]);
308 } elsif ($barcodefilter) {
309 $sth->execute($barcodefilter);
310 } else {
311 $sth->execute();
313 my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
314 while ( my ($celvalue) = $sth->fetchrow ) {
315 my %cell;
316 if (defined $celvalue and $celvalue ne '') {
317 if($rowauthvals and $rowauthvals->{$celvalue}) {
318 $cell{rowtitle} = $rowauthvals->{$celvalue};
319 } else {
320 $cell{rowtitle} = $celvalue;
322 $cell{value} = $celvalue;
324 else {
325 $cell{rowtitle} = "NULL";
326 $cell{value} = "zzEMPTY";
328 $cell{totalrow} = 0;
329 push @loopline, \%cell;
332 # 2nd, loop cols.
333 my $origcolumn = $column;
334 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
335 my $colfield;
336 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
337 $colfield = "left($column,$cotedigits)";
338 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
339 $colfield = "DATE($column)";
340 } else {
341 $colfield = $column;
344 my $strsth2 = "
345 SELECT distinctrow $colfield
346 FROM $itemstable
347 LEFT JOIN biblioitems
348 USING (biblioitemnumber)
349 LEFT JOIN biblio
350 ON (biblioitems.biblionumber = biblio.biblionumber)
351 WHERE 1 ";
352 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
354 if ( (@colfilter) and ( $colfilter[1] ) ) {
355 $strsth2 .= " AND $column >= ? AND $column <= ?";
356 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
357 $colfilter[0] =~ s/\*/%/g;
358 $strsth2 .= " AND $column LIKE ? ";
360 $strsth2 .= " ORDER BY $colfield";
361 $debug and print STDERR "SQL: $strsth2";
362 my $sth2 = $dbh->prepare($strsth2);
363 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
364 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
365 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
366 $sth2->execute( $colfilter[0], $colfilter[1] );
367 } elsif ( $barcodefilter && $colfilter[0] ) {
368 $sth2->execute( $barcodefilter , $colfilter[0] );
369 } elsif ( $colfilter[0]) {
370 $sth2->execute( $colfilter[0] );
371 } elsif ($barcodefilter) {
372 $sth2->execute($barcodefilter);
373 } else {
374 $sth2->execute();
376 my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
377 while ( my ($celvalue) = $sth2->fetchrow ) {
378 my %cell;
379 if (defined $celvalue and $celvalue ne '') {
380 if($colauthvals and $colauthvals->{$celvalue}) {
381 $cell{coltitle} = $colauthvals->{$celvalue};
382 } else {
383 $cell{coltitle} = $celvalue;
385 $cell{value} = $celvalue;
387 else {
388 $cell{coltitle} = "NULL";
389 $cell{value} = "zzEMPTY";
391 $cell{totalcol} = 0;
392 push @loopcol, \%cell;
395 my $i = 0;
396 my $hilighted = -1;
398 #Initialization of cell values.....
399 my %table;
401 foreach my $row (@loopline) {
402 foreach my $col (@loopcol) {
403 $table{ $row->{value} }->{ $col->{value} } = 0;
405 $table{ $row->{value} }->{totalrow} = 0;
408 # preparing calculation
409 my $select_cellvalue = " COUNT(*) ";
410 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
411 my $strcalc = "
412 SELECT $linefield, $colfield, $select_cellvalue
413 FROM $itemstable
414 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
415 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
416 WHERE 1 ";
418 my @sqlargs;
420 if ($barcodefilter) {
421 $strcalc .= "AND barcode $not like ? ";
422 push @sqlargs, $barcodefilter;
425 if ( @$filters[0] ) {
426 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
427 @$filters[0] =~ s/\*/%/g;
428 push @sqlargs, @$filters[0];
431 if ( @$filters[1] ) {
432 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
433 @$filters[1] =~ s/\*/%/g;
434 push @sqlargs, @$filters[1];
437 if ( @$filters[2] ) {
438 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
439 @$filters[2] =~ s/\*/%/g;
440 push @sqlargs, @$filters[2];
443 if ( @$filters[3] ) {
444 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
445 @$filters[3] =~ s/\*/%/g;
446 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
447 push @sqlargs, @$filters[3];
449 if ( @$filters[4] ) {
450 $strcalc .= " AND " .
451 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
452 . "> ? ";
453 @$filters[4] =~ s/\*/%/g;
454 push @sqlargs, @$filters[4];
456 if ( @$filters[5] ) {
457 @$filters[5] =~ s/\*/%/g;
458 $strcalc .= " AND " .
459 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
460 . "< ? ";
461 push @sqlargs, @$filters[5];
463 if ( @$filters[6] ) {
464 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
465 @$filters[6] =~ s/\*/%/g;
466 push @sqlargs, @$filters[6];
468 if ( @$filters[7] ) {
469 $strcalc .= " AND $itemstable.location LIKE ? ";
470 @$filters[7] =~ s/\*/%/g;
471 push @sqlargs, @$filters[7];
473 if ( @$filters[8] ) {
474 $strcalc .= " AND $itemstable.ccode LIKE ? ";
475 @$filters[8] =~ s/\*/%/g;
476 push @sqlargs, @$filters[8];
478 if ( defined @$filters[9] and @$filters[9] ne '' ) {
479 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
480 @$filters[9] =~ s/\*/%/g;
481 push @sqlargs, @$filters[9];
483 if ( defined @$filters[10] and @$filters[10] ne '' ) {
484 $strcalc .= " AND $itemstable.materials LIKE ? ";
485 @$filters[10] =~ s/\*/%/g;
486 push @sqlargs, @$filters[10];
488 if ( @$filters[13] ) {
489 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
490 @$filters[13] =~ s/\*/%/g;
491 push @sqlargs, @$filters[13];
493 if ( @$filters[14] ) {
494 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
495 @$filters[14] =~ s/\*/%/g;
496 push @sqlargs, @$filters[14];
498 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
499 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
500 @$filters[15] =~ s/\*/%/g;
501 push @sqlargs, @$filters[15];
503 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
504 @$filters[16] =~ s/\*/%/g;
505 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
506 push @sqlargs, @$filters[16];
508 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
509 $debug and warn "SQL: $strcalc";
510 my $dbcalc = $dbh->prepare($strcalc);
511 $dbcalc->execute(@sqlargs);
513 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
515 $col = "zzEMPTY" if ( !defined($col) );
516 $row = "zzEMPTY" if ( !defined($row) );
518 $table{$row}->{$col} += $value;
519 $table{$row}->{totalrow} += $value;
520 $grantotal += $value;
523 foreach my $row ( @loopline ) {
524 my @loopcell;
526 #@loopcol ensures the order for columns is common with column titles
527 # and the number matches the number of columns
528 foreach my $col (@loopcol) {
529 my $value = $table{$row->{value}}->{ $col->{value} };
530 push @loopcell, { value => $value };
532 push @looprow,
533 { 'rowtitle' => $row->{rowtitle},
534 'value' => $row->{value},
535 'loopcell' => \@loopcell,
536 'hilighted' => ( $hilighted *= -1 > 0 ),
537 'totalrow' => $table{$row->{value}}->{totalrow}
541 foreach my $col (@loopcol) {
542 my $total = 0;
543 foreach my $row (@looprow) {
544 $total += $table{ $row->{value} }->{ $col->{value} };
547 push @loopfooter, { 'totalcol' => $total };
550 # the header of the table
551 $globalline{loopfilter} = \@loopfilter;
553 # the core of the table
554 $globalline{looprow} = \@looprow;
555 $globalline{loopcol} = \@loopcol;
557 # the foot (totals by borrower type)
558 $globalline{loopfooter} = \@loopfooter;
559 $globalline{total} = $grantotal;
560 $globalline{line} = $line;
561 $globalline{column} = $column;
562 push @mainloop, \%globalline;
563 return \@mainloop;