5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Branch; # GetBranches
33 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/catalogue_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 my $deweydigits = $input->param("deweydigits");
49 my $lccndigits = $input->param("lccndigits");
50 my $cotedigits = $input->param("cotedigits");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {reports => 1},
64 $template->param(do_it => $do_it);
66 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
67 if ($output eq "screen"){
68 $template->param(mainloop => $results);
69 output_html_with_http_headers $input, $cookie, $template->output;
72 print $input->header(-type => 'application/vnd.sun.xml.calc',
74 -attachment=>"$basename.csv",
75 -name=>"$basename.csv" );
76 my $cols = @$results[0]->{loopcol};
77 my $lines = @$results[0]->{looprow};
79 $sep =C4::Context->preference("delimiter");
80 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
81 foreach my $col ( @$cols ) {
82 print $col->{coltitle}.$sep;
85 foreach my $line ( @$lines ) {
86 my $x = $line->{loopcell};
87 print $line->{rowtitle}.$sep;
88 foreach my $cell (@$x) {
89 print $cell->{value}.$sep;
91 print $line->{totalrow};
95 $cols = @$results[0]->{loopfooter};
96 foreach my $col ( @$cols ) {
97 print $sep.$col->{totalcol};
99 print $sep.@$results[0]->{total};
103 my $dbh = C4::Context->dbh;
108 $req = $dbh->prepare("select count(dewey) from biblioitems ");
113 while (my ($value) =$req->fetchrow) {
114 $hasdewey =1 if (($value>2) and (! $hasdewey));
115 $count++ if (($value>2) and (! $hasdewey));
116 # push @select, $value;
118 # my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
120 # -values => \@select,
124 $req = $dbh->prepare( "select count(lccn) from biblioitems ");
130 while (my ($value) =$req->fetchrow) {
131 $hlghtlccn = !($hasdewey);
132 $haslccn =1 if (($value>2) and (! $haslccn));
133 $count++ if (($value) and (! $haslccn));
134 # push @select, $value;
136 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
138 # -values => \@select,
142 $req = $dbh->prepare("select count(itemcallnumber) from items");
148 while (my ($value) =$req->fetchrow) {
149 $hascote =1 if (($value>2) and (! $hascote));
150 $count++ if (($value) and (! $hascote));
151 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
152 # push @select, $value;
154 # my $CGIcote=CGI::scrolling_list( -name => 'Filter',
156 # -values => \@select,
160 my $hglghtDT =$count % 2;
161 # warn "highlightDT ".$hglghtDT;
163 my $hglghtPub =$count % 2;
164 # warn "highlightPub ".$hglghtPub;
166 my $hglghtPY =$count % 2;
167 # warn "highlightPY ".$hglghtPY;
169 my $hglghtHB =$count % 2;
170 # warn "highlightHB ".$hglghtHB;
172 my $hglghtLOC =$count % 2;
173 # warn "highlightLOC ".$hglghtLOC;
176 $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
180 while (my ($value) =$req->fetchrow) {
181 push @select, $value;
183 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
189 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
193 # while (my ($value) =$req->fetchrow) {
194 # push @select, $value;
196 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
198 # -values => \@select,
204 my $branches=GetBranches();
206 $select_branches{""} = "";
207 foreach my $branch (keys %$branches) {
208 push @select, $branch;
209 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
211 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
214 -labels => \%select_branches,
218 $req = $dbh->prepare("select distinctrow location from items order by location");
222 while (my ($value) =$req->fetchrow) {
223 push @select, $value;
225 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
231 my @mime = ( C4::Context->preference("MIME") );
232 foreach my $mime (@mime){
236 my $CGIextChoice=CGI::scrolling_list(
243 my @dels = ( C4::Context->preference("delimiter") );
244 my $CGIsepChoice=CGI::scrolling_list(
251 $template->param(hasdewey=>$hasdewey,
252 # CGIFromDeweyClass => $CGIdewey,
253 # CGIToDeweyClass => $CGIdewey,
255 hlghtlccn => $hlghtlccn,
256 # CGIFromLoCClass => $CGIlccn,
257 # CGIToLoCClass => $CGIlccn,
259 hlghtcote => $hlghtcote,
260 hglghtDT => $hglghtDT,
261 hglghtPub => $hglghtPub,
262 hglghtPY => $hglghtPY,
263 hglghtHB => $hglghtHB,
264 hglghtLOC => $hglghtLOC,
265 # CGIFromCoteClass => $CGIcote,
266 # CGIToCoteClass => $CGIcote,
267 CGIItemType => $CGIitemtype,
268 # CGIFromPublicationYear => $CGIpublicationyear,
269 # CGIToPublicationYear => $CGIpublicationyear,
270 # CGIPublisher => $CGIpublisher,
271 CGIBranch => $CGIbranch,
272 CGILocation => $CGIlocation,
273 CGIextChoice => $CGIextChoice,
274 CGIsepChoice => $CGIsepChoice
278 output_html_with_http_headers $input, $cookie, $template->output;
283 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
292 my $dbh = C4::Context->dbh;
298 for (my $i=0;$i<=11;$i++) {
300 if ( @$filters[$i] ) {
301 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
302 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
304 $cell{filter} .= @$filters[$i];
305 $cell{crit} .="Dewey Classification From" if ($i==0);
306 $cell{crit} .="Dewey Classification To" if ($i==1);
307 $cell{crit} .="Lccn Classification From" if ($i==2);
308 $cell{crit} .="Lccn Classification To" if ($i==3);
309 $cell{crit} .="Cote Classification From" if ($i==4);
310 $cell{crit} .="Cote Classification To" if ($i==5);
311 $cell{crit} .="Document type" if ($i==6);
312 $cell{crit} .="Publisher" if ($i==7);
313 $cell{crit} .="Publication year From" if ($i==8);
314 $cell{crit} .="Publication year To" if ($i==9);
315 $cell{crit} .="Branch :" if ($i==10);
316 $cell{crit} .="Location:" if ($i==11);
317 push @loopfilter, \%cell;
322 # warn "filtres ".@filters[0];
323 # warn "filtres ".@filters[1];
324 # warn "filtres ".@filters[2];
325 # warn "filtres ".@filters[3];
327 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
328 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
329 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
330 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
331 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
332 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
333 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
334 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
335 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
336 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
337 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
338 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
341 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
342 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
343 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
344 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
345 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
346 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
347 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
348 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
349 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
350 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
351 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
352 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
356 if (($line =~/dewey/) and ($deweydigits)) {
357 $linefield .="left($line,$deweydigits)";
358 } elsif (($line=~/lccn/) and ($lccndigits)) {
359 $linefield .="left($line,$lccndigits)";
360 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
361 $linefield .="left($line,$cotedigits)";
368 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
371 $strsth .= " and $line >= ? " ;
372 $strsth .= " and $line <= ? " ;
373 } elsif ($linefilter[0]) {
374 $linefilter[0] =~ s/\*/%/g;
375 $strsth .= " and $line LIKE ? " ;
378 $strsth .=" order by $linefield";
381 my $sth = $dbh->prepare( $strsth );
382 if (( @linefilter ) and ($linefilter[1])){
383 $sth->execute($linefilter[0],$linefilter[1]);
384 } elsif ($linefilter[0]) {
385 $sth->execute($linefilter[0]);
389 while ( my ($celvalue) = $sth->fetchrow) {
392 $cell{rowtitle} = $celvalue;
394 # $cell{rowtitle} = "";
397 push @loopline, \%cell;
402 if (($column =~/dewey/) and ($deweydigits)) {
403 $colfield .="left($column,$deweydigits)";
404 }elsif (($column=~/lccn/) and ($lccndigits)) {
405 $colfield .="left($column,$lccndigits)";
406 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
407 $colfield .="left($column,$cotedigits)";
409 $colfield .= $column;
413 $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
414 if (( @colfilter ) and ($colfilter[1])) {
415 $strsth2 .= " and $column> ? and $column< ?";
416 }elsif ($colfilter[0]){
417 $colfilter[0] =~ s/\*/%/g;
418 $strsth2 .= " and $column LIKE ? ";
420 $strsth2 .= " order by $colfield";
422 my $sth2 = $dbh->prepare( $strsth2 );
423 if ((@colfilter) and ($colfilter[1])) {
424 $sth2->execute($colfilter[0],$colfilter[1]);
425 } elsif ($colfilter[0]){
426 $sth2->execute($colfilter[0]);
430 while (my ($celvalue) = $sth2->fetchrow) {
434 $cell{coltitle} = $celvalue;
436 # $cell{coltitle} = "";
439 push @loopcol, \%cell;
447 #Initialization of cell values.....
450 foreach my $row ( @loopline ) {
451 foreach my $col ( @loopcol ) {
452 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
453 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
455 $table{$row->{rowtitle}}->{totalrow}=0;
458 # preparing calculation
459 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
461 @$filters[0]=~ s/\*/%/g;
462 $strcalc .= " AND dewey >" . @$filters[0] ."";
465 @$filters[1]=~ s/\*/%/g ;
466 $strcalc .= " AND dewey <" . @$filters[1] ."";
470 @$filters[2]=~ s/\*/%/g ;
471 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
474 @$filters[3]=~ s/\*/%/g;
475 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
478 @$filters[4]=~ s/\*/%/g ;
479 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
483 @$filters[5]=~ s/\*/%/g;
484 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
488 @$filters[6]=~ s/\*/%/g;
489 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
493 @$filters[7]=~ s/\*/%/g;
494 @$filters[7].="%" unless @$filters[7]=~/%/;
495 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
498 @$filters[8]=~ s/\*/%/g;
499 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
502 @$filters[9]=~ s/\*/%/g;
503 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
506 @$filters[10]=~ s/\*/%/g;
507 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
510 @$filters[11]=~ s/\*/%/g;
511 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
514 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
516 my $dbcalc = $dbh->prepare($strcalc);
518 # warn "filling table";
521 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
522 # warn "filling table $row / $col / $value ";
523 $emptycol = 1 if ($col eq undef);
524 $col = "zzEMPTY" if ($col eq undef);
525 $row = "zzEMPTY" if ($row eq undef);
527 $table{$row}->{$col}+=$value;
528 $table{$row}->{totalrow}+=$value;
529 $grantotal += $value;
532 # my %cell = {rowtitle => 'zzROWEMPTY'};
533 # push @loopline,\%cell;
536 # %cell = {coltitle => "zzEMPTY"};
537 push @loopcol,{coltitle => "NULL"} if ($emptycol);
539 foreach my $row ( sort keys %table ) {
541 #@loopcol ensures the order for columns is common with column titles
542 # and the number matches the number of columns
543 foreach my $col ( @loopcol ) {
544 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
545 push @loopcell, {value => $value } ;
547 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
548 'loopcell' => \@loopcell,
549 'hilighted' => ($hilighted >0),
550 'totalrow' => $table{$row}->{totalrow}
552 $hilighted = -$hilighted;
555 # warn "footer processing";
556 foreach my $col ( @loopcol ) {
558 foreach my $row ( @looprow ) {
559 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
560 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
562 # warn "summ for column ".$col->{coltitle}." = ".$total;
563 push @loopfooter, {'totalcol' => $total};
567 # the header of the table
568 $globalline{loopfilter}=\@loopfilter;
569 # the core of the table
570 $globalline{looprow} = \@looprow;
571 $globalline{loopcol} = \@loopcol;
572 # # the foot (totals by borrower type)
573 $globalline{loopfooter} = \@loopfooter;
574 $globalline{total}= $grantotal;
575 $globalline{line} = $line;
576 $globalline{column} = $column;
577 push @mainloop,\%globalline;