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
41 my $do_it=$input->param('do_it');
42 my $fullreportname = "reports/catalogue_stats.tmpl";
43 my $line = $input->param("Line");
44 my $column = $input->param("Column");
45 my @filters = $input->param("Filter");
46 my $deweydigits = $input->param("deweydigits");
47 my $lccndigits = $input->param("lccndigits");
48 my $cotedigits = $input->param("cotedigits");
49 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
52 my $del = $input->param("sep");
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
59 flagsrequired => {reports => 1},
62 $template->param(do_it => $do_it);
64 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
65 if ($output eq "screen"){
66 $template->param(mainloop => $results);
67 output_html_with_http_headers $input, $cookie, $template->output;
70 print $input->header(-type => 'application/vnd.sun.xml.calc',
72 -attachment=>"$basename.csv",
73 -name=>"$basename.csv" );
74 my $cols = @$results[0]->{loopcol};
75 my $lines = @$results[0]->{looprow};
77 $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;
106 $req = $dbh->prepare("select count(dewey) from biblioitems ");
111 while (my ($value) =$req->fetchrow) {
112 $hasdewey =1 if (($value>2) and (! $hasdewey));
113 $count++ if (($value>2) and (! $hasdewey));
114 # push @select, $value;
116 # my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
118 # -values => \@select,
122 $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 $req = $dbh->prepare("select count(itemcallnumber) from items");
146 while (my ($value) =$req->fetchrow) {
147 $hascote =1 if (($value>2) and (! $hascote));
148 $count++ if (($value) and (! $hascote));
149 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
150 # push @select, $value;
152 # my $CGIcote=CGI::scrolling_list( -name => 'Filter',
154 # -values => \@select,
158 my $hglghtDT =$count % 2;
159 # warn "highlightDT ".$hglghtDT;
161 my $hglghtPub =$count % 2;
162 # warn "highlightPub ".$hglghtPub;
164 my $hglghtPY =$count % 2;
165 # warn "highlightPY ".$hglghtPY;
167 my $hglghtHB =$count % 2;
168 # warn "highlightHB ".$hglghtHB;
170 my $hglghtLOC =$count % 2;
171 # warn "highlightLOC ".$hglghtLOC;
174 $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
178 while (my ($value) =$req->fetchrow) {
179 push @select, $value;
181 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
187 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
191 # while (my ($value) =$req->fetchrow) {
192 # push @select, $value;
194 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
196 # -values => \@select,
202 my $branches=GetBranches();
204 $select_branches{""} = "";
205 foreach my $branch (keys %$branches) {
206 push @select, $branch;
207 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
209 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
212 -labels => \%select_branches,
216 $req = $dbh->prepare("select distinctrow location from items order by location");
220 while (my ($value) =$req->fetchrow) {
221 push @select, $value;
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) WHERE 1";
459 @$filters[0]=~ s/\*/%/g;
460 $strcalc .= " AND dewey >" . @$filters[0] ."";
463 @$filters[1]=~ s/\*/%/g ;
464 $strcalc .= " AND dewey <" . @$filters[1] ."";
468 @$filters[2]=~ s/\*/%/g ;
469 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
472 @$filters[3]=~ s/\*/%/g;
473 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
476 @$filters[4]=~ s/\*/%/g ;
477 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
481 @$filters[5]=~ s/\*/%/g;
482 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
486 @$filters[6]=~ s/\*/%/g;
487 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
491 @$filters[7]=~ s/\*/%/g;
492 @$filters[7].="%" unless @$filters[7]=~/%/;
493 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
496 @$filters[8]=~ s/\*/%/g;
497 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
500 @$filters[9]=~ s/\*/%/g;
501 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
504 @$filters[10]=~ s/\*/%/g;
505 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
508 @$filters[11]=~ s/\*/%/g;
509 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
512 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
514 my $dbcalc = $dbh->prepare($strcalc);
516 # warn "filling table";
519 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
520 # warn "filling table $row / $col / $value ";
521 $emptycol = 1 if ($col eq undef);
522 $col = "zzEMPTY" if ($col eq undef);
523 $row = "zzEMPTY" if ($row eq undef);
525 $table{$row}->{$col}+=$value;
526 $table{$row}->{totalrow}+=$value;
527 $grantotal += $value;
530 # my %cell = {rowtitle => 'zzROWEMPTY'};
531 # push @loopline,\%cell;
534 # %cell = {coltitle => "zzEMPTY"};
535 push @loopcol,{coltitle => "NULL"} if ($emptycol);
537 foreach my $row ( sort keys %table ) {
539 #@loopcol ensures the order for columns is common with column titles
540 # and the number matches the number of columns
541 foreach my $col ( @loopcol ) {
542 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
543 push @loopcell, {value => $value } ;
545 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
546 'loopcell' => \@loopcell,
547 'hilighted' => ($hilighted >0),
548 'totalrow' => $table{$row}->{totalrow}
550 $hilighted = -$hilighted;
553 # warn "footer processing";
554 foreach my $col ( @loopcol ) {
556 foreach my $row ( @looprow ) {
557 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
558 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
560 # warn "summ for column ".$col->{coltitle}." = ".$total;
561 push @loopfooter, {'totalcol' => $total};
565 # the header of the table
566 $globalline{loopfilter}=\@loopfilter;
567 # the core of the table
568 $globalline{looprow} = \@looprow;
569 $globalline{loopcol} = \@loopcol;
570 # # the foot (totals by borrower type)
571 $globalline{loopfooter} = \@loopfooter;
572 $globalline{total}= $grantotal;
573 $globalline{line} = $line;
574 $globalline{column} = $column;
575 push @mainloop,\%globalline;