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
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.
22 #use warnings; FIXME - Bug 2505
30 use C4
::Branch
; # GetBranches
35 use C4
::Dates qw
/format_date format_date_in_iso/;
40 plugin that shows circulation stats
48 # my $debug = 1; # override for now.
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,
70 flagsrequired
=> {reports
=> '*'},
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;
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;
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
};
108 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
110 foreach my $col ( @
$cols ) {
111 print $col->{coltitle
}.$sep;
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";
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;
135 # create itemtype arrayref for <select>.
137 for my $itype ( sort {$itemtypes->{$a}->{description
} cmp $itemtypes->{$b}->{description
}} keys(%$itemtypes)) {
138 push @itemtypeloop, { code
=> $itype , description
=> $itemtypes->{$itype}->{description
} } ;
143 foreach (sort keys %$locations) {
144 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
148 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
149 push @ccodes, { code
=> $_, description
=> $ccodes->{$_} };
153 my @mime = (C4
::Context
->preference("MIME"));
155 my $CGIextChoice=CGI
::scrolling_list
(
162 my $CGIsepChoice=GetDelimiterChoices
;
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,
174 CGIextChoice
=> $CGIextChoice,
175 CGIsepChoice
=> $CGIsepChoice,
177 output_html_with_http_headers
$input, $cookie, $template->output;
180 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
188 my $dbh = C4
::Context
->dbh;
194 for (my $i=0;$i<=10;$i++) {
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
202 $cell{filter
} = @
$filters[$i];
204 $cell{filter
} = format_date
(@
$filters[$i]);
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);
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';
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';
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
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]);
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/) {
312 ($celvalue eq $_->{authorised_value
}) or next;
313 $cell{rowtitle_display
} = $_->{lib
} and last;
315 } elsif ($line =~ /sort2/) {
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;
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
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]);
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/) {
386 ($celvalue eq $_->{authorised_value
}) or next;
387 $cell{coltitle_display
} = $_->{lib
} and last;
389 } elsif ($column =~ /sort2/) {
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.....
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))" : '';
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 " ;
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};
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) {
461 unless (defined $row) {
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) {
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
};
482 'rowtitle_display' => $row->{rowtitle_display
},
483 'rowtitle' => $rowtitle,
484 'loopcell' => \
@loopcell,
485 'totalrow' => $table{$rowtitle}->{totalrow
}
488 for my $col ( @loopcol ) {
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
($) {
512 defined($string) or return 'zzEMPTY';
513 ($string eq "NULL") and return 'zzEMPTY';
514 return $string; # else return the valid value