Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / reports / issues_stats.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI qw ( -utf8 );
24 use Date::Manip;
25
26 use C4::Auth;
27 use C4::Debug;
28 use C4::Context;
29 use C4::Branch; # GetBranches
30 use C4::Koha;
31 use C4::Output;
32 use C4::Circulation;
33 use C4::Reports;
34 use C4::Dates qw/format_date format_date_in_iso/;
35 use C4::Members;
36
37 =head1 NAME
38
39 reports/issues_stats.pl
40
41 =head1 DESCRIPTION
42
43 Plugin that shows circulation stats
44
45 =cut
46
47 # my $debug = 1;        # override for now.
48 my $input = new CGI;
49 my $fullreportname = "reports/issues_stats.tt";
50 my $do_it    = $input->param('do_it');
51 my $line     = $input->param("Line");
52 my $column   = $input->param("Column");
53 my @filters  = $input->param("Filter");
54 $filters[0]=format_date_in_iso($filters[0]);
55 $filters[1]=format_date_in_iso($filters[1]);
56 my $podsp    = $input->param("DisplayBy");
57 my $type     = $input->param("PeriodTypeSel");
58 my $daysel   = $input->param("PeriodDaySel");
59 my $monthsel = $input->param("PeriodMonthSel");
60 my $calc     = $input->param("Cellvalue");
61 my $output   = $input->param("output");
62 my $basename = $input->param("basename");
63 my ($template, $borrowernumber, $cookie) = get_template_and_user({
64         template_name => $fullreportname,
65         query => $input,
66         type => "intranet",
67         authnotrequired => 0,
68         flagsrequired => {reports => '*'},
69         debug => 0,
70 });
71 our $sep     = $input->param("sep");
72 $sep = "\t" if ($sep eq 'tabulation');
73 $template->param(do_it => $do_it,
74 );
75
76 my $itemtypes = GetItemTypes();
77 my $categoryloop = GetBorrowercategoryList;
78
79 my $ccodes    = GetKohaAuthorisedValues("items.ccode");
80 my $locations = GetKohaAuthorisedValues("items.location");
81
82 my $Bsort1 = GetAuthorisedValues("Bsort1");
83 my $Bsort2 = GetAuthorisedValues("Bsort2");
84 my ($hassort1,$hassort2);
85 $hassort1=1 if $Bsort1;
86 $hassort2=1 if $Bsort2;
87
88
89 if ($do_it) {
90 # Displaying results
91         my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
92         if ($output eq "screen"){
93 # Printing results to screen
94                 $template->param(mainloop => $results);
95                 output_html_with_http_headers $input, $cookie, $template->output;
96         } else {
97 # Printing to a csv file
98         print $input->header(-type => 'application/vnd.sun.xml.calc',
99                             -encoding    => 'utf-8',
100                             -attachment=>"$basename.csv",
101                             -filename=>"$basename.csv" );
102                 my $cols  = @$results[0]->{loopcol};
103                 my $lines = @$results[0]->{looprow};
104 # header top-right
105                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
106 # Other header
107                 foreach my $col ( @$cols ) {
108                         print $col->{coltitle}.$sep;
109                 }
110                 print "Total\n";
111 # Table
112                 foreach my $line ( @$lines ) {
113                         my $x = $line->{loopcell};
114                         print $line->{rowtitle}.$sep;
115                         print map {$_->{value}.$sep} @$x;
116                         print $line->{totalrow}, "\n";
117                 }
118 # footer
119         print "TOTAL";
120         $cols = @$results[0]->{loopfooter};
121                 print map {$sep.$_->{totalcol}} @$cols;
122         print $sep.@$results[0]->{total};
123         }
124         exit; # exit either way after $do_it
125 }
126
127 my $dbh = C4::Context->dbh;
128 my @values;
129 my %labels;
130 my %select;
131
132 # create itemtype arrayref for <select>.
133 my @itemtypeloop;
134 for my $itype ( sort {$itemtypes->{$a}->{description} cmp $itemtypes->{$b}->{description}} keys(%$itemtypes)) {
135         push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{description} } ;
136 }
137
138     # location list
139 my @locations;
140 foreach (sort keys %$locations) {
141         push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
142 }
143     
144 my @ccodes;
145 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
146         push @ccodes, { code => $_, description => $ccodes->{$_} };
147 }
148
149 my $CGIextChoice = ( 'CSV' ); # FIXME translation
150 my $CGIsepChoice=GetDelimiterChoices;
151  
152 $template->param(
153         categoryloop => $categoryloop,
154         itemtypeloop => \@itemtypeloop,
155         locationloop => \@locations,
156            ccodeloop => \@ccodes,
157           branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
158         hassort1=> $hassort1,
159         hassort2=> $hassort2,
160         Bsort1 => $Bsort1,
161         Bsort2 => $Bsort2,
162         CGIextChoice => $CGIextChoice,
163         CGIsepChoice => $CGIsepChoice,
164 );
165 output_html_with_http_headers $input, $cookie, $template->output;
166
167 sub calculate {
168         my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
169         my @loopfooter;
170         my @loopcol;
171         my @loopline;
172         my @looprow;
173         my %globalline;
174         my $grantotal =0;
175 # extract parameters
176         my $dbh = C4::Context->dbh;
177
178 # Filters
179 # Checking filters
180 #
181         my @loopfilter;
182         for (my $i=0;$i<=12;$i++) {
183                 my %cell;
184                 (@$filters[$i]) or next;
185         if (($i==1) and (@$filters[$i-1])) {
186             $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
187         }
188             # format the dates filters, otherwise just fill as is
189         if ($i>=2) {
190                 $cell{filter} = @$filters[$i];
191         } else {
192                 $cell{filter} = format_date(@$filters[$i]);
193                 }
194                 $cell{crit} = 
195             ( $i == 0 )  ? "Period From"
196           : ( $i == 1 )  ? "Period To"
197           : ( $i == 2 )  ? "Patron Category ="
198           : ( $i == 3 )  ? "Item Type ="
199           : ( $i == 4 )  ? "Library ="
200           : ( $i == 5 )  ? "Collection ="
201           : ( $i == 6 )  ? "Location ="
202           : ( $i == 7 )  ? "Item callnumber >="
203           : ( $i == 8 )  ? "Item callnumber <"
204           : ( $i == 9 )  ? "sort1 ="
205           : ( $i == 10 ) ? "sort2 ="
206           : ( $i == 11 ) ? "Home library ="
207           : ( $i == 12 )? "Holding library ="
208           :                "UNKNOWN FILTER ($i)";
209
210                 # FIXME - no translation mechanism !
211                 push @loopfilter, \%cell;
212     }
213         push @loopfilter,{crit=>"Event",       filter=>$type    };
214         push @loopfilter,{crit=>"Display by",  filter=>$dsp     } if ($dsp);
215         push @loopfilter,{crit=>"Select Day",  filter=>$daysel  } if ($daysel);
216         push @loopfilter,{crit=>"Select Month",filter=>$monthsel} if ($monthsel);
217
218         my @linefilter;
219         $debug and warn "filtres ". join "|", @filters;
220         my ($colsource, $linesource);
221         $linefilter[1] = @$filters[1] if ($line =~ /datetime/);
222         $linefilter[0] = 
223             ( $line =~ /datetime/ ) ? @$filters[0]
224       : ( $line =~ /category/ ) ? @$filters[2]
225       : ( $line =~ /itemtype/ ) ? @$filters[3]
226       : ( $line =~ /^branch/ )   ? @$filters[4]
227       : ( $line =~ /ccode/ )    ? @$filters[5]
228       : ( $line =~ /location/ ) ? @$filters[6]
229       : ( $line =~ /sort1/ )    ? @$filters[9]
230       : ( $line =~ /sort2/ )    ? @$filters[10]
231       : ( $line =~ /homebranch/)    ? @$filters[11]
232       : ( $line =~ /holdingbranch/) ? @$filters[12]
233       :                           undef;
234
235     if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) {
236                 $linesource = 'items';
237         }
238
239         my @colfilter;
240         $colfilter[1] = @$filters[1] if ($column =~ /datetime/);
241         $colfilter[0] = 
242         ( $column =~ /datetime/ ) ? @$filters[0]
243       : ( $column =~ /category/ ) ? @$filters[2]
244       : ( $column =~ /itemtype/ ) ? @$filters[3]
245       : ( $column =~ /^branch/ )   ? @$filters[4]
246       : ( $column =~ /ccode/ )    ? @$filters[5]
247       : ( $column =~ /location/ ) ? @$filters[6]
248       : ( $column =~ /sort1/ )    ? @$filters[9]
249       : ( $column =~ /sort1/ )    ? @$filters[10]
250       : ( $column =~ /homebranch/)    ? @$filters[11]
251       : ( $column =~ /holdingbranch/) ? @$filters[12]
252       :                             undef;
253
254     if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) {
255                 $colsource = 'items';
256         }
257 # 1st, loop rows.
258         my $linefield;
259         if ($line =~ /datetime/) {
260                 # by Day, Month or Year (1,2,3 respectively)
261                 $linefield = ($dsp == 1) ? "  dayname($line)" :
262                                          ($dsp == 2) ? "monthname($line)" :
263                                          ($dsp == 3) ? "     Year($line)" :
264                                         'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through C4::Dates
265         } else {
266                 $linefield = $line;
267         }
268         my $lineorder = ($linefield =~ /dayname/) ? "weekday($line)" :
269                                         ($linefield =~ /^month/ ) ? "  month($line)" : $linefield;
270
271         my $strsth = "SELECT distinctrow $linefield FROM statistics, ";
272                 # get stats on items if ccode or location, otherwise borrowers.
273         $strsth .= ($linesource eq 'items' ) ?
274                         " items     WHERE (statistics.itemnumber=items.itemnumber) " :
275                         " borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) ";
276         $strsth .= " AND $line is not null ";
277
278         if ($line =~ /datetime/) {
279                 if ($linefilter[1] and ($linefilter[0])) {
280                         $strsth .= " AND $line between ? AND ? ";
281                 } elsif ($linefilter[1]) {
282                         $strsth .= " AND $line < ? ";
283                 } elsif ($linefilter[0]) {
284                 $strsth .= " AND $line > ? ";
285                 }
286                 $strsth .= " AND type ='".$type."' " if $type;
287                 $strsth .= " AND   dayname(datetime) ='".   $daysel ."' " if $daysel;
288                 $strsth .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel;
289         } elsif ($linefilter[0]) {
290                 $linefilter[0] =~ s/\*/%/g;
291                 $strsth .= " AND $line LIKE ? ";
292         }
293         $strsth .=" group by $linefield order by $lineorder ";
294         $debug and warn $strsth;
295         push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strsth};
296         my $sth = $dbh->prepare( $strsth );
297         if ((@linefilter) and ($linefilter[1])){
298                 $sth->execute($linefilter[0],$linefilter[1]);
299         } elsif ($linefilter[0]) {
300                 $sth->execute($linefilter[0]);
301         } else {
302                 $sth->execute;
303         }
304
305         while (my ($celvalue) = $sth->fetchrow) {
306                 my %cell = (rowtitle => $celvalue, totalrow => 0); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display
307                 $cell{rowtitle_display} =
308                         ($line =~ /ccode/   ) ? $ccodes->{$celvalue}    :
309                         ($line =~ /location/) ? $locations->{$celvalue} :
310                         ($line =~ /itemtype/) ? $itemtypes->{$celvalue}->{description} :
311                         $celvalue; # default fallback
312                 if ($line =~ /sort1/) {
313                         foreach (@$Bsort1) {
314                                 ($celvalue eq $_->{authorised_value}) or next;
315                                 $cell{rowtitle_display} = $_->{lib} and last;
316                         }
317                 } elsif ($line =~ /sort2/) {
318                         foreach (@$Bsort2) {
319                                 ($celvalue eq $_->{authorised_value}) or next;
320                                 $cell{rowtitle_display} = $_->{lib} and last;
321                         }
322                 } elsif ($line =~ /category/) {
323                         foreach (@$categoryloop) {
324                                 ($celvalue eq $_->{categorycode}) or next;
325                                 $cell{rowtitle_display} = $_->{description} and last;
326                         }
327                 }
328                 push @loopline, \%cell;
329         }
330
331 # 2nd, loop cols.
332         my $colfield;
333         my $colorder;
334         if ($column =~ /datetime/) {
335                 #Display by Day, Month or Year (1,2,3 respectively)
336                 $colfield = ($dsp == 1) ? "  dayname($column)" :
337                                         ($dsp == 2) ? "monthname($column)" :
338                                         ($dsp == 3) ? "     Year($column)" :
339                                         'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through C4::Dates
340         } else {
341                 $colfield = $column;
342         }
343         $colorder = ($colfield =~ /dayname/) ? "weekday($column)" :
344                                 ($colfield =~ /^month/ ) ? "  month($column)" : $colfield;
345         my $strsth2 = "SELECT distinctrow $colfield FROM statistics, ";
346         # get stats on items if ccode or location, otherwise borrowers.
347         $strsth2 .= ($colsource eq 'items' ) ?
348                                 "items     WHERE (statistics.itemnumber=items.itemnumber) " :
349                                 "borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) ";
350         $strsth2 .= " AND $column IS NOT NULL ";
351
352         if ($column =~ /datetime/){
353         if (($colfilter[1]) and ($colfilter[0])){
354                         $strsth2 .= " AND $column BETWEEN ? AND ? " ;
355         } elsif ($colfilter[1]) {
356                         $strsth2 .= " AND $column < ? " ;
357         } elsif ($colfilter[0]) {
358                         $strsth2 .= " AND $column > ? " ;
359         }
360         $strsth2 .= " AND                type ='". $type     ."' " if $type;
361         $strsth2 .= " AND   dayname(datetime) ='". $daysel   ."' " if $daysel;
362         $strsth2 .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel;
363     } elsif ($colfilter[0]) {
364         $colfilter[0] =~ s/\*/%/g;
365         $strsth2 .= " AND $column LIKE ? " ;
366     }
367         $strsth2 .=" GROUP BY $colfield ORDER BY $colorder ";
368
369         my $sth2 = $dbh->prepare($strsth2);
370         push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strsth2};
371         if ((@colfilter) and ($colfilter[1])){
372                 $sth2->execute($colfilter[0], $colfilter[1]);
373         } elsif ($colfilter[0]) {
374                 $sth2->execute($colfilter[0]);
375         } else {
376                 $sth2->execute;
377         }
378
379         while (my ($celvalue) = $sth2->fetchrow) {
380                 my %cell = (coltitle => $celvalue); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display
381                 $cell{coltitle_display} =
382                         ($column =~ /ccode/   ) ?    $ccodes->{$celvalue} :
383                         ($column =~ /location/) ? $locations->{$celvalue} :
384                         ($column =~ /itemtype/) ? $itemtypes->{$celvalue}->{description} :
385                         $celvalue; # default fallback
386                 if ($column =~ /sort1/) {
387                         foreach (@$Bsort1) {
388                                 ($celvalue eq $_->{authorised_value}) or next;
389                                 $cell{coltitle_display} = $_->{lib} and last;
390                         }
391                 } elsif ($column =~ /sort2/) {
392                         foreach (@$Bsort2) {
393                                 ($celvalue eq $_->{authorised_value}) or next;
394                                 $cell{coltitle_display} = $_->{lib} and last;
395                         }
396                 } elsif ($column =~ /category/) {
397                         foreach (@$categoryloop) {
398                                 ($celvalue eq $_->{categorycode}) or next;
399                                 $cell{coltitle_display} = $_->{description} and last;
400                         }
401                 }
402                 push @loopcol, \%cell;
403         }
404
405         #Initialization of cell values.....
406         my %table;
407         foreach my $row (@loopline) {
408                 foreach my $col (@loopcol) {
409                         $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} )  ";
410                         $table{$row->{rowtitle}}->{$col->{coltitle}} = 0;
411                 }
412                 $table{$row->{rowtitle}}->{totalrow} = 0;
413         }
414
415 # preparing calculation
416     my $strcalc = "SELECT $linefield, $colfield, ";
417         $strcalc .= ($process == 1) ? " COUNT(*) "                                 :
418                                         ($process == 2) ? "(COUNT(DISTINCT borrowers.borrowernumber))" :
419                                 ($process == 3) ? "(COUNT(DISTINCT statistics.itemnumber))"        : '';
420         if ($process == 4) {
421                 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
422                 $rqbookcount->execute;
423                 my ($bookcount) = $rqbookcount->fetchrow;
424                 $strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount " ;
425         }
426         $strcalc .= "
427         FROM statistics
428         LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber
429         ";
430         $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber "
431         if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or ($colsource eq 'items')
432             ||@$filters[5]||@$filters[6]||@$filters[7]||@$filters[8]);
433         
434         $strcalc .= "WHERE 1=1 ";
435         @$filters = map {defined($_) and s/\*/%/g; $_} @$filters;
436         $strcalc .= " AND statistics.datetime > '"       . @$filters[0] ."'" if (@$filters[0] );
437         $strcalc .= " AND statistics.datetime < '"       . @$filters[1] ."'" if (@$filters[1] );
438         $strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] ."'" if (@$filters[2] );
439         $strcalc .= " AND statistics.itemtype LIKE '"    . @$filters[3] ."'" if (@$filters[3] );
440         $strcalc .= " AND statistics.branch LIKE '"      . @$filters[4] ."'" if (@$filters[4] );
441         $strcalc .= " AND items.ccode LIKE '"            . @$filters[5] ."'" if (@$filters[5] );
442         $strcalc .= " AND items.location LIKE '"         . @$filters[6] ."'" if (@$filters[6] );
443         $strcalc .= " AND items.itemcallnumber >='"      . @$filters[7] ."'" if (@$filters[7] );
444         $strcalc .= " AND items.itemcallnumber <'"       . @$filters[8] ."'" if (@$filters[8] );
445         $strcalc .= " AND borrowers.sort1 LIKE '"        . @$filters[9] ."'" if (@$filters[9] );
446         $strcalc .= " AND borrowers.sort2 LIKE '"        . @$filters[10]."'" if (@$filters[10]);
447         $strcalc .= " AND dayname(datetime) LIKE '"      . $daysel      ."'" if ($daysel  );
448         $strcalc .= " AND monthname(datetime) LIKE '"    . $monthsel    ."'" if ($monthsel);
449         $strcalc .= " AND statistics.type LIKE '"        . $type        ."'" if ($type    );
450
451         $strcalc .= " GROUP BY $linefield, $colfield order by $lineorder,$colorder";
452         ($debug) and warn $strcalc;
453         my $dbcalc = $dbh->prepare($strcalc);
454         push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strcalc};
455         $dbcalc->execute;
456         my ($emptycol,$emptyrow); 
457         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
458                 ($debug) and warn "filling table $row / $col / $value ";
459                 unless (defined $col) {
460                         $emptycol = 1; 
461                         $col = "zzEMPTY" ;
462                 }
463                 unless (defined $row) {
464                         $emptyrow = 1;
465                         $row = "zzEMPTY"; 
466                 }
467                 $table{$row}->{$col}     += $value;
468                 $table{$row}->{totalrow} += $value;
469                 $grantotal += $value;
470         }
471         push @loopcol, {coltitle => "NULL", coltitle_display => 'NULL'} if ($emptycol);
472         push @loopline,{rowtitle => "NULL", rowtitle_display => 'NULL'} if ($emptyrow);
473
474         foreach my $row (@loopline) {
475                 my @loopcell;
476                 #@loopcol ensures the order for columns is common with column titles
477                 # and the number matches the number of columns
478                 foreach my $col (@loopcol) {
479                         my $value = $table{null_to_zzempty($row->{rowtitle})}->{null_to_zzempty($col->{coltitle})};
480                         push @loopcell, {value => $value};
481                 }
482                 my $rowtitle = ($row->{rowtitle} eq "NULL") ? "zzEMPTY" : $row->{rowtitle};
483                 push @looprow, {
484                         'rowtitle_display' => $row->{rowtitle_display},
485                         'rowtitle' => $rowtitle,
486                         'loopcell' => \@loopcell,
487                         'totalrow' => $table{$rowtitle}->{totalrow}
488                 };
489         }
490         for my $col ( @loopcol ) {
491                 my $total = 0;
492                 foreach my $row (@looprow) {
493                         $total += $table{null_to_zzempty($row->{rowtitle})}->{null_to_zzempty($col->{coltitle})};
494                         $debug and warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
495                 }
496                 push @loopfooter, {'totalcol' => $total};
497         }
498
499         # the header of the table
500         $globalline{loopfilter}=\@loopfilter;
501         # the core of the table
502         $globalline{looprow} = \@looprow;
503         $globalline{loopcol} = \@loopcol;
504         #       # the foot (totals by borrower type)
505         $globalline{loopfooter} = \@loopfooter;
506         $globalline{total}  = $grantotal;
507         $globalline{line}   = $line;
508         $globalline{column} = $column;
509         return [(\%globalline)];
510 }
511
512 sub null_to_zzempty ($) {
513         my $string = shift;
514         defined($string)    or  return 'zzEMPTY';
515         ($string eq "NULL") and return 'zzEMPTY';
516         return $string;         # else return the valid value
517 }
518
519 1;