3 # Copyright 2010 BibLibre
5 # This file is part of Koha.
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
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.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use C4
::Branch
; # GetBranches
34 use C4
::Dates qw
/format_date format_date_in_iso/;
36 use List
::MoreUtils qw
/any/;
41 plugin that shows circulation stats
49 # my $debug = 1; # override for now.
51 my $fullreportname = "reports/reserves_stats.tmpl";
52 my $do_it = $input->param('do_it');
53 my $line = $input->param("Line");
54 my $column = $input->param("Column");
55 my $podsp = $input->param("DisplayBy");
56 my $type = $input->param("PeriodTypeSel");
57 my $daysel = $input->param("PeriodDaySel");
58 my $monthsel = $input->param("PeriodMonthSel");
59 my $calc = $input->param("Cellvalue");
60 my $output = $input->param("output");
61 my $basename = $input->param("basename");
62 my $mime = $input->param("MIME");
63 my $hash_params = $input->Vars;
65 foreach my $filter (grep {$_ =~/^filter/} keys %$hash_params){
66 my $filterstring=$filter;
67 $filterstring=~s/^filter_//g;
68 $$filter_hashref{$filterstring}=$$hash_params{$filter} if (defined $$hash_params{$filter} && $$hash_params{$filter} ne "");
70 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
71 template_name
=> $fullreportname,
75 flagsrequired
=> {reports
=> '*'},
78 our $sep = $input->param("sep") || '';
79 $sep = "\t" if ($sep eq 'tabulation');
80 $template->param(do_it
=> $do_it,
83 my $itemtypes = GetItemTypes
();
84 my $categoryloop = GetBorrowercategoryList
;
86 my $ccodes = GetKohaAuthorisedValues
("items.ccode");
87 my $locations = GetKohaAuthorisedValues
("items.location");
88 my $authvalue = GetKohaAuthorisedValues
("items.authvalue");
90 my $Bsort1 = GetAuthorisedValues
("Bsort1");
91 my $Bsort2 = GetAuthorisedValues
("Bsort2");
92 my ($hassort1,$hassort2);
93 $hassort1=1 if $Bsort1;
94 $hassort2=1 if $Bsort2;
99 my $results = calculate
($line, $column, $calc, $filter_hashref);
100 if ($output eq "screen"){
101 # Printing results to screen
102 $template->param(mainloop
=> $results);
103 output_html_with_http_headers
$input, $cookie, $template->output;
105 # Printing to a csv file
106 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
107 -encoding
=> 'utf-8',
108 -attachment
=>"$basename.csv",
109 -filename
=>"$basename.csv" );
110 my $cols = @
$results[0]->{loopcol
};
111 my $lines = @
$results[0]->{looprow
};
113 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
115 foreach my $col ( @
$cols ) {
116 print $col->{coltitle
}.$sep;
120 foreach my $line ( @
$lines ) {
121 my $x = $line->{loopcell
};
122 print $line->{rowtitle
}.$sep;
123 print map {$_->{value
}.$sep} @
$x;
124 print $line->{totalrow
}, "\n";
128 $cols = @
$results[0]->{loopfooter
};
129 print map {$sep.$_->{totalcol
}} @
$cols;
130 print $sep.@
$results[0]->{total
};
132 exit; # exit either way after $do_it
135 my $dbh = C4
::Context
->dbh;
140 # create itemtype arrayref for <select>.
142 for my $itype ( sort {$itemtypes->{$a}->{description
} cmp $itemtypes->{$b}->{description
}} keys(%$itemtypes)) {
143 push @itemtypeloop, { code
=> $itype , description
=> $itemtypes->{$itype}->{description
} } ;
148 foreach (sort keys %$locations) {
149 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
153 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
154 push @ccodes, { code
=> $_, description
=> $ccodes->{$_} };
158 my @mime = (C4
::Context
->preference("MIME"));
160 my $CGIextChoice=CGI
::scrolling_list
(
167 my $CGIsepChoice=GetDelimiterChoices
;
170 categoryloop
=> $categoryloop,
171 itemtypeloop
=> \
@itemtypeloop,
172 locationloop
=> \
@locations,
173 ccodeloop
=> \
@ccodes,
174 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
175 hassort1
=> $hassort1,
176 hassort2
=> $hassort2,
179 CGIextChoice
=> $CGIextChoice,
180 CGIsepChoice
=> $CGIsepChoice,
182 output_html_with_http_headers
$input, $cookie, $template->output;
185 my ($linefield, $colfield, $process, $filters_hashref) = @_;
193 my $dbh = C4
::Context
->dbh;
199 foreach my $filter ( keys %$filters_hashref ) {
200 $filters_hashref->{$filter} =~ s/\*/%/;
201 $filters_hashref->{$filter} =
202 format_date_in_iso
( $filters_hashref->{$filter} )
203 if ( $filter =~ /date/ );
212 ? format_date
( $filters_hashref->{$_} )
213 : $filters_hashref->{$_}
216 } sort keys %$filters_hashref;
221 my $linesql=changeifreservestatus
($linefield);
222 my $colsql=changeifreservestatus
($colfield);
223 #Initialization of cell values.....
225 # preparing calculation
226 my $strcalc = "(SELECT $linesql line, $colsql col, ";
227 $strcalc .= ($process == 1) ?
" COUNT(*) calculation" :
228 ($process == 2) ?
"(COUNT(DISTINCT reserves.borrowernumber)) calculation" :
229 ($process == 3) ?
"(COUNT(DISTINCT reserves.itemnumber)) calculation" :
230 ($process == 4) ?
"(COUNT(DISTINCT reserves.biblionumber)) calculation" : '*';
233 LEFT JOIN borrowers USING (borrowernumber)
235 $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
236 if ($linefield =~ /^biblio\./ or $colfield =~ /^biblio\./ or any
{$_=~/biblio/}keys %$filters_hashref);
237 $strcalc .= "LEFT JOIN items ON reserves.itemnumber=items.itemnumber "
238 if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or any
{$_=~/items/}keys %$filters_hashref);
244 ($debug) and print STDERR Dump
($filters_hashref);
245 foreach my $filter (keys %$filters_hashref){
247 my $stringfield=$filter;
248 $stringfield=~s/\_[a-z_]+$//;
250 $string=$stringfield;
252 elsif ($filter=~/_or/){
253 push @sqlor, qq{( }.changeifreservestatus
($filter)." = ? ) ";
254 push @sqlorparams, $$filters_hashref{$filter};
256 elsif ($filter=~/_endex$/){
257 $string = " $stringfield < ? ";
259 elsif ($filter=~/_end$/){
260 $string = " $stringfield <= ? ";
262 elsif ($filter=~/_begin$/){
263 $string = " $stringfield >= ? ";
266 $string = " $stringfield LIKE ? ";
269 push @sqlwhere, $string;
270 push @sqlparams, $$filters_hashref{$filter};
274 $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
275 $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
276 $strcalc .= " GROUP BY line, col )";
277 my $strcalc_old=$strcalc;
278 $strcalc_old=~s/reserves/old_reserves/g;
279 $strcalc.=qq{ UNION
$strcalc_old ORDER BY line
, col
};
280 ($debug) and print STDERR
$strcalc;
281 my $dbcalc = $dbh->prepare($strcalc);
282 push @loopfilter, {crit
=>'SQL =', sql
=>1, filter
=>$strcalc};
283 @sqlparams=(@sqlparams,@sqlorparams);
284 $dbcalc->execute(@sqlparams,@sqlparams);
285 my ($emptycol,$emptyrow);
286 my $data = $dbcalc->fetchall_hashref([qw(line col)]);
288 foreach my $row (keys %$data){
289 push @loopline, $row;
290 foreach my $col (keys %{$$data{$row}}){
291 $$data{$row}{totalrow
}+=$$data{$row}{$col}{calculation
};
292 $grantotal+=$$data{$row}{$col}{calculation
};
296 my $urlbase="do_it=1&".join("&",map{"filter_$_=$$filters_hashref{$_}"} keys %$filters_hashref);
297 foreach my $row (sort @loopline) {
299 #@loopcol ensures the order for columns is common with column titles
300 # and the number matches the number of columns
301 foreach my $col (sort keys %cols_hash) {
302 push @loopcell, {value
=>( $$data{$row}{$col}{calculation
} or ""),
303 # url_complement=>($urlbase=~/&$/?$urlbase."&":$urlbase)."filter_$linefield=$row&filter_$colfield=$col"
307 'rowtitle_display' => display_value
($linefield,$row),
309 'loopcell' => \
@loopcell,
310 'totalrow' => $$data{$row}{totalrow
}
313 for my $col ( sort keys %cols_hash ) {
315 foreach my $row (@loopline) {
316 $total += $data->{$row}{$col}{calculation
} if $data->{$row}{$col}{calculation
};
317 $debug and warn "value added ".$$data{$row}{$col}{calculation
}. "for line ".$row;
319 push @loopfooter, {'totalcol' => $total};
320 push @loopcol, {'coltitle' => $col,
321 coltitle_display
=>display_value
($colfield,$col)};
323 # the header of the table
324 $globalline{loopfilter
}=\
@loopfilter;
325 # the core of the table
326 $globalline{looprow
} = \
@looprow;
327 $globalline{loopcol
} = \
@loopcol;
328 # # the foot (totals by borrower type)
329 $globalline{loopfooter
} = \
@loopfooter;
330 $globalline{total
} = $grantotal;
331 $globalline{line
} = $linefield;
332 $globalline{column
} = $colfield;
333 return [(\
%globalline)];
336 sub null_to_zzempty
($) {
338 defined($string) or return 'zzEMPTY';
339 ($string eq "NULL") and return 'zzEMPTY';
340 return $string; # else return the valid value
343 my ( $crit, $value ) = @_;
345 ( $crit =~ /ccode/ ) ?
$ccodes->{$value}
346 : ( $crit =~ /location/ ) ?
$locations->{$value}
347 : ( $crit =~ /itemtype/ ) ?
$itemtypes->{$value}->{description
}
348 : ( $crit =~ /branch/ ) ? GetBranchName
($value)
349 : ( $crit =~ /reservestatus/ ) ? reservestatushuman
($value)
350 : $value; # default fallback
351 if ($crit =~ /sort1/) {
353 ($value eq $_->{authorised_value
}) or next;
354 $display_value = $_->{lib
} and last;
357 elsif ($crit =~ /sort2/) {
359 ($value eq $_->{authorised_value
}) or next;
360 $display_value = $_->{lib
} and last;
363 elsif ( $crit =~ /category/ ) {
364 foreach (@
$categoryloop) {
365 ( $value eq $_->{categorycode
} ) or next;
366 $display_value = $_->{description
} and last;
369 return $display_value;
371 sub reservestatushuman
{
383 sub changeifreservestatus
{
385 ($val=~/reservestatus/
387 when priority
>0 then
1
390 when found
='f' then
4
393 when cancellationdate is null then
3