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.tmpl";
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 my $mime = $input->param("MIME");
55 our $sep = $input->param("sep");
56 $sep = "\t" if ($sep eq 'tabulation');
58 if(C4::Context->preference('item-level_itypes')) {
59 $item_itype = "items\.itype"
61 $item_itype = "itemtype";
64 my ($template, $borrowernumber, $cookie)
65 = get_template_and_user({template_name => $fullreportname,
69 flagsrequired => {reports => '*'},
72 $template->param(do_it => $do_it);
74 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
75 if ($output eq "screen"){
76 $template->param(mainloop => $results);
77 output_html_with_http_headers $input, $cookie, $template->output;
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
82 -attachment=>"$basename.csv",
83 -name=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
109 my $dbh = C4::Context->dbh;
115 # FIXME: no such field "dewey"
116 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
120 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
121 # This is not the LC Classification. It's the Control Number.
122 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
125 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
128 # while (my ($value) =$req->fetchrow) {
129 # $hlghtlccn = !($hasdewey);
130 # $haslccn =1 if (($value>2) and (! $haslccn));
131 # $count++ if (($value) and (! $haslccn));
132 # push @select, $value;
134 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
136 # -values => \@select,
140 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
141 # FIXME: Hardcoding to 5 chars on itemcallnum.
146 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
148 my $CGIitemtype = $req->fetchall_arrayref({});
150 my $authvals = GetKohaAuthorisedValues("items.ccode");
152 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
153 push @authvals, { code => $_, description => $authvals->{$_} };
156 my $locations = GetKohaAuthorisedValues("items.location");
158 foreach (sort keys %$locations) {
159 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
162 my @mime = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
164 $template->param(hasdewey=>$hasdewey,
167 CGIItemType => $CGIitemtype,
168 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
169 locationloop => \@locations,
170 authvals => \@authvals,
171 CGIextChoice => \@mime,
172 CGIsepChoice => GetDelimiterChoices,
173 item_itype => $item_itype
177 output_html_with_http_headers $input, $cookie, $template->output;
183 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
191 my $barcodelike = @$filters[13];
192 my $barcodefilter = @$filters[14];
196 my $dbh = C4::Context->dbh;
198 # if barcodefilter is empty set as %
200 # Check if barcodefilter is "like" or "not like"
205 $barcodefilter =~ s/\*/%/g;
212 for (my $i=0;$i<=12;$i++) {
214 if ( @$filters[$i] ) {
215 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
216 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
218 $cell{filter} .= @$filters[$i];
220 ($i== 0) ? "Dewey Classification From" :
221 ($i== 1) ? "Dewey Classification To" :
222 ($i== 2) ? "Lccn Classification From" :
223 ($i== 3) ? "Lccn Classification To" :
224 ($i== 4) ? "Item CallNumber From" :
225 ($i== 5) ? "Item CallNumber To" :
226 ($i== 6) ? "Item type" :
227 ($i== 7) ? "Publisher" :
228 ($i== 8) ? "Publication year From" :
229 ($i== 9) ? "Publication year To" :
230 ($i==10) ? "Library :" :
231 ($i==11) ? "Shelving Location :" :
232 ($i==12) ? "Collection Code :" : '';
233 push @loopfilter, \%cell;
237 # warn map {"filtres $_\n"} @filters[0..3];
240 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
241 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
242 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
243 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
244 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
245 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
246 if (C4::Context->preference('item-level_itypes')) {
247 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
249 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
251 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
252 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
253 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
254 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
255 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
256 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
259 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
260 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
261 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
262 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
263 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
264 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
265 if (C4::Context->preference('item-level_itypes')) {
266 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
268 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
270 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
271 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
272 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
273 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
274 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
275 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
279 if (($line =~/dewey/) and ($deweydigits)) {
280 $linefield .="left($line,$deweydigits)";
281 } elsif (($line=~/lccn/) and ($lccndigits)) {
282 $linefield .="left($line,$lccndigits)";
283 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
284 $linefield .="left($line,$cotedigits)";
289 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
290 INNER JOIN items USING (biblioitemnumber)
291 WHERE $line IS NOT NULL ";
292 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
295 $strsth .= " AND $line >= ? " ;
296 $strsth .= " AND $line <= ? " ;
297 } elsif ($linefilter[0]) {
298 $linefilter[0] =~ s/\*/%/g;
299 $strsth .= " AND $line LIKE ? " ;
302 $strsth .=" ORDER BY $linefield";
303 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
305 my $sth = $dbh->prepare( $strsth );
306 if (( @linefilter ) and ($linefilter[1])){
307 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
308 } elsif ($barcodefilter,$linefilter[0]) {
309 $sth->execute($barcodefilter,$linefilter[0]);
310 } elsif ($barcodefilter) {
311 $sth->execute($barcodefilter);
315 while ( my ($celvalue) = $sth->fetchrow) {
318 $cell{rowtitle} = $celvalue;
320 # $cell{rowtitle} = "";
323 push @loopline, \%cell;
328 if (($column =~/dewey/) and ($deweydigits)) {
329 $colfield = "left($column,$deweydigits)";
330 }elsif (($column=~/lccn/) and ($lccndigits)) {
331 $colfield = "left($column,$lccndigits)";
332 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
333 $colfield = "left($column,$cotedigits)";
339 SELECT distinctrow $colfield
342 USING (biblioitemnumber)
343 WHERE $column IS NOT NULL ";
344 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
346 if (( @colfilter ) and ($colfilter[1])) {
347 $strsth2 .= " AND $column> ? AND $column< ?";
348 }elsif ($colfilter[0]){
349 $colfilter[0] =~ s/\*/%/g;
350 $strsth2 .= " AND $column LIKE ? ";
352 $strsth2 .= " ORDER BY $colfield";
353 $debug and print STDERR "SQL: $strsth2";
354 my $sth2 = $dbh->prepare( $strsth2 );
355 if ((@colfilter) and ($colfilter[1])) {
356 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
357 } elsif ($colfilter[0]){
358 $sth2->execute($barcodefilter,$colfilter[0]);
359 } elsif ($barcodefilter){
360 $sth2->execute($barcodefilter);
364 while (my ($celvalue) = $sth2->fetchrow) {
368 $cell{coltitle} = $celvalue;
370 # $cell{coltitle} = "";
373 push @loopcol, \%cell;
380 #Initialization of cell values.....
383 foreach my $row ( @loopline ) {
384 foreach my $col ( @loopcol ) {
385 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
386 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
388 $table{$row->{rowtitle}}->{totalrow}=0;
391 # preparing calculation
392 my $strcalc = "SELECT $linefield, $colfield, count(*) FROM biblioitems INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 ";
393 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
396 @$filters[0]=~ s/\*/%/g;
397 $strcalc .= " AND dewey >" . @$filters[0];
400 @$filters[1]=~ s/\*/%/g ;
401 $strcalc .= " AND dewey <" . @$filters[1];
404 @$filters[2]=~ s/\*/%/g ;
405 $strcalc .= " AND lccn >" . @$filters[2];
408 @$filters[3]=~ s/\*/%/g;
409 $strcalc .= " AND lccn <" . @$filters[3];
412 @$filters[4]=~ s/\*/%/g ;
413 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
417 @$filters[5]=~ s/\*/%/g;
418 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
422 @$filters[6]=~ s/\*/%/g;
423 $strcalc .= " AND " .
424 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
425 . " LIKE '" . @$filters[6] ."'";
429 @$filters[7]=~ s/\*/%/g;
430 @$filters[7].="%" unless @$filters[7]=~/%/;
431 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
434 @$filters[8]=~ s/\*/%/g;
435 $strcalc .= " AND publicationyear >" . @$filters[8];
438 @$filters[9]=~ s/\*/%/g;
439 $strcalc .= " AND publicationyear <" . @$filters[9];
442 @$filters[10]=~ s/\*/%/g;
443 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
446 @$filters[11]=~ s/\*/%/g;
447 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
450 @$filters[12]=~ s/\*/%/g;
451 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
454 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
455 $debug and warn "SQL: $strcalc";
456 my $dbcalc = $dbh->prepare($strcalc);
458 $dbcalc->execute($barcodefilter);
462 # warn "filling table";
465 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
466 # warn "filling table $row / $col / $value ";
467 $emptycol = 1 if (!defined($col));
468 $col = "zzEMPTY" if (!defined($col));
469 $row = "zzEMPTY" if (!defined($row));
471 $table{$row}->{$col}+=$value;
472 $table{$row}->{totalrow}+=$value;
473 $grantotal += $value;
476 # my %cell = {rowtitle => 'zzROWEMPTY'};
477 # push @loopline,\%cell;
480 # %cell = {coltitle => "zzEMPTY"};
481 push @loopcol,{coltitle => "NULL"} if ($emptycol);
483 foreach my $row ( sort keys %table ) {
485 #@loopcol ensures the order for columns is common with column titles
486 # and the number matches the number of columns
487 foreach my $col ( @loopcol ) {
488 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
489 push @loopcell, {value => $value } ;
491 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
492 'loopcell' => \@loopcell,
493 'hilighted' => ($hilighted *= -1 > 0),
494 'totalrow' => $table{$row}->{totalrow}
498 # warn "footer processing";
499 foreach my $col ( @loopcol ) {
501 foreach my $row ( @looprow ) {
502 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
503 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
505 # warn "summ for column ".$col->{coltitle}." = ".$total;
506 push @loopfooter, {'totalcol' => $total};
510 # the header of the table
511 $globalline{loopfilter}=\@loopfilter;
512 # the core of the table
513 $globalline{looprow} = \@looprow;
514 $globalline{loopcol} = \@loopcol;
515 # # the foot (totals by borrower type)
516 $globalline{loopfooter} = \@loopfooter;
517 $globalline{total}= $grantotal;
518 $globalline{line} = $line;
519 $globalline{column} = $column;
520 push @mainloop,\%globalline;