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