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