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