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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use C4::Branch; # GetBranches
32 plugin that shows a stats on borrowers
42 my $fullreportname = "reports/catalogue_stats.tmpl";
43 my $do_it = $input->param('do_it');
44 my $line = $input->param("Line");
45 my $column = $input->param("Column");
46 my @filters = $input->param("Filter");
47 my $deweydigits = $input->param("deweydigits");
48 my $lccndigits = $input->param("lccndigits");
49 my $cotedigits = $input->param("cotedigits");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => {reports => 1},
63 $template->param(do_it => $do_it);
65 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
66 if ($output eq "screen"){
67 $template->param(mainloop => $results);
68 output_html_with_http_headers $input, $cookie, $template->output;
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
73 -attachment=>"$basename.csv",
74 -name=>"$basename.csv" );
75 my $cols = @$results[0]->{loopcol};
76 my $lines = @$results[0]->{looprow};
77 my $sep = C4::Context->preference("delimiter");
78 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
79 foreach my $col ( @$cols ) {
80 print $col->{coltitle}.$sep;
83 foreach my $line ( @$lines ) {
84 my $x = $line->{loopcell};
85 print $line->{rowtitle}.$sep;
86 foreach my $cell (@$x) {
87 print $cell->{value}.$sep;
89 print $line->{totalrow};
93 $cols = @$results[0]->{loopfooter};
94 foreach my $col ( @$cols ) {
95 print $sep.$col->{totalcol};
97 print $sep.@$results[0]->{total};
101 my $dbh = C4::Context->dbh;
107 # FIXME: no such field "dewey"
108 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
112 # while (my ($value) =$req->fetchrow) {
113 # if (($value>2) and (! $hasdewey)) {
117 # push @select, $value;
119 # my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
121 # -values => \@select,
125 $req = $dbh->prepare( "select count(lccn) from biblioitems ");
131 while (my ($value) =$req->fetchrow) {
132 $hlghtlccn = !($hasdewey);
133 $haslccn =1 if (($value>2) and (! $haslccn));
134 $count++ if (($value) and (! $haslccn));
135 # push @select, $value;
137 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
139 # -values => \@select,
143 $req = $dbh->prepare("select count(itemcallnumber) from items");
149 while (my ($value) =$req->fetchrow) {
150 $hascote =1 if (($value>2) and (! $hascote));
151 $count++ if (($value) and (! $hascote));
152 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
153 # push @select, $value;
155 # my $CGIcote=CGI::scrolling_list( -name => 'Filter',
157 # -values => \@select,
160 my $hglghtDT = ++$count % 2;
161 my $hglghtPub = ++$count % 2;
162 my $hglghtPY = ++$count % 2;
163 my $hglghtHB = ++$count % 2;
164 my $hglghtLOC = ++$count % 2;
165 # warn "highlightDT " .$hglghtDT;
166 # warn "highlightPub " .$hglghtPub;
167 # warn "highlightPY " .$hglghtPY;
168 # warn "highlightHB " .$hglghtHB;
169 # warn "highlightLOC " .$hglghtLOC;
172 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
174 my $CGIitemtype = $req->fetchall_arrayref({});
176 my $authvals = GetKohaAuthorisedValues("items.ccode");
178 foreach (keys %$authvals) {
179 push @authvals, { code => $_, description => $authvals->{$_} };
182 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
186 # while (my ($value) =$req->fetchrow) {
187 # push @select, $value;
189 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
191 # -values => \@select,
195 my $branches=GetBranches();
197 foreach (keys %$branches) {
198 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
199 my %row = (branchcode => $_,
200 selected => ($thisbranch eq $_ ? 1 : 0),
201 branchname => $branches->{$_}->{'branchname'},
203 push @branchloop, \%row;
206 $req = $dbh->prepare("select distinctrow location from items order by location");
208 my $locationloop = $req->fetchall_arrayref({});
209 my $locations = GetKohaAuthorisedValues("items.location");
211 foreach (sort keys %$locations) {
212 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
215 my @mime = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
216 my @delim = ( map { +{delim=>$_} } (split //,C4::Context->preference("delimiter")) );
218 $template->param(hasdewey=>$hasdewey,
219 # CGIFromDeweyClass => $CGIdewey,
220 # CGIToDeweyClass => $CGIdewey,
222 hlghtlccn => $hlghtlccn,
223 # CGIFromLoCClass => $CGIlccn,
224 # CGIToLoCClass => $CGIlccn,
226 hlghtcote => $hlghtcote,
227 hglghtDT => $hglghtDT,
228 hglghtPub => $hglghtPub,
229 hglghtPY => $hglghtPY,
230 hglghtHB => $hglghtHB,
231 hglghtLOC => $hglghtLOC,
232 # CGIFromCoteClass => $CGIcote,
233 # CGIToCoteClass => $CGIcote,
234 CGIItemType => $CGIitemtype,
235 # CGIFromPublicationYear => $CGIpublicationyear,
236 # CGIToPublicationYear => $CGIpublicationyear,
237 # CGIPublisher => $CGIpublisher,
238 CGIBranch => \@branchloop,
239 locationloop => \@locations,
240 authvals => \@authvals,
241 CGIextChoice => \@mime,
242 CGIsepChoice => \@delim,
246 output_html_with_http_headers $input, $cookie, $template->output;
252 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
261 my $dbh = C4::Context->dbh;
267 for (my $i=0;$i<=12;$i++) {
269 if ( @$filters[$i] ) {
270 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
271 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
273 $cell{filter} .= @$filters[$i];
275 ($i== 0) ? "Dewey Classification From" :
276 ($i== 1) ? "Dewey Classification To" :
277 ($i== 2) ? "Lccn Classification From" :
278 ($i== 3) ? "Lccn Classification To" :
279 ($i== 4) ? "Cote Classification From" :
280 ($i== 5) ? "Cote Classification To" :
281 ($i== 6) ? "Document type" :
282 ($i== 7) ? "Publisher" :
283 ($i== 8) ? "Publication year From" :
284 ($i== 9) ? "Publication year To" :
285 ($i==10) ? "Branch :" :
286 ($i==11) ? "Location :" :
287 ($i==12) ? "Catalog Code :" : '';
288 push @loopfilter, \%cell;
292 # warn map {"filtres $_\n"} @filters[0..3];
295 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
296 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
297 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
298 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
299 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
300 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
301 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
302 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
303 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
304 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
305 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
306 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
307 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
310 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
311 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
312 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
313 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
314 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
315 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
316 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
317 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
318 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
319 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
320 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
321 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
322 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
326 if (($line =~/dewey/) and ($deweydigits)) {
327 $linefield .="left($line,$deweydigits)";
328 } elsif (($line=~/lccn/) and ($lccndigits)) {
329 $linefield .="left($line,$lccndigits)";
330 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
331 $linefield .="left($line,$cotedigits)";
337 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
340 $strsth .= " and $line >= ? " ;
341 $strsth .= " and $line <= ? " ;
342 } elsif ($linefilter[0]) {
343 $linefilter[0] =~ s/\*/%/g;
344 $strsth .= " and $line LIKE ? " ;
347 $strsth .=" order by $linefield";
348 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
350 my $sth = $dbh->prepare( $strsth );
351 if (( @linefilter ) and ($linefilter[1])){
352 $sth->execute($linefilter[0],$linefilter[1]);
353 } elsif ($linefilter[0]) {
354 $sth->execute($linefilter[0]);
358 while ( my ($celvalue) = $sth->fetchrow) {
361 $cell{rowtitle} = $celvalue;
363 # $cell{rowtitle} = "";
366 push @loopline, \%cell;
371 if (($column =~/dewey/) and ($deweydigits)) {
372 $colfield = "left($column,$deweydigits)";
373 }elsif (($column=~/lccn/) and ($lccndigits)) {
374 $colfield = "left($column,$lccndigits)";
375 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
376 $colfield = "left($column,$cotedigits)";
382 SELECT distinctrow $colfield
385 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
386 WHERE $column IS NOT NULL ";
387 if (( @colfilter ) and ($colfilter[1])) {
388 $strsth2 .= " and $column> ? and $column< ?";
389 }elsif ($colfilter[0]){
390 $colfilter[0] =~ s/\*/%/g;
391 $strsth2 .= " and $column LIKE ? ";
393 $strsth2 .= " order by $colfield";
394 $debug and print STDERR "SQL: $strsth2";
395 my $sth2 = $dbh->prepare( $strsth2 );
396 if ((@colfilter) and ($colfilter[1])) {
397 $sth2->execute($colfilter[0],$colfilter[1]);
398 } elsif ($colfilter[0]){
399 $sth2->execute($colfilter[0]);
403 while (my ($celvalue) = $sth2->fetchrow) {
407 $cell{coltitle} = $celvalue;
409 # $cell{coltitle} = "";
412 push @loopcol, \%cell;
419 #Initialization of cell values.....
422 foreach my $row ( @loopline ) {
423 foreach my $col ( @loopcol ) {
424 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
425 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
427 $table{$row->{rowtitle}}->{totalrow}=0;
430 # preparing calculation
431 my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
433 @$filters[0]=~ s/\*/%/g;
434 $strcalc .= " AND dewey >" . @$filters[0];
437 @$filters[1]=~ s/\*/%/g ;
438 $strcalc .= " AND dewey <" . @$filters[1];
441 @$filters[2]=~ s/\*/%/g ;
442 $strcalc .= " AND lccn >" . @$filters[2];
445 @$filters[3]=~ s/\*/%/g;
446 $strcalc .= " AND lccn <" . @$filters[3];
449 @$filters[4]=~ s/\*/%/g ;
450 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
454 @$filters[5]=~ s/\*/%/g;
455 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
459 @$filters[6]=~ s/\*/%/g;
460 $strcalc .= " AND " .
461 (C4::Context::preference('Item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
462 . " LIKE '" . @$filters[6] ."'";
466 @$filters[7]=~ s/\*/%/g;
467 @$filters[7].="%" unless @$filters[7]=~/%/;
468 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
471 @$filters[8]=~ s/\*/%/g;
472 $strcalc .= " AND publicationyear >" . @$filters[8];
475 @$filters[9]=~ s/\*/%/g;
476 $strcalc .= " AND publicationyear <" . @$filters[9];
479 @$filters[10]=~ s/\*/%/g;
480 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
483 @$filters[11]=~ s/\*/%/g;
484 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
487 @$filters[12]=~ s/\*/%/g;
488 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
491 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
492 $debug and warn "SQL: $strcalc";
493 my $dbcalc = $dbh->prepare($strcalc);
495 # warn "filling table";
498 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
499 # warn "filling table $row / $col / $value ";
500 $emptycol = 1 if ($col eq undef);
501 $col = "zzEMPTY" if ($col eq undef);
502 $row = "zzEMPTY" if ($row eq undef);
504 $table{$row}->{$col}+=$value;
505 $table{$row}->{totalrow}+=$value;
506 $grantotal += $value;
509 # my %cell = {rowtitle => 'zzROWEMPTY'};
510 # push @loopline,\%cell;
513 # %cell = {coltitle => "zzEMPTY"};
514 push @loopcol,{coltitle => "NULL"} if ($emptycol);
516 foreach my $row ( sort keys %table ) {
518 #@loopcol ensures the order for columns is common with column titles
519 # and the number matches the number of columns
520 foreach my $col ( @loopcol ) {
521 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
522 push @loopcell, {value => $value } ;
524 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
525 'loopcell' => \@loopcell,
526 'hilighted' => ($hilighted *= -1 > 0),
527 'totalrow' => $table{$row}->{totalrow}
531 # warn "footer processing";
532 foreach my $col ( @loopcol ) {
534 foreach my $row ( @looprow ) {
535 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
536 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
538 # warn "summ for column ".$col->{coltitle}." = ".$total;
539 push @loopfooter, {'totalcol' => $total};
543 # the header of the table
544 $globalline{loopfilter}=\@loopfilter;
545 # the core of the table
546 $globalline{looprow} = \@looprow;
547 $globalline{loopcol} = \@loopcol;
548 # # the foot (totals by borrower type)
549 $globalline{loopfooter} = \@loopfooter;
550 $globalline{total}= $grantotal;
551 $globalline{line} = $line;
552 $globalline{column} = $column;
553 push @mainloop,\%globalline;