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