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>.
22 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Koha qw( GetAuthorisedValues );
27 use C4::Reports qw( GetDelimiterChoices );
28 use C4::Biblio qw( GetMarcSubfieldStructureFromKohaField );
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils qw( dt_from_string );
36 plugin that shows a stats on borrowers
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');
56 if(C4::Context->preference('item-level_itypes')) {
57 $item_itype = "items\.itype"
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,
72 flagsrequired => {reports => '*'},
74 $template->param(do_it => $do_it);
76 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
77 if ( $output eq "screen" ) {
78 $template->param( mainloop => $results );
79 output_html_with_http_headers $input, $cookie, $template->output;
85 -attachment => "$basename.csv",
86 -name => "$basename.csv"
88 my $cols = @$results[0]->{loopcol};
89 my $lines = @$results[0]->{looprow};
90 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
91 foreach my $col (@$cols) {
92 print $col->{coltitle} . $sep;
95 foreach my $line (@$lines) {
96 my $x = $line->{loopcell};
97 print $line->{rowtitle} . $sep;
98 foreach my $cell (@$x) {
99 print $cell->{value} . $sep;
101 print $line->{totalrow};
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col (@$cols) {
107 print $sep. $col->{totalcol};
109 print $sep. @$results[0]->{total};
113 my $dbh = C4::Context->dbh;
116 my $itemtypes = Koha::ItemTypes->search_with_localization;
118 my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
119 my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
121 foreach my $kohafield (qw(items.notforloan items.materials)) {
122 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
123 if($subfield_structure) {
125 my $avcategory = $subfield_structure->{authorised_value};
127 $avlist = GetAuthorisedValues($avcategory);
133 $kf."_label" => $subfield_structure->{liblibrarian},
134 $kf."_avlist" => $avlist
139 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
142 itemtypes => $itemtypes,
143 locationloop => \@locations,
144 authvals => \@authvals,
145 CGIextChoice => \@mime,
146 CGIsepChoice => GetDelimiterChoices,
147 item_itype => $item_itype,
151 output_html_with_http_headers $input, $cookie, $template->output;
157 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
165 my $barcodelike = @$filters[16];
166 my $barcodefilter = @$filters[17];
168 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
170 my $dbh = C4::Context->dbh;
172 # if barcodefilter is empty set as %
173 if ($barcodefilter) {
175 # Check if barcodefilter is "like" or "not like"
176 if ( !$barcodelike ) {
181 $barcodefilter =~ s/\*/%/g;
188 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
190 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
191 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
192 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
194 $cell{filter} .= @$filters[$i];
196 ( $i == 0 ) ? "Item CallNumber From"
197 : ( $i == 1 ) ? "Item CallNumber To"
198 : ( $i == 2 ) ? "Item type"
199 : ( $i == 3 ) ? "Publisher"
200 : ( $i == 4 ) ? "Publication year From"
201 : ( $i == 5 ) ? "Publication year To"
202 : ( $i == 6 ) ? "Library"
203 : ( $i == 7 ) ? "Shelving Location"
204 : ( $i == 8 ) ? "Collection Code"
205 : ( $i == 9 ) ? "Status"
206 : ( $i == 10 ) ? "Materials"
207 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
208 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
209 : ( $i == 13 ) ? "Date acquired (item) from"
210 : ( $i == 14 ) ? "Date acquired (item) to"
211 : ( $i == 15 ) ? "Date deleted (item) from"
212 : ( $i == 16 ) ? "Date deleted (item) to"
215 push @loopfilter, \%cell;
219 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
220 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
221 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
222 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
225 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
226 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
227 if ( C4::Context->preference('item-level_itypes') ) {
228 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
230 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
232 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
233 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
234 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
236 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
237 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
238 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
239 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
240 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
241 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
242 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
243 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
244 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
247 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
248 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
249 if ( C4::Context->preference('item-level_itypes') ) {
250 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
252 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
254 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
255 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
256 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
257 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
258 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
259 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
260 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
261 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
262 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
263 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
264 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
265 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
268 my $origline = $line;
269 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
271 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
272 $linefield = "left($line,$cotedigits)";
273 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
274 $linefield = "DATE($line)";
279 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
280 LEFT JOIN biblioitems USING (biblioitemnumber)
281 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
283 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
285 if ( $linefilter[1] ) {
286 $strsth .= " AND $line >= ? ";
287 $strsth .= " AND $line <= ? ";
288 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
289 $linefilter[0] =~ s/\*/%/g;
290 $strsth .= " AND $line LIKE ? ";
293 $strsth .= " ORDER BY $linefield";
294 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
296 my $sth = $dbh->prepare($strsth);
297 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
298 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
299 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
300 $sth->execute( $linefilter[0], $linefilter[1] );
301 } elsif ( $barcodefilter and $linefilter[0] ) {
302 $sth->execute( $barcodefilter, $linefilter[0] );
303 } elsif ( $linefilter[0] ) {
304 $sth->execute($linefilter[0]);
305 } elsif ($barcodefilter) {
306 $sth->execute($barcodefilter);
310 my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
311 while ( my ($celvalue) = $sth->fetchrow ) {
313 if (defined $celvalue and $celvalue ne '') {
314 if($rowauthvals and $rowauthvals->{$celvalue}) {
315 $cell{rowtitle} = $rowauthvals->{$celvalue};
317 $cell{rowtitle} = $celvalue;
319 $cell{value} = $celvalue;
322 $cell{rowtitle} = "NULL";
323 $cell{value} = "zzEMPTY";
326 push @loopline, \%cell;
330 my $origcolumn = $column;
331 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
333 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
334 $colfield = "left($column,$cotedigits)";
335 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
336 $colfield = "DATE($column)";
342 SELECT distinctrow $colfield
344 LEFT JOIN biblioitems
345 USING (biblioitemnumber)
347 ON (biblioitems.biblionumber = biblio.biblionumber)
349 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
351 if ( (@colfilter) and ( $colfilter[1] ) ) {
352 $strsth2 .= " AND $column >= ? AND $column <= ?";
353 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
354 $colfilter[0] =~ s/\*/%/g;
355 $strsth2 .= " AND $column LIKE ? ";
357 $strsth2 .= " ORDER BY $colfield";
358 $debug and print STDERR "SQL: $strsth2";
359 my $sth2 = $dbh->prepare($strsth2);
360 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
361 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
362 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
363 $sth2->execute( $colfilter[0], $colfilter[1] );
364 } elsif ( $barcodefilter && $colfilter[0] ) {
365 $sth2->execute( $barcodefilter , $colfilter[0] );
366 } elsif ( $colfilter[0]) {
367 $sth2->execute( $colfilter[0] );
368 } elsif ($barcodefilter) {
369 $sth2->execute($barcodefilter);
373 my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
374 while ( my ($celvalue) = $sth2->fetchrow ) {
376 if (defined $celvalue and $celvalue ne '') {
377 if($colauthvals and $colauthvals->{$celvalue}) {
378 $cell{coltitle} = $colauthvals->{$celvalue};
380 $cell{coltitle} = $celvalue;
382 $cell{value} = $celvalue;
385 $cell{coltitle} = "NULL";
386 $cell{value} = "zzEMPTY";
389 push @loopcol, \%cell;
395 #Initialization of cell values.....
398 foreach my $row (@loopline) {
399 foreach my $col (@loopcol) {
400 $table{ $row->{value} }->{ $col->{value} } = 0;
402 $table{ $row->{value} }->{totalrow} = 0;
405 # preparing calculation
406 my $select_cellvalue = " COUNT(*) ";
407 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
409 SELECT $linefield, $colfield, $select_cellvalue
411 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
412 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
417 if ($barcodefilter) {
418 $strcalc .= "AND barcode $not like ? ";
419 push @sqlargs, $barcodefilter;
422 if ( @$filters[0] ) {
423 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
424 @$filters[0] =~ s/\*/%/g;
425 push @sqlargs, @$filters[0];
428 if ( @$filters[1] ) {
429 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
430 @$filters[1] =~ s/\*/%/g;
431 push @sqlargs, @$filters[1];
434 if ( @$filters[2] ) {
435 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
436 @$filters[2] =~ s/\*/%/g;
437 push @sqlargs, @$filters[2];
440 if ( @$filters[3] ) {
441 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
442 @$filters[3] =~ s/\*/%/g;
443 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
444 push @sqlargs, @$filters[3];
446 if ( @$filters[4] ) {
447 $strcalc .= " AND " .
448 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
450 @$filters[4] =~ s/\*/%/g;
451 push @sqlargs, @$filters[4];
453 if ( @$filters[5] ) {
454 @$filters[5] =~ s/\*/%/g;
455 $strcalc .= " AND " .
456 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
458 push @sqlargs, @$filters[5];
460 if ( @$filters[6] ) {
461 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
462 @$filters[6] =~ s/\*/%/g;
463 push @sqlargs, @$filters[6];
465 if ( @$filters[7] ) {
466 $strcalc .= " AND $itemstable.location LIKE ? ";
467 @$filters[7] =~ s/\*/%/g;
468 push @sqlargs, @$filters[7];
470 if ( @$filters[8] ) {
471 $strcalc .= " AND $itemstable.ccode LIKE ? ";
472 @$filters[8] =~ s/\*/%/g;
473 push @sqlargs, @$filters[8];
475 if ( defined @$filters[9] and @$filters[9] ne '' ) {
476 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
477 @$filters[9] =~ s/\*/%/g;
478 push @sqlargs, @$filters[9];
480 if ( defined @$filters[10] and @$filters[10] ne '' ) {
481 $strcalc .= " AND $itemstable.materials LIKE ? ";
482 @$filters[10] =~ s/\*/%/g;
483 push @sqlargs, @$filters[10];
485 if ( @$filters[13] ) {
486 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
487 @$filters[13] =~ s/\*/%/g;
488 push @sqlargs, @$filters[13];
490 if ( @$filters[14] ) {
491 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
492 @$filters[14] =~ s/\*/%/g;
493 push @sqlargs, @$filters[14];
495 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
496 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
497 @$filters[15] =~ s/\*/%/g;
498 push @sqlargs, @$filters[15];
500 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
501 @$filters[16] =~ s/\*/%/g;
502 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
503 push @sqlargs, @$filters[16];
505 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
506 $debug and warn "SQL: $strcalc";
507 my $dbcalc = $dbh->prepare($strcalc);
508 $dbcalc->execute(@sqlargs);
510 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
512 $col = "zzEMPTY" if ( !defined($col) );
513 $row = "zzEMPTY" if ( !defined($row) );
515 $table{$row}->{$col} += $value;
516 $table{$row}->{totalrow} += $value;
517 $grantotal += $value;
520 foreach my $row ( @loopline ) {
523 #@loopcol ensures the order for columns is common with column titles
524 # and the number matches the number of columns
525 foreach my $col (@loopcol) {
526 my $value = $table{$row->{value}}->{ $col->{value} };
527 push @loopcell, { value => $value };
530 { 'rowtitle' => $row->{rowtitle},
531 'value' => $row->{value},
532 'loopcell' => \@loopcell,
533 'hilighted' => ( $hilighted *= -1 > 0 ),
534 'totalrow' => $table{$row->{value}}->{totalrow}
538 foreach my $col (@loopcol) {
540 foreach my $row (@looprow) {
541 $total += $table{ $row->{value} }->{ $col->{value} };
544 push @loopfooter, { 'totalcol' => $total };
547 # the header of the table
548 $globalline{loopfilter} = \@loopfilter;
550 # the core of the table
551 $globalline{looprow} = \@looprow;
552 $globalline{loopcol} = \@loopcol;
554 # the foot (totals by borrower type)
555 $globalline{loopfooter} = \@loopfooter;
556 $globalline{total} = $grantotal;
557 $globalline{line} = $line;
558 $globalline{column} = $column;
559 push @mainloop, \%globalline;