3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
21 #use warnings; FIXME - Bug 2505
29 use C4
::Branch
; # GetBranches
34 use C4
::Dates qw
/format_date format_date_in_iso/;
39 reports/issues_stats.pl
43 Plugin that shows circulation stats
47 # my $debug = 1; # override for now.
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,
68 flagsrequired
=> {reports
=> '*'},
71 our $sep = $input->param("sep");
72 $sep = "\t" if ($sep eq 'tabulation');
73 $template->param(do_it
=> $do_it,
76 my $itemtypes = GetItemTypes
();
77 my $categoryloop = GetBorrowercategoryList
;
79 my $ccodes = GetKohaAuthorisedValues
("items.ccode");
80 my $locations = GetKohaAuthorisedValues
("items.location");
82 my $Bsort1 = GetAuthorisedValues
("Bsort1");
83 my $Bsort2 = GetAuthorisedValues
("Bsort2");
84 my ($hassort1,$hassort2);
85 $hassort1=1 if $Bsort1;
86 $hassort2=1 if $Bsort2;
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;
97 # Printing to a csv file
98 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
100 -attachment
=>"$basename.csv",
101 -filename
=>"$basename.csv" );
102 my $cols = @
$results[0]->{loopcol
};
103 my $lines = @
$results[0]->{looprow
};
105 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
107 foreach my $col ( @
$cols ) {
108 print $col->{coltitle
}.$sep;
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";
120 $cols = @
$results[0]->{loopfooter
};
121 print map {$sep.$_->{totalcol
}} @
$cols;
122 print $sep.@
$results[0]->{total
};
124 exit; # exit either way after $do_it
127 my $dbh = C4
::Context
->dbh;
132 # create itemtype arrayref for <select>.
134 for my $itype ( sort {$itemtypes->{$a}->{description
} cmp $itemtypes->{$b}->{description
}} keys(%$itemtypes)) {
135 push @itemtypeloop, { code
=> $itype , description
=> $itemtypes->{$itype}->{description
} } ;
140 foreach (sort keys %$locations) {
141 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
145 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
146 push @ccodes, { code
=> $_, description
=> $ccodes->{$_} };
149 my $CGIextChoice = ( 'CSV' ); # FIXME translation
150 my $CGIsepChoice=GetDelimiterChoices
;
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,
162 CGIextChoice
=> $CGIextChoice,
163 CGIsepChoice
=> $CGIsepChoice,
165 output_html_with_http_headers
$input, $cookie, $template->output;
168 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
176 my $dbh = C4
::Context
->dbh;
182 for (my $i=0;$i<=12;$i++) {
184 (@
$filters[$i]) or next;
185 if (($i==1) and (@
$filters[$i-1])) {
186 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
188 # format the dates filters, otherwise just fill as is
190 $cell{filter
} = @
$filters[$i];
192 $cell{filter
} = format_date
(@
$filters[$i]);
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)";
210 # FIXME - no translation mechanism !
211 push @loopfilter, \
%cell;
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);
219 $debug and warn "filtres ". join "|", @filters;
220 my ($colsource, $linesource);
221 $linefilter[1] = @
$filters[1] if ($line =~ /datetime/);
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]
235 if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) {
236 $linesource = 'items';
240 $colfilter[1] = @
$filters[1] if ($column =~ /datetime/);
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]
254 if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) {
255 $colsource = 'items';
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
268 my $lineorder = ($linefield =~ /dayname/) ?
"weekday($line)" :
269 ($linefield =~ /^month/ ) ?
" month($line)" : $linefield;
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 ";
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 > ? ";
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 ? ";
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]);
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/) {
314 ($celvalue eq $_->{authorised_value
}) or next;
315 $cell{rowtitle_display
} = $_->{lib
} and last;
317 } elsif ($line =~ /sort2/) {
319 ($celvalue eq $_->{authorised_value
}) or next;
320 $cell{rowtitle_display
} = $_->{lib
} and last;
322 } elsif ($line =~ /category/) {
323 foreach (@
$categoryloop) {
324 ($celvalue eq $_->{categorycode
}) or next;
325 $cell{rowtitle_display
} = $_->{description
} and last;
328 push @loopline, \
%cell;
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
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 ";
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 > ? " ;
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 ? " ;
367 $strsth2 .=" GROUP BY $colfield ORDER BY $colorder ";
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]);
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/) {
388 ($celvalue eq $_->{authorised_value
}) or next;
389 $cell{coltitle_display
} = $_->{lib
} and last;
391 } elsif ($column =~ /sort2/) {
393 ($celvalue eq $_->{authorised_value
}) or next;
394 $cell{coltitle_display
} = $_->{lib
} and last;
396 } elsif ($column =~ /category/) {
397 foreach (@
$categoryloop) {
398 ($celvalue eq $_->{categorycode
}) or next;
399 $cell{coltitle_display
} = $_->{description
} and last;
402 push @loopcol, \
%cell;
405 #Initialization of cell values.....
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;
412 $table{$row->{rowtitle
}}->{totalrow
} = 0;
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))" : '';
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 " ;
428 LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber
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]);
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 );
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};
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) {
463 unless (defined $row) {
467 $table{$row}->{$col} += $value;
468 $table{$row}->{totalrow
} += $value;
469 $grantotal += $value;
471 push @loopcol, {coltitle
=> "NULL", coltitle_display
=> 'NULL'} if ($emptycol);
472 push @loopline,{rowtitle
=> "NULL", rowtitle_display
=> 'NULL'} if ($emptyrow);
474 foreach my $row (@loopline) {
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};
482 my $rowtitle = ($row->{rowtitle
} eq "NULL") ?
"zzEMPTY" : $row->{rowtitle
};
484 'rowtitle_display' => $row->{rowtitle_display
},
485 'rowtitle' => $rowtitle,
486 'loopcell' => \
@loopcell,
487 'totalrow' => $table{$rowtitle}->{totalrow
}
490 for my $col ( @loopcol ) {
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
};
496 push @loopfooter, {'totalcol' => $total};
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)];
512 sub null_to_zzempty
($) {
514 defined($string) or return 'zzEMPTY';
515 ($string eq "NULL") and return 'zzEMPTY';
516 return $string; # else return the valid value