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 my $itemtypes = GetItemTypes( style => 'array' );
151 my $authvals = GetKohaAuthorisedValues("items.ccode");
153 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
154 push @authvals, { code => $_, description => $authvals->{$_} };
157 my $locations = GetKohaAuthorisedValues("items.location");
159 foreach (sort keys %$locations) {
160 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
163 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
165 $template->param(hasdewey=>$hasdewey,
168 itemtypes => $itemtypes,
169 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
170 locationloop => \@locations,
171 authvals => \@authvals,
172 CGIextChoice => \@mime,
173 CGIsepChoice => GetDelimiterChoices,
174 item_itype => $item_itype
178 output_html_with_http_headers $input, $cookie, $template->output;
184 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
192 my $barcodelike = @$filters[13];
193 my $barcodefilter = @$filters[14];
197 my $dbh = C4::Context->dbh;
199 # if barcodefilter is empty set as %
201 # Check if barcodefilter is "like" or "not like"
206 $barcodefilter =~ s/\*/%/g;
213 for (my $i=0;$i<=12;$i++) {
215 if ( @$filters[$i] ) {
216 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
217 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
219 $cell{filter} .= @$filters[$i];
221 ($i== 0) ? "Dewey Classification From" :
222 ($i== 1) ? "Dewey Classification To" :
223 ($i== 2) ? "Lccn Classification From" :
224 ($i== 3) ? "Lccn Classification To" :
225 ($i== 4) ? "Item CallNumber From" :
226 ($i== 5) ? "Item CallNumber To" :
227 ($i== 6) ? "Item type" :
228 ($i== 7) ? "Publisher" :
229 ($i== 8) ? "Publication year From" :
230 ($i== 9) ? "Publication year To" :
231 ($i==10) ? "Library :" :
232 ($i==11) ? "Shelving Location :" :
233 ($i==12) ? "Collection Code :" : '';
234 push @loopfilter, \%cell;
238 # warn map {"filtres $_\n"} @filters[0..3];
241 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
242 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
243 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
244 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
245 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
246 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
247 if (C4::Context->preference('item-level_itypes')) {
248 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
250 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
252 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
253 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
254 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
255 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
256 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
257 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
260 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
261 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
262 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
263 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
264 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
265 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
266 if (C4::Context->preference('item-level_itypes')) {
267 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
269 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
271 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
272 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
273 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
274 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
275 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
276 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
280 if (($line =~/dewey/) and ($deweydigits)) {
281 $linefield .="left($line,$deweydigits)";
282 } elsif (($line=~/lccn/) and ($lccndigits)) {
283 $linefield .="left($line,$lccndigits)";
284 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
285 $linefield .="left($line,$cotedigits)";
290 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
291 INNER JOIN items USING (biblioitemnumber)
292 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
293 WHERE $line IS NOT NULL ";
294 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
297 $strsth .= " AND $line >= ? " ;
298 $strsth .= " AND $line <= ? " ;
299 } elsif ($linefilter[0]) {
300 $linefilter[0] =~ s/\*/%/g;
301 $strsth .= " AND $line LIKE ? " ;
304 $strsth .=" ORDER BY $linefield";
305 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
307 my $sth = $dbh->prepare( $strsth );
308 if (( @linefilter ) and ($linefilter[1])){
309 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
310 } elsif ($barcodefilter,$linefilter[0]) {
311 $sth->execute($barcodefilter,$linefilter[0]);
312 } elsif ($barcodefilter) {
313 $sth->execute($barcodefilter);
317 while ( my ($celvalue) = $sth->fetchrow) {
320 $cell{rowtitle} = $celvalue;
322 # $cell{rowtitle} = "";
325 push @loopline, \%cell;
330 if (($column =~/dewey/) and ($deweydigits)) {
331 $colfield = "left($column,$deweydigits)";
332 }elsif (($column=~/lccn/) and ($lccndigits)) {
333 $colfield = "left($column,$lccndigits)";
334 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
335 $colfield = "left($column,$cotedigits)";
341 SELECT distinctrow $colfield
344 USING (biblioitemnumber)
346 ON (biblioitems.biblionumber = biblio.biblionumber)
347 WHERE $column IS NOT NULL ";
348 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
350 if (( @colfilter ) and ($colfilter[1])) {
351 $strsth2 .= " AND $column> ? AND $column< ?";
352 }elsif ($colfilter[0]){
353 $colfilter[0] =~ s/\*/%/g;
354 $strsth2 .= " AND $column LIKE ? ";
356 $strsth2 .= " ORDER BY $colfield";
357 $debug and print STDERR "SQL: $strsth2";
358 my $sth2 = $dbh->prepare( $strsth2 );
359 if ((@colfilter) and ($colfilter[1])) {
360 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
361 } elsif ($colfilter[0]){
362 $sth2->execute($barcodefilter,$colfilter[0]);
363 } elsif ($barcodefilter){
364 $sth2->execute($barcodefilter);
368 while (my ($celvalue) = $sth2->fetchrow) {
372 $cell{coltitle} = $celvalue;
374 # $cell{coltitle} = "";
377 push @loopcol, \%cell;
384 #Initialization of cell values.....
387 foreach my $row ( @loopline ) {
388 foreach my $col ( @loopcol ) {
389 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
390 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
392 $table{$row->{rowtitle}}->{totalrow}=0;
395 # preparing calculation
397 SELECT $linefield, $colfield, count(*)
399 INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
400 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
402 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
405 @$filters[0]=~ s/\*/%/g;
406 $strcalc .= " AND dewey >" . @$filters[0];
409 @$filters[1]=~ s/\*/%/g ;
410 $strcalc .= " AND dewey <" . @$filters[1];
413 @$filters[2]=~ s/\*/%/g ;
414 $strcalc .= " AND lccn >" . @$filters[2];
417 @$filters[3]=~ s/\*/%/g;
418 $strcalc .= " AND lccn <" . @$filters[3];
421 @$filters[4]=~ s/\*/%/g ;
422 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
426 @$filters[5]=~ s/\*/%/g;
427 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
431 @$filters[6]=~ s/\*/%/g;
432 $strcalc .= " AND " .
433 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
434 . " LIKE '" . @$filters[6] ."'";
438 @$filters[7]=~ s/\*/%/g;
439 @$filters[7].="%" unless @$filters[7]=~/%/;
440 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
443 @$filters[8]=~ s/\*/%/g;
444 $strcalc .= " AND " .
445 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
446 . ">" . @$filters[8];
449 @$filters[9]=~ s/\*/%/g;
450 $strcalc .= " AND " .
451 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
452 . "<" . @$filters[9];
455 @$filters[10]=~ s/\*/%/g;
456 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
459 @$filters[11]=~ s/\*/%/g;
460 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
463 @$filters[12]=~ s/\*/%/g;
464 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
467 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
468 $debug and warn "SQL: $strcalc";
469 my $dbcalc = $dbh->prepare($strcalc);
471 $dbcalc->execute($barcodefilter);
475 # warn "filling table";
477 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
478 # warn "filling table $row / $col / $value ";
479 $emptycol = 1 if (!defined($col));
480 $col = "zzEMPTY" if (!defined($col));
481 $row = "zzEMPTY" if (!defined($row));
483 $table{$row}->{$col}+=$value;
484 $table{$row}->{totalrow}+=$value;
485 $grantotal += $value;
488 # my %cell = {rowtitle => 'zzROWEMPTY'};
489 # push @loopline,\%cell;
492 # %cell = {coltitle => "zzEMPTY"};
493 push @loopcol,{coltitle => "NULL"} if ($emptycol);
495 foreach my $row ( sort keys %table ) {
497 #@loopcol ensures the order for columns is common with column titles
498 # and the number matches the number of columns
499 foreach my $col ( @loopcol ) {
500 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
501 push @loopcell, {value => $value } ;
503 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
504 'loopcell' => \@loopcell,
505 'hilighted' => ($hilighted *= -1 > 0),
506 'totalrow' => $table{$row}->{totalrow}
510 # warn "footer processing";
511 foreach my $col ( @loopcol ) {
513 foreach my $row ( @looprow ) {
514 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
515 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
517 # warn "summ for column ".$col->{coltitle}." = ".$total;
518 push @loopfooter, {'totalcol' => $total};
522 # the header of the table
523 $globalline{loopfilter}=\@loopfilter;
524 # the core of the table
525 $globalline{looprow} = \@looprow;
526 $globalline{loopcol} = \@loopcol;
527 # # the foot (totals by borrower type)
528 $globalline{loopfooter} = \@loopfooter;
529 $globalline{total}= $grantotal;
530 $globalline{line} = $line;
531 $globalline{column} = $column;
532 push @mainloop,\%globalline;