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