removing a compilation warn.
[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 CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Koha;
28 use C4::Acquisition;
29 use C4::Output;
30 use C4::Circulation;
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38
39 =over2
40
41 =cut
42
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $digits = $input->param("digits");
50 my $borstat = $input->param("status");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55
56 my ($template, $borrowernumber, $cookie)
57         = get_template_and_user({template_name => $fullreportname,
58                                 query => $input,
59                                 type => "intranet",
60                                 authnotrequired => 0,
61                                 flagsrequired => {reports=> 1},
62                                 debug => 1,
63                                 });
64 $template->param(do_it => $do_it);
65 if ($do_it) {
66         my $results = calculate($line, $column, $digits, $borstat, \@filters);
67         if ($output eq "screen"){
68                 $template->param(mainloop => $results);
69                 output_html_with_http_headers $input, $cookie, $template->output;
70                 exit(1);
71         } else {
72                 print $input->header(-type => 'application/vnd.sun.xml.calc',
73                                      -encoding    => 'utf-8',
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 my $branches = GetBranches;
125 my @branchloop;
126 foreach my $thisbranch (keys %$branches) {
127    # my $selected = 1 if $thisbranch eq $branch;
128     my %row =(value => $thisbranch,
129 #                selected => $selected,
130                 branchname => $branches->{$thisbranch}->{'branchname'},
131             );
132     push @branchloop, \%row;
133 }
134         
135         $req = $dbh->prepare( "select distinctrow sort1 from borrowers order by sort1");
136         $req->execute;
137         my @select_sort1;
138         push @select_sort1,"";
139         my $hassort1;
140         while (my ($value) =$req->fetchrow) {
141                 if ($value) {
142                         $hassort1=1;
143                         push @select_sort1, $value;
144                 }
145         }
146         my $CGIsort1=CGI::scrolling_list( -name     => 'Filter',
147                                 -id => 'Filter',
148                                 -values   => \@select_sort1,
149                                 -size     => 1,
150                                 -multiple => 0 );
151         
152         $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
153         $req->execute;
154         my @select_sort2;
155         push @select_sort2,"";
156         my $hassort2;
157         while (my ($value) =$req->fetchrow) {
158                 if ($value) {
159                         $hassort2 = 1;
160                         push @select_sort2, $value;
161                 }
162         }
163         my $CGIsort2=CGI::scrolling_list( -name     => 'Filter',
164                                 -id => 'Filter',
165                                 -values   => \@select_sort2,
166                                 -size     => 1,
167                                 -multiple => 0 );
168         
169         my @mime = ( C4::Context->preference("MIME") );
170         foreach my $mime (@mime){
171                 warn "".$mime;
172         }
173         
174         my $CGIextChoice=CGI::scrolling_list(
175                                 -name => 'MIME',
176                                 -id => 'MIME',
177                                 -values   => \@mime,
178                                 -size     => 1,
179                                 -multiple => 0 );
180         
181         my @dels = ( C4::Context->preference("delimiter") );
182         my $CGIsepChoice=CGI::scrolling_list(
183                                 -name => 'sep',
184                                 -id => 'sep',
185                                 -values   => \@dels,
186                                 -size     => 1,
187                                 -multiple => 0 );
188         $template->param(CGICatcode => $CGICatCode,
189                                         CGISort1 => $CGIsort1,
190                                         hassort1 => $hassort1,
191                                         CGISort2 => $CGIsort2,
192                                         hassort2 => $hassort2,
193                                         CGIextChoice => $CGIextChoice,
194                                         CGIsepChoice => $CGIsepChoice,
195                                         CGIBranch => @branchloop
196                                         );
197
198 }
199 output_html_with_http_headers $input, $cookie, $template->output;
200
201
202
203 sub calculate {
204         my ($line, $column, $digits, $status, $filters) = @_;
205         my @mainloop;
206         my @loopfooter;
207         my @loopcol;
208         my @loopline;
209         my @looprow;
210         my %globalline;
211         my $grantotal =0;
212 # extract parameters
213         my $dbh = C4::Context->dbh;
214
215 # Filters
216         my $linefilter = "";
217 #       warn "filtres ".@filters[0];
218 #       warn "filtres ".@filters[1];
219 #       warn "filtres ".@filters[2];
220 #       warn "filtres ".@filters[3];
221         
222         $linefilter = @$filters[0] if ($line =~ /categorycode/ )  ;
223         $linefilter = @$filters[1] if ($line =~ /zipcode/ )  ;
224         $linefilter = @$filters[2] if ($line =~ /branccode/ ) ;
225         $linefilter = @$filters[3] if ($line =~ /sort1/ ) ;
226         $linefilter = @$filters[4] if ($line =~ /sort2/ ) ;
227
228         my $colfilter = "";
229         $colfilter = @$filters[0] if ($column =~ /categorycode/);
230         $colfilter = @$filters[1] if ($column =~ /zipcode/);
231         $colfilter = @$filters[2] if ($column =~ /branchcode/);
232         $colfilter = @$filters[3] if ($column =~ /sort1/);
233         $colfilter = @$filters[4] if ($column =~ /sort2/);
234
235         my @loopfilter;
236         for (my $i=0;$i<=3;$i++) {
237                 my %cell;
238                 if ( @$filters[$i] ) {
239                         $cell{filter} .= @$filters[$i];
240                         $cell{crit} .="Cat Code " if ($i==0);
241                         $cell{crit} .="Zip Code" if ($i==1);
242                         $cell{crit} .="Branchcode" if ($i==2);
243                         $cell{crit} .="Sort1" if ($i==3);
244                         $cell{crit} .="Sort2" if ($i==4);
245                         push @loopfilter, \%cell;
246                 }
247         }
248         if ($status) {
249                 push @loopfilter,{crit=>"Status",filter=>$status}
250         }
251 # 1st, loop rows.
252         my $linefield;
253         if (($line =~/zipcode/) and ($digits)) {
254                 $linefield .="left($line,$digits)";
255         } else{
256                 $linefield .= $line;
257         }
258         
259         my $strsth;
260         $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
261         $linefilter =~ s/\*/%/g;
262         if ( $linefilter ) {
263                 $strsth .= " and $linefield LIKE ? " ;
264         }
265         $strsth .= " and $status='1' " if ($status);
266         $strsth .=" order by $linefield";
267 #       warn "". $strsth;
268         
269         my $sth = $dbh->prepare( $strsth );
270         if ( $linefilter ) {
271                 $sth->execute($linefilter);
272         } else {
273                 $sth->execute;
274         }
275         while ( my ($celvalue) = $sth->fetchrow) {
276                 my %cell;
277                 if ($celvalue) {
278                         $cell{rowtitle} = $celvalue;
279 #               } else {
280 #                       $cell{rowtitle} = "";
281                 }
282                 $cell{totalrow} = 0;
283                 push @loopline, \%cell;
284         }
285
286 # 2nd, loop cols.
287         my $colfield;
288         if (($column =~/zipcode/) and ($digits)) {
289                 $colfield .= "left($column,$digits)";
290         } else{
291                 $colfield .= $column;
292         }
293         my $strsth2;
294         $colfilter =~ s/\*/%/g;
295         $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
296         if ( $colfilter ) {
297                 $strsth2 .= " and $colfield LIKE ? ";
298         }
299         $strsth2 .= " and $status='1' " if ($status);
300         $strsth2 .= " order by $colfield";
301 #       warn "". $strsth2;
302         my $sth2 = $dbh->prepare( $strsth2 );
303         if ($colfilter) {
304                 $sth2->execute($colfilter);
305         } else {
306                 $sth2->execute;
307         }
308         while (my ($celvalue) = $sth2->fetchrow) {
309                 my %cell;
310                 my %ft;
311                 if ($celvalue) {
312                         $cell{coltitle} = $celvalue;
313                 }
314                 push @loopcol, \%cell;
315         }
316         
317
318         my $i=0;
319         my @totalcol;
320         my $hilighted=-1;
321         
322         #Initialization of cell values.....
323         my %table;
324 #       warn "init table";
325         foreach my $row ( @loopline ) {
326                 foreach my $col ( @loopcol ) {
327 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
328                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
329                 }
330                 $table{$row->{rowtitle}}->{totalrow}=0;
331         }
332
333 # preparing calculation
334         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
335         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
336         $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
337         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
338         $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
339         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
340         $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
341         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
342         $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
343         $strcalc .= " AND $status='1' " if ($status);
344         $strcalc .= " group by $linefield, $colfield";
345 #       warn "". $strcalc;
346         my $dbcalc = $dbh->prepare($strcalc);
347         $dbcalc->execute;
348 #       warn "filling table";
349         
350         my $emptycol; 
351         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
352 #               warn "filling table $row / $col / $value ";
353                 $emptycol = 1 if ($col eq undef);
354                 $col = "zzEMPTY" if ($col eq undef);
355                 $row = "zzEMPTY" if ($row eq undef);
356                 
357                 $table{$row}->{$col}+=$value;
358                 $table{$row}->{totalrow}+=$value;
359                 $grantotal += $value;
360         }
361         
362         push @loopcol,{coltitle => "NULL"} if ($emptycol);
363         
364         foreach my $row ( sort keys %table ) {
365                 my @loopcell;
366                 #@loopcol ensures the order for columns is common with column titles
367                 # and the number matches the number of columns
368                 foreach my $col ( @loopcol ) {
369                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
370                         push @loopcell, {value => $value  } ;
371                 }
372                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
373                                                 'loopcell' => \@loopcell,
374                                                 'hilighted' => ($hilighted >0),
375                                                 'totalrow' => $table{$row}->{totalrow}
376                                         };
377                 $hilighted = -$hilighted;
378         }
379         
380         foreach my $col ( @loopcol ) {
381                 my $total=0;
382                 foreach my $row ( @looprow ) {
383                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
384 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
385                 }
386 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
387                 push @loopfooter, {'totalcol' => $total};
388         }
389                         
390
391         # the header of the table
392         $globalline{loopfilter}=\@loopfilter;
393         # the core of the table
394         $globalline{looprow} = \@looprow;
395         $globalline{loopcol} = \@loopcol;
396 #       # the foot (totals by borrower type)
397         $globalline{loopfooter} = \@loopfooter;
398         $globalline{total}= $grantotal;
399         $globalline{line} = $line;
400         $globalline{column} = $column;
401         push @mainloop,\%globalline;
402         return \@mainloop;
403 }
404