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/;
35 plugin that shows a stats on borrowers
43 my $fullreportname = "reports/catalogue_stats.tt";
44 my $do_it = $input->param('do_it');
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my $cellvalue = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
48 my @filters = $input->param("Filter");
49 my $cotedigits = $input->param("cotedigits");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 our $sep = $input->param("sep");
53 $sep = "\t" if ($sep eq 'tabulation');
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,
72 flagsrequired => {reports => '*'},
75 $template->param(do_it => $do_it);
77 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
78 if ( $output eq "screen" ) {
79 $template->param( mainloop => $results );
80 output_html_with_http_headers $input, $cookie, $template->output;
84 -type => 'application/vnd.sun.xml.calc',
86 -attachment => "$basename.csv",
87 -name => "$basename.csv"
89 my $cols = @$results[0]->{loopcol};
90 my $lines = @$results[0]->{looprow};
91 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
92 foreach my $col (@$cols) {
93 print $col->{coltitle} . $sep;
96 foreach my $line (@$lines) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle} . $sep;
99 foreach my $cell (@$x) {
100 print $cell->{value} . $sep;
102 print $line->{totalrow};
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col (@$cols) {
108 print $sep. $col->{totalcol};
110 print $sep. @$results[0]->{total};
114 my $dbh = C4::Context->dbh;
121 my $itemtypes = GetItemTypes( style => 'array' );
123 my $authvals = GetKohaAuthorisedValues("items.ccode");
125 foreach ( sort { $authvals->{$a} cmp $authvals->{$b} || $a cmp $b } keys %$authvals ) {
126 push @authvals, { code => $_, description => $authvals->{$_} };
129 my $locations = GetKohaAuthorisedValues("items.location");
131 foreach ( sort keys %$locations ) {
132 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
135 foreach my $kohafield (qw(items.notforloan items.materials)) {
136 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
137 if($subfield_structure) {
139 my $avcategory = $subfield_structure->{authorised_value};
141 $avlist = GetAuthorisedValues($avcategory);
147 $kf."_label" => $subfield_structure->{liblibrarian},
148 $kf."_avlist" => $avlist
153 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
156 itemtypes => $itemtypes,
157 CGIBranch => GetBranchesLoop( C4::Context->userenv->{'branch'} ),
158 locationloop => \@locations,
159 authvals => \@authvals,
160 CGIextChoice => \@mime,
161 CGIsepChoice => GetDelimiterChoices,
162 item_itype => $item_itype,
166 output_html_with_http_headers $input, $cookie, $template->output;
172 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
180 my $barcodelike = @$filters[16];
181 my $barcodefilter = @$filters[17];
183 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
185 my $dbh = C4::Context->dbh;
187 # if barcodefilter is empty set as %
188 if ($barcodefilter) {
190 # Check if barcodefilter is "like" or "not like"
191 if ( !$barcodelike ) {
196 $barcodefilter =~ s/\*/%/g;
203 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
205 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
206 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
207 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
209 $cell{filter} .= @$filters[$i];
211 ( $i == 0 ) ? "Item CallNumber From"
212 : ( $i == 1 ) ? "Item CallNumber To"
213 : ( $i == 2 ) ? "Item type"
214 : ( $i == 3 ) ? "Publisher"
215 : ( $i == 4 ) ? "Publication year From"
216 : ( $i == 5 ) ? "Publication year To"
217 : ( $i == 6 ) ? "Library"
218 : ( $i == 7 ) ? "Shelving Location"
219 : ( $i == 8 ) ? "Collection Code"
220 : ( $i == 9 ) ? "Status"
221 : ( $i == 10 ) ? "Materials"
222 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
223 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
224 : ( $i == 13 ) ? "Acquired date from"
225 : ( $i == 14 ) ? "Acquired date to"
226 : ( $i == 15 ) ? "Removed date from"
227 : ( $i == 16 ) ? "Removed date to"
230 push @loopfilter, \%cell;
234 @$filters[14] = C4::Dates->new(@$filters[14])->output('iso') if @$filters[14];
235 @$filters[15] = C4::Dates->new(@$filters[15])->output('iso') if @$filters[15];
236 @$filters[16] = C4::Dates->new(@$filters[16])->output('iso') if @$filters[16];
237 @$filters[17] = C4::Dates->new(@$filters[17])->output('iso') if @$filters[17];
240 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
241 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
242 if ( C4::Context->preference('item-level_itypes') ) {
243 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
245 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
247 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
248 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
249 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
251 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
252 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
253 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
254 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
255 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
256 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
257 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
258 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
259 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
262 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
263 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
264 if ( C4::Context->preference('item-level_itypes') ) {
265 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
267 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
269 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
270 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
271 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
272 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
273 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
274 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
275 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
276 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
277 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
278 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
279 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
280 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
283 my $origline = $line;
284 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
286 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
287 $linefield = "left($line,$cotedigits)";
288 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
289 $linefield = "DATE($line)";
294 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
295 LEFT JOIN biblioitems USING (biblioitemnumber)
296 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
298 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
300 if ( $linefilter[1] ) {
301 $strsth .= " AND $line >= ? ";
302 $strsth .= " AND $line <= ? ";
303 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
304 $linefilter[0] =~ s/\*/%/g;
305 $strsth .= " AND $line LIKE ? ";
308 $strsth .= " ORDER BY $linefield";
309 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
311 my $sth = $dbh->prepare($strsth);
312 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
313 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
314 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
315 $sth->execute( $linefilter[0], $linefilter[1] );
316 } elsif ( $barcodefilter and $linefilter[0] ) {
317 $sth->execute( $barcodefilter, $linefilter[0] );
318 } elsif ( $linefilter[0] ) {
319 $sth->execute($linefilter[0]);
320 } elsif ($barcodefilter) {
321 $sth->execute($barcodefilter);
325 my $rowauthvals = GetKohaAuthorisedValues($origline);
326 while ( my ($celvalue) = $sth->fetchrow ) {
328 if (defined $celvalue and $celvalue ne '') {
329 if($rowauthvals and $rowauthvals->{$celvalue}) {
330 $cell{rowtitle} = $rowauthvals->{$celvalue};
332 $cell{rowtitle} = $celvalue;
334 $cell{value} = $celvalue;
337 $cell{rowtitle} = "NULL";
338 $cell{value} = "zzEMPTY";
341 push @loopline, \%cell;
345 my $origcolumn = $column;
346 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
348 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
349 $colfield = "left($column,$cotedigits)";
350 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
351 $colfield = "DATE($column)";
357 SELECT distinctrow $colfield
359 LEFT JOIN biblioitems
360 USING (biblioitemnumber)
362 ON (biblioitems.biblionumber = biblio.biblionumber)
364 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
366 if ( (@colfilter) and ( $colfilter[1] ) ) {
367 $strsth2 .= " AND $column >= ? AND $column <= ?";
368 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
369 $colfilter[0] =~ s/\*/%/g;
370 $strsth2 .= " AND $column LIKE ? ";
372 $strsth2 .= " ORDER BY $colfield";
373 $debug and print STDERR "SQL: $strsth2";
374 my $sth2 = $dbh->prepare($strsth2);
375 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
376 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
377 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
378 $sth2->execute( $colfilter[0], $colfilter[1] );
379 } elsif ( $barcodefilter && $colfilter[0] ) {
380 $sth2->execute( $barcodefilter , $colfilter[0] );
381 } elsif ( $colfilter[0]) {
382 $sth2->execute( $colfilter[0] );
383 } elsif ($barcodefilter) {
384 $sth2->execute($barcodefilter);
388 my $colauthvals = GetKohaAuthorisedValues($origcolumn);
389 while ( my ($celvalue) = $sth2->fetchrow ) {
391 if (defined $celvalue and $celvalue ne '') {
392 if($colauthvals and $colauthvals->{$celvalue}) {
393 $cell{coltitle} = $colauthvals->{$celvalue};
395 $cell{coltitle} = $celvalue;
397 $cell{value} = $celvalue;
400 $cell{coltitle} = "NULL";
401 $cell{value} = "zzEMPTY";
404 push @loopcol, \%cell;
411 #Initialization of cell values.....
414 foreach my $row (@loopline) {
415 foreach my $col (@loopcol) {
416 $table{ $row->{value} }->{ $col->{value} } = 0;
418 $table{ $row->{value} }->{totalrow} = 0;
421 # preparing calculation
422 my $select_cellvalue = " COUNT(*) ";
423 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
425 SELECT $linefield, $colfield, $select_cellvalue
427 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
428 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
430 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
432 if ( @$filters[0] ) {
433 @$filters[0] =~ s/\*/%/g;
434 $strcalc .= " AND $itemstable.itemcallnumber >=" . $dbh->quote( @$filters[0] );
437 if ( @$filters[1] ) {
438 @$filters[1] =~ s/\*/%/g;
439 $strcalc .= " AND $itemstable.itemcallnumber <=" . $dbh->quote( @$filters[1] );
442 if ( @$filters[2] ) {
443 @$filters[2] =~ s/\*/%/g;
444 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE '" . @$filters[2] . "'";
447 if ( @$filters[3] ) {
448 @$filters[3] =~ s/\*/%/g;
449 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
450 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[3] . "\"";
452 if ( @$filters[4] ) {
453 @$filters[4] =~ s/\*/%/g;
454 $strcalc .= " AND " .
455 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
456 . ">" . @$filters[4];
458 if ( @$filters[5] ) {
459 @$filters[5] =~ s/\*/%/g;
460 $strcalc .= " AND " .
461 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
462 . "<" . @$filters[5];
464 if ( @$filters[6] ) {
465 @$filters[6] =~ s/\*/%/g;
466 $strcalc .= " AND $itemstable.homebranch LIKE '" . @$filters[6] . "'";
468 if ( @$filters[7] ) {
469 @$filters[7] =~ s/\*/%/g;
470 $strcalc .= " AND $itemstable.location LIKE '" . @$filters[7] . "'";
472 if ( @$filters[8] ) {
473 @$filters[8] =~ s/\*/%/g;
474 $strcalc .= " AND $itemstable.ccode LIKE '" . @$filters[8] . "'";
476 if ( defined @$filters[9] and @$filters[9] ne '' ) {
477 @$filters[9] =~ s/\*/%/g;
478 $strcalc .= " AND $itemstable.notforloan LIKE '" . @$filters[9] . "'";
480 if ( defined @$filters[10] and @$filters[10] ne '' ) {
481 @$filters[10] =~ s/\*/%/g;
482 $strcalc .= " AND $itemstable.materials LIKE '" . @$filters[10] . "'";
484 if ( @$filters[13] ) {
485 @$filters[13] =~ s/\*/%/g;
486 $strcalc .= " AND $itemstable.dateaccessioned >= '@$filters[13]' ";
488 if ( @$filters[14] ) {
489 @$filters[14] =~ s/\*/%/g;
490 $strcalc .= " AND $itemstable.dateaccessioned <= '@$filters[14]' ";
492 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
493 @$filters[15] =~ s/\*/%/g;
494 $strcalc .= " AND DATE(deleteditems.timestamp) >= '@$filters[15]' ";
496 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
497 @$filters[16] =~ s/\*/%/g;
498 $strcalc .= " AND DATE(deleteditems.timestamp) <= '@$filters[16]' ";
500 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
501 $debug and warn "SQL: $strcalc";
502 my $dbcalc = $dbh->prepare($strcalc);
503 if ($barcodefilter) {
504 $dbcalc->execute($barcodefilter);
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;