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>.
29 use C4
::Biblio qw
/GetMarcSubfieldStructureFromKohaField/;
31 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,
73 flagsrequired
=> {reports
=> '*'},
76 $template->param(do_it
=> $do_it);
78 my $results = calculate
( $line, $column, $cellvalue, $cotedigits, \
@filters );
79 if ( $output eq "screen" ) {
80 $template->param( mainloop
=> $results );
81 output_html_with_http_headers
$input, $cookie, $template->output;
87 -attachment
=> "$basename.csv",
88 -name
=> "$basename.csv"
90 my $cols = @
$results[0]->{loopcol
};
91 my $lines = @
$results[0]->{looprow
};
92 print @
$results[0]->{line
} . "/" . @
$results[0]->{column
} . $sep;
93 foreach my $col (@
$cols) {
94 print $col->{coltitle
} . $sep;
97 foreach my $line (@
$lines) {
98 my $x = $line->{loopcell
};
99 print $line->{rowtitle
} . $sep;
100 foreach my $cell (@
$x) {
101 print $cell->{value
} . $sep;
103 print $line->{totalrow
};
107 $cols = @
$results[0]->{loopfooter
};
108 foreach my $col (@
$cols) {
109 print $sep. $col->{totalcol
};
111 print $sep. @
$results[0]->{total
};
115 my $dbh = C4
::Context
->dbh;
118 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
120 my @authvals = map { { code
=> $_->{authorised_value
}, description
=> $_->{lib
} } } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.ccode' }, { order_by
=> ['description'] } );
121 my @locations = map { { code
=> $_->{authorised_value
}, description
=> $_->{lib
} } } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' }, { order_by
=> ['description'] } );
123 foreach my $kohafield (qw(items.notforloan items.materials)) {
124 my $subfield_structure = GetMarcSubfieldStructureFromKohaField
($kohafield);
125 if($subfield_structure) {
127 my $avcategory = $subfield_structure->{authorised_value
};
129 $avlist = GetAuthorisedValues
($avcategory);
135 $kf."_label" => $subfield_structure->{liblibrarian
},
136 $kf."_avlist" => $avlist
141 my @mime = ( map { +{type
=>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
144 itemtypes
=> $itemtypes,
145 locationloop
=> \
@locations,
146 authvals
=> \
@authvals,
147 CGIextChoice
=> \
@mime,
148 CGIsepChoice
=> GetDelimiterChoices
,
149 item_itype
=> $item_itype,
153 output_html_with_http_headers
$input, $cookie, $template->output;
159 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
167 my $barcodelike = @
$filters[16];
168 my $barcodefilter = @
$filters[17];
170 my $itemstable = ($cellvalue eq 'deleteditems') ?
'deleteditems' : 'items';
172 my $dbh = C4
::Context
->dbh;
174 # if barcodefilter is empty set as %
175 if ($barcodefilter) {
177 # Check if barcodefilter is "like" or "not like"
178 if ( !$barcodelike ) {
183 $barcodefilter =~ s/\*/%/g;
190 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
192 if ( defined @
$filters[$i] and @
$filters[$i] ne '' and $i != 11 ) {
193 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @
$filters[ $i - 1 ] ) ) {
194 $cell{err
} = 1 if ( @
$filters[$i] < @
$filters[ $i - 1 ] );
196 $cell{filter
} .= @
$filters[$i];
198 ( $i == 0 ) ?
"Item CallNumber From"
199 : ( $i == 1 ) ?
"Item CallNumber To"
200 : ( $i == 2 ) ?
"Item type"
201 : ( $i == 3 ) ?
"Publisher"
202 : ( $i == 4 ) ?
"Publication year From"
203 : ( $i == 5 ) ?
"Publication year To"
204 : ( $i == 6 ) ?
"Library"
205 : ( $i == 7 ) ?
"Shelving Location"
206 : ( $i == 8 ) ?
"Collection Code"
207 : ( $i == 9 ) ?
"Status"
208 : ( $i == 10 ) ?
"Materials"
209 : ( $i == 12 and $filters->[11] == 0 ) ?
"Barcode (not like)"
210 : ( $i == 12 and $filters->[11] == 1 ) ?
"Barcode (like)"
211 : ( $i == 13 ) ?
"Date acquired (item) from"
212 : ( $i == 14 ) ?
"Date acquired (item) to"
213 : ( $i == 15 ) ?
"Date deleted (item) from"
214 : ( $i == 16 ) ?
"Date deleted (item) to"
217 push @loopfilter, \
%cell;
221 @
$filters[13] = dt_from_string
(@
$filters[13])->date() if @
$filters[13];
222 @
$filters[14] = dt_from_string
(@
$filters[14])->date() if @
$filters[14];
223 @
$filters[15] = dt_from_string
(@
$filters[15])->date() if @
$filters[15];
224 @
$filters[16] = dt_from_string
(@
$filters[16])->date() if @
$filters[16];
227 $linefilter[0] = @
$filters[0] if ( $line =~ /items\.itemcallnumber/ );
228 $linefilter[1] = @
$filters[1] if ( $line =~ /items\.itemcallnumber/ );
229 if ( C4
::Context
->preference('item-level_itypes') ) {
230 $linefilter[0] = @
$filters[2] if ( $line =~ /items\.itype/ );
232 $linefilter[0] = @
$filters[2] if ( $line =~ /itemtype/ );
234 $linefilter[0] = @
$filters[3] if ( $line =~ /publishercode/ );
235 $linefilter[0] = @
$filters[4] if ( $line =~ /publicationyear/ );
236 $linefilter[1] = @
$filters[5] if ( $line =~ /publicationyear/ );
238 $linefilter[0] = @
$filters[6] if ( $line =~ /items\.homebranch/ );
239 $linefilter[0] = @
$filters[7] if ( $line =~ /items\.location/ );
240 $linefilter[0] = @
$filters[8] if ( $line =~ /items\.ccode/ );
241 $linefilter[0] = @
$filters[9] if ( $line =~ /items\.notforloan/ );
242 $linefilter[0] = @
$filters[10] if ( $line =~ /items\.materials/ );
243 $linefilter[0] = @
$filters[13] if ( $line =~ /items\.dateaccessioned/ );
244 $linefilter[1] = @
$filters[14] if ( $line =~ /items\.dateaccessioned/ );
245 $linefilter[0] = @
$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
246 $linefilter[1] = @
$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
249 $colfilter[0] = @
$filters[0] if ( $column =~ /items\.itemcallnumber/ );
250 $colfilter[1] = @
$filters[1] if ( $column =~ /items\.itemcallnumber/ );
251 if ( C4
::Context
->preference('item-level_itypes') ) {
252 $colfilter[0] = @
$filters[2] if ( $column =~ /items\.itype/ );
254 $colfilter[0] = @
$filters[2] if ( $column =~ /itemtype/ );
256 $colfilter[0] = @
$filters[3] if ( $column =~ /publishercode/ );
257 $colfilter[0] = @
$filters[4] if ( $column =~ /publicationyear/ );
258 $colfilter[1] = @
$filters[5] if ( $column =~ /publicationyear/ );
259 $colfilter[0] = @
$filters[6] if ( $column =~ /items\.homebranch/ );
260 $colfilter[0] = @
$filters[7] if ( $column =~ /items\.location/ );
261 $colfilter[0] = @
$filters[8] if ( $column =~ /items\.ccode/ );
262 $colfilter[0] = @
$filters[9] if ( $column =~ /items\.notforloan/ );
263 $colfilter[0] = @
$filters[10] if ( $column =~ /items\.materials/ );
264 $colfilter[0] = @
$filters[13] if ( $column =~ /items.dateaccessioned/ );
265 $colfilter[1] = @
$filters[14] if ( $column =~ /items\.dateaccessioned/ );
266 $colfilter[0] = @
$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
267 $colfilter[1] = @
$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
270 my $origline = $line;
271 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
273 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
274 $linefield = "left($line,$cotedigits)";
275 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
276 $linefield = "DATE($line)";
281 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
282 LEFT JOIN biblioitems USING (biblioitemnumber)
283 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
285 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
287 if ( $linefilter[1] ) {
288 $strsth .= " AND $line >= ? ";
289 $strsth .= " AND $line <= ? ";
290 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
291 $linefilter[0] =~ s/\*/%/g;
292 $strsth .= " AND $line LIKE ? ";
295 $strsth .= " ORDER BY $linefield";
296 $debug and print STDERR
"catalogue_stats SQL: $strsth\n";
298 my $sth = $dbh->prepare($strsth);
299 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
300 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
301 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
302 $sth->execute( $linefilter[0], $linefilter[1] );
303 } elsif ( $barcodefilter and $linefilter[0] ) {
304 $sth->execute( $barcodefilter, $linefilter[0] );
305 } elsif ( $linefilter[0] ) {
306 $sth->execute($linefilter[0]);
307 } elsif ($barcodefilter) {
308 $sth->execute($barcodefilter);
312 my $rowauthvals = { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> $origline } ) };
313 while ( my ($celvalue) = $sth->fetchrow ) {
315 if (defined $celvalue and $celvalue ne '') {
316 if($rowauthvals and $rowauthvals->{$celvalue}) {
317 $cell{rowtitle
} = $rowauthvals->{$celvalue};
319 $cell{rowtitle
} = $celvalue;
321 $cell{value
} = $celvalue;
324 $cell{rowtitle
} = "NULL";
325 $cell{value
} = "zzEMPTY";
328 push @loopline, \
%cell;
332 my $origcolumn = $column;
333 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
335 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
336 $colfield = "left($column,$cotedigits)";
337 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
338 $colfield = "DATE($column)";
344 SELECT distinctrow $colfield
346 LEFT JOIN biblioitems
347 USING (biblioitemnumber)
349 ON (biblioitems.biblionumber = biblio.biblionumber)
351 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
353 if ( (@colfilter) and ( $colfilter[1] ) ) {
354 $strsth2 .= " AND $column >= ? AND $column <= ?";
355 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
356 $colfilter[0] =~ s/\*/%/g;
357 $strsth2 .= " AND $column LIKE ? ";
359 $strsth2 .= " ORDER BY $colfield";
360 $debug and print STDERR
"SQL: $strsth2";
361 my $sth2 = $dbh->prepare($strsth2);
362 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
363 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
364 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
365 $sth2->execute( $colfilter[0], $colfilter[1] );
366 } elsif ( $barcodefilter && $colfilter[0] ) {
367 $sth2->execute( $barcodefilter , $colfilter[0] );
368 } elsif ( $colfilter[0]) {
369 $sth2->execute( $colfilter[0] );
370 } elsif ($barcodefilter) {
371 $sth2->execute($barcodefilter);
375 my $colauthvals = { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> $origcolumn } ) };
376 while ( my ($celvalue) = $sth2->fetchrow ) {
378 if (defined $celvalue and $celvalue ne '') {
379 if($colauthvals and $colauthvals->{$celvalue}) {
380 $cell{coltitle
} = $colauthvals->{$celvalue};
382 $cell{coltitle
} = $celvalue;
384 $cell{value
} = $celvalue;
387 $cell{coltitle
} = "NULL";
388 $cell{value
} = "zzEMPTY";
391 push @loopcol, \
%cell;
397 #Initialization of cell values.....
400 foreach my $row (@loopline) {
401 foreach my $col (@loopcol) {
402 $table{ $row->{value
} }->{ $col->{value
} } = 0;
404 $table{ $row->{value
} }->{totalrow
} = 0;
407 # preparing calculation
408 my $select_cellvalue = " COUNT(*) ";
409 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
411 SELECT $linefield, $colfield, $select_cellvalue
413 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
414 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
419 if ($barcodefilter) {
420 $strcalc .= "AND barcode $not like ? ";
421 push @sqlargs, $barcodefilter;
424 if ( @
$filters[0] ) {
425 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
426 @
$filters[0] =~ s/\*/%/g;
427 push @sqlargs, @
$filters[0];
430 if ( @
$filters[1] ) {
431 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
432 @
$filters[1] =~ s/\*/%/g;
433 push @sqlargs, @
$filters[1];
436 if ( @
$filters[2] ) {
437 $strcalc .= " AND " . ( C4
::Context
->preference('item-level_itypes') ?
"$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
438 @
$filters[2] =~ s/\*/%/g;
439 push @sqlargs, @
$filters[2];
442 if ( @
$filters[3] ) {
443 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
444 @
$filters[3] =~ s/\*/%/g;
445 @
$filters[3] .= "%" unless @
$filters[3] =~ /%/;
446 push @sqlargs, @
$filters[3];
448 if ( @
$filters[4] ) {
449 $strcalc .= " AND " .
450 (C4
::Context
->preference('marcflavour') eq 'UNIMARC' ?
'publicationyear' : 'copyrightdate')
452 @
$filters[4] =~ s/\*/%/g;
453 push @sqlargs, @
$filters[4];
455 if ( @
$filters[5] ) {
456 @
$filters[5] =~ s/\*/%/g;
457 $strcalc .= " AND " .
458 (C4
::Context
->preference('marcflavour') eq 'UNIMARC' ?
'publicationyear' : 'copyrightdate')
460 push @sqlargs, @
$filters[5];
462 if ( @
$filters[6] ) {
463 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
464 @
$filters[6] =~ s/\*/%/g;
465 push @sqlargs, @
$filters[6];
467 if ( @
$filters[7] ) {
468 $strcalc .= " AND $itemstable.location LIKE ? ";
469 @
$filters[7] =~ s/\*/%/g;
470 push @sqlargs, @
$filters[7];
472 if ( @
$filters[8] ) {
473 $strcalc .= " AND $itemstable.ccode LIKE ? ";
474 @
$filters[8] =~ s/\*/%/g;
475 push @sqlargs, @
$filters[8];
477 if ( defined @
$filters[9] and @
$filters[9] ne '' ) {
478 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
479 @
$filters[9] =~ s/\*/%/g;
480 push @sqlargs, @
$filters[9];
482 if ( defined @
$filters[10] and @
$filters[10] ne '' ) {
483 $strcalc .= " AND $itemstable.materials LIKE ? ";
484 @
$filters[10] =~ s/\*/%/g;
485 push @sqlargs, @
$filters[10];
487 if ( @
$filters[13] ) {
488 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
489 @
$filters[13] =~ s/\*/%/g;
490 push @sqlargs, @
$filters[13];
492 if ( @
$filters[14] ) {
493 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
494 @
$filters[14] =~ s/\*/%/g;
495 push @sqlargs, @
$filters[14];
497 if ( $cellvalue eq 'deleteditems' and @
$filters[15] ) {
498 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
499 @
$filters[15] =~ s/\*/%/g;
500 push @sqlargs, @
$filters[15];
502 if ( $cellvalue eq 'deleteditems' and @
$filters[16] ) {
503 @
$filters[16] =~ s/\*/%/g;
504 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
505 push @sqlargs, @
$filters[16];
507 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
508 $debug and warn "SQL: $strcalc";
509 my $dbcalc = $dbh->prepare($strcalc);
510 $dbcalc->execute(@sqlargs);
512 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
514 $col = "zzEMPTY" if ( !defined($col) );
515 $row = "zzEMPTY" if ( !defined($row) );
517 $table{$row}->{$col} += $value;
518 $table{$row}->{totalrow
} += $value;
519 $grantotal += $value;
522 foreach my $row ( @loopline ) {
525 #@loopcol ensures the order for columns is common with column titles
526 # and the number matches the number of columns
527 foreach my $col (@loopcol) {
528 my $value = $table{$row->{value
}}->{ $col->{value
} };
529 push @loopcell, { value
=> $value };
532 { 'rowtitle' => $row->{rowtitle
},
533 'value' => $row->{value
},
534 'loopcell' => \
@loopcell,
535 'hilighted' => ( $hilighted *= -1 > 0 ),
536 'totalrow' => $table{$row->{value
}}->{totalrow
}
540 foreach my $col (@loopcol) {
542 foreach my $row (@looprow) {
543 $total += $table{ $row->{value
} }->{ $col->{value
} };
546 push @loopfooter, { 'totalcol' => $total };
549 # the header of the table
550 $globalline{loopfilter
} = \
@loopfilter;
552 # the core of the table
553 $globalline{looprow
} = \
@looprow;
554 $globalline{loopcol
} = \
@loopcol;
556 # the foot (totals by borrower type)
557 $globalline{loopfooter
} = \
@loopfooter;
558 $globalline{total
} = $grantotal;
559 $globalline{line
} = $line;
560 $globalline{column
} = $column;
561 push @mainloop, \
%globalline;