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,
81 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
84 my $itemtypes = GetItemTypes
();
85 my $categoryloop = GetBorrowercategoryList
;
87 my $ccodes = GetKohaAuthorisedValues
("items.ccode");
88 my $locations = GetKohaAuthorisedValues
("items.location");
89 my $authvalue = GetKohaAuthorisedValues
("items.authvalue");
91 my $Bsort1 = GetAuthorisedValues
("Bsort1");
92 my $Bsort2 = GetAuthorisedValues
("Bsort2");
93 my ($hassort1,$hassort2);
94 $hassort1=1 if $Bsort1;
95 $hassort2=1 if $Bsort2;
100 my $results = calculate
($line, $column, $calc, $filter_hashref);
101 if ($output eq "screen"){
102 # Printing results to screen
103 $template->param(mainloop
=> $results);
104 output_html_with_http_headers
$input, $cookie, $template->output;
106 # Printing to a csv file
107 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
108 -encoding
=> 'utf-8',
109 -attachment
=>"$basename.csv",
110 -filename
=>"$basename.csv" );
111 my $cols = @
$results[0]->{loopcol
};
112 my $lines = @
$results[0]->{looprow
};
114 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
116 foreach my $col ( @
$cols ) {
117 print $col->{coltitle
}.$sep;
121 foreach my $line ( @
$lines ) {
122 my $x = $line->{loopcell
};
123 print $line->{rowtitle
}.$sep;
124 print map {$_->{value
}.$sep} @
$x;
125 print $line->{totalrow
}, "\n";
129 $cols = @
$results[0]->{loopfooter
};
130 print map {$sep.$_->{totalcol
}} @
$cols;
131 print $sep.@
$results[0]->{total
};
133 exit(1); # exit either way after $do_it
136 my $dbh = C4
::Context
->dbh;
141 # create itemtype arrayref for <select>.
143 for my $itype ( sort {$itemtypes->{$a}->{description
} cmp $itemtypes->{$b}->{description
}} keys(%$itemtypes)) {
144 push @itemtypeloop, { code
=> $itype , description
=> $itemtypes->{$itype}->{description
} } ;
149 foreach (sort keys %$locations) {
150 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
154 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
155 push @ccodes, { code
=> $_, description
=> $ccodes->{$_} };
159 my @mime = (C4
::Context
->preference("MIME"));
161 my $CGIextChoice=CGI
::scrolling_list
(
168 my $CGIsepChoice=GetDelimiterChoices
;
171 categoryloop
=> $categoryloop,
172 itemtypeloop
=> \
@itemtypeloop,
173 locationloop
=> \
@locations,
174 ccodeloop
=> \
@ccodes,
175 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
176 hassort1
=> $hassort1,
177 hassort2
=> $hassort2,
180 CGIextChoice
=> $CGIextChoice,
181 CGIsepChoice
=> $CGIsepChoice,
183 output_html_with_http_headers
$input, $cookie, $template->output;
186 my ($linefield, $colfield, $process, $filters_hashref) = @_;
194 my $dbh = C4
::Context
->dbh;
200 foreach my $filter ( keys %$filters_hashref ) {
201 $filters_hashref->{$filter} =~ s/\*/%/;
202 $filters_hashref->{$filter} =
203 format_date_in_iso
( $filters_hashref->{$filter} )
204 if ( $filter =~ /date/ );
213 ? format_date
( $filters_hashref->{$_} )
214 : $filters_hashref->{$_}
217 } sort keys %$filters_hashref;
222 my $linesql=changeifreservestatus
($linefield);
223 my $colsql=changeifreservestatus
($colfield);
224 #Initialization of cell values.....
226 # preparing calculation
227 my $strcalc = "(SELECT $linesql line, $colsql col, ";
228 $strcalc .= ($process == 1) ?
" COUNT(*) calculation" :
229 ($process == 2) ?
"(COUNT(DISTINCT reserves.borrowernumber)) calculation" :
230 ($process == 3) ?
"(COUNT(DISTINCT reserves.itemnumber)) calculation" :
231 ($process == 4) ?
"(COUNT(DISTINCT reserves.biblionumber)) calculation" : '*';
234 LEFT JOIN borrowers USING (borrowernumber)
236 $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
237 if ($linefield =~ /^biblio\./ or $colfield =~ /^biblio\./ or any
{$_=~/biblio/}keys %$filters_hashref);
238 $strcalc .= "LEFT JOIN items ON reserves.itemnumber=items.itemnumber "
239 if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or any
{$_=~/items/}keys %$filters_hashref);
245 ($debug) and print STDERR Dump
($filters_hashref);
246 foreach my $filter (keys %$filters_hashref){
248 my $stringfield=$filter;
249 $stringfield=~s/\_[a-z_]+$//;
251 $string=$stringfield;
253 elsif ($filter=~/_or/){
254 push @sqlor, qq{( }.changeifreservestatus
($filter)." = ? ) ";
255 push @sqlorparams, $$filters_hashref{$filter};
257 elsif ($filter=~/_endex$/){
258 $string = " $stringfield < ? ";
260 elsif ($filter=~/_end$/){
261 $string = " $stringfield <= ? ";
263 elsif ($filter=~/_begin$/){
264 $string = " $stringfield >= ? ";
267 $string = " $stringfield LIKE ? ";
270 push @sqlwhere, $string;
271 push @sqlparams, $$filters_hashref{$filter};
275 $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
276 $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
277 $strcalc .= " GROUP BY line, col )";
278 my $strcalc_old=$strcalc;
279 $strcalc_old=~s/reserves/old_reserves/g;
280 $strcalc.=qq{ UNION
$strcalc_old ORDER BY line
, col
};
281 ($debug) and print STDERR
$strcalc;
282 my $dbcalc = $dbh->prepare($strcalc);
283 push @loopfilter, {crit
=>'SQL =', sql
=>1, filter
=>$strcalc};
284 @sqlparams=(@sqlparams,@sqlorparams);
285 $dbcalc->execute(@sqlparams,@sqlparams);
286 my ($emptycol,$emptyrow);
287 my $data = $dbcalc->fetchall_hashref([qw(line col)]);
289 foreach my $row (keys %$data){
290 push @loopline, $row;
291 foreach my $col (keys %{$$data{$row}}){
292 $$data{$row}{totalrow
}+=$$data{$row}{$col}{calculation
};
293 $grantotal+=$$data{$row}{$col}{calculation
};
297 my $urlbase="do_it=1&".join("&",map{"filter_$_=$$filters_hashref{$_}"} keys %$filters_hashref);
298 foreach my $row (sort @loopline) {
300 #@loopcol ensures the order for columns is common with column titles
301 # and the number matches the number of columns
302 foreach my $col (sort keys %cols_hash) {
303 push @loopcell, {value
=>( $$data{$row}{$col}{calculation
} or ""),
304 # url_complement=>($urlbase=~/&$/?$urlbase."&":$urlbase)."filter_$linefield=$row&filter_$colfield=$col"
308 'rowtitle_display' => display_value
($linefield,$row),
310 'loopcell' => \
@loopcell,
311 'totalrow' => $$data{$row}{totalrow
}
314 for my $col ( sort keys %cols_hash ) {
316 foreach my $row (@loopline) {
317 $total += $data->{$row}{$col}{calculation
} if $data->{$row}{$col}{calculation
};
318 $debug and warn "value added ".$$data{$row}{$col}{calculation
}. "for line ".$row;
320 push @loopfooter, {'totalcol' => $total};
321 push @loopcol, {'coltitle' => $col,
322 coltitle_display
=>display_value
($colfield,$col)};
324 # the header of the table
325 $globalline{loopfilter
}=\
@loopfilter;
326 # the core of the table
327 $globalline{looprow
} = \
@looprow;
328 $globalline{loopcol
} = \
@loopcol;
329 # # the foot (totals by borrower type)
330 $globalline{loopfooter
} = \
@loopfooter;
331 $globalline{total
} = $grantotal;
332 $globalline{line
} = $linefield;
333 $globalline{column
} = $colfield;
334 return [(\
%globalline)];
337 sub null_to_zzempty
($) {
339 defined($string) or return 'zzEMPTY';
340 ($string eq "NULL") and return 'zzEMPTY';
341 return $string; # else return the valid value
344 my ( $crit, $value ) = @_;
346 ( $crit =~ /ccode/ ) ?
$ccodes->{$value}
347 : ( $crit =~ /location/ ) ?
$locations->{$value}
348 : ( $crit =~ /itemtype/ ) ?
$itemtypes->{$value}->{description
}
349 : ( $crit =~ /branch/ ) ? GetBranchName
($value)
350 : ( $crit =~ /reservestatus/ ) ? reservestatushuman
($value)
351 : $value; # default fallback
352 if ($crit =~ /sort1/) {
354 ($value eq $_->{authorised_value
}) or next;
355 $display_value = $_->{lib
} and last;
358 elsif ($crit =~ /sort2/) {
360 ($value eq $_->{authorised_value
}) or next;
361 $display_value = $_->{lib
} and last;
364 elsif ( $crit =~ /category/ ) {
365 foreach (@
$categoryloop) {
366 ( $value eq $_->{categorycode
} ) or next;
367 $display_value = $_->{description
} and last;
370 return $display_value;
372 sub reservestatushuman
{
384 sub changeifreservestatus
{
386 ($val=~/reservestatus/
388 when priority
>0 then
1
391 when found
='f' then
4
394 when cancellationdate is null then
3