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