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