Cleaning up perldocs for make utility that MJ wrote. I was getting a bunch
[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 use C4::Input;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =over 2
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         
125 my $branches = GetBranches;
126 my @branchloop;
127 foreach my $thisbranch (keys %$branches) {
128    # my $selected = 1 if $thisbranch eq $branch;
129     my %row =(value => $thisbranch,
130 #                selected => $selected,
131                 branchname => $branches->{$thisbranch}->{'branchname'},
132             );
133     push @branchloop, \%row;
134 }
135         $req = $dbh->prepare( "select distinctrow zipcode from borrowers order by zipcode");
136         $req->execute;
137         my @select_zipcode;
138         push @select_zipcode,"";
139         while (my ($value) =$req->fetchrow) {
140                 if ($value) {
141                         push @select_zipcode, $value;
142                 }
143         }
144
145         my $CGIZipCode=CGI::scrolling_list( -name     => 'Filter',
146                                 -id => 'Filter',
147                                 -values   => \@select_zipcode,
148                                 -size     => 1,
149                                 -multiple => 0 );
150
151         $req = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' order by lib");
152         $req->execute;
153         my @select_sort1;
154         my %select_sort1;
155         push @select_sort1,"";
156         $select_sort1{""}="";
157         my $hassort1;
158         while (my ($auth_value,$lib) =$req->fetchrow) {
159                 if ($auth_value) {
160                         $hassort1=1;
161                         push @select_sort1, $auth_value;
162                         $select_sort1{$auth_value}=$lib
163                 }
164         }
165
166         my $CGIsort1=CGI::scrolling_list( -name     => 'Filter',
167                                 -id => 'Filter',
168                                 -values   => \@select_sort1,
169                                 -labels =>\%select_sort1,
170                                 -size     => 1,
171                                 -multiple => 0 );
172         
173         $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
174         $req->execute;
175         my @select_sort2;
176         push @select_sort2,"";
177         my $hassort2;
178         while (my ($value) =$req->fetchrow) {
179                 if ($value) {
180                         $hassort2 = 1;
181                         push @select_sort2, $value;
182                 }
183         }
184         my $CGIsort2=CGI::scrolling_list( -name     => 'Filter',
185                                 -id => 'Filter',
186                                 -values   => \@select_sort2,
187                                 -size     => 1,
188                                 -multiple => 0 );
189         
190         my @mime = ( C4::Context->preference("MIME") );
191         foreach my $mime (@mime){
192                 warn "".$mime;
193         }
194         
195         my $CGIextChoice=CGI::scrolling_list(
196                                 -name => 'MIME',
197                                 -id => 'MIME',
198                                 -values   => \@mime,
199                                 -size     => 1,
200                                 -multiple => 0 );
201         
202         my @dels = ( C4::Context->preference("delimiter") );
203         my $CGIsepChoice=CGI::scrolling_list(
204                                 -name => 'sep',
205                                 -id => 'sep',
206                                 -values   => \@dels,
207                                 -size     => 1,
208                                 -multiple => 0 );
209
210         $template->param(               CGICatCode => $CGICatCode,
211                                         CGIZipCode => $CGIZipCode,
212                                         CGISort1 => $CGIsort1,
213                                         hassort1 => $hassort1,
214                                         CGISort2 => $CGIsort2,
215                                         hassort2 => $hassort2,
216                                         CGIextChoice => $CGIextChoice,
217                                         CGIsepChoice => $CGIsepChoice,
218                                         CGIBranch => @branchloop
219                                         );
220
221 }
222 output_html_with_http_headers $input, $cookie, $template->output;
223
224
225
226 sub calculate {
227         my ($line, $column, $digits, $status, $filters) = @_;
228         my @mainloop;
229         my @loopfooter;
230         my @loopcol;
231         my @loopline;
232         my @looprow;
233         my %globalline;
234         my $grantotal =0;
235 # extract parameters
236         my $dbh = C4::Context->dbh;
237
238 # Filters
239         my $linefilter = "";
240 #       warn "filtres ".@filters[0];
241 #       warn "filtres ".@filters[1];
242 #       warn "filtres ".@filters[2];
243 #       warn "filtres ".@filters[3];
244         
245         $linefilter = @$filters[0] if ($line =~ /categorycode/ )  ;
246         $linefilter = @$filters[1] if ($line =~ /zipcode/ )  ;
247         $linefilter = @$filters[2] if ($line =~ /branccode/ ) ;
248         $linefilter = @$filters[3] if ($line =~ /sort1/ ) ;
249         $linefilter = @$filters[4] if ($line =~ /sort2/ ) ;
250
251         my $colfilter = "";
252         $colfilter = @$filters[0] if ($column =~ /categorycode/);
253         $colfilter = @$filters[1] if ($column =~ /zipcode/);
254         $colfilter = @$filters[2] if ($column =~ /branchcode/);
255         $colfilter = @$filters[3] if ($column =~ /sort1/);
256         $colfilter = @$filters[4] if ($column =~ /sort2/);
257
258         my @loopfilter;
259         for (my $i=0;$i<=3;$i++) {
260                 my %cell;
261                 if ( @$filters[$i] ) {
262                         $cell{filter} .= @$filters[$i];
263                         $cell{crit} .="Cat Code " if ($i==0);
264                         $cell{crit} .="Zip Code" if ($i==1);
265                         $cell{crit} .="Branchcode" if ($i==2);
266                         $cell{crit} .="Sort1" if ($i==3);
267                         $cell{crit} .="Sort2" if ($i==4);
268                         push @loopfilter, \%cell;
269                 }
270         }
271         if ($status) {
272                 push @loopfilter,{crit=>"Status",filter=>$status}
273         }
274 # 1st, loop rows.
275         my $linefield;
276         if (($line =~/zipcode/) and ($digits)) {
277                 $linefield .="left($line,$digits)";
278         } else{
279                 $linefield .= $line;
280         }
281         
282         my $strsth;
283         $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
284         $linefilter =~ s/\*/%/g;
285         if ( $linefilter ) {
286                 $strsth .= " and $linefield LIKE ? " ;
287         }
288         $strsth .= " and $status='1' " if ($status);
289         $strsth .=" order by $linefield";
290 #       warn "". $strsth;
291         
292         my $sth = $dbh->prepare( $strsth );
293         if ( $linefilter ) {
294                 $sth->execute($linefilter);
295         } else {
296                 $sth->execute;
297         }
298         while ( my ($celvalue) = $sth->fetchrow) {
299                 my %cell;
300                 if ($celvalue) {
301                         $cell{rowtitle} = $celvalue;
302 #               } else {
303 #                       $cell{rowtitle} = "";
304                 }
305                 $cell{totalrow} = 0;
306                 push @loopline, \%cell;
307         }
308
309 # 2nd, loop cols.
310         my $colfield;
311         if (($column =~/zipcode/) and ($digits)) {
312                 $colfield .= "left($column,$digits)";
313         } else{
314                 $colfield .= $column;
315         }
316         my $strsth2;
317         $colfilter =~ s/\*/%/g;
318         $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
319         if ( $colfilter ) {
320                 $strsth2 .= " and $colfield LIKE ? ";
321         }
322         $strsth2 .= " and $status='1' " if ($status);
323         $strsth2 .= " order by $colfield";
324 #       warn "". $strsth2;
325         my $sth2 = $dbh->prepare( $strsth2 );
326         if ($colfilter) {
327                 $sth2->execute($colfilter);
328         } else {
329                 $sth2->execute;
330         }
331         while (my ($celvalue) = $sth2->fetchrow) {
332                 my %cell;
333                 my %ft;
334                 if ($celvalue) {
335                         $cell{coltitle} = $celvalue;
336                 }
337                 push @loopcol, \%cell;
338         }
339         
340
341         my $i=0;
342         my @totalcol;
343         my $hilighted=-1;
344         
345         #Initialization of cell values.....
346         my %table;
347 #       warn "init table";
348         foreach my $row ( @loopline ) {
349                 foreach my $col ( @loopcol ) {
350 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
351                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
352                 }
353                 $table{$row->{rowtitle}}->{totalrow}=0;
354         }
355
356 # preparing calculation
357         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
358         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
359         $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
360         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
361         $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
362         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
363         $strcalc .= " AND sort1 like '" . @$filters[2] ."'" if ( @$filters[2] );
364         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
365         $strcalc .= " AND sort2 like '" . @$filters[3] ."'" if ( @$filters[3] );
366         $strcalc .= " AND $status='1' " if ($status);
367         $strcalc .= " group by $linefield, $colfield";
368 #       warn "". $strcalc;
369         my $dbcalc = $dbh->prepare($strcalc);
370         $dbcalc->execute;
371 #       warn "filling table";
372         
373         my $emptycol; 
374         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
375 #               warn "filling table $row / $col / $value ";
376                 $emptycol = 1 if ($col eq undef);
377                 $col = "zzEMPTY" if ($col eq undef);
378                 $row = "zzEMPTY" if ($row eq undef);
379                 
380                 $table{$row}->{$col}+=$value;
381                 $table{$row}->{totalrow}+=$value;
382                 $grantotal += $value;
383         }
384         
385         push @loopcol,{coltitle => "NULL"} if ($emptycol);
386         
387         foreach my $row ( sort keys %table ) {
388                 my @loopcell;
389                 #@loopcol ensures the order for columns is common with column titles
390                 # and the number matches the number of columns
391                 foreach my $col ( @loopcol ) {
392                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
393                         push @loopcell, {value => $value  } ;
394                 }
395                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
396                                                 'loopcell' => \@loopcell,
397                                                 'hilighted' => ($hilighted >0),
398                                                 'totalrow' => $table{$row}->{totalrow}
399                                         };
400                 $hilighted = -$hilighted;
401         }
402         
403         foreach my $col ( @loopcol ) {
404                 my $total=0;
405                 foreach my $row ( @looprow ) {
406                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
407 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
408                 }
409 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
410                 push @loopfooter, {'totalcol' => $total};
411         }
412                         
413
414         # the header of the table
415         $globalline{loopfilter}=\@loopfilter;
416         # the core of the table
417         $globalline{looprow} = \@looprow;
418         $globalline{loopcol} = \@loopcol;
419 #       # the foot (totals by borrower type)
420         $globalline{loopfooter} = \@loopfooter;
421         $globalline{total}= $grantotal;
422         $globalline{line} = $line;
423         $globalline{column} = $column;
424         push @mainloop,\%globalline;
425         return \@mainloop;
426 }
427