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