adding a pagination bar & displaying only 20 results per page.
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30
31 =head1 NAME
32
33 plugin that shows a stats on borrowers
34
35 =head1 DESCRIPTION
36
37 =over 2
38
39 =cut
40
41 my $input = new CGI;
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/catalogue_stats.tmpl";
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");
54
55 my ($template, $borrowernumber, $cookie)
56         = get_template_and_user({template_name => $fullreportname,
57                                 query => $input,
58                                 type => "intranet",
59                                 authnotrequired => 0,
60                                 flagsrequired => {reports => 1},
61                                 debug => 1,
62                                 });
63 $template->param(do_it => $do_it);
64 if ($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;
69                 exit(1);
70         } else {
71                 print $input->header(-type => 'application/vnd.sun.xml.calc',
72                                      -encoding    => 'utf-8',
73                                                          -attachment=>"$basename.csv",
74                                                          -name=>"$basename.csv" );
75                 my $cols = @$results[0]->{loopcol};
76                 my $lines = @$results[0]->{looprow};
77                 my $sep;
78                 $sep =C4::Context->preference("delimiter");
79                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
80                 foreach my $col ( @$cols ) {
81                         print $col->{coltitle}.$sep;
82                 }
83                 print "Total\n";
84                 foreach my $line ( @$lines ) {
85                         my $x = $line->{loopcell};
86                         print $line->{rowtitle}.$sep;
87                         foreach my $cell (@$x) {
88                                 print $cell->{value}.$sep;
89                         }
90                         print $line->{totalrow};
91                         print "\n";
92                 }
93                 print "TOTAL";
94                 $cols = @$results[0]->{loopfooter};
95                 foreach my $col ( @$cols ) {
96                         print $sep.$col->{totalcol};
97                 }
98                 print $sep.@$results[0]->{total};
99                 exit(1);
100         }
101 } else {
102         my $dbh = C4::Context->dbh;
103         my @values;
104         my %labels;
105         my $count=0;
106         my $req;
107         $req = $dbh->prepare("select count(dewey) from biblioitems ");
108         $req->execute;
109         my $hasdewey;
110         my @select;
111 #       push @select,"";
112         while (my ($value) =$req->fetchrow) {
113                 $hasdewey =1 if (($value>2) and (! $hasdewey));
114                 $count++ if (($value>2) and (! $hasdewey));
115 #               push @select, $value;
116         }
117 #       my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
118 #                               -id => 'Filter',
119 #                               -values   => \@select,
120 #                               -size     => 1,
121 #                               -multiple => 0 );
122         
123         $req = $dbh->prepare( "select count(lccn) from biblioitems ");
124         $req->execute;
125 #       undef @select;
126 #       push @select,"";
127         my $haslccn;
128         my $hlghtlccn;
129         while (my ($value) =$req->fetchrow) {
130                 $hlghtlccn = !($hasdewey);
131                 $haslccn =1 if (($value>2) and (! $haslccn));
132                 $count++ if (($value) and (! $haslccn));
133 #               push @select, $value;
134         }
135 #       my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
136 #                               -id => 'Filter',
137 #                               -values   => \@select,
138 #                               -size     => 1,
139 #                               -multiple => 0 );
140         
141         $req = $dbh->prepare("select count(itemcallnumber) from items");
142         $req->execute;
143 #       undef @select;
144 #       push @select,"";
145         my $hascote;
146         my $hlghtcote;
147         while (my ($value) =$req->fetchrow) {
148                 $hascote =1 if (($value>2) and (! $hascote));
149                 $count++ if (($value) and (! $hascote));
150                 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
151 #               push @select, $value;
152         }
153 #       my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
154 #                               -id => 'Filter',
155 #                               -values   => \@select,
156 #                               -size     => 1,
157 #                               -multiple => 0 );
158         $count++;
159         my $hglghtDT =$count % 2;
160 #       warn "highlightDT ".$hglghtDT;
161         $count++;
162         my $hglghtPub =$count % 2;
163 #       warn "highlightPub ".$hglghtPub;
164         $count++;
165         my $hglghtPY =$count % 2;
166 #       warn "highlightPY ".$hglghtPY;
167         $count++;
168         my $hglghtHB =$count % 2;
169 #       warn "highlightHB ".$hglghtHB;
170         $count++;
171         my $hglghtLOC =$count % 2;
172 #       warn "highlightLOC ".$hglghtLOC;
173         
174         
175         $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
176         $req->execute;
177         undef @select;
178         push @select,"";
179         while (my ($value) =$req->fetchrow) {
180                 push @select, $value;
181         }
182         my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
183                                 -id => 'itemtype',
184                                 -values   => \@select,
185                                 -size     => 1,
186                                 -multiple => 0 );
187         
188 #       $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
189 #       $req->execute;
190 #       undef @select;
191 #       push @select,"";
192 #       while (my ($value) =$req->fetchrow) {
193 #               push @select, $value;
194 #       }
195 #       my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
196 #                               -id => 'Filter',
197 #                               -values   => \@select,
198 #                               -size     => 1,
199 #                               -multiple => 0 );
200
201         undef @select;
202         push @select,"";
203         my $branches=GetBranches();
204         my %select_branches;
205         $select_branches{""} = "";
206         foreach my $branch (keys %$branches) {
207                 push @select, $branch;
208                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
209         }
210         my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
211                                 -id => 'branch',
212                                 -values   => \@select,
213                                 -labels   => \%select_branches,
214                                 -size     => 1,
215                                 -multiple => 0 );
216         
217         $req = $dbh->prepare("select distinctrow location from items order by location");
218         $req->execute;
219         undef @select;
220         push @select,"";
221         while (my ($value) =$req->fetchrow) {
222                 push @select, $value;
223         }
224         my $CGIlocation=CGI::scrolling_list( -name     => 'Filter',
225                                 -id => 'location',
226                                 -values   => \@select,
227                                 -size     => 1,
228                                 -multiple => 0 );
229         
230         my @mime = ( C4::Context->preference("MIME") );
231         foreach my $mime (@mime){
232 #               warn "".$mime;
233         }
234         
235         my $CGIextChoice=CGI::scrolling_list(
236                                 -name => 'MIME',
237                                 -id => 'MIME',
238                                 -values   => \@mime,
239                                 -size     => 1,
240                                 -multiple => 0 );
241         
242         my @dels = ( C4::Context->preference("delimiter") );
243         my $CGIsepChoice=CGI::scrolling_list(
244                                 -name => 'sep',
245                                 -id => 'sep',
246                                 -values   => \@dels,
247                                 -size     => 1,
248                                 -multiple => 0 );
249         
250         $template->param(hasdewey=>$hasdewey,
251 #                                       CGIFromDeweyClass => $CGIdewey,
252 #                                       CGIToDeweyClass => $CGIdewey,
253                                         haslccn=> $haslccn,
254                                         hlghtlccn => $hlghtlccn,
255 #                                       CGIFromLoCClass => $CGIlccn,
256 #                                       CGIToLoCClass => $CGIlccn,
257                                         hascote=> $hascote,
258                                         hlghtcote => $hlghtcote,
259                                         hglghtDT => $hglghtDT,
260                                         hglghtPub => $hglghtPub,
261                                         hglghtPY => $hglghtPY,
262                                         hglghtHB => $hglghtHB,
263                                         hglghtLOC => $hglghtLOC,
264 #                                       CGIFromCoteClass => $CGIcote,
265 #                                       CGIToCoteClass => $CGIcote,
266                                         CGIItemType => $CGIitemtype,
267 #                                       CGIFromPublicationYear => $CGIpublicationyear,
268 #                                       CGIToPublicationYear => $CGIpublicationyear,
269 #                                       CGIPublisher => $CGIpublisher,
270                                         CGIBranch => $CGIbranch,
271                                         CGILocation => $CGIlocation,
272                                         CGIextChoice => $CGIextChoice,
273                                         CGIsepChoice => $CGIsepChoice
274                                         );
275
276 }
277 output_html_with_http_headers $input, $cookie, $template->output;
278
279
280
281 sub calculate {
282         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
283         my @mainloop;
284         my @loopfooter;
285         my @loopcol;
286         my @loopline;
287         my @looprow;
288         my %globalline;
289         my $grantotal =0;
290 # extract parameters
291         my $dbh = C4::Context->dbh;
292
293 # Filters
294 # Checking filters
295 #
296         my @loopfilter;
297         for (my $i=0;$i<=11;$i++) {
298                 my %cell;
299                 if ( @$filters[$i] ) {
300                         if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
301                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
302                         }
303                         $cell{filter} .= @$filters[$i];
304                         $cell{crit} .="Dewey Classification From" if ($i==0);
305                         $cell{crit} .="Dewey Classification To" if ($i==1);
306                         $cell{crit} .="Lccn Classification From" if ($i==2);
307                         $cell{crit} .="Lccn Classification To" if ($i==3);
308                         $cell{crit} .="Cote Classification From" if ($i==4);
309                         $cell{crit} .="Cote Classification To" if ($i==5);
310                         $cell{crit} .="Document type" if ($i==6);
311                         $cell{crit} .="Publisher" if ($i==7);
312                         $cell{crit} .="Publication year From" if ($i==8);
313                         $cell{crit} .="Publication year To" if ($i==9);
314                         $cell{crit} .="Branch :" if ($i==10);
315                         $cell{crit} .="Location:" if ($i==11);
316                         push @loopfilter, \%cell;
317                 }
318         }
319         
320         my @linefilter;
321 #       warn "filtres ".@filters[0];
322 #       warn "filtres ".@filters[1];
323 #       warn "filtres ".@filters[2];
324 #       warn "filtres ".@filters[3];
325         
326         $linefilter[0] = @$filters[0] if ($line =~ /dewey/ )  ;
327         $linefilter[1] = @$filters[1] if ($line =~ /dewey/ )  ;
328         $linefilter[0] = @$filters[2] if ($line =~ /lccn/ )  ;
329         $linefilter[1] = @$filters[3] if ($line =~ /lccn/ )  ;
330         $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ )  ;
331         $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ )  ;
332         $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
333         $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
334         $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
335         $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
336         @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
337         @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
338
339         my @colfilter ;
340         $colfilter[0] = @$filters[0] if ($column =~ /dewey/ )  ;
341         $colfilter[1] = @$filters[1] if ($column =~ /dewey/ )  ;
342         $colfilter[0] = @$filters[2] if ($column =~ /lccn/ )  ;
343         $colfilter[1] = @$filters[3] if ($column =~ /lccn/ )  ;
344         $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ )  ;
345         $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ )  ;
346         $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
347         $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
348         $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
349         $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
350         @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
351         @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
352
353 # 1st, loop rows.
354         my $linefield;
355         if (($line =~/dewey/)  and ($deweydigits)) {
356                 $linefield .="left($line,$deweydigits)";
357         } elsif (($line=~/lccn/) and ($lccndigits)) {
358                 $linefield .="left($line,$lccndigits)";
359         } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
360                 $linefield .="left($line,$cotedigits)";
361         }else {
362                 $linefield .= $line;
363         }
364         
365         
366         my $strsth;
367         $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
368         if ( @linefilter ) {
369                 if ($linefilter[1]){
370                         $strsth .= " and $line >= ? " ;
371                         $strsth .= " and $line <= ? " ;
372                 } elsif ($linefilter[0]) {
373                         $linefilter[0] =~ s/\*/%/g;
374                         $strsth .= " and $line LIKE ? " ;
375                 }
376         }
377         $strsth .=" order by $linefield";
378         warn "". $strsth;
379         
380         my $sth = $dbh->prepare( $strsth );
381         if (( @linefilter ) and ($linefilter[1])){
382                 $sth->execute($linefilter[0],$linefilter[1]);
383         } elsif ($linefilter[0]) {
384                 $sth->execute($linefilter[0]);
385         } else {
386                 $sth->execute;
387         }
388         while ( my ($celvalue) = $sth->fetchrow) {
389                 my %cell;
390                 if ($celvalue) {
391                         $cell{rowtitle} = $celvalue;
392 #               } else {
393 #                       $cell{rowtitle} = "";
394                 }
395                 $cell{totalrow} = 0;
396                 push @loopline, \%cell;
397         }
398
399 # 2nd, loop cols.
400         my $colfield;
401         if (($column =~/dewey/)  and ($deweydigits)) {
402                 $colfield .="left($column,$deweydigits)";
403         }elsif (($column=~/lccn/) and ($lccndigits)) {
404                 $colfield .="left($column,$lccndigits)";
405         }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
406                 $colfield .="left($column,$cotedigits)";
407         }else {
408                 $colfield .= $column;
409         }
410         
411         my $strsth2;
412         $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
413         if (( @colfilter ) and ($colfilter[1])) {
414                 $strsth2 .= " and $column> ? and $column< ?";
415         }elsif ($colfilter[0]){
416                 $colfilter[0] =~ s/\*/%/g;
417                 $strsth2 .= " and $column LIKE ? ";
418         } 
419         $strsth2 .= " order by $colfield";
420         warn "". $strsth2;
421         my $sth2 = $dbh->prepare( $strsth2 );
422         if ((@colfilter) and ($colfilter[1])) {
423                 $sth2->execute($colfilter[0],$colfilter[1]);
424         } elsif ($colfilter[0]){
425                 $sth2->execute($colfilter[0]);
426         } else {
427                 $sth2->execute;
428         }
429         while (my ($celvalue) = $sth2->fetchrow) {
430                 my %cell;
431                 my %ft;
432                 if ($celvalue) {
433                         $cell{coltitle} = $celvalue;
434 #               } else {
435 #                       $cell{coltitle} = "";
436                 }
437                 $ft{totalcol} = 0;
438                 push @loopcol, \%cell;
439         }
440         
441
442         my $i=0;
443         my @totalcol;
444         my $hilighted=-1;
445         
446         #Initialization of cell values.....
447         my %table;
448 #       warn "init table";
449         foreach my $row ( @loopline ) {
450                 foreach my $col ( @loopcol ) {
451 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
452                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
453                 }
454                 $table{$row->{rowtitle}}->{totalrow}=0;
455         }
456
457 # preparing calculation
458         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
459         if (@$filters[0]){
460                 @$filters[0]=~ s/\*/%/g;
461                 $strcalc .= " AND dewey >" . @$filters[0] ."";
462         }
463         if (@$filters[1]){
464                 @$filters[1]=~ s/\*/%/g ;
465                 $strcalc .= " AND dewey <" . @$filters[1] ."";
466                 
467         }
468         if (@$filters[2]){
469                 @$filters[2]=~ s/\*/%/g ;
470                 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
471         }
472         if (@$filters[3]){
473                 @$filters[3]=~ s/\*/%/g;
474                 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
475         }
476         if (@$filters[4]){
477                 @$filters[4]=~ s/\*/%/g ;
478                 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
479         }
480         
481         if (@$filters[5]){
482                 @$filters[5]=~ s/\*/%/g;
483                 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
484         }
485         
486         if (@$filters[6]){
487                 @$filters[6]=~ s/\*/%/g;
488                 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
489         }
490         
491         if (@$filters[7]){
492                 @$filters[7]=~ s/\*/%/g;
493                 @$filters[7].="%" unless @$filters[7]=~/%/;
494                 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
495         }
496         if (@$filters[8]){
497                 @$filters[8]=~ s/\*/%/g;
498                 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
499         }
500         if (@$filters[9]){
501                 @$filters[9]=~ s/\*/%/g;
502                 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
503         }
504         if (@$filters[10]){
505                 @$filters[10]=~ s/\*/%/g;
506                 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
507         }
508         if (@$filters[11]){
509                 @$filters[11]=~ s/\*/%/g;
510                 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
511         }
512         
513         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
514         warn "". $strcalc;
515         my $dbcalc = $dbh->prepare($strcalc);
516         $dbcalc->execute;
517 #       warn "filling table";
518         
519         my $emptycol; 
520         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
521 #               warn "filling table $row / $col / $value ";
522                 $emptycol = 1 if ($col eq undef);
523                 $col = "zzEMPTY" if ($col eq undef);
524                 $row = "zzEMPTY" if ($row eq undef);
525                 
526                 $table{$row}->{$col}+=$value;
527                 $table{$row}->{totalrow}+=$value;
528                 $grantotal += $value;
529         }
530
531 #       my %cell = {rowtitle => 'zzROWEMPTY'};
532 #       push @loopline,\%cell;
533 #       undef %cell;
534 #       my %cell;
535 #       %cell = {coltitle => "zzEMPTY"};
536         push @loopcol,{coltitle => "NULL"} if ($emptycol);
537         
538         foreach my $row ( sort keys %table ) {
539                 my @loopcell;
540                 #@loopcol ensures the order for columns is common with column titles
541                 # and the number matches the number of columns
542                 foreach my $col ( @loopcol ) {
543                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
544                         push @loopcell, {value => $value  } ;
545                 }
546                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
547                                                 'loopcell' => \@loopcell,
548                                                 'hilighted' => ($hilighted >0),
549                                                 'totalrow' => $table{$row}->{totalrow}
550                                         };
551                 $hilighted = -$hilighted;
552         }
553         
554 #       warn "footer processing";
555         foreach my $col ( @loopcol ) {
556                 my $total=0;
557                 foreach my $row ( @looprow ) {
558                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
559 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
560                 }
561 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
562                 push @loopfooter, {'totalcol' => $total};
563         }
564                         
565
566         # the header of the table
567         $globalline{loopfilter}=\@loopfilter;
568         # the core of the table
569         $globalline{looprow} = \@looprow;
570         $globalline{loopcol} = \@loopcol;
571 #       # the foot (totals by borrower type)
572         $globalline{loopfooter} = \@loopfooter;
573         $globalline{total}= $grantotal;
574         $globalline{line} = $line;
575         $globalline{column} = $column;
576         push @mainloop,\%globalline;
577         return \@mainloop;
578 }
579
580 1;