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