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 warnings; FIXME - Bug 2505
30 use C4
::Biblio qw
/GetMarcSubfieldStructureFromKohaField/;
32 use Koha
::AuthorisedValues
;
37 plugin that shows a stats on borrowers
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');
57 if(C4
::Context
->preference('item-level_itypes')) {
58 $item_itype = "items\.itype"
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,
74 flagsrequired
=> {reports
=> '*'},
77 $template->param(do_it
=> $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;
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;
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
};
108 $cols = @
$results[0]->{loopfooter
};
109 foreach my $col (@
$cols) {
110 print $sep. $col->{totalcol
};
112 print $sep. @
$results[0]->{total
};
116 my $dbh = C4
::Context
->dbh;
123 my $itemtypes = GetItemTypes
( style
=> 'array' );
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) {
132 my $avcategory = $subfield_structure->{authorised_value
};
134 $avlist = GetAuthorisedValues
($avcategory);
140 $kf."_label" => $subfield_structure->{liblibrarian
},
141 $kf."_avlist" => $avlist
146 my @mime = ( map { +{type
=>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
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;
164 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
172 my $barcodelike = @
$filters[16];
173 my $barcodefilter = @
$filters[17];
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 ) {
188 $barcodefilter =~ s/\*/%/g;
195 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
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];
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"
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];
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/ );
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/ );
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/ );
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/ );
275 my $origline = $line;
276 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
278 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
279 $linefield = "left($line,$cotedigits)";
280 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
281 $linefield = "DATE($line)";
286 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
287 LEFT JOIN biblioitems USING (biblioitemnumber)
288 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
290 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
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);
317 my $rowauthvals = { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> $origline } ) };
318 while ( my ($celvalue) = $sth->fetchrow ) {
320 if (defined $celvalue and $celvalue ne '') {
321 if($rowauthvals and $rowauthvals->{$celvalue}) {
322 $cell{rowtitle
} = $rowauthvals->{$celvalue};
324 $cell{rowtitle
} = $celvalue;
326 $cell{value
} = $celvalue;
329 $cell{rowtitle
} = "NULL";
330 $cell{value
} = "zzEMPTY";
333 push @loopline, \
%cell;
337 my $origcolumn = $column;
338 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
340 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
341 $colfield = "left($column,$cotedigits)";
342 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
343 $colfield = "DATE($column)";
349 SELECT distinctrow $colfield
351 LEFT JOIN biblioitems
352 USING (biblioitemnumber)
354 ON (biblioitems.biblionumber = biblio.biblionumber)
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);
380 my $colauthvals = { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> $origcolumn } ) };
381 while ( my ($celvalue) = $sth2->fetchrow ) {
383 if (defined $celvalue and $celvalue ne '') {
384 if($colauthvals and $colauthvals->{$celvalue}) {
385 $cell{coltitle
} = $colauthvals->{$celvalue};
387 $cell{coltitle
} = $celvalue;
389 $cell{value
} = $celvalue;
392 $cell{coltitle
} = "NULL";
393 $cell{value
} = "zzEMPTY";
396 push @loopcol, \
%cell;
403 #Initialization of cell values.....
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');
417 SELECT $linefield, $colfield, $select_cellvalue
419 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
420 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
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')
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')
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 ) {
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 };
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) {
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;