3 # Copyright 2010 BibLibre
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>.
31 use Koha
::AuthorisedValues
;
35 use Koha
::Patron
::Categories
;
36 use List
::MoreUtils qw
/any/;
41 reports/reserve_stats.pl
45 Plugin that shows reserve stats
49 # my $debug = 1; # override for now.
51 my $fullreportname = "reports/reserves_stats.tt";
52 my $do_it = $input->param('do_it');
53 my $line = $input->param("Line");
54 my $column = $input->param("Column");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $hash_params = $input->Vars;
60 foreach my $filter (grep {$_ =~/^filter/} keys %$hash_params){
61 my $filterstring=$filter;
62 $filterstring=~s/^filter_//g;
63 $$filter_hashref{$filterstring}=$$hash_params{$filter} if (defined $$hash_params{$filter} && $$hash_params{$filter} ne "");
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,
78 my @patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
80 my $locations = { map { ( $_->{authorised_value
} => $_->{lib
} ) } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' }, { order_by
=> ['description'] } ) };
81 my $ccodes = { map { ( $_->{authorised_value
} => $_->{lib
} ) } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.ccode' }, { order_by
=> ['description'] } ) };
83 my $Bsort1 = GetAuthorisedValues
("Bsort1");
84 my $Bsort2 = GetAuthorisedValues
("Bsort2");
85 my ($hassort1,$hassort2);
86 $hassort1=1 if $Bsort1;
87 $hassort2=1 if $Bsort2;
92 my $results = calculate
($line, $column, $calc, $filter_hashref);
93 if ($output eq "screen"){
94 # Printing results to screen
95 $template->param(mainloop
=> $results);
96 output_html_with_http_headers
$input, $cookie, $template->output;
98 # Printing to a csv file
99 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
100 -encoding
=> 'utf-8',
101 -attachment
=>"$basename.csv",
102 -filename
=>"$basename.csv" );
103 my $cols = @
$results[0]->{loopcol
};
104 my $lines = @
$results[0]->{looprow
};
106 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
108 foreach my $col ( @
$cols ) {
109 print $col->{coltitle
}.$sep;
113 foreach my $line ( @
$lines ) {
114 my $x = $line->{loopcell
};
115 print $line->{rowtitle
}.$sep;
116 print map {$_->{value
}.$sep} @
$x;
117 print $line->{totalrow
}, "\n";
121 $cols = @
$results[0]->{loopfooter
};
122 print map {$sep.$_->{totalcol
}} @
$cols;
123 print $sep.@
$results[0]->{total
};
125 exit; # exit either way after $do_it
128 my $dbh = C4
::Context
->dbh;
130 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
134 foreach (sort keys %$locations) {
135 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
139 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
140 push @ccodes, { code
=> $_, description
=> $ccodes->{$_} };
144 my $CGIextChoice = ( 'CSV' ); # FIXME translation
145 my $CGIsepChoice=GetDelimiterChoices
;
148 categoryloop
=> \
@patron_categories,
149 itemtypes
=> $itemtypes,
150 locationloop
=> \
@locations,
151 ccodeloop
=> \
@ccodes,
152 hassort1
=> $hassort1,
153 hassort2
=> $hassort2,
156 CGIextChoice
=> $CGIextChoice,
157 CGIsepChoice
=> $CGIsepChoice,
159 output_html_with_http_headers
$input, $cookie, $template->output;
162 my ($linefield, $colfield, $process, $filters_hashref) = @_;
170 my $dbh = C4
::Context
->dbh;
176 foreach my $filter ( keys %$filters_hashref ) {
177 $filters_hashref->{$filter} =~ s/\*/%/;
178 if ( $filter =~ /date/ ) {
179 $filters_hashref->{$filter} =
180 eval { output_pref
( { dt
=> dt_from_string
( $filters_hashref->{$filter} ), dateonly
=> 1, dateformat
=> 'iso' }); };
190 ?
eval { output_pref
( { dt
=> dt_from_string
( $filters_hashref->{$_} ), dateonly
=> 1 }); }
191 : $filters_hashref->{$_}
194 } sort keys %$filters_hashref;
199 my $linesql=changeifreservestatus
($linefield);
200 my $colsql=changeifreservestatus
($colfield);
201 #Initialization of cell values.....
203 # preparing calculation
204 my $strcalc = "(SELECT $linesql line, $colsql col, ";
205 $strcalc .= ($process == 1) ?
" COUNT(*) calculation" :
206 ($process == 2) ?
"(COUNT(DISTINCT reserves.borrowernumber)) calculation" :
207 ($process == 3) ?
"(COUNT(DISTINCT reserves.itemnumber)) calculation" :
208 ($process == 4) ?
"(COUNT(DISTINCT reserves.biblionumber)) calculation" : '*';
210 FROM (select * from reserves union select * from old_reserves) reserves
211 LEFT JOIN borrowers USING (borrowernumber)
213 $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
214 if ($linefield =~ /^biblio\./ or $colfield =~ /^biblio\./ or any
{$_=~/biblio/}keys %$filters_hashref);
215 $strcalc .= "LEFT JOIN items ON reserves.itemnumber=items.itemnumber "
216 if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or any
{$_=~/items/}keys %$filters_hashref);
222 ($debug) and print STDERR Dump
($filters_hashref);
223 foreach my $filter (keys %$filters_hashref){
225 my $stringfield=$filter;
226 $stringfield=~s/\_[a-z_]+$//;
228 $string=$stringfield;
230 elsif ($filter=~/_or/){
231 push @sqlor, qq{( }.changeifreservestatus
($filter)." = ? ) ";
232 push @sqlorparams, $$filters_hashref{$filter};
234 elsif ($filter=~/_endex$/){
235 $string = " $stringfield < ? ";
237 elsif ($filter=~/_end$/){
238 $string = " $stringfield <= ? ";
240 elsif ($filter=~/_begin$/){
241 $string = " $stringfield >= ? ";
244 $string = " $stringfield LIKE ? ";
247 push @sqlwhere, $string;
248 push @sqlparams, $$filters_hashref{$filter};
252 $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
253 $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
254 $strcalc .= " GROUP BY line, col )";
255 ($debug) and print STDERR
$strcalc;
256 my $dbcalc = $dbh->prepare($strcalc);
257 push @loopfilter, {crit
=>'SQL =', sql
=>1, filter
=>$strcalc};
258 @sqlparams=(@sqlparams,@sqlorparams);
259 $dbcalc->execute(@sqlparams);
260 my $data = $dbcalc->fetchall_hashref([qw(line col)]);
262 foreach my $row (keys %$data){
263 push @loopline, $row;
264 foreach my $col (keys %{$$data{$row}}){
265 $$data{$row}{totalrow
}+=$$data{$row}{$col}{calculation
};
266 $grantotal+=$$data{$row}{$col}{calculation
};
270 my $urlbase="do_it=1&".join("&",map{"filter_$_=$$filters_hashref{$_}"} keys %$filters_hashref);
271 foreach my $row (sort @loopline) {
273 #@loopcol ensures the order for columns is common with column titles
274 # and the number matches the number of columns
275 foreach my $col (sort keys %cols_hash) {
276 push @loopcell, {value
=>( $$data{$row}{$col}{calculation
} or ""),
277 # url_complement=>($urlbase=~/&$/?$urlbase."&":$urlbase)."filter_$linefield=$row&filter_$colfield=$col"
281 'rowtitle_display' => display_value
($linefield,$row),
283 'loopcell' => \
@loopcell,
284 'totalrow' => $$data{$row}{totalrow
}
287 for my $col ( sort keys %cols_hash ) {
289 foreach my $row (@loopline) {
290 $total += $data->{$row}{$col}{calculation
} if $data->{$row}{$col}{calculation
};
291 $debug and warn "value added ".$$data{$row}{$col}{calculation
}. "for line ".$row;
293 push @loopfooter, {'totalcol' => $total};
294 push @loopcol, {'coltitle' => $col,
295 coltitle_display
=>display_value
($colfield,$col)};
297 # the header of the table
298 $globalline{loopfilter
}=\
@loopfilter;
299 # the core of the table
300 $globalline{looprow
} = \
@looprow;
301 $globalline{loopcol
} = \
@loopcol;
302 # # the foot (totals by borrower type)
303 $globalline{loopfooter
} = \
@loopfooter;
304 $globalline{total
} = $grantotal;
305 $globalline{line
} = $linefield;
306 $globalline{column
} = $colfield;
307 return [(\
%globalline)];
311 my ( $crit, $value ) = @_;
312 my $locations = { map { ( $_->{authorised_value
} => $_->{lib
} ) } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' }, { order_by
=> ['description'] } ) };
313 my $ccodes = { map { ( $_->{authorised_value
} => $_->{lib
} ) } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.ccode' }, { order_by
=> ['description'] } ) };
314 my $Bsort1 = GetAuthorisedValues
("Bsort1");
315 my $Bsort2 = GetAuthorisedValues
("Bsort2");
317 ( $crit =~ /ccode/ ) ?
$ccodes->{$value}
318 : ( $crit =~ /location/ ) ?
$locations->{$value}
319 : ( $crit =~ /itemtype/ ) ? Koha
::ItemTypes
->find( $value )->translated_description
320 : ( $crit =~ /branch/ ) ? Koha
::Libraries
->find($value)->branchname
321 : ( $crit =~ /reservestatus/ ) ? reservestatushuman
($value)
322 : $value; # default fallback
323 if ($crit =~ /sort1/) {
325 ($value eq $_->{authorised_value
}) or next;
326 $display_value = $_->{lib
} and last;
329 elsif ($crit =~ /sort2/) {
331 ($value eq $_->{authorised_value
}) or next;
332 $display_value = $_->{lib
} and last;
335 elsif ( $crit =~ /category/ ) {
336 my @patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
337 foreach my $patron_category ( @patron_categories ) {
338 ( $value eq $patron_category->categorycode ) or next;
339 $display_value = $patron_category->description and last;
342 return $display_value;
345 sub reservestatushuman
{
358 sub changeifreservestatus
{
360 ($val=~/reservestatus/
362 when priority
>0 then
1
365 when found
='f' then
4
368 when cancellationdate is null then
3