tidying lists
[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 $req;
106         $req = $dbh->prepare("select distinctrow left(dewey,3) from biblioitems order by dewey");
107         $req->execute;
108         my $hasdewey=1;
109         my @select;
110         push @select,"";
111         while (my ($value) =$req->fetchrow) {
112                 $hasdewey =1 if (($value) and (! $hasdewey));
113                 push @select, $value;
114         }
115         my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
116                                 -id => 'Filter',
117                                 -values   => \@select,
118                                 -size     => 1,
119                                 -multiple => 0 );
120         
121         $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems order by lccn");
122         $req->execute;
123         undef @select;
124         push @select,"";
125         my $haslccn=1;
126         my $hlghtlccn;
127         while (my ($value) =$req->fetchrow) {
128                 $hlghtlccn = !($hasdewey);
129                 $haslccn =1 if (($value) and (! $haslccn));
130                 push @select, $value;
131         }
132         my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
133                                 -id => 'Filter',
134                                 -values   => \@select,
135                                 -size     => 1,
136                                 -multiple => 0 );
137         
138         $req = $dbh->prepare("select distinctrow left(itemcallnumber,5) from items order by itemcallnumber");
139         $req->execute;
140         undef @select;
141         push @select,"";
142         my $hascote=1;
143         my $hlghtcote;
144         while (my ($value) =$req->fetchrow) {
145                 $hascote =1 if (($value) and (! $hascote));
146                 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
147                 push @select, $value;
148         }
149         my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
150                                 -id => 'Filter',
151                                 -values   => \@select,
152                                 -size     => 1,
153                                 -multiple => 0 );
154         
155         undef @select;
156         push @select,"";
157         for (my $i=1950;$i<=2050;$i++) {
158                 push @select, $i;
159         }
160         my $CGIpublicationyear=CGI::scrolling_list( -name     => 'Filter',
161                                 -id => 'Filter',
162                                 -values   => \@select,
163                                 -size     => 1,
164                                 -multiple => 0 );
165         
166         $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
167         $req->execute;
168         undef @select;
169         push @select,"";
170         while (my ($value) =$req->fetchrow) {
171                 push @select, $value;
172         }
173         my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
174                                 -id => 'Filter',
175                                 -values   => \@select,
176                                 -size     => 1,
177                                 -multiple => 0 );
178         
179         $req = $dbh->prepare("select distinctrow publishercode from biblioitems order by publishercode");
180         $req->execute;
181         undef @select;
182         push @select,"";
183         while (my ($value) =$req->fetchrow) {
184                 push @select, $value;
185         }
186         my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
187                                 -id => 'Filter',
188                                 -values   => \@select,
189                                 -size     => 1,
190                                 -multiple => 0 );
191
192         undef @select;
193         push @select,"";
194         my $branches=getbranches();
195         my %select_branches;
196         $select_branches{""} = "";
197         foreach my $branch (keys %$branches) {
198                 push @select, $branch;
199                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
200         }
201         my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
202                                 -id => 'Filter',
203                                 -values   => \@select,
204                                 -labels   => \%select_branches,
205                                 -size     => 1,
206                                 -multiple => 0 );
207         
208         $req = $dbh->prepare("select distinctrow location from items order by location");
209         $req->execute;
210         undef @select;
211         push @select,"";
212         my $CGIlocation=CGI::scrolling_list( -name     => 'Filter',
213                                 -id => 'Filter',
214                                 -values   => \@select,
215                                 -size     => 1,
216                                 -multiple => 0 );
217         
218         my @mime = ( C4::Context->preference("MIME") );
219         foreach my $mime (@mime){
220                 warn "".$mime;
221         }
222         
223         my $CGIextChoice=CGI::scrolling_list(
224                                 -name => 'MIME',
225                                 -id => 'MIME',
226                                 -values   => \@mime,
227                                 -size     => 1,
228                                 -multiple => 0 );
229         
230         my @dels = ( C4::Context->preference("delimiter") );
231         my $CGIsepChoice=CGI::scrolling_list(
232                                 -name => 'sep',
233                                 -id => 'sep',
234                                 -values   => \@dels,
235                                 -size     => 1,
236                                 -multiple => 0 );
237         
238         $template->param(hasdewey=>$hasdewey,
239                                         CGIFromDeweyClass => $CGIdewey,
240                                         CGIToDeweyClass => $CGIdewey,
241                                         haslccn=> $haslccn,
242                                         hlghtlccn => $hlghtlccn,
243                                         CGIFromLoCClass => $CGIlccn,
244                                         CGIToLoCClass => $CGIlccn,
245                                         hascote=> $hascote,
246                                         hlghtcote => $hlghtcote,
247                                         CGIFromCoteClass => $CGIcote,
248                                         CGIToCoteClass => $CGIcote,
249                                         CGIItemType => $CGIitemtype,
250                                         CGIFromPublicationYear => $CGIpublicationyear,
251                                         CGIToPublicationYear => $CGIpublicationyear,
252                                         CGIPublisher => $CGIpublisher,
253                                         CGIBranch => $CGIbranch,
254                                         CGILocation => $CGIlocation,
255                                         CGIextChoice => $CGIextChoice,
256                                         CGIsepChoice => $CGIsepChoice
257                                         );
258
259 }
260 output_html_with_http_headers $input, $cookie, $template->output;
261
262
263
264 sub calculate {
265         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
266         my @mainloop;
267         my @loopfooter;
268         my @loopcol;
269         my @loopline;
270         my @looprow;
271         my %globalline;
272         my $grantotal =0;
273 # extract parameters
274         my $dbh = C4::Context->dbh;
275
276 # Filters
277 # Checking filters
278 #
279         my @loopfilter;
280         for (my $i=0;$i<=11;$i++) {
281                 my %cell;
282                 if ( @$filters[$i] ) {
283                         if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
284                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
285                         }
286                         $cell{filter} .= @$filters[$i];
287                         $cell{crit} .="Dewey Classification From" if ($i==0);
288                         $cell{crit} .="Dewey Classification To" if ($i==1);
289                         $cell{crit} .="Lccn Classification From" if ($i==2);
290                         $cell{crit} .="Lccn Classification To" if ($i==3);
291                         $cell{crit} .="Cote Classification From" if ($i==4);
292                         $cell{crit} .="Cote Classification To" if ($i==5);
293                         $cell{crit} .="Document type" if ($i==6);
294                         $cell{crit} .="Publisher" if ($i==7);
295                         $cell{crit} .="Publication year From" if ($i==8);
296                         $cell{crit} .="Publication year To" if ($i==9);
297                         $cell{crit} .="Branch :" if ($i==10);
298                         $cell{crit} .="Location:" if ($i==11);
299                         push @loopfilter, \%cell;
300                 }
301         }
302         
303         my @linefilter;
304 #       warn "filtres ".@filters[0];
305 #       warn "filtres ".@filters[1];
306 #       warn "filtres ".@filters[2];
307 #       warn "filtres ".@filters[3];
308         
309         $linefilter[0] = @$filters[0] if ($line =~ /dewey/ )  ;
310         $linefilter[1] = @$filters[1] if ($line =~ /dewey/ )  ;
311         $linefilter[0] = @$filters[2] if ($line =~ /lccn/ )  ;
312         $linefilter[1] = @$filters[3] if ($line =~ /lccn/ )  ;
313         $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ )  ;
314         $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ )  ;
315         @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
316         @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
317         $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
318         $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
319         @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
320         @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
321
322         my @colfilter ;
323         $colfilter[0] = @$filters[0] if ($column =~ /dewey/ )  ;
324         $colfilter[1] = @$filters[1] if ($column =~ /dewey/ )  ;
325         $colfilter[0] = @$filters[2] if ($column =~ /lccn/ )  ;
326         $colfilter[1] = @$filters[3] if ($column =~ /lccn/ )  ;
327         $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ )  ;
328         $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ )  ;
329         @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
330         @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
331         $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
332         $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
333         @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
334         @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
335
336 # 1st, loop rows.
337         my $linefield;
338         if (($line =~/dewey/)  and ($deweydigits)) {
339                 $linefield .="left($line,$deweydigits)";
340         } elsif (($line=~/lccn/) and ($lccndigits)) {
341                 $linefield .="left($line,$lccndigits)";
342         } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
343                 $linefield .="left($line,$cotedigits)";
344         }else {
345                 $linefield .= $line;
346         }
347         
348         
349         my $strsth;
350         $strsth .= "select distinctrow $linefield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $line is not null ";
351         if ( @linefilter ) {
352                 if ($linefilter[1]){
353                         $strsth .= " and $line >= ? " ;
354                         $strsth .= " and $line <= ? " ;
355                 } elsif ($linefilter[0]) {
356                         $linefilter[0] =~ s/\*/%/g;
357                         $strsth .= " and $line LIKE ? " ;
358                 }
359         }
360         $strsth .=" order by $linefield";
361         warn "". $strsth;
362         
363         my $sth = $dbh->prepare( $strsth );
364         if (( @linefilter ) and ($linefilter[1])){
365                 $sth->execute($linefilter[0],$linefilter[1]);
366         } elsif ($linefilter[0]) {
367                 $sth->execute($linefilter[0]);
368         } else {
369                 $sth->execute;
370         }
371         while ( my ($celvalue) = $sth->fetchrow) {
372                 my %cell;
373                 if ($celvalue) {
374                         $cell{rowtitle} = $celvalue;
375                 } else {
376                         $cell{rowtitle} = "";
377                 }
378                 $cell{totalrow} = 0;
379                 push @loopline, \%cell;
380         }
381
382 # 2nd, loop cols.
383         my $colfield;
384         if (($column =~/dewey/)  and ($deweydigits)) {
385                 $colfield .="left($column,$deweydigits)";
386         }elsif (($column=~/lccn/) and ($lccndigits)) {
387                 $colfield .="left($column,$lccndigits)";
388         }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
389                 $colfield .="left($column,$cotedigits)";
390         }else {
391                 $colfield .= $column;
392         }
393         
394         my $strsth2;
395         $strsth2 .= "select distinctrow $colfield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $column is not null ";
396         if (( @colfilter ) and ($colfilter[1])) {
397                 $strsth2 .= " and $column> ? and $column< ?";
398         }elsif ($colfilter[0]){
399                 $colfilter[0] =~ s/\*/%/g;
400                 $strsth2 .= " and $column LIKE ? ";
401         } 
402         $strsth2 .= " order by $colfield";
403         warn "". $strsth2;
404         my $sth2 = $dbh->prepare( $strsth2 );
405         if ((@colfilter) and ($colfilter[1])) {
406                 $sth2->execute($colfilter[0],$colfilter[1]);
407         } elsif ($colfilter[0]){
408                 $sth2->execute($colfilter[0]);
409         } else {
410                 $sth2->execute;
411         }
412         while (my ($celvalue) = $sth2->fetchrow) {
413                 my %cell;
414                 my %ft;
415                 $cell{coltitle} = $celvalue;
416                 $ft{totalcol} = 0;
417                 push @loopcol, \%cell;
418         }
419         
420
421         my $i=0;
422         my @totalcol;
423         my $hilighted=-1;
424         
425         #Initialization of cell values.....
426         my %table;
427 #       warn "init table";
428         foreach my $row ( @loopline ) {
429                 foreach my $col ( @loopcol ) {
430 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
431                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
432                 }
433                 $table{$row->{rowtitle}}->{totalrow}=0;
434         }
435
436 # preparing calculation
437         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems, items WHERE (items.biblioitemnumber = biblioitems.biblioitemnumber) AND $line is not null AND $column is not null";
438         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
439         $strcalc .= " AND dewey >" . @$filters[0] ."" if ( @$filters[0] );
440         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
441         $strcalc .= " AND dewey <" . @$filters[1] ."" if ( @$filters[1] );
442         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
443         $strcalc .= " AND lccn >" . @$filters[2] ."" if ( @$filters[2] );
444         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
445         $strcalc .= " AND lccn <" . @$filters[3] ."" if ( @$filters[3] );
446         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
447         $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" if ( @$filters[4] );
448         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
449         $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" if ( @$filters[5] );
450         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
451         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'" if ( @$filters[6] );
452         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
453         $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'" if ( @$filters[7] );
454         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
455         $strcalc .= " AND publicationyear >" . @$filters[8] ."" if ( @$filters[8] );
456         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
457         $strcalc .= " AND publicationyear <" . @$filters[9] ."" if ( @$filters[9] );
458         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
459         $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'" if ( @$filters[10] );
460         @$filters[11]=~ s/\*/%/g if (@$filters[11]);
461         $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
462         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
463         warn "". $strcalc;
464         my $dbcalc = $dbh->prepare($strcalc);
465         $dbcalc->execute;
466         warn "filling table";
467         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
468                 warn "filling table $row / $col / $value ";
469                 $table{$row}->{$col}=$value;
470                 $table{$row}->{totalrow}+=$value;
471                 $grantotal += $value;
472         }
473         
474         foreach my $row ( sort keys %table ) {
475                 my @loopcell;
476                 #@loopcol ensures the order for columns is common with column titles
477                 foreach my $col ( @loopcol ) {
478                         push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
479                 }
480                 push @looprow,{ 'rowtitle' => $row,
481                                                 'loopcell' => \@loopcell,
482                                                 'hilighted' => 1 ,
483                                                 'totalrow' => $table{$row}->{totalrow}
484                                         };
485                 $hilighted = -$hilighted;
486         }
487         
488         warn "footer processing";
489         foreach my $col ( @loopcol ) {
490                 my $total=0;
491                 foreach my $row ( @looprow ) {
492                         $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
493                         warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
494                 }
495                 warn "summ for column ".$col->{coltitle}."  = ".$total;
496                 push @loopfooter, {'totalcol' => $total};
497         }
498                         
499
500         # the header of the table
501         $globalline{loopfilter}=\@loopfilter;
502         # the core of the table
503         $globalline{looprow} = \@looprow;
504         $globalline{loopcol} = \@loopcol;
505 #       # the foot (totals by borrower type)
506         $globalline{loopfooter} = \@loopfooter;
507         $globalline{total}= $grantotal;
508         $globalline{line} = $line;
509         $globalline{column} = $column;
510         push @mainloop,\%globalline;
511         return \@mainloop;
512 }
513
514 1;