patch to hide empty fields
[koha.git] / reports / borrowers_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/borrowers_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $digits = $input->param("digits");
51 my $borstat = $input->param("status");
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 => {editcatalogue => 1},
63                                 debug => 1,
64                                 });
65 $template->param(do_it => $do_it);
66 if ($do_it) {
67         my $results = calculate($line, $column, $digits, $borstat, \@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', -name=>"$basename.csv" );
74                 my $cols = @$results[0]->{loopcol};
75                 my $lines = @$results[0]->{looprow};
76                 my $sep;
77                 $sep =C4::Context->preference("delimiter");
78                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
79                 foreach my $col ( @$cols ) {
80                         print $col->{coltitle}.$sep;
81                 }
82                 print "\n";
83                 foreach my $line ( @$lines ) {
84                         my $x = $line->{loopcell};
85                         print $line->{rowtitle}.$sep;
86                         foreach my $cell (@$x) {
87                                 print $cell->{value}.$sep;
88                         }
89                         print $line->{totalrow};
90                         print "\n";
91                 }
92                 print "TOTAL";
93                 $cols = @$results[0]->{loopfooter};
94                 foreach my $col ( @$cols ) {
95                         print $sep.$col->{totalcol};
96                 }
97                 print $sep.@$results[0]->{total};
98                 exit(1);
99         }
100 } else {
101         my $dbh = C4::Context->dbh;
102         my @values;
103         my %labels;
104         my $req;
105         $req = $dbh->prepare( "select categorycode, description from categories");
106         $req->execute;
107         my %select_catcode;
108         my @select_catcode;
109         push @select_catcode,"";
110         $select_catcode{""} = "";
111         while (my ($catcode, $description) =$req->fetchrow) {
112                 push @select_catcode, $catcode;
113                 $select_catcode{$catcode} = $description
114         }
115         my $CGICatCode=CGI::scrolling_list( -name     => 'Filter',
116                                 -id => 'Filter',
117                                 -values   => \@select_catcode,
118                                 -labels   => \%select_catcode,
119                                 -size     => 1,
120                                 -multiple => 0 );
121         
122         $req = $dbh->prepare( "select distinctrow sort1 from borrowers");
123         $req->execute;
124         my @select_sort1;
125         push @select_sort1,"";
126         my $hassort1;
127         while (my ($value) =$req->fetchrow) {
128                 if ($value) {
129                         $hassort1=1;
130                         push @select_sort1, $value;
131                 }
132         }
133         my $CGIsort1=CGI::scrolling_list( -name     => 'Filter',
134                                 -id => 'Filter',
135                                 -values   => \@select_sort1,
136                                 -size     => 1,
137                                 -multiple => 0 );
138         
139         $req = $dbh->prepare( "select distinctrow sort2 from borrowers");
140         $req->execute;
141         my @select_sort2;
142         push @select_sort2,"";
143         while (my ($value) =$req->fetchrow) {
144                 push @select_sort2, $value;
145         }
146         my $CGIsort2=CGI::scrolling_list( -name     => 'Filter',
147                                 -id => 'Filter',
148                                 -values   => \@select_sort2,
149                                 -size     => 1,
150                                 -multiple => 0 );
151         
152         my @mime = ( C4::Context->preference("MIME") );
153         foreach my $mime (@mime){
154                 warn "".$mime;
155         }
156         
157         my $CGIextChoice=CGI::scrolling_list(
158                                 -name => 'MIME',
159                                 -id => 'MIME',
160                                 -values   => \@mime,
161                                 -size     => 1,
162                                 -multiple => 0 );
163         
164         my @dels = ( C4::Context->preference("delimiter") );
165         my $CGIsepChoice=CGI::scrolling_list(
166                                 -name => 'sep',
167                                 -id => 'sep',
168                                 -values   => \@dels,
169                                 -size     => 1,
170                                 -multiple => 0 );
171         $template->param(CGICatcode => $CGICatCode,
172                                         CGISort1 => $CGIsort1,
173                                         hassort1 => $hassort1,
174                                         CGISort2 => $CGIsort2,
175                                         CGIextChoice => $CGIextChoice,
176                                         CGIsepChoice => $CGIsepChoice
177                                         );
178
179 }
180 output_html_with_http_headers $input, $cookie, $template->output;
181
182
183
184 sub calculate {
185         my ($line, $column, $digits, $status, $filters) = @_;
186         my @mainloop;
187         my @loopfooter;
188         my @loopcol;
189         my @loopline;
190         my @looprow;
191         my %globalline;
192         my $grantotal =0;
193 # extract parameters
194         my $dbh = C4::Context->dbh;
195
196 # Filters
197         my $linefilter = "";
198 #       warn "filtres ".@filters[0];
199 #       warn "filtres ".@filters[1];
200 #       warn "filtres ".@filters[2];
201 #       warn "filtres ".@filters[3];
202         
203         $linefilter = @$filters[0] if ($line =~ /categorycode/ )  ;
204         $linefilter = @$filters[1] if ($line =~ /zipcode/ )  ;
205         $linefilter = @$filters[2] if ($line =~ /sort1/ ) ;
206         $linefilter = @$filters[3] if ($line =~ /sort2/ ) ;
207
208         my $colfilter = "";
209         $colfilter = @$filters[0] if ($column =~ /categorycode/);
210         $colfilter = @$filters[1] if ($column =~ /zipcode/);
211         $colfilter = @$filters[2] if ($column =~ /sort1/);
212         $colfilter = @$filters[3] if ($column =~ /sort2/);
213
214         my @loopfilter;
215         for (my $i=0;$i<=3;$i++) {
216                 my %cell;
217                 if ( @$filters[$i] ) {
218                         $cell{filter} .= @$filters[$i];
219                         $cell{crit} .="Cat Code " if ($i==0);
220                         $cell{crit} .="Zip Code" if ($i==1);
221                         $cell{crit} .="Sort1" if ($i==2);
222                         $cell{crit} .="Sort2" if ($i==3);
223                         push @loopfilter, \%cell;
224                 }
225         }
226         if ($status) {
227                 push @loopfilter,{crit=>"Status",filter=>$status}
228         }
229 # 1st, loop rows.
230         my $linefield;
231         if (($line =~/zipcode/) and ($digits)) {
232                 $linefield .="left($line,$digits)";
233         } else{
234                 $linefield .= $line;
235         }
236         
237         my $strsth;
238         $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
239         $linefilter =~ s/\*/%/g;
240         if ( $linefilter ) {
241                 $strsth .= " and $linefield LIKE ? " ;
242         }
243         $strsth .= " and $status='1' " if ($status);
244         $strsth .=" order by $linefield";
245 #       warn "". $strsth;
246         
247         my $sth = $dbh->prepare( $strsth );
248         if ( $linefilter ) {
249                 $sth->execute($linefilter);
250         } else {
251                 $sth->execute;
252         }
253         while ( my ($celvalue) = $sth->fetchrow) {
254                 my %cell;
255                 if ($celvalue) {
256                         $cell{rowtitle} = $celvalue;
257                 } else {
258                         $cell{rowtitle} = "";
259                 }
260                 $cell{totalrow} = 0;
261                 push @loopline, \%cell;
262         }
263
264 # 2nd, loop cols.
265         my $colfield;
266         if (($column =~/zipcode/) and ($digits)) {
267                 $colfield .= "left($column,$digits)";
268         } else{
269                 $colfield .= $column;
270         }
271         my $strsth2;
272         $colfilter =~ s/\*/%/g;
273         $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
274         if ( $colfilter ) {
275                 $strsth2 .= " and $colfield LIKE ? ";
276         } 
277         $strsth2 .= " and $status='1' " if ($status);
278         $strsth2 .= " order by $colfield";
279 #       warn "". $strsth2;
280         my $sth2 = $dbh->prepare( $strsth2 );
281         if ($colfilter) {
282                 $sth2->execute($colfilter);
283         } else {
284                 $sth2->execute;
285         }
286         while (my ($celvalue) = $sth2->fetchrow) {
287                 my %cell;
288                 my %ft;
289                 $cell{coltitle} = $celvalue;
290                 $ft{totalcol} = 0;
291                 push @loopcol, \%cell;
292         }
293         
294
295         my $i=0;
296         my @totalcol;
297         my $hilighted=-1;
298         
299         #Initialization of cell values.....
300         my %table;
301 #       warn "init table";
302         foreach my $row ( @loopline ) {
303                 foreach my $col ( @loopcol ) {
304 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
305                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
306                 }
307                 $table{$row->{rowtitle}}->{totalrow}=0;
308         }
309
310 # preparing calculation
311         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE $line is not null AND $column is not null";
312         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
313         $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
314         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
315         $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
316         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
317         $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
318         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
319         $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
320         $strcalc .= " AND $status='1' " if ($status);
321         $strcalc .= " group by $linefield, $colfield";
322 #       warn "". $strcalc;
323         my $dbcalc = $dbh->prepare($strcalc);
324         $dbcalc->execute;
325 #       warn "filling table";
326         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
327 #               warn "filling table $row / $col / $value ";
328                 $table{$row}->{$col}=$value;
329                 $table{$row}->{totalrow}+=$value;
330                 $grantotal += $value;
331         }
332         
333         foreach my $row ( keys %table ) {
334                 my @loopcell;
335                 #@loopcol ensures the order for columns is common with column titles
336                 foreach my $col ( @loopcol ) {
337                         push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
338                 }
339                 push @looprow,{ 'rowtitle' => $row,
340                                                 'loopcell' => \@loopcell,
341                                                 'hilighted' => 1 ,
342                                                 'totalrow' => $table{$row}->{totalrow}
343                                         };
344                 $hilighted = -$hilighted;
345         }
346         
347         foreach my $col ( @loopcol ) {
348                 my $total=0;
349                 foreach my $row ( @loopline ) {
350                         $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
351                 }
352                 push @loopfooter, {'totalcol' => $total};
353         }
354                         
355
356         # the header of the table
357         $globalline{loopfilter}=\@loopfilter;
358         # the core of the table
359         $globalline{looprow} = \@looprow;
360         $globalline{loopcol} = \@loopcol;
361 #       # the foot (totals by borrower type)
362         $globalline{loopfooter} = \@loopfooter;
363         $globalline{total}= $grantotal;
364         $globalline{line} = $line;
365         $globalline{column} = $column;
366         push @mainloop,\%globalline;
367         return \@mainloop;
368 }
369
370 1;