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