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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/catalogue_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $deweydigits = $input->param("deweydigits");
51 my $lccndigits = $input->param("lccndigits");
52 my $cotedigits = $input->param("cotedigits");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
63 flagsrequired => {editcatalogue => 1},
66 $template->param(do_it => $do_it);
68 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
69 if ($output eq "screen"){
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 print $input->header(-type => 'application/vnd.sun.xml.calc',
75 -attachment=>"$basename.csv",
76 -name=>"$basename.csv" );
77 my $cols = @$results[0]->{loopcol};
78 my $lines = @$results[0]->{looprow};
80 $sep =C4::Context->preference("delimiter");
81 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82 foreach my $col ( @$cols ) {
83 print $col->{coltitle}.$sep;
86 foreach my $line ( @$lines ) {
87 my $x = $line->{loopcell};
88 print $line->{rowtitle}.$sep;
89 foreach my $cell (@$x) {
90 print $cell->{value}.$sep;
92 print $line->{totalrow};
96 $cols = @$results[0]->{loopfooter};
97 foreach my $col ( @$cols ) {
98 print $sep.$col->{totalcol};
100 print $sep.@$results[0]->{total};
104 my $dbh = C4::Context->dbh;
109 $req = $dbh->prepare("select count(dewey) from biblioitems ");
114 while (my ($value) =$req->fetchrow) {
115 $hasdewey =1 if (($value>2) and (! $hasdewey));
116 $count++ 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,
161 my $hglghtDT =$count % 2;
162 # warn "highlightDT ".$hglghtDT;
164 my $hglghtPub =$count % 2;
165 # warn "highlightPub ".$hglghtPub;
167 my $hglghtPY =$count % 2;
168 # warn "highlightPY ".$hglghtPY;
170 my $hglghtHB =$count % 2;
171 # warn "highlightHB ".$hglghtHB;
173 my $hglghtLOC =$count % 2;
174 # warn "highlightLOC ".$hglghtLOC;
177 $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
181 while (my ($value) =$req->fetchrow) {
182 push @select, $value;
184 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
190 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
194 # while (my ($value) =$req->fetchrow) {
195 # push @select, $value;
197 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
199 # -values => \@select,
205 my $branches=getbranches();
207 $select_branches{""} = "";
208 foreach my $branch (keys %$branches) {
209 push @select, $branch;
210 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
212 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
215 -labels => \%select_branches,
219 $req = $dbh->prepare("select distinctrow location from items order by location");
223 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
229 my @mime = ( C4::Context->preference("MIME") );
230 foreach my $mime (@mime){
234 my $CGIextChoice=CGI::scrolling_list(
241 my @dels = ( C4::Context->preference("delimiter") );
242 my $CGIsepChoice=CGI::scrolling_list(
249 $template->param(hasdewey=>$hasdewey,
250 # CGIFromDeweyClass => $CGIdewey,
251 # CGIToDeweyClass => $CGIdewey,
253 hlghtlccn => $hlghtlccn,
254 # CGIFromLoCClass => $CGIlccn,
255 # CGIToLoCClass => $CGIlccn,
257 hlghtcote => $hlghtcote,
258 hglghtDT => $hglghtDT,
259 hglghtPub => $hglghtPub,
260 hglghtPY => $hglghtPY,
261 hglghtHB => $hglghtHB,
262 hglghtLOC => $hglghtLOC,
263 # CGIFromCoteClass => $CGIcote,
264 # CGIToCoteClass => $CGIcote,
265 CGIItemType => $CGIitemtype,
266 # CGIFromPublicationYear => $CGIpublicationyear,
267 # CGIToPublicationYear => $CGIpublicationyear,
268 # CGIPublisher => $CGIpublisher,
269 CGIBranch => $CGIbranch,
270 CGILocation => $CGIlocation,
271 CGIextChoice => $CGIextChoice,
272 CGIsepChoice => $CGIsepChoice
276 output_html_with_http_headers $input, $cookie, $template->output;
281 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
290 my $dbh = C4::Context->dbh;
296 for (my $i=0;$i<=11;$i++) {
298 if ( @$filters[$i] ) {
299 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
300 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
302 $cell{filter} .= @$filters[$i];
303 $cell{crit} .="Dewey Classification From" if ($i==0);
304 $cell{crit} .="Dewey Classification To" if ($i==1);
305 $cell{crit} .="Lccn Classification From" if ($i==2);
306 $cell{crit} .="Lccn Classification To" if ($i==3);
307 $cell{crit} .="Cote Classification From" if ($i==4);
308 $cell{crit} .="Cote Classification To" if ($i==5);
309 $cell{crit} .="Document type" if ($i==6);
310 $cell{crit} .="Publisher" if ($i==7);
311 $cell{crit} .="Publication year From" if ($i==8);
312 $cell{crit} .="Publication year To" if ($i==9);
313 $cell{crit} .="Branch :" if ($i==10);
314 $cell{crit} .="Location:" if ($i==11);
315 push @loopfilter, \%cell;
320 # warn "filtres ".@filters[0];
321 # warn "filtres ".@filters[1];
322 # warn "filtres ".@filters[2];
323 # warn "filtres ".@filters[3];
325 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
326 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
327 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
328 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
329 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
330 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
331 @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
332 @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
333 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
334 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
335 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
336 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
339 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
340 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
341 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
342 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
343 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
344 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
345 @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
346 @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
347 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
348 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
349 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
350 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
354 if (($line =~/dewey/) and ($deweydigits)) {
355 $linefield .="left($line,$deweydigits)";
356 } elsif (($line=~/lccn/) and ($lccndigits)) {
357 $linefield .="left($line,$lccndigits)";
358 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
359 $linefield .="left($line,$cotedigits)";
366 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
369 $strsth .= " and $line >= ? " ;
370 $strsth .= " and $line <= ? " ;
371 } elsif ($linefilter[0]) {
372 $linefilter[0] =~ s/\*/%/g;
373 $strsth .= " and $line LIKE ? " ;
376 $strsth .=" order by $linefield";
379 my $sth = $dbh->prepare( $strsth );
380 if (( @linefilter ) and ($linefilter[1])){
381 $sth->execute($linefilter[0],$linefilter[1]);
382 } elsif ($linefilter[0]) {
383 $sth->execute($linefilter[0]);
387 while ( my ($celvalue) = $sth->fetchrow) {
390 $cell{rowtitle} = $celvalue;
392 # $cell{rowtitle} = "";
395 push @loopline, \%cell;
400 if (($column =~/dewey/) and ($deweydigits)) {
401 $colfield .="left($column,$deweydigits)";
402 }elsif (($column=~/lccn/) and ($lccndigits)) {
403 $colfield .="left($column,$lccndigits)";
404 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
405 $colfield .="left($column,$cotedigits)";
407 $colfield .= $column;
411 $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
412 if (( @colfilter ) and ($colfilter[1])) {
413 $strsth2 .= " and $column> ? and $column< ?";
414 }elsif ($colfilter[0]){
415 $colfilter[0] =~ s/\*/%/g;
416 $strsth2 .= " and $column LIKE ? ";
418 $strsth2 .= " order by $colfield";
420 my $sth2 = $dbh->prepare( $strsth2 );
421 if ((@colfilter) and ($colfilter[1])) {
422 $sth2->execute($colfilter[0],$colfilter[1]);
423 } elsif ($colfilter[0]){
424 $sth2->execute($colfilter[0]);
428 while (my ($celvalue) = $sth2->fetchrow) {
432 $cell{coltitle} = $celvalue;
434 # $cell{coltitle} = "";
437 push @loopcol, \%cell;
445 #Initialization of cell values.....
448 foreach my $row ( @loopline ) {
449 foreach my $col ( @loopcol ) {
450 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
451 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
453 $table{$row->{rowtitle}}->{totalrow}=0;
456 # preparing calculation
457 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)";
460 @$filters[0]=~ s/\*/%/g;
461 $strcalc .= " WHERE dewey >" . @$filters[0] ."";
465 @$filters[1]=~ s/\*/%/g ;
467 $strcalc .= " AND dewey <" . @$filters[1] ."";
469 $strcalc .= " WHERE dewey <" . @$filters[1] ."" ;
475 @$filters[2]=~ s/\*/%/g ;
477 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
479 $strcalc .= " WHERE lccn > " . @$filters[2] ."" ;
484 @$filters[3]=~ s/\*/%/g;
486 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
488 $strcalc .= " WHERE lccn <" . @$filters[3] ."" ;
493 @$filters[4]=~ s/\*/%/g ;
495 $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" ;
497 $strcalc .= " WHERE items.itemcallnumber >" . @$filters[4] ."" ;
503 @$filters[5]=~ s/\*/%/g;
505 $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" ;
507 $strcalc .= " WHERE items.itemcallnumber <" . @$filters[5] ."" ;
513 @$filters[6]=~ s/\*/%/g;
515 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
517 $strcalc .= " WHERE biblioitems.itemtype like '" . @$filters[6] ."'";
523 @$filters[7]=~ s/\*/%/g;
524 @$filters[7].="%" unless @$filters[7]=~/%/;
526 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
528 $strcalc .= " WHERE biblioitems.publishercode like \"" . @$filters[7] ."\"";
533 @$filters[8]=~ s/\*/%/g;
535 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
537 $strcalc .= " WHERE publicationyear >" . @$filters[8] ."" ;
542 @$filters[9]=~ s/\*/%/g;
544 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
546 $strcalc .= " WHERE publicationyear <" . @$filters[9] ."";
551 @$filters[10]=~ s/\*/%/g;
553 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
555 $strcalc .= " WHERE items.homebranch like '" . @$filters[10] ."'";
560 @$filters[11]=~ s/\*/%/g;
562 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
564 $strcalc .= " WHERE items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
568 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
570 my $dbcalc = $dbh->prepare($strcalc);
572 # warn "filling table";
575 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
576 # warn "filling table $row / $col / $value ";
577 $emptycol = 1 if ($col eq undef);
578 $col = "zzEMPTY" if ($col eq undef);
579 $row = "zzEMPTY" if ($row eq undef);
581 $table{$row}->{$col}+=$value;
582 $table{$row}->{totalrow}+=$value;
583 $grantotal += $value;
586 # my %cell = {rowtitle => 'zzROWEMPTY'};
587 # push @loopline,\%cell;
590 # %cell = {coltitle => "zzEMPTY"};
591 push @loopcol,{coltitle => "NULL"} if ($emptycol);
593 foreach my $row ( sort keys %table ) {
595 #@loopcol ensures the order for columns is common with column titles
596 # and the number matches the number of columns
597 foreach my $col ( @loopcol ) {
598 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
599 push @loopcell, {value => $value } ;
601 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
602 'loopcell' => \@loopcell,
603 'hilighted' => ($hilighted >0),
604 'totalrow' => $table{$row}->{totalrow}
606 $hilighted = -$hilighted;
609 # warn "footer processing";
610 foreach my $col ( @loopcol ) {
612 foreach my $row ( @looprow ) {
613 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
614 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
616 # warn "summ for column ".$col->{coltitle}." = ".$total;
617 push @loopfooter, {'totalcol' => $total};
621 # the header of the table
622 $globalline{loopfilter}=\@loopfilter;
623 # the core of the table
624 $globalline{looprow} = \@looprow;
625 $globalline{loopcol} = \@loopcol;
626 # # the foot (totals by borrower type)
627 $globalline{loopfooter} = \@loopfooter;
628 $globalline{total}= $grantotal;
629 $globalline{line} = $line;
630 $globalline{column} = $column;
631 push @mainloop,\%globalline;