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
26 use C4
::Branch
; # GetBranches
31 use C4
::Biblio qw
/GetMarcSubfieldStructureFromKohaField/;
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 = GetKohaAuthorisedValues
("items.ccode");
127 foreach ( sort { $authvals->{$a} cmp $authvals->{$b} || $a cmp $b } keys %$authvals ) {
128 push @authvals, { code
=> $_, description
=> $authvals->{$_} };
131 my $locations = GetKohaAuthorisedValues
("items.location");
133 foreach ( sort keys %$locations ) {
134 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
137 foreach my $kohafield (qw(items.notforloan items.materials)) {
138 my $subfield_structure = GetMarcSubfieldStructureFromKohaField
($kohafield);
139 if($subfield_structure) {
141 my $avcategory = $subfield_structure->{authorised_value
};
143 $avlist = GetAuthorisedValues
($avcategory);
149 $kf."_label" => $subfield_structure->{liblibrarian
},
150 $kf."_avlist" => $avlist
155 my @mime = ( map { +{type
=>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
158 itemtypes
=> $itemtypes,
159 CGIBranch
=> GetBranchesLoop
( C4
::Context
->userenv->{'branch'} ),
160 locationloop
=> \
@locations,
161 authvals
=> \
@authvals,
162 CGIextChoice
=> \
@mime,
163 CGIsepChoice
=> GetDelimiterChoices
,
164 item_itype
=> $item_itype,
168 output_html_with_http_headers
$input, $cookie, $template->output;
174 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
182 my $barcodelike = @
$filters[16];
183 my $barcodefilter = @
$filters[17];
185 my $itemstable = ($cellvalue eq 'deleteditems') ?
'deleteditems' : 'items';
187 my $dbh = C4
::Context
->dbh;
189 # if barcodefilter is empty set as %
190 if ($barcodefilter) {
192 # Check if barcodefilter is "like" or "not like"
193 if ( !$barcodelike ) {
198 $barcodefilter =~ s/\*/%/g;
205 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
207 if ( defined @
$filters[$i] and @
$filters[$i] ne '' and $i != 11 ) {
208 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @
$filters[ $i - 1 ] ) ) {
209 $cell{err
} = 1 if ( @
$filters[$i] < @
$filters[ $i - 1 ] );
211 $cell{filter
} .= @
$filters[$i];
213 ( $i == 0 ) ?
"Item CallNumber From"
214 : ( $i == 1 ) ?
"Item CallNumber To"
215 : ( $i == 2 ) ?
"Item type"
216 : ( $i == 3 ) ?
"Publisher"
217 : ( $i == 4 ) ?
"Publication year From"
218 : ( $i == 5 ) ?
"Publication year To"
219 : ( $i == 6 ) ?
"Library"
220 : ( $i == 7 ) ?
"Shelving Location"
221 : ( $i == 8 ) ?
"Collection Code"
222 : ( $i == 9 ) ?
"Status"
223 : ( $i == 10 ) ?
"Materials"
224 : ( $i == 12 and $filters->[11] == 0 ) ?
"Barcode (not like)"
225 : ( $i == 12 and $filters->[11] == 1 ) ?
"Barcode (like)"
226 : ( $i == 13 ) ?
"Date acquired (item) from"
227 : ( $i == 14 ) ?
"Date acquired (item) to"
228 : ( $i == 15 ) ?
"Date deleted (item) from"
229 : ( $i == 16 ) ?
"Date deleted (item) to"
232 push @loopfilter, \
%cell;
236 @
$filters[13] = dt_from_string
(@
$filters[13])->date() if @
$filters[13];
237 @
$filters[14] = dt_from_string
(@
$filters[14])->date() if @
$filters[14];
238 @
$filters[15] = dt_from_string
(@
$filters[15])->date() if @
$filters[15];
239 @
$filters[16] = dt_from_string
(@
$filters[16])->date() if @
$filters[16];
242 $linefilter[0] = @
$filters[0] if ( $line =~ /items\.itemcallnumber/ );
243 $linefilter[1] = @
$filters[1] if ( $line =~ /items\.itemcallnumber/ );
244 if ( C4
::Context
->preference('item-level_itypes') ) {
245 $linefilter[0] = @
$filters[2] if ( $line =~ /items\.itype/ );
247 $linefilter[0] = @
$filters[2] if ( $line =~ /itemtype/ );
249 $linefilter[0] = @
$filters[3] if ( $line =~ /publishercode/ );
250 $linefilter[0] = @
$filters[4] if ( $line =~ /publicationyear/ );
251 $linefilter[1] = @
$filters[5] if ( $line =~ /publicationyear/ );
253 $linefilter[0] = @
$filters[6] if ( $line =~ /items\.homebranch/ );
254 $linefilter[0] = @
$filters[7] if ( $line =~ /items\.location/ );
255 $linefilter[0] = @
$filters[8] if ( $line =~ /items\.ccode/ );
256 $linefilter[0] = @
$filters[9] if ( $line =~ /items\.notforloan/ );
257 $linefilter[0] = @
$filters[10] if ( $line =~ /items\.materials/ );
258 $linefilter[0] = @
$filters[13] if ( $line =~ /items\.dateaccessioned/ );
259 $linefilter[1] = @
$filters[14] if ( $line =~ /items\.dateaccessioned/ );
260 $linefilter[0] = @
$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
261 $linefilter[1] = @
$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
264 $colfilter[0] = @
$filters[0] if ( $column =~ /items\.itemcallnumber/ );
265 $colfilter[1] = @
$filters[1] if ( $column =~ /items\.itemcallnumber/ );
266 if ( C4
::Context
->preference('item-level_itypes') ) {
267 $colfilter[0] = @
$filters[2] if ( $column =~ /items\.itype/ );
269 $colfilter[0] = @
$filters[2] if ( $column =~ /itemtype/ );
271 $colfilter[0] = @
$filters[3] if ( $column =~ /publishercode/ );
272 $colfilter[0] = @
$filters[4] if ( $column =~ /publicationyear/ );
273 $colfilter[1] = @
$filters[5] if ( $column =~ /publicationyear/ );
274 $colfilter[0] = @
$filters[6] if ( $column =~ /items\.homebranch/ );
275 $colfilter[0] = @
$filters[7] if ( $column =~ /items\.location/ );
276 $colfilter[0] = @
$filters[8] if ( $column =~ /items\.ccode/ );
277 $colfilter[0] = @
$filters[9] if ( $column =~ /items\.notforloan/ );
278 $colfilter[0] = @
$filters[10] if ( $column =~ /items\.materials/ );
279 $colfilter[0] = @
$filters[13] if ( $column =~ /items.dateaccessioned/ );
280 $colfilter[1] = @
$filters[14] if ( $column =~ /items\.dateaccessioned/ );
281 $colfilter[0] = @
$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
282 $colfilter[1] = @
$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
285 my $origline = $line;
286 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
288 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
289 $linefield = "left($line,$cotedigits)";
290 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
291 $linefield = "DATE($line)";
296 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
297 LEFT JOIN biblioitems USING (biblioitemnumber)
298 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
300 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
302 if ( $linefilter[1] ) {
303 $strsth .= " AND $line >= ? ";
304 $strsth .= " AND $line <= ? ";
305 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
306 $linefilter[0] =~ s/\*/%/g;
307 $strsth .= " AND $line LIKE ? ";
310 $strsth .= " ORDER BY $linefield";
311 $debug and print STDERR
"catalogue_stats SQL: $strsth\n";
313 my $sth = $dbh->prepare($strsth);
314 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
315 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
316 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
317 $sth->execute( $linefilter[0], $linefilter[1] );
318 } elsif ( $barcodefilter and $linefilter[0] ) {
319 $sth->execute( $barcodefilter, $linefilter[0] );
320 } elsif ( $linefilter[0] ) {
321 $sth->execute($linefilter[0]);
322 } elsif ($barcodefilter) {
323 $sth->execute($barcodefilter);
327 my $rowauthvals = GetKohaAuthorisedValues
($origline);
328 while ( my ($celvalue) = $sth->fetchrow ) {
330 if (defined $celvalue and $celvalue ne '') {
331 if($rowauthvals and $rowauthvals->{$celvalue}) {
332 $cell{rowtitle
} = $rowauthvals->{$celvalue};
334 $cell{rowtitle
} = $celvalue;
336 $cell{value
} = $celvalue;
339 $cell{rowtitle
} = "NULL";
340 $cell{value
} = "zzEMPTY";
343 push @loopline, \
%cell;
347 my $origcolumn = $column;
348 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
350 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
351 $colfield = "left($column,$cotedigits)";
352 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
353 $colfield = "DATE($column)";
359 SELECT distinctrow $colfield
361 LEFT JOIN biblioitems
362 USING (biblioitemnumber)
364 ON (biblioitems.biblionumber = biblio.biblionumber)
366 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
368 if ( (@colfilter) and ( $colfilter[1] ) ) {
369 $strsth2 .= " AND $column >= ? AND $column <= ?";
370 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
371 $colfilter[0] =~ s/\*/%/g;
372 $strsth2 .= " AND $column LIKE ? ";
374 $strsth2 .= " ORDER BY $colfield";
375 $debug and print STDERR
"SQL: $strsth2";
376 my $sth2 = $dbh->prepare($strsth2);
377 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
378 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
379 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
380 $sth2->execute( $colfilter[0], $colfilter[1] );
381 } elsif ( $barcodefilter && $colfilter[0] ) {
382 $sth2->execute( $barcodefilter , $colfilter[0] );
383 } elsif ( $colfilter[0]) {
384 $sth2->execute( $colfilter[0] );
385 } elsif ($barcodefilter) {
386 $sth2->execute($barcodefilter);
390 my $colauthvals = GetKohaAuthorisedValues
($origcolumn);
391 while ( my ($celvalue) = $sth2->fetchrow ) {
393 if (defined $celvalue and $celvalue ne '') {
394 if($colauthvals and $colauthvals->{$celvalue}) {
395 $cell{coltitle
} = $colauthvals->{$celvalue};
397 $cell{coltitle
} = $celvalue;
399 $cell{value
} = $celvalue;
402 $cell{coltitle
} = "NULL";
403 $cell{value
} = "zzEMPTY";
406 push @loopcol, \
%cell;
413 #Initialization of cell values.....
416 foreach my $row (@loopline) {
417 foreach my $col (@loopcol) {
418 $table{ $row->{value
} }->{ $col->{value
} } = 0;
420 $table{ $row->{value
} }->{totalrow
} = 0;
423 # preparing calculation
424 my $select_cellvalue = " COUNT(*) ";
425 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
427 SELECT $linefield, $colfield, $select_cellvalue
429 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
430 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
435 if ($barcodefilter) {
436 $strcalc .= "AND barcode $not like ? ";
437 push @sqlargs, $barcodefilter;
440 if ( @
$filters[0] ) {
441 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
442 @
$filters[0] =~ s/\*/%/g;
443 push @sqlargs, @
$filters[0];
446 if ( @
$filters[1] ) {
447 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
448 @
$filters[1] =~ s/\*/%/g;
449 push @sqlargs, @
$filters[1];
452 if ( @
$filters[2] ) {
453 $strcalc .= " AND " . ( C4
::Context
->preference('item-level_itypes') ?
"$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
454 @
$filters[2] =~ s/\*/%/g;
455 push @sqlargs, @
$filters[2];
458 if ( @
$filters[3] ) {
459 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
460 @
$filters[3] =~ s/\*/%/g;
461 @
$filters[3] .= "%" unless @
$filters[3] =~ /%/;
462 push @sqlargs, @
$filters[3];
464 if ( @
$filters[4] ) {
465 $strcalc .= " AND " .
466 (C4
::Context
->preference('marcflavour') eq 'UNIMARC' ?
'publicationyear' : 'copyrightdate')
468 @
$filters[4] =~ s/\*/%/g;
469 push @sqlargs, @
$filters[4];
471 if ( @
$filters[5] ) {
472 @
$filters[5] =~ s/\*/%/g;
473 $strcalc .= " AND " .
474 (C4
::Context
->preference('marcflavour') eq 'UNIMARC' ?
'publicationyear' : 'copyrightdate')
476 push @sqlargs, @
$filters[5];
478 if ( @
$filters[6] ) {
479 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
480 @
$filters[6] =~ s/\*/%/g;
481 push @sqlargs, @
$filters[6];
483 if ( @
$filters[7] ) {
484 $strcalc .= " AND $itemstable.location LIKE ? ";
485 @
$filters[7] =~ s/\*/%/g;
486 push @sqlargs, @
$filters[7];
488 if ( @
$filters[8] ) {
489 $strcalc .= " AND $itemstable.ccode LIKE ? ";
490 @
$filters[8] =~ s/\*/%/g;
491 push @sqlargs, @
$filters[8];
493 if ( defined @
$filters[9] and @
$filters[9] ne '' ) {
494 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
495 @
$filters[9] =~ s/\*/%/g;
496 push @sqlargs, @
$filters[9];
498 if ( defined @
$filters[10] and @
$filters[10] ne '' ) {
499 $strcalc .= " AND $itemstable.materials LIKE ? ";
500 @
$filters[10] =~ s/\*/%/g;
501 push @sqlargs, @
$filters[10];
503 if ( @
$filters[13] ) {
504 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
505 @
$filters[13] =~ s/\*/%/g;
506 push @sqlargs, @
$filters[13];
508 if ( @
$filters[14] ) {
509 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
510 @
$filters[14] =~ s/\*/%/g;
511 push @sqlargs, @
$filters[14];
513 if ( $cellvalue eq 'deleteditems' and @
$filters[15] ) {
514 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
515 @
$filters[15] =~ s/\*/%/g;
516 push @sqlargs, @
$filters[15];
518 if ( $cellvalue eq 'deleteditems' and @
$filters[16] ) {
519 @
$filters[16] =~ s/\*/%/g;
520 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
521 push @sqlargs, @
$filters[16];
523 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
524 $debug and warn "SQL: $strcalc";
525 my $dbcalc = $dbh->prepare($strcalc);
526 $dbcalc->execute(@sqlargs);
528 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
530 $col = "zzEMPTY" if ( !defined($col) );
531 $row = "zzEMPTY" if ( !defined($row) );
533 $table{$row}->{$col} += $value;
534 $table{$row}->{totalrow
} += $value;
535 $grantotal += $value;
538 foreach my $row ( @loopline ) {
541 #@loopcol ensures the order for columns is common with column titles
542 # and the number matches the number of columns
543 foreach my $col (@loopcol) {
544 my $value = $table{$row->{value
}}->{ $col->{value
} };
545 push @loopcell, { value
=> $value };
548 { 'rowtitle' => $row->{rowtitle
},
549 'value' => $row->{value
},
550 'loopcell' => \
@loopcell,
551 'hilighted' => ( $hilighted *= -1 > 0 ),
552 'totalrow' => $table{$row->{value
}}->{totalrow
}
556 foreach my $col (@loopcol) {
558 foreach my $row (@looprow) {
559 $total += $table{ $row->{value
} }->{ $col->{value
} };
562 push @loopfooter, { 'totalcol' => $total };
565 # the header of the table
566 $globalline{loopfilter
} = \
@loopfilter;
568 # the core of the table
569 $globalline{looprow
} = \
@looprow;
570 $globalline{loopcol
} = \
@loopcol;
572 # the foot (totals by borrower type)
573 $globalline{loopfooter
} = \
@loopfooter;
574 $globalline{total
} = $grantotal;
575 $globalline{line
} = $line;
576 $globalline{column
} = $column;
577 push @mainloop, \
%globalline;