bugfixes (various), handling utf-8 without guessencoding (as suggested by joshua...
[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
38 =over2
39
40 =cut
41
42 my $input = new CGI;
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");
55
56 my ($template, $borrowernumber, $cookie)
57         = get_template_and_user({template_name => $fullreportname,
58                                 query => $input,
59                                 type => "intranet",
60                                 authnotrequired => 0,
61                                 flagsrequired => {reports => 1},
62                                 debug => 1,
63                                 });
64 $template->param(do_it => $do_it);
65 if ($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;
70                 exit(1);
71         } else {
72                 print $input->header(-type => 'application/vnd.sun.xml.calc',
73                                      -encoding    => 'utf-8',
74                                                          -attachment=>"$basename.csv",
75                                                          -name=>"$basename.csv" );
76                 my $cols = @$results[0]->{loopcol};
77                 my $lines = @$results[0]->{looprow};
78                 my $sep;
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;
83                 }
84                 print "Total\n";
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;
90                         }
91                         print $line->{totalrow};
92                         print "\n";
93                 }
94                 print "TOTAL";
95                 $cols = @$results[0]->{loopfooter};
96                 foreach my $col ( @$cols ) {
97                         print $sep.$col->{totalcol};
98                 }
99                 print $sep.@$results[0]->{total};
100                 exit(1);
101         }
102 } else {
103         my $dbh = C4::Context->dbh;
104         my @values;
105         my %labels;
106         my $count=0;
107         my $req;
108         $req = $dbh->prepare("select count(dewey) from biblioitems ");
109         $req->execute;
110         my $hasdewey;
111         my @select;
112 #       push @select,"";
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;
117         }
118 #       my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
119 #                               -id => 'Filter',
120 #                               -values   => \@select,
121 #                               -size     => 1,
122 #                               -multiple => 0 );
123         
124         $req = $dbh->prepare( "select count(lccn) from biblioitems ");
125         $req->execute;
126 #       undef @select;
127 #       push @select,"";
128         my $haslccn;
129         my $hlghtlccn;
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;
135         }
136 #       my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
137 #                               -id => 'Filter',
138 #                               -values   => \@select,
139 #                               -size     => 1,
140 #                               -multiple => 0 );
141         
142         $req = $dbh->prepare("select count(itemcallnumber) from items");
143         $req->execute;
144 #       undef @select;
145 #       push @select,"";
146         my $hascote;
147         my $hlghtcote;
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;
153         }
154 #       my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
155 #                               -id => 'Filter',
156 #                               -values   => \@select,
157 #                               -size     => 1,
158 #                               -multiple => 0 );
159         $count++;
160         my $hglghtDT =$count % 2;
161 #       warn "highlightDT ".$hglghtDT;
162         $count++;
163         my $hglghtPub =$count % 2;
164 #       warn "highlightPub ".$hglghtPub;
165         $count++;
166         my $hglghtPY =$count % 2;
167 #       warn "highlightPY ".$hglghtPY;
168         $count++;
169         my $hglghtHB =$count % 2;
170 #       warn "highlightHB ".$hglghtHB;
171         $count++;
172         my $hglghtLOC =$count % 2;
173 #       warn "highlightLOC ".$hglghtLOC;
174         
175         
176         $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
177         $req->execute;
178         undef @select;
179         push @select,"";
180         while (my ($value) =$req->fetchrow) {
181                 push @select, $value;
182         }
183         my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
184                                 -id => 'itemtype',
185                                 -values   => \@select,
186                                 -size     => 1,
187                                 -multiple => 0 );
188         
189 #       $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
190 #       $req->execute;
191 #       undef @select;
192 #       push @select,"";
193 #       while (my ($value) =$req->fetchrow) {
194 #               push @select, $value;
195 #       }
196 #       my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
197 #                               -id => 'Filter',
198 #                               -values   => \@select,
199 #                               -size     => 1,
200 #                               -multiple => 0 );
201
202         undef @select;
203         push @select,"";
204         my $branches=GetBranches();
205         my %select_branches;
206         $select_branches{""} = "";
207         foreach my $branch (keys %$branches) {
208                 push @select, $branch;
209                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
210         }
211         my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
212                                 -id => 'branch',
213                                 -values   => \@select,
214                                 -labels   => \%select_branches,
215                                 -size     => 1,
216                                 -multiple => 0 );
217         
218         $req = $dbh->prepare("select distinctrow location from items order by location");
219         $req->execute;
220         undef @select;
221         push @select,"";
222         while (my ($value) =$req->fetchrow) {
223                 push @select, $value;
224         }
225         my $CGIlocation=CGI::scrolling_list( -name     => 'Filter',
226                                 -id => 'location',
227                                 -values   => \@select,
228                                 -size     => 1,
229                                 -multiple => 0 );
230         
231         my @mime = ( C4::Context->preference("MIME") );
232         foreach my $mime (@mime){
233 #               warn "".$mime;
234         }
235         
236         my $CGIextChoice=CGI::scrolling_list(
237                                 -name => 'MIME',
238                                 -id => 'MIME',
239                                 -values   => \@mime,
240                                 -size     => 1,
241                                 -multiple => 0 );
242         
243         my @dels = ( C4::Context->preference("delimiter") );
244         my $CGIsepChoice=CGI::scrolling_list(
245                                 -name => 'sep',
246                                 -id => 'sep',
247                                 -values   => \@dels,
248                                 -size     => 1,
249                                 -multiple => 0 );
250         
251         $template->param(hasdewey=>$hasdewey,
252 #                                       CGIFromDeweyClass => $CGIdewey,
253 #                                       CGIToDeweyClass => $CGIdewey,
254                                         haslccn=> $haslccn,
255                                         hlghtlccn => $hlghtlccn,
256 #                                       CGIFromLoCClass => $CGIlccn,
257 #                                       CGIToLoCClass => $CGIlccn,
258                                         hascote=> $hascote,
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
275                                         );
276
277 }
278 output_html_with_http_headers $input, $cookie, $template->output;
279
280
281
282 sub calculate {
283         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
284         my @mainloop;
285         my @loopfooter;
286         my @loopcol;
287         my @loopline;
288         my @looprow;
289         my %globalline;
290         my $grantotal =0;
291 # extract parameters
292         my $dbh = C4::Context->dbh;
293
294 # Filters
295 # Checking filters
296 #
297         my @loopfilter;
298         for (my $i=0;$i<=11;$i++) {
299                 my %cell;
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]) ;
303                         }
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;
318                 }
319         }
320         
321         my @linefilter;
322 #       warn "filtres ".@filters[0];
323 #       warn "filtres ".@filters[1];
324 #       warn "filtres ".@filters[2];
325 #       warn "filtres ".@filters[3];
326         
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/ ) ;
339
340         my @colfilter ;
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/ ) ;
353
354 # 1st, loop rows.
355         my $linefield;
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)";
362         }else {
363                 $linefield .= $line;
364         }
365         
366         
367         my $strsth;
368         $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
369         if ( @linefilter ) {
370                 if ($linefilter[1]){
371                         $strsth .= " and $line >= ? " ;
372                         $strsth .= " and $line <= ? " ;
373                 } elsif ($linefilter[0]) {
374                         $linefilter[0] =~ s/\*/%/g;
375                         $strsth .= " and $line LIKE ? " ;
376                 }
377         }
378         $strsth .=" order by $linefield";
379         warn "". $strsth;
380         
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]);
386         } else {
387                 $sth->execute;
388         }
389         while ( my ($celvalue) = $sth->fetchrow) {
390                 my %cell;
391                 if ($celvalue) {
392                         $cell{rowtitle} = $celvalue;
393 #               } else {
394 #                       $cell{rowtitle} = "";
395                 }
396                 $cell{totalrow} = 0;
397                 push @loopline, \%cell;
398         }
399
400 # 2nd, loop cols.
401         my $colfield;
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)";
408         }else {
409                 $colfield .= $column;
410         }
411         
412         my $strsth2;
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 ? ";
419         } 
420         $strsth2 .= " order by $colfield";
421         warn "". $strsth2;
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]);
427         } else {
428                 $sth2->execute;
429         }
430         while (my ($celvalue) = $sth2->fetchrow) {
431                 my %cell;
432                 my %ft;
433                 if ($celvalue) {
434                         $cell{coltitle} = $celvalue;
435 #               } else {
436 #                       $cell{coltitle} = "";
437                 }
438                 $ft{totalcol} = 0;
439                 push @loopcol, \%cell;
440         }
441         
442
443         my $i=0;
444         my @totalcol;
445         my $hilighted=-1;
446         
447         #Initialization of cell values.....
448         my %table;
449 #       warn "init table";
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;
454                 }
455                 $table{$row->{rowtitle}}->{totalrow}=0;
456         }
457
458 # preparing calculation
459         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
460         if (@$filters[0]){
461                 @$filters[0]=~ s/\*/%/g;
462                 $strcalc .= " AND dewey >" . @$filters[0] ."";
463         }
464         if (@$filters[1]){
465                 @$filters[1]=~ s/\*/%/g ;
466                 $strcalc .= " AND dewey <" . @$filters[1] ."";
467                 
468         }
469         if (@$filters[2]){
470                 @$filters[2]=~ s/\*/%/g ;
471                 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
472         }
473         if (@$filters[3]){
474                 @$filters[3]=~ s/\*/%/g;
475                 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
476         }
477         if (@$filters[4]){
478                 @$filters[4]=~ s/\*/%/g ;
479                 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
480         }
481         
482         if (@$filters[5]){
483                 @$filters[5]=~ s/\*/%/g;
484                 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
485         }
486         
487         if (@$filters[6]){
488                 @$filters[6]=~ s/\*/%/g;
489                 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
490         }
491         
492         if (@$filters[7]){
493                 @$filters[7]=~ s/\*/%/g;
494                 @$filters[7].="%" unless @$filters[7]=~/%/;
495                 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
496         }
497         if (@$filters[8]){
498                 @$filters[8]=~ s/\*/%/g;
499                 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
500         }
501         if (@$filters[9]){
502                 @$filters[9]=~ s/\*/%/g;
503                 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
504         }
505         if (@$filters[10]){
506                 @$filters[10]=~ s/\*/%/g;
507                 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
508         }
509         if (@$filters[11]){
510                 @$filters[11]=~ s/\*/%/g;
511                 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
512         }
513         
514         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
515         warn "". $strcalc;
516         my $dbcalc = $dbh->prepare($strcalc);
517         $dbcalc->execute;
518 #       warn "filling table";
519         
520         my $emptycol; 
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);
526                 
527                 $table{$row}->{$col}+=$value;
528                 $table{$row}->{totalrow}+=$value;
529                 $grantotal += $value;
530         }
531
532 #       my %cell = {rowtitle => 'zzROWEMPTY'};
533 #       push @loopline,\%cell;
534 #       undef %cell;
535 #       my %cell;
536 #       %cell = {coltitle => "zzEMPTY"};
537         push @loopcol,{coltitle => "NULL"} if ($emptycol);
538         
539         foreach my $row ( sort keys %table ) {
540                 my @loopcell;
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  } ;
546                 }
547                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
548                                                 'loopcell' => \@loopcell,
549                                                 'hilighted' => ($hilighted >0),
550                                                 'totalrow' => $table{$row}->{totalrow}
551                                         };
552                 $hilighted = -$hilighted;
553         }
554         
555 #       warn "footer processing";
556         foreach my $col ( @loopcol ) {
557                 my $total=0;
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};
561                 }
562 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
563                 push @loopfooter, {'totalcol' => $total};
564         }
565                         
566
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;
578         return \@mainloop;
579 }
580
581 1;