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.
28 use C4
::Branch
; # GetBranches
33 use C4
::Dates qw
/format_date format_date_in_iso/;
35 use List
::MoreUtils qw
/any/;
40 plugin that shows circulation stats
48 # my $debug = 1; # override for now.
50 my $fullreportname = "reports/reserves_stats.tmpl";
51 my $do_it = $input->param('do_it');
52 my $line = $input->param("Line");
53 my $column = $input->param("Column");
54 my $podsp = $input->param("DisplayBy");
55 my $type = $input->param("PeriodTypeSel");
56 my $daysel = $input->param("PeriodDaySel");
57 my $monthsel = $input->param("PeriodMonthSel");
58 my $calc = $input->param("Cellvalue");
59 my $output = $input->param("output");
60 my $basename = $input->param("basename");
61 my $mime = $input->param("MIME");
62 my $hash_params = $input->Vars;
64 foreach my $filter (grep {$_ =~/^filter/} keys %$hash_params){
65 my $filterstring=$filter;
66 $filterstring=~s/^filter_//g;
67 $$filter_hashref{$filterstring}=$$hash_params{$filter} if (defined $$hash_params{$filter} && $$hash_params{$filter} ne "");
69 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
70 template_name
=> $fullreportname,
74 flagsrequired
=> {reports
=> '*'},
77 our $sep = $input->param("sep");
78 $sep = "\t" if ($sep eq 'tabulation');
79 $template->param(do_it
=> $do_it,
80 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
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(1); # 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 ccodeloop
=> \
@ccodes,
173 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
174 hassort1
=> $hassort1,
175 hassort2
=> $hassort2,
178 CGIextChoice
=> $CGIextChoice,
179 CGIsepChoice
=> $CGIsepChoice,
181 output_html_with_http_headers
$input, $cookie, $template->output;
184 my ($linefield, $colfield, $process, $filters_hashref) = @_;
192 my $dbh = C4
::Context
->dbh;
198 foreach my $filter (keys %$filters_hashref){
199 $$filters_hashref{$filter} =~s/\*/%/;
200 $$filters_hashref{$filter} = format_date_in_iso
($$filters_hashref{$_}) if ($_=~/date/);
203 @loopfilter= map{{ crit
=>$_ ,filter
=>($_=~/date/?
204 format_date
($$filters_hashref{$_})
205 :$$filters_hashref{$_}
208 } sort keys %$filters_hashref;
213 my $linesql=changeifreservestatus
($linefield);
214 my $colsql=changeifreservestatus
($colfield);
215 #Initialization of cell values.....
217 # preparing calculation
218 my $strcalc = "(SELECT $linesql line, $colsql col, ";
219 $strcalc .= ($process == 1) ?
" COUNT(*) calculation" :
220 ($process == 2) ?
"(COUNT(DISTINCT reserves.borrowernumber)) calculation" :
221 ($process == 3) ?
"(COUNT(DISTINCT reserves.itemnumber)) calculation" :
222 ($process == 4) ?
"(COUNT(DISTINCT reserves.biblionumber)) calculation" : '*';
225 LEFT JOIN borrowers USING (borrowernumber)
227 $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
228 if ($linefield =~ /^biblio\./ or $colfield =~ /^biblio\./ or any
{$_=~/biblio/}keys %$filters_hashref);
229 $strcalc .= "LEFT JOIN items ON reserves.itemnumber=items.itemnumber "
230 if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or any
{$_=~/items/}keys %$filters_hashref);
236 ($debug) and print STDERR Dump
($filters_hashref);
237 foreach my $filter (keys %$filters_hashref){
239 my $stringfield=$filter;
240 $stringfield=~s/\_[a-z_]+$//;
243 $string=$stringfield;
245 elsif ($filter=~/_or/){
246 push @sqlor, qq{( }.changeifreservestatus
($filter)." = ? ) ";
247 push @sqlorparams, $$filters_hashref{$filter};
249 elsif ($filter=~/_end$/){
250 $string = " $stringfield <= ? ";
252 elsif ($filter=~/_begin$/){
253 $string = " $stringfield >= ? ";
256 $string = " $stringfield LIKE ? ";
259 push @sqlwhere, $string;
260 push @sqlparams, $$filters_hashref{$filter};
264 $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
265 $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
266 $strcalc .= " GROUP BY line, col )";
267 my $strcalc_old=$strcalc;
268 $strcalc_old=~s/reserves/old_reserves/g;
269 $strcalc.=qq{ UNION
$strcalc_old ORDER BY line
, col
};
270 ($debug) and print STDERR
$strcalc;
271 my $dbcalc = $dbh->prepare($strcalc);
272 push @loopfilter, {crit
=>'SQL =', sql
=>1, filter
=>$strcalc};
273 @sqlparams=(@sqlparams,@sqlorparams);
274 $dbcalc->execute(@sqlparams,@sqlparams);
275 my ($emptycol,$emptyrow);
276 my $data = $dbcalc->fetchall_hashref([qw(line col)]);
279 foreach my $row (keys %$data){
280 push @loopline, $row;
281 foreach my $col (keys %{$$data{$row}}){
282 $$data{$row}{totalrow
}+=$$data{$row}{$col}{calculation
};
283 $grantotal+=$$data{$row}{$col}{calculation
};
287 my $urlbase="do_it=1&".join("&",map{"filter_$_=$$filters_hashref{$_}"} keys %$filters_hashref);
288 foreach my $row (sort @loopline) {
290 #@loopcol ensures the order for columns is common with column titles
291 # and the number matches the number of columns
292 foreach my $col (sort keys %cols_hash) {
293 push @loopcell, {value
=>( $$data{$row}{$col}{calculation
} or ""),
294 # url_complement=>($urlbase=~/&$/?$urlbase."&":$urlbase)."filter_$linefield=$row&filter_$colfield=$col"
298 'rowtitle_display' => display_value
($linefield,$row),
300 'loopcell' => \
@loopcell,
301 'totalrow' => $$data{$row}{totalrow
}
304 for my $col ( sort keys %cols_hash ) {
306 foreach my $row (@loopline) {
307 $total += $$data{$row}{$col}{calculation
};
308 $debug and warn "value added ".$$data{$row}{$col}{calculation
}. "for line ".$row;
310 push @loopfooter, {'totalcol' => $total};
311 push @loopcol, {'coltitle' => $col,
312 coltitle_display
=>display_value
($colfield,$col)};
315 # the header of the table
316 $globalline{loopfilter
}=\
@loopfilter;
317 # the core of the table
318 $globalline{looprow
} = \
@looprow;
319 $globalline{loopcol
} = \
@loopcol;
320 # # the foot (totals by borrower type)
321 $globalline{loopfooter
} = \
@loopfooter;
322 $globalline{total
} = $grantotal;
323 $globalline{line
} = $linefield;
324 $globalline{column
} = $colfield;
325 return [(\
%globalline)];
328 sub null_to_zzempty
($) {
330 defined($string) or return 'zzEMPTY';
331 ($string eq "NULL") and return 'zzEMPTY';
332 return $string; # else return the valid value
335 my ($crit,$value)=@_;
337 ($crit =~ /ccode/ ) ?
$ccodes->{$value} :
338 ($crit =~ /location/) ?
$locations->{$value} :
339 ($crit =~ /itemtype/) ?
$itemtypes->{$value}->{description
} :
340 ($crit =~ /branch/) ? GetBranchName
($value):
341 ($crit =~ /reservestatus/) ? reservestatushuman
($value):
342 $value; # default fallback
343 if ($crit =~ /(sort1|sort2)/) {
344 $display_value=GetAuthorisedValues
("B$_",$value);
345 } elsif ($crit =~ /category/) {
346 my $element=any
{$value eq $_->{categorycode
}} @
$categoryloop;
347 $display_value=$$element{description
};
349 return $display_value;
351 sub reservestatushuman
{
363 sub changeifreservestatus
{
365 ($val=~/reservestatus/
367 when priority
>0 then
1
370 when found
='f' then
4
373 when cancellationdate is null then
3