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>.
24 use C4::Auth qw( get_template_and_user );
26 use C4::Koha qw( GetAuthorisedValues );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Reports qw( GetDelimiterChoices );
30 use Koha::AuthorisedValues;
33 use Koha::Patron::Categories;
34 use List::MoreUtils qw( any );
38 reports/reserve_stats.pl
42 Plugin that shows reserve stats
47 my $fullreportname = "reports/reserves_stats.tt";
48 my $do_it = $input->param('do_it');
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my $calc = $input->param("Cellvalue");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $hash_params = $input->Vars;
56 foreach my $filter (grep {$_ =~/^filter/} keys %$hash_params){
57 my $filterstring=$filter;
58 $filterstring=~s/^filter_//g;
59 $$filter_hashref{$filterstring}=$$hash_params{$filter} if (defined $$hash_params{$filter} && $$hash_params{$filter} ne "");
61 my ($template, $borrowernumber, $cookie) = get_template_and_user({
62 template_name => $fullreportname,
65 flagsrequired => {reports => '*'},
67 our $sep = C4::Context->csv_delimiter(scalar $input->param("sep"));
68 $template->param(do_it => $do_it,
71 my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
73 my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
74 my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
76 my $Bsort1 = GetAuthorisedValues("Bsort1");
77 my $Bsort2 = GetAuthorisedValues("Bsort2");
78 my ($hassort1,$hassort2);
79 $hassort1=1 if $Bsort1;
80 $hassort2=1 if $Bsort2;
85 my $results = calculate($line, $column, $calc, $filter_hashref);
86 if ($output eq "screen"){
87 # Printing results to screen
88 $template->param(mainloop => $results);
89 output_html_with_http_headers $input, $cookie, $template->output;
91 # Printing to a csv file
92 print $input->header(-type => 'application/vnd.sun.xml.calc',
94 -attachment=>"$basename.csv",
95 -filename=>"$basename.csv" );
96 my $cols = @$results[0]->{loopcol};
97 my $lines = @$results[0]->{looprow};
99 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
101 foreach my $col ( @$cols ) {
102 print $col->{coltitle}.$sep;
106 foreach my $line ( @$lines ) {
107 my $x = $line->{loopcell};
108 print $line->{rowtitle}.$sep;
109 print map {$_->{value}.$sep} @$x;
110 print $line->{totalrow}, "\n";
114 $cols = @$results[0]->{loopfooter};
115 print map {$sep.$_->{totalcol}} @$cols;
116 print $sep.@$results[0]->{total};
118 exit; # exit either way after $do_it
121 my $dbh = C4::Context->dbh;
123 my $itemtypes = Koha::ItemTypes->search_with_localization;
127 foreach (sort keys %$locations) {
128 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
132 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
133 push @ccodes, { code => $_, description => $ccodes->{$_} };
137 my $CGIextChoice = ( 'CSV' ); # FIXME translation
138 my $CGIsepChoice=GetDelimiterChoices;
141 categoryloop => \@patron_categories,
142 itemtypes => $itemtypes,
143 locationloop => \@locations,
144 ccodeloop => \@ccodes,
145 hassort1=> $hassort1,
146 hassort2=> $hassort2,
149 CGIextChoice => $CGIextChoice,
150 CGIsepChoice => $CGIsepChoice,
152 output_html_with_http_headers $input, $cookie, $template->output;
155 my ($linefield, $colfield, $process, $filters_hashref) = @_;
163 my $dbh = C4::Context->dbh;
169 foreach my $filter ( keys %$filters_hashref ) {
170 $filters_hashref->{$filter} =~ s/\*/%/;
177 filter => $filters_hashref->{$_},
179 } sort keys %$filters_hashref;
184 my $linesql=changeifreservestatus($linefield);
185 my $colsql=changeifreservestatus($colfield);
186 #Initialization of cell values.....
188 # preparing calculation
189 my $strcalc = "(SELECT $linesql line, $colsql col, ";
190 $strcalc .= ($process == 1) ? " COUNT(*) calculation" :
191 ($process == 2) ? "(COUNT(DISTINCT reserves.borrowernumber)) calculation" :
192 ($process == 3) ? "(COUNT(DISTINCT reserves.itemnumber)) calculation" :
193 ($process == 4) ? "(COUNT(DISTINCT reserves.biblionumber)) calculation" : '*';
195 FROM (select * from reserves union select * from old_reserves) reserves
196 LEFT JOIN borrowers USING (borrowernumber)
198 $strcalc .= "LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber "
199 if ($linefield =~ /^biblio\./ or $colfield =~ /^biblio\./ or any {$_=~/biblio/}keys %$filters_hashref);
200 $strcalc .= "LEFT JOIN items ON reserves.itemnumber=items.itemnumber "
201 if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or any {$_=~/items/}keys %$filters_hashref);
207 foreach my $filter (keys %$filters_hashref){
209 my $stringfield=$filter;
210 $stringfield=~s/\_[a-z_]+$//;
212 $string=$stringfield;
214 elsif ($filter=~/_or/){
215 push @sqlor, qq{( }.changeifreservestatus($filter)." = ? ) ";
216 push @sqlorparams, $$filters_hashref{$filter};
218 elsif ($filter=~/_endex$/){
219 $string = " $stringfield < ? ";
221 elsif ($filter=~/_end$/){
222 $string = " $stringfield <= ? ";
224 elsif ($filter=~/_begin$/){
225 $string = " $stringfield >= ? ";
228 $string = " $stringfield LIKE ? ";
231 push @sqlwhere, $string;
232 push @sqlparams, $$filters_hashref{$filter};
236 $strcalc .= " WHERE ".join(" AND ",@sqlwhere) if (@sqlwhere);
237 $strcalc .= " AND (".join(" OR ",@sqlor).")" if (@sqlor);
238 $strcalc .= " GROUP BY line, col )";
239 my $dbcalc = $dbh->prepare($strcalc);
240 push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strcalc};
241 @sqlparams=(@sqlparams,@sqlorparams);
242 $dbcalc->execute(@sqlparams);
243 my $data = $dbcalc->fetchall_hashref([qw(line col)]);
245 foreach my $row (keys %$data){
246 push @loopline, $row;
247 foreach my $col (keys %{$$data{$row}}){
248 $$data{$row}{totalrow}+=$$data{$row}{$col}{calculation};
249 $grantotal+=$$data{$row}{$col}{calculation};
253 my $urlbase="do_it=1&".join("&",map{"filter_$_=$$filters_hashref{$_}"} keys %$filters_hashref);
254 foreach my $row (sort @loopline) {
256 #@loopcol ensures the order for columns is common with column titles
257 # and the number matches the number of columns
258 foreach my $col (sort keys %cols_hash) {
259 push @loopcell, {value =>( $$data{$row}{$col}{calculation} or ""),
260 # url_complement=>($urlbase=~/&$/?$urlbase."&":$urlbase)."filter_$linefield=$row&filter_$colfield=$col"
264 'rowtitle_display' => display_value($linefield,$row),
266 'loopcell' => \@loopcell,
267 'totalrow' => $$data{$row}{totalrow}
270 for my $col ( sort keys %cols_hash ) {
272 foreach my $row (@loopline) {
273 $total += $data->{$row}{$col}{calculation} if $data->{$row}{$col}{calculation};
275 push @loopfooter, {'totalcol' => $total};
276 push @loopcol, {'coltitle' => $col,
277 coltitle_display=>display_value($colfield,$col)};
279 # the header of the table
280 $globalline{loopfilter}=\@loopfilter;
281 # the core of the table
282 $globalline{looprow} = \@looprow;
283 $globalline{loopcol} = \@loopcol;
284 # # the foot (totals by borrower type)
285 $globalline{loopfooter} = \@loopfooter;
286 $globalline{total} = $grantotal;
287 $globalline{line} = $linefield;
288 $globalline{column} = $colfield;
289 return [(\%globalline)];
293 my ( $crit, $value ) = @_;
294 my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
295 my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
296 my $Bsort1 = GetAuthorisedValues("Bsort1");
297 my $Bsort2 = GetAuthorisedValues("Bsort2");
299 ( $crit =~ /ccode/ ) ? $ccodes->{$value}
300 : ( $crit =~ /location/ ) ? $locations->{$value}
301 : ( $crit =~ /itemtype/ ) ? Koha::ItemTypes->find( $value )->translated_description
302 : ( $crit =~ /branch/ ) ? Koha::Libraries->find($value)->branchname
303 : $value; # default fallback
304 if ($crit =~ /sort1/) {
306 ($value eq $_->{authorised_value}) or next;
307 $display_value = $_->{lib} and last;
310 elsif ($crit =~ /sort2/) {
312 ($value eq $_->{authorised_value}) or next;
313 $display_value = $_->{lib} and last;
316 elsif ( $crit =~ /category/ ) {
317 my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
318 foreach my $patron_category ( @patron_categories ) {
319 ( $value eq $patron_category->categorycode ) or next;
320 $display_value = $patron_category->description and last;
323 return $display_value;
326 sub changeifreservestatus{
328 return $field unless $field =~ /reservestatus/;
329 # For reservestatus we return C- Cancelled, W- Waiting, F- Filled, P- Placed (or other like processing, transit etc.)
330 return q|CASE WHEN cancellationdate IS NOT NULL THEN 'C' WHEN found='W' THEN 'W' WHEN found='F' THEN 'F' ELSE 'P' END|;