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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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";
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user
({template_name
=> $fullreportname,
68 flagsrequired
=> {reports
=> '*'},
71 $template->param(do_it
=> $do_it);
73 my $results = calculate
($line, $column, $deweydigits, $lccndigits, $cotedigits, \
@filters);
74 if ($output eq "screen"){
75 $template->param(mainloop
=> $results);
76 output_html_with_http_headers
$input, $cookie, $template->output;
79 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
81 -attachment
=>"$basename.csv",
82 -name
=>"$basename.csv" );
83 my $cols = @
$results[0]->{loopcol
};
84 my $lines = @
$results[0]->{looprow
};
85 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
86 foreach my $col ( @
$cols ) {
87 print $col->{coltitle
}.$sep;
90 foreach my $line ( @
$lines ) {
91 my $x = $line->{loopcell
};
92 print $line->{rowtitle
}.$sep;
93 foreach my $cell (@
$x) {
94 print $cell->{value
}.$sep;
96 print $line->{totalrow
};
100 $cols = @
$results[0]->{loopfooter
};
101 foreach my $col ( @
$cols ) {
102 print $sep.$col->{totalcol
};
104 print $sep.@
$results[0]->{total
};
108 my $dbh = C4
::Context
->dbh;
114 # FIXME: no such field "dewey"
115 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
119 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
120 # This is not the LC Classification. It's the Control Number.
121 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
124 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
127 # while (my ($value) =$req->fetchrow) {
128 # $hlghtlccn = !($hasdewey);
129 # $haslccn =1 if (($value>2) and (! $haslccn));
130 # $count++ if (($value) and (! $haslccn));
131 # push @select, $value;
134 # values => \@select,
137 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
138 # FIXME: Hardcoding to 5 chars on itemcallnum.
143 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
145 my $CGIitemtype = $req->fetchall_arrayref({});
147 my $authvals = GetKohaAuthorisedValues
("items.ccode");
149 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
150 push @authvals, { code
=> $_, description
=> $authvals->{$_} };
153 my $locations = GetKohaAuthorisedValues
("items.location");
155 foreach (sort keys %$locations) {
156 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
159 my @mime = ( map { +{type
=>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
161 $template->param(hasdewey
=>$hasdewey,
164 CGIItemType
=> $CGIitemtype,
165 CGIBranch
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
166 locationloop
=> \
@locations,
167 authvals
=> \
@authvals,
168 CGIextChoice
=> \
@mime,
169 CGIsepChoice
=> GetDelimiterChoices
,
170 item_itype
=> $item_itype
174 output_html_with_http_headers
$input, $cookie, $template->output;
180 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
188 my $barcodelike = @
$filters[13];
189 my $barcodefilter = @
$filters[14];
193 my $dbh = C4
::Context
->dbh;
195 # if barcodefilter is empty set as %
197 # Check if barcodefilter is "like" or "not like"
202 $barcodefilter =~ s/\*/%/g;
209 for (my $i=0;$i<=12;$i++) {
211 if ( @
$filters[$i] ) {
212 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@
$filters[$i-1])) {
213 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
215 $cell{filter
} .= @
$filters[$i];
217 ($i== 0) ?
"Dewey Classification From" :
218 ($i== 1) ?
"Dewey Classification To" :
219 ($i== 2) ?
"Lccn Classification From" :
220 ($i== 3) ?
"Lccn Classification To" :
221 ($i== 4) ?
"Item CallNumber From" :
222 ($i== 5) ?
"Item CallNumber To" :
223 ($i== 6) ?
"Item type" :
224 ($i== 7) ?
"Publisher" :
225 ($i== 8) ?
"Publication year From" :
226 ($i== 9) ?
"Publication year To" :
227 ($i==10) ?
"Library :" :
228 ($i==11) ?
"Shelving Location :" :
229 ($i==12) ?
"Collection Code :" : '';
230 push @loopfilter, \
%cell;
234 # warn map {"filtres $_\n"} @filters[0..3];
237 $linefilter[0] = @
$filters[0] if ($line =~ /dewey/ ) ;
238 $linefilter[1] = @
$filters[1] if ($line =~ /dewey/ ) ;
239 $linefilter[0] = @
$filters[2] if ($line =~ /lccn/ ) ;
240 $linefilter[1] = @
$filters[3] if ($line =~ /lccn/ ) ;
241 $linefilter[0] = @
$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
242 $linefilter[1] = @
$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
243 if (C4
::Context
->preference('item-level_itypes')) {
244 $linefilter[0] = @
$filters[6] if ($line =~ /items\.itype/ ) ;
246 $linefilter[0] = @
$filters[6] if ($line =~ /itemtype/ ) ;
248 $linefilter[0] = @
$filters[7] if ($line =~ /publishercode/ ) ;
249 $linefilter[0] = @
$filters[8] if ($line =~ /publicationyear/ ) ;
250 $linefilter[1] = @
$filters[9] if ($line =~ /publicationyear/ ) ;
251 $linefilter[0] = @
$filters[10] if ($line =~ /items\.homebranch/ ) ;
252 $linefilter[0] = @
$filters[11] if ($line =~ /items\.location/ ) ;
253 $linefilter[0] = @
$filters[12] if ($line =~ /items\.ccode/ ) ;
256 $colfilter[0] = @
$filters[0] if ($column =~ /dewey/ ) ;
257 $colfilter[1] = @
$filters[1] if ($column =~ /dewey/ ) ;
258 $colfilter[0] = @
$filters[2] if ($column =~ /lccn/ ) ;
259 $colfilter[1] = @
$filters[3] if ($column =~ /lccn/ ) ;
260 $colfilter[0] = @
$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
261 $colfilter[1] = @
$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
262 if (C4
::Context
->preference('item-level_itypes')) {
263 $colfilter[0] = @
$filters[6] if ($column =~ /items\.itype/ ) ;
265 $colfilter[0] = @
$filters[6] if ($column =~ /itemtype/ ) ;
267 $colfilter[0] = @
$filters[7] if ($column =~ /publishercode/ ) ;
268 $colfilter[0] = @
$filters[8] if ($column =~ /publicationyear/ ) ;
269 $colfilter[1] = @
$filters[9] if ($column =~ /publicationyear/ ) ;
270 $colfilter[0] = @
$filters[10] if ($column =~ /items\.homebranch/ ) ;
271 $colfilter[0] = @
$filters[11] if ($column =~ /items\.location/ ) ;
272 $colfilter[0] = @
$filters[12] if ($column =~ /items\.ccode/ ) ;
276 if (($line =~/dewey/) and ($deweydigits)) {
277 $linefield .="left($line,$deweydigits)";
278 } elsif (($line=~/lccn/) and ($lccndigits)) {
279 $linefield .="left($line,$lccndigits)";
280 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
281 $linefield .="left($line,$cotedigits)";
286 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
287 INNER JOIN items USING (biblioitemnumber)
288 WHERE $line IS NOT NULL ";
289 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
292 $strsth .= " AND $line >= ? " ;
293 $strsth .= " AND $line <= ? " ;
294 } elsif ($linefilter[0]) {
295 $linefilter[0] =~ s/\*/%/g;
296 $strsth .= " AND $line LIKE ? " ;
299 $strsth .=" ORDER BY $linefield";
300 $debug and print STDERR
"catalogue_stats SQL: $strsth\n";
302 my $sth = $dbh->prepare( $strsth );
303 if (( @linefilter ) and ($linefilter[1])){
304 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
305 } elsif ($barcodefilter,$linefilter[0]) {
306 $sth->execute($barcodefilter,$linefilter[0]);
307 } elsif ($barcodefilter) {
308 $sth->execute($barcodefilter);
312 while ( my ($celvalue) = $sth->fetchrow) {
315 $cell{rowtitle
} = $celvalue;
317 # $cell{rowtitle} = "";
320 push @loopline, \
%cell;
325 if (($column =~/dewey/) and ($deweydigits)) {
326 $colfield = "left($column,$deweydigits)";
327 }elsif (($column=~/lccn/) and ($lccndigits)) {
328 $colfield = "left($column,$lccndigits)";
329 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
330 $colfield = "left($column,$cotedigits)";
336 SELECT distinctrow $colfield
339 USING (biblioitemnumber)
340 WHERE $column IS NOT NULL ";
341 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
343 if (( @colfilter ) and ($colfilter[1])) {
344 $strsth2 .= " AND $column> ? AND $column< ?";
345 }elsif ($colfilter[0]){
346 $colfilter[0] =~ s/\*/%/g;
347 $strsth2 .= " AND $column LIKE ? ";
349 $strsth2 .= " ORDER BY $colfield";
350 $debug and print STDERR
"SQL: $strsth2";
351 my $sth2 = $dbh->prepare( $strsth2 );
352 if ((@colfilter) and ($colfilter[1])) {
353 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
354 } elsif ($colfilter[0]){
355 $sth2->execute($barcodefilter,$colfilter[0]);
356 } elsif ($barcodefilter){
357 $sth2->execute($barcodefilter);
361 while (my ($celvalue) = $sth2->fetchrow) {
365 $cell{coltitle
} = $celvalue;
367 # $cell{coltitle} = "";
370 push @loopcol, \
%cell;
377 #Initialization of cell values.....
380 foreach my $row ( @loopline ) {
381 foreach my $col ( @loopcol ) {
382 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
383 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
385 $table{$row->{rowtitle
}}->{totalrow
}=0;
388 # preparing calculation
389 my $strcalc = "SELECT $linefield, $colfield, count(*) FROM biblioitems INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 ";
390 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
393 @
$filters[0]=~ s/\*/%/g;
394 $strcalc .= " AND dewey >" . @
$filters[0];
397 @
$filters[1]=~ s/\*/%/g ;
398 $strcalc .= " AND dewey <" . @
$filters[1];
401 @
$filters[2]=~ s/\*/%/g ;
402 $strcalc .= " AND lccn >" . @
$filters[2];
405 @
$filters[3]=~ s/\*/%/g;
406 $strcalc .= " AND lccn <" . @
$filters[3];
409 @
$filters[4]=~ s/\*/%/g ;
410 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@
$filters[4]);
414 @
$filters[5]=~ s/\*/%/g;
415 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@
$filters[5]);
419 @
$filters[6]=~ s/\*/%/g;
420 $strcalc .= " AND " .
421 (C4
::Context
->preference('item-level_itypes') ?
'items.itype' : 'biblioitems.itemtype')
422 . " LIKE '" . @
$filters[6] ."'";
426 @
$filters[7]=~ s/\*/%/g;
427 @
$filters[7].="%" unless @
$filters[7]=~/%/;
428 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @
$filters[7] ."\"";
431 @
$filters[8]=~ s/\*/%/g;
432 $strcalc .= " AND publicationyear >" . @
$filters[8];
435 @
$filters[9]=~ s/\*/%/g;
436 $strcalc .= " AND publicationyear <" . @
$filters[9];
439 @
$filters[10]=~ s/\*/%/g;
440 $strcalc .= " AND items.homebranch LIKE '" . @
$filters[10] ."'";
443 @
$filters[11]=~ s/\*/%/g;
444 $strcalc .= " AND items.location LIKE '" . @
$filters[11] ."'";
447 @
$filters[12]=~ s/\*/%/g;
448 $strcalc .= " AND items.ccode LIKE '" . @
$filters[12] ."'";
451 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
452 $debug and warn "SQL: $strcalc";
453 my $dbcalc = $dbh->prepare($strcalc);
455 $dbcalc->execute($barcodefilter);
459 # warn "filling table";
462 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
463 # warn "filling table $row / $col / $value ";
464 $emptycol = 1 if (!defined($col));
465 $col = "zzEMPTY" if (!defined($col));
466 $row = "zzEMPTY" if (!defined($row));
468 $table{$row}->{$col}+=$value;
469 $table{$row}->{totalrow
}+=$value;
470 $grantotal += $value;
473 # my %cell = {rowtitle => 'zzROWEMPTY'};
474 # push @loopline,\%cell;
477 # %cell = {coltitle => "zzEMPTY"};
478 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
480 foreach my $row ( sort keys %table ) {
482 #@loopcol ensures the order for columns is common with column titles
483 # and the number matches the number of columns
484 foreach my $col ( @loopcol ) {
485 my $value =$table{$row}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
486 push @loopcell, {value
=> $value } ;
488 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
489 'loopcell' => \
@loopcell,
490 'hilighted' => ($hilighted *= -1 > 0),
491 'totalrow' => $table{$row}->{totalrow
}
495 # warn "footer processing";
496 foreach my $col ( @loopcol ) {
498 foreach my $row ( @looprow ) {
499 $total += $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
500 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
502 # warn "summ for column ".$col->{coltitle}." = ".$total;
503 push @loopfooter, {'totalcol' => $total};
507 # the header of the table
508 $globalline{loopfilter
}=\
@loopfilter;
509 # the core of the table
510 $globalline{looprow
} = \
@looprow;
511 $globalline{loopcol
} = \
@loopcol;
512 # # the foot (totals by borrower type)
513 $globalline{loopfooter
} = \
@loopfooter;
514 $globalline{total
}= $grantotal;
515 $globalline{line
} = $line;
516 $globalline{column
} = $column;
517 push @mainloop,\
%globalline;