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
34 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 @filters = $input->param("Filter");
49 my $deweydigits = $input->param("deweydigits");
50 my $lccndigits = $input->param("lccndigits");
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, $deweydigits, $lccndigits, $cotedigits, \@filters);
80 if ($output eq "screen"){
81 $template->param(mainloop => $results);
82 output_html_with_http_headers $input, $cookie, $template->output;
85 print $input->header(-type => 'application/vnd.sun.xml.calc',
87 -attachment=>"$basename.csv",
88 -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;
120 # FIXME: no such field "dewey"
121 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
125 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
126 # This is not the LC Classification. It's the Control Number.
127 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
130 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
133 # while (my ($value) =$req->fetchrow) {
134 # $hlghtlccn = !($hasdewey);
135 # $haslccn =1 if (($value>2) and (! $haslccn));
136 # $count++ if (($value) and (! $haslccn));
137 # push @select, $value;
140 # values => \@select,
143 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
144 # FIXME: Hardcoding to 5 chars on itemcallnum.
149 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
151 my $CGIitemtype = $req->fetchall_arrayref({});
153 my $authvals = GetKohaAuthorisedValues("items.ccode");
155 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
156 push @authvals, { code => $_, description => $authvals->{$_} };
159 my $locations = GetKohaAuthorisedValues("items.location");
161 foreach (sort keys %$locations) {
162 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
165 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
167 $template->param(hasdewey=>$hasdewey,
170 CGIItemType => $CGIitemtype,
171 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
172 locationloop => \@locations,
173 authvals => \@authvals,
174 CGIextChoice => \@mime,
175 CGIsepChoice => GetDelimiterChoices,
176 item_itype => $item_itype
180 output_html_with_http_headers $input, $cookie, $template->output;
186 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
194 my $barcodelike = @$filters[13];
195 my $barcodefilter = @$filters[14];
199 my $dbh = C4::Context->dbh;
201 # if barcodefilter is empty set as %
203 # Check if barcodefilter is "like" or "not like"
208 $barcodefilter =~ s/\*/%/g;
215 for (my $i=0;$i<=12;$i++) {
217 if ( @$filters[$i] ) {
218 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
219 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
221 $cell{filter} .= @$filters[$i];
223 ($i== 0) ? "Dewey Classification From" :
224 ($i== 1) ? "Dewey Classification To" :
225 ($i== 2) ? "Lccn Classification From" :
226 ($i== 3) ? "Lccn Classification To" :
227 ($i== 4) ? "Item CallNumber From" :
228 ($i== 5) ? "Item CallNumber To" :
229 ($i== 6) ? "Item type" :
230 ($i== 7) ? "Publisher" :
231 ($i== 8) ? "Publication year From" :
232 ($i== 9) ? "Publication year To" :
233 ($i==10) ? "Library :" :
234 ($i==11) ? "Shelving Location :" :
235 ($i==12) ? "Collection Code :" : '';
236 push @loopfilter, \%cell;
240 # warn map {"filtres $_\n"} @filters[0..3];
243 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
244 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
245 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
246 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
247 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
248 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
249 if (C4::Context->preference('item-level_itypes')) {
250 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
252 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
254 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
255 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
256 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
257 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
258 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
259 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
262 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
263 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
264 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
265 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
266 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
267 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
268 if (C4::Context->preference('item-level_itypes')) {
269 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
271 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
273 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
274 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
275 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
276 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
277 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
278 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
282 if (($line =~/dewey/) and ($deweydigits)) {
283 $linefield .="left($line,$deweydigits)";
284 } elsif (($line=~/lccn/) and ($lccndigits)) {
285 $linefield .="left($line,$lccndigits)";
286 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
287 $linefield .="left($line,$cotedigits)";
292 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
293 INNER JOIN items USING (biblioitemnumber)
294 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
295 WHERE $line IS NOT NULL ";
296 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
299 $strsth .= " AND $line >= ? " ;
300 $strsth .= " AND $line <= ? " ;
301 } elsif ($linefilter[0]) {
302 $linefilter[0] =~ s/\*/%/g;
303 $strsth .= " AND $line LIKE ? " ;
306 $strsth .=" ORDER BY $linefield";
307 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
309 my $sth = $dbh->prepare( $strsth );
310 if (( @linefilter ) and ($linefilter[1])){
311 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
312 } elsif ($barcodefilter,$linefilter[0]) {
313 $sth->execute($barcodefilter,$linefilter[0]);
314 } elsif ($barcodefilter) {
315 $sth->execute($barcodefilter);
319 while ( my ($celvalue) = $sth->fetchrow) {
322 $cell{rowtitle} = $celvalue;
324 # $cell{rowtitle} = "";
327 push @loopline, \%cell;
332 if (($column =~/dewey/) and ($deweydigits)) {
333 $colfield = "left($column,$deweydigits)";
334 }elsif (($column=~/lccn/) and ($lccndigits)) {
335 $colfield = "left($column,$lccndigits)";
336 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
337 $colfield = "left($column,$cotedigits)";
343 SELECT distinctrow $colfield
346 USING (biblioitemnumber)
348 ON (biblioitems.biblionumber = biblio.biblionumber)
349 WHERE $column IS NOT NULL ";
350 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
352 if (( @colfilter ) and ($colfilter[1])) {
353 $strsth2 .= " AND $column> ? AND $column< ?";
354 }elsif ($colfilter[0]){
355 $colfilter[0] =~ s/\*/%/g;
356 $strsth2 .= " AND $column LIKE ? ";
358 $strsth2 .= " ORDER BY $colfield";
359 $debug and print STDERR "SQL: $strsth2";
360 my $sth2 = $dbh->prepare( $strsth2 );
361 if ((@colfilter) and ($colfilter[1])) {
362 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
363 } elsif ($colfilter[0]){
364 $sth2->execute($barcodefilter,$colfilter[0]);
365 } elsif ($barcodefilter){
366 $sth2->execute($barcodefilter);
370 while (my ($celvalue) = $sth2->fetchrow) {
374 $cell{coltitle} = $celvalue;
376 # $cell{coltitle} = "";
379 push @loopcol, \%cell;
386 #Initialization of cell values.....
389 foreach my $row ( @loopline ) {
390 foreach my $col ( @loopcol ) {
391 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
392 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
394 $table{$row->{rowtitle}}->{totalrow}=0;
397 # preparing calculation
399 SELECT $linefield, $colfield, count(*)
401 INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
402 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
404 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
407 @$filters[0]=~ s/\*/%/g;
408 $strcalc .= " AND dewey >" . @$filters[0];
411 @$filters[1]=~ s/\*/%/g ;
412 $strcalc .= " AND dewey <" . @$filters[1];
415 @$filters[2]=~ s/\*/%/g ;
416 $strcalc .= " AND lccn >" . @$filters[2];
419 @$filters[3]=~ s/\*/%/g;
420 $strcalc .= " AND lccn <" . @$filters[3];
423 @$filters[4]=~ s/\*/%/g ;
424 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
428 @$filters[5]=~ s/\*/%/g;
429 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
433 @$filters[6]=~ s/\*/%/g;
434 $strcalc .= " AND " .
435 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
436 . " LIKE '" . @$filters[6] ."'";
440 @$filters[7]=~ s/\*/%/g;
441 @$filters[7].="%" unless @$filters[7]=~/%/;
442 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
445 @$filters[8]=~ s/\*/%/g;
446 $strcalc .= " AND " .
447 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
448 . ">" . @$filters[8];
451 @$filters[9]=~ s/\*/%/g;
452 $strcalc .= " AND " .
453 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
454 . "<" . @$filters[9];
457 @$filters[10]=~ s/\*/%/g;
458 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
461 @$filters[11]=~ s/\*/%/g;
462 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
465 @$filters[12]=~ s/\*/%/g;
466 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
469 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
470 $debug and warn "SQL: $strcalc";
471 my $dbcalc = $dbh->prepare($strcalc);
473 $dbcalc->execute($barcodefilter);
477 # warn "filling table";
479 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
480 # warn "filling table $row / $col / $value ";
481 $emptycol = 1 if (!defined($col));
482 $col = "zzEMPTY" if (!defined($col));
483 $row = "zzEMPTY" if (!defined($row));
485 $table{$row}->{$col}+=$value;
486 $table{$row}->{totalrow}+=$value;
487 $grantotal += $value;
490 # my %cell = {rowtitle => 'zzROWEMPTY'};
491 # push @loopline,\%cell;
494 # %cell = {coltitle => "zzEMPTY"};
495 push @loopcol,{coltitle => "NULL"} if ($emptycol);
497 foreach my $row ( sort keys %table ) {
499 #@loopcol ensures the order for columns is common with column titles
500 # and the number matches the number of columns
501 foreach my $col ( @loopcol ) {
502 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
503 push @loopcell, {value => $value } ;
505 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
506 'loopcell' => \@loopcell,
507 'hilighted' => ($hilighted *= -1 > 0),
508 'totalrow' => $table{$row}->{totalrow}
512 # warn "footer processing";
513 foreach my $col ( @loopcol ) {
515 foreach my $row ( @looprow ) {
516 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
517 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
519 # warn "summ for column ".$col->{coltitle}." = ".$total;
520 push @loopfooter, {'totalcol' => $total};
524 # the header of the table
525 $globalline{loopfilter}=\@loopfilter;
526 # the core of the table
527 $globalline{looprow} = \@looprow;
528 $globalline{loopcol} = \@loopcol;
529 # # the foot (totals by borrower type)
530 $globalline{loopfooter} = \@loopfooter;
531 $globalline{total}= $grantotal;
532 $globalline{line} = $line;
533 $globalline{column} = $column;
534 push @mainloop,\%globalline;