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