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 = C4::Context->csv_delimiter(scalar $input->param("sep"));
55 if(C4::Context->preference('item-level_itypes')) {
56 $item_itype = "items\.itype"
58 $item_itype = "itemtype";
60 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
61 $line = "copyrightdate";
63 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
64 $column = "copyrightdate";
67 my ($template, $borrowernumber, $cookie)
68 = get_template_and_user({template_name => $fullreportname,
71 flagsrequired => {reports => '*'},
73 $template->param(do_it => $do_it);
75 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
76 if ( $output eq "screen" ) {
77 $template->param( mainloop => $results );
78 output_html_with_http_headers $input, $cookie, $template->output;
84 -attachment => "$basename.csv",
85 -name => "$basename.csv"
87 my $cols = @$results[0]->{loopcol};
88 my $lines = @$results[0]->{looprow};
89 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
90 foreach my $col (@$cols) {
91 print $col->{coltitle} . $sep;
94 foreach my $line (@$lines) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle} . $sep;
97 foreach my $cell (@$x) {
98 print $cell->{value} . $sep;
100 print $line->{totalrow};
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col (@$cols) {
106 print $sep. $col->{totalcol};
108 print $sep. @$results[0]->{total};
112 my $dbh = C4::Context->dbh;
115 my $itemtypes = Koha::ItemTypes->search_with_localization;
117 my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
118 my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
120 foreach my $kohafield (qw(items.notforloan items.materials)) {
121 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
122 if($subfield_structure) {
124 my $avcategory = $subfield_structure->{authorised_value};
126 $avlist = GetAuthorisedValues($avcategory);
132 $kf."_label" => $subfield_structure->{liblibrarian},
133 $kf."_avlist" => $avlist
138 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
141 itemtypes => $itemtypes,
142 locationloop => \@locations,
143 authvals => \@authvals,
144 CGIextChoice => \@mime,
145 CGIsepChoice => GetDelimiterChoices,
146 item_itype => $item_itype,
150 output_html_with_http_headers $input, $cookie, $template->output;
156 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
164 my $barcodelike = @$filters[16];
165 my $barcodefilter = @$filters[17];
167 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
169 my $dbh = C4::Context->dbh;
171 # if barcodefilter is empty set as %
172 if ($barcodefilter) {
174 # Check if barcodefilter is "like" or "not like"
175 if ( !$barcodelike ) {
180 $barcodefilter =~ s/\*/%/g;
187 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
189 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
190 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
191 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
193 $cell{filter} .= @$filters[$i];
195 ( $i == 0 ) ? "Item CallNumber From"
196 : ( $i == 1 ) ? "Item CallNumber To"
197 : ( $i == 2 ) ? "Item type"
198 : ( $i == 3 ) ? "Publisher"
199 : ( $i == 4 ) ? "Publication year From"
200 : ( $i == 5 ) ? "Publication year To"
201 : ( $i == 6 ) ? "Library"
202 : ( $i == 7 ) ? "Shelving Location"
203 : ( $i == 8 ) ? "Collection Code"
204 : ( $i == 9 ) ? "Status"
205 : ( $i == 10 ) ? "Materials"
206 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
207 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
208 : ( $i == 13 ) ? "Date acquired (item) from"
209 : ( $i == 14 ) ? "Date acquired (item) to"
210 : ( $i == 15 ) ? "Date deleted (item) from"
211 : ( $i == 16 ) ? "Date deleted (item) to"
214 push @loopfilter, \%cell;
218 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
219 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
220 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
221 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
224 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
225 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
226 if ( C4::Context->preference('item-level_itypes') ) {
227 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
229 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
231 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
232 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
233 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
235 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
236 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
237 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
238 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
239 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
240 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
241 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
242 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
243 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
246 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
247 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
248 if ( C4::Context->preference('item-level_itypes') ) {
249 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
251 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
253 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
254 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
255 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
256 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
257 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
258 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
259 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
260 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
261 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
262 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
263 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
264 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
267 my $origline = $line;
268 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
270 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
271 $linefield = "left($line,$cotedigits)";
272 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
273 $linefield = "DATE($line)";
278 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
279 LEFT JOIN biblioitems USING (biblioitemnumber)
280 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
282 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
284 if ( $linefilter[1] ) {
285 $strsth .= " AND $line >= ? ";
286 $strsth .= " AND $line <= ? ";
287 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
288 $linefilter[0] =~ s/\*/%/g;
289 $strsth .= " AND $line LIKE ? ";
292 $strsth .= " ORDER BY $linefield";
293 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
295 my $sth = $dbh->prepare($strsth);
296 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
297 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
298 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
299 $sth->execute( $linefilter[0], $linefilter[1] );
300 } elsif ( $barcodefilter and $linefilter[0] ) {
301 $sth->execute( $barcodefilter, $linefilter[0] );
302 } elsif ( $linefilter[0] ) {
303 $sth->execute($linefilter[0]);
304 } elsif ($barcodefilter) {
305 $sth->execute($barcodefilter);
309 my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
310 while ( my ($celvalue) = $sth->fetchrow ) {
312 if (defined $celvalue and $celvalue ne '') {
313 if($rowauthvals and $rowauthvals->{$celvalue}) {
314 $cell{rowtitle} = $rowauthvals->{$celvalue};
316 $cell{rowtitle} = $celvalue;
318 $cell{value} = $celvalue;
321 $cell{rowtitle} = "NULL";
322 $cell{value} = "zzEMPTY";
325 push @loopline, \%cell;
329 my $origcolumn = $column;
330 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
332 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
333 $colfield = "left($column,$cotedigits)";
334 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
335 $colfield = "DATE($column)";
341 SELECT distinctrow $colfield
343 LEFT JOIN biblioitems
344 USING (biblioitemnumber)
346 ON (biblioitems.biblionumber = biblio.biblionumber)
348 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
350 if ( (@colfilter) and ( $colfilter[1] ) ) {
351 $strsth2 .= " AND $column >= ? AND $column <= ?";
352 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
353 $colfilter[0] =~ s/\*/%/g;
354 $strsth2 .= " AND $column LIKE ? ";
356 $strsth2 .= " ORDER BY $colfield";
357 $debug and print STDERR "SQL: $strsth2";
358 my $sth2 = $dbh->prepare($strsth2);
359 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
360 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
361 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
362 $sth2->execute( $colfilter[0], $colfilter[1] );
363 } elsif ( $barcodefilter && $colfilter[0] ) {
364 $sth2->execute( $barcodefilter , $colfilter[0] );
365 } elsif ( $colfilter[0]) {
366 $sth2->execute( $colfilter[0] );
367 } elsif ($barcodefilter) {
368 $sth2->execute($barcodefilter);
372 my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
373 while ( my ($celvalue) = $sth2->fetchrow ) {
375 if (defined $celvalue and $celvalue ne '') {
376 if($colauthvals and $colauthvals->{$celvalue}) {
377 $cell{coltitle} = $colauthvals->{$celvalue};
379 $cell{coltitle} = $celvalue;
381 $cell{value} = $celvalue;
384 $cell{coltitle} = "NULL";
385 $cell{value} = "zzEMPTY";
388 push @loopcol, \%cell;
394 #Initialization of cell values.....
397 foreach my $row (@loopline) {
398 foreach my $col (@loopcol) {
399 $table{ $row->{value} }->{ $col->{value} } = 0;
401 $table{ $row->{value} }->{totalrow} = 0;
404 # preparing calculation
405 my $select_cellvalue = " COUNT(*) ";
406 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
408 SELECT $linefield, $colfield, $select_cellvalue
410 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
411 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
416 if ($barcodefilter) {
417 $strcalc .= "AND barcode $not like ? ";
418 push @sqlargs, $barcodefilter;
421 if ( @$filters[0] ) {
422 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
423 @$filters[0] =~ s/\*/%/g;
424 push @sqlargs, @$filters[0];
427 if ( @$filters[1] ) {
428 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
429 @$filters[1] =~ s/\*/%/g;
430 push @sqlargs, @$filters[1];
433 if ( @$filters[2] ) {
434 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
435 @$filters[2] =~ s/\*/%/g;
436 push @sqlargs, @$filters[2];
439 if ( @$filters[3] ) {
440 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
441 @$filters[3] =~ s/\*/%/g;
442 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
443 push @sqlargs, @$filters[3];
445 if ( @$filters[4] ) {
446 $strcalc .= " AND " .
447 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
449 @$filters[4] =~ s/\*/%/g;
450 push @sqlargs, @$filters[4];
452 if ( @$filters[5] ) {
453 @$filters[5] =~ s/\*/%/g;
454 $strcalc .= " AND " .
455 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
457 push @sqlargs, @$filters[5];
459 if ( @$filters[6] ) {
460 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
461 @$filters[6] =~ s/\*/%/g;
462 push @sqlargs, @$filters[6];
464 if ( @$filters[7] ) {
465 $strcalc .= " AND $itemstable.location LIKE ? ";
466 @$filters[7] =~ s/\*/%/g;
467 push @sqlargs, @$filters[7];
469 if ( @$filters[8] ) {
470 $strcalc .= " AND $itemstable.ccode LIKE ? ";
471 @$filters[8] =~ s/\*/%/g;
472 push @sqlargs, @$filters[8];
474 if ( defined @$filters[9] and @$filters[9] ne '' ) {
475 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
476 @$filters[9] =~ s/\*/%/g;
477 push @sqlargs, @$filters[9];
479 if ( defined @$filters[10] and @$filters[10] ne '' ) {
480 $strcalc .= " AND $itemstable.materials LIKE ? ";
481 @$filters[10] =~ s/\*/%/g;
482 push @sqlargs, @$filters[10];
484 if ( @$filters[13] ) {
485 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
486 @$filters[13] =~ s/\*/%/g;
487 push @sqlargs, @$filters[13];
489 if ( @$filters[14] ) {
490 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
491 @$filters[14] =~ s/\*/%/g;
492 push @sqlargs, @$filters[14];
494 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
495 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
496 @$filters[15] =~ s/\*/%/g;
497 push @sqlargs, @$filters[15];
499 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
500 @$filters[16] =~ s/\*/%/g;
501 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
502 push @sqlargs, @$filters[16];
504 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
505 $debug and warn "SQL: $strcalc";
506 my $dbcalc = $dbh->prepare($strcalc);
507 $dbcalc->execute(@sqlargs);
509 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
511 $col = "zzEMPTY" if ( !defined($col) );
512 $row = "zzEMPTY" if ( !defined($row) );
514 $table{$row}->{$col} += $value;
515 $table{$row}->{totalrow} += $value;
516 $grantotal += $value;
519 foreach my $row ( @loopline ) {
522 #@loopcol ensures the order for columns is common with column titles
523 # and the number matches the number of columns
524 foreach my $col (@loopcol) {
525 my $value = $table{$row->{value}}->{ $col->{value} };
526 push @loopcell, { value => $value };
529 { 'rowtitle' => $row->{rowtitle},
530 'value' => $row->{value},
531 'loopcell' => \@loopcell,
532 'hilighted' => ( $hilighted *= -1 > 0 ),
533 'totalrow' => $table{$row->{value}}->{totalrow}
537 foreach my $col (@loopcol) {
539 foreach my $row (@looprow) {
540 $total += $table{ $row->{value} }->{ $col->{value} };
543 push @loopfooter, { 'totalcol' => $total };
546 # the header of the table
547 $globalline{loopfilter} = \@loopfilter;
549 # the core of the table
550 $globalline{looprow} = \@looprow;
551 $globalline{loopcol} = \@loopcol;
553 # the foot (totals by borrower type)
554 $globalline{loopfooter} = \@loopfooter;
555 $globalline{total} = $grantotal;
556 $globalline{line} = $line;
557 $globalline{column} = $column;
558 push @mainloop, \%globalline;