Bug 20068: Check if user logged in before passing dashboard params to template
[koha.git] / reports / catalogue_stats.pl
blobdcd28894c063ec8cce72ab03c5a5f8dbafcc9a16
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Reports;
29 use C4::Circulation;
30 use C4::Biblio qw/GetMarcSubfieldStructureFromKohaField/;
32 use Koha::AuthorisedValues;
33 use Koha::DateUtils;
34 use Koha::ItemTypes;
36 =head1 NAME
38 plugin that shows a stats on borrowers
40 =head1 DESCRIPTION
42 =cut
44 our $debug = 0;
45 my $input = new CGI;
46 my $fullreportname = "reports/catalogue_stats.tt";
47 my $do_it = $input->param('do_it');
48 my $line = $input->param("Line");
49 my $column = $input->param("Column");
50 my $cellvalue = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
51 my @filters = $input->multi_param("Filter");
52 my $cotedigits = $input->param("cotedigits");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 our $sep = $input->param("sep");
56 $sep = "\t" if ($sep eq 'tabulation');
57 my $item_itype;
58 if(C4::Context->preference('item-level_itypes')) {
59 $item_itype = "items\.itype"
60 } else {
61 $item_itype = "itemtype";
63 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
64 $line = "copyrightdate";
66 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
67 $column = "copyrightdate";
70 my ($template, $borrowernumber, $cookie)
71 = get_template_and_user({template_name => $fullreportname,
72 query => $input,
73 type => "intranet",
74 authnotrequired => 0,
75 flagsrequired => {reports => '*'},
76 debug => 1,
77 });
78 $template->param(do_it => $do_it);
79 if ($do_it) {
80 my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
81 if ( $output eq "screen" ) {
82 $template->param( mainloop => $results );
83 output_html_with_http_headers $input, $cookie, $template->output;
84 exit;
85 } else {
86 print $input->header(
87 -type => 'text/csv',
88 -encoding => 'utf-8',
89 -attachment => "$basename.csv",
90 -name => "$basename.csv"
92 my $cols = @$results[0]->{loopcol};
93 my $lines = @$results[0]->{looprow};
94 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
95 foreach my $col (@$cols) {
96 print $col->{coltitle} . $sep;
98 print "Total\n";
99 foreach my $line (@$lines) {
100 my $x = $line->{loopcell};
101 print $line->{rowtitle} . $sep;
102 foreach my $cell (@$x) {
103 print $cell->{value} . $sep;
105 print $line->{totalrow};
106 print "\n";
108 print "TOTAL";
109 $cols = @$results[0]->{loopfooter};
110 foreach my $col (@$cols) {
111 print $sep. $col->{totalcol};
113 print $sep. @$results[0]->{total};
114 exit;
116 } else {
117 my $dbh = C4::Context->dbh;
118 my @values;
119 my %labels;
120 my $count=0;
121 my $req;
122 my @select;
124 my $itemtypes = Koha::ItemTypes->search_with_localization;
126 my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
127 my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
129 foreach my $kohafield (qw(items.notforloan items.materials)) {
130 my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
131 if($subfield_structure) {
132 my $avlist;
133 my $avcategory = $subfield_structure->{authorised_value};
134 if($avcategory) {
135 $avlist = GetAuthorisedValues($avcategory);
137 my $kf = $kohafield;
138 $kf =~ s/^items\.//;
139 $template->param(
140 $kf => 1,
141 $kf."_label" => $subfield_structure->{liblibrarian},
142 $kf."_avlist" => $avlist
147 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
149 $template->param(
150 itemtypes => $itemtypes,
151 locationloop => \@locations,
152 authvals => \@authvals,
153 CGIextChoice => \@mime,
154 CGIsepChoice => GetDelimiterChoices,
155 item_itype => $item_itype,
159 output_html_with_http_headers $input, $cookie, $template->output;
161 ## End of Main Body
164 sub calculate {
165 my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
166 my @mainloop;
167 my @loopfooter;
168 my @loopcol;
169 my @loopline;
170 my @looprow;
171 my %globalline;
172 my $grantotal = 0;
173 my $barcodelike = @$filters[16];
174 my $barcodefilter = @$filters[17];
175 my $not;
176 my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
178 my $dbh = C4::Context->dbh;
180 # if barcodefilter is empty set as %
181 if ($barcodefilter) {
183 # Check if barcodefilter is "like" or "not like"
184 if ( !$barcodelike ) {
185 $not = "not";
188 # Change * to %
189 $barcodefilter =~ s/\*/%/g;
192 # Filters
193 # Checking filters
195 my @loopfilter;
196 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
197 my %cell;
198 if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
199 if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
200 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
202 $cell{filter} .= @$filters[$i];
203 $cell{crit} .=
204 ( $i == 0 ) ? "Item CallNumber From"
205 : ( $i == 1 ) ? "Item CallNumber To"
206 : ( $i == 2 ) ? "Item type"
207 : ( $i == 3 ) ? "Publisher"
208 : ( $i == 4 ) ? "Publication year From"
209 : ( $i == 5 ) ? "Publication year To"
210 : ( $i == 6 ) ? "Library"
211 : ( $i == 7 ) ? "Shelving Location"
212 : ( $i == 8 ) ? "Collection Code"
213 : ( $i == 9 ) ? "Status"
214 : ( $i == 10 ) ? "Materials"
215 : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
216 : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
217 : ( $i == 13 ) ? "Date acquired (item) from"
218 : ( $i == 14 ) ? "Date acquired (item) to"
219 : ( $i == 15 ) ? "Date deleted (item) from"
220 : ( $i == 16 ) ? "Date deleted (item) to"
221 : '';
223 push @loopfilter, \%cell;
227 @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
228 @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
229 @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
230 @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
232 my @linefilter;
233 $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
234 $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
235 if ( C4::Context->preference('item-level_itypes') ) {
236 $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
237 } else {
238 $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
240 $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
241 $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
242 $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
244 $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
245 $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
246 $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
247 $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
248 $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
249 $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
250 $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
251 $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
252 $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
254 my @colfilter;
255 $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
256 $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
257 if ( C4::Context->preference('item-level_itypes') ) {
258 $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
259 } else {
260 $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
262 $colfilter[0] = @$filters[3] if ( $column =~ /publishercode/ );
263 $colfilter[0] = @$filters[4] if ( $column =~ /publicationyear/ );
264 $colfilter[1] = @$filters[5] if ( $column =~ /publicationyear/ );
265 $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
266 $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
267 $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
268 $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
269 $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
270 $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
271 $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
272 $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
273 $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
275 # 1st, loop rows.
276 my $origline = $line;
277 $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
278 my $linefield;
279 if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
280 $linefield = "left($line,$cotedigits)";
281 } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
282 $linefield = "DATE($line)";
283 } else {
284 $linefield = $line;
287 my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
288 LEFT JOIN biblioitems USING (biblioitemnumber)
289 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
290 WHERE 1 ";
291 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
292 if (@linefilter) {
293 if ( $linefilter[1] ) {
294 $strsth .= " AND $line >= ? ";
295 $strsth .= " AND $line <= ? ";
296 } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
297 $linefilter[0] =~ s/\*/%/g;
298 $strsth .= " AND $line LIKE ? ";
301 $strsth .= " ORDER BY $linefield";
302 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
304 my $sth = $dbh->prepare($strsth);
305 if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
306 $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
307 } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
308 $sth->execute( $linefilter[0], $linefilter[1] );
309 } elsif ( $barcodefilter and $linefilter[0] ) {
310 $sth->execute( $barcodefilter, $linefilter[0] );
311 } elsif ( $linefilter[0] ) {
312 $sth->execute($linefilter[0]);
313 } elsif ($barcodefilter) {
314 $sth->execute($barcodefilter);
315 } else {
316 $sth->execute();
318 my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
319 while ( my ($celvalue) = $sth->fetchrow ) {
320 my %cell;
321 if (defined $celvalue and $celvalue ne '') {
322 if($rowauthvals and $rowauthvals->{$celvalue}) {
323 $cell{rowtitle} = $rowauthvals->{$celvalue};
324 } else {
325 $cell{rowtitle} = $celvalue;
327 $cell{value} = $celvalue;
329 else {
330 $cell{rowtitle} = "NULL";
331 $cell{value} = "zzEMPTY";
333 $cell{totalrow} = 0;
334 push @loopline, \%cell;
337 # 2nd, loop cols.
338 my $origcolumn = $column;
339 $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
340 my $colfield;
341 if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
342 $colfield = "left($column,$cotedigits)";
343 } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
344 $colfield = "DATE($column)";
345 } else {
346 $colfield = $column;
349 my $strsth2 = "
350 SELECT distinctrow $colfield
351 FROM $itemstable
352 LEFT JOIN biblioitems
353 USING (biblioitemnumber)
354 LEFT JOIN biblio
355 ON (biblioitems.biblionumber = biblio.biblionumber)
356 WHERE 1 ";
357 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
359 if ( (@colfilter) and ( $colfilter[1] ) ) {
360 $strsth2 .= " AND $column >= ? AND $column <= ?";
361 } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
362 $colfilter[0] =~ s/\*/%/g;
363 $strsth2 .= " AND $column LIKE ? ";
365 $strsth2 .= " ORDER BY $colfield";
366 $debug and print STDERR "SQL: $strsth2";
367 my $sth2 = $dbh->prepare($strsth2);
368 if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
369 $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
370 } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
371 $sth2->execute( $colfilter[0], $colfilter[1] );
372 } elsif ( $barcodefilter && $colfilter[0] ) {
373 $sth2->execute( $barcodefilter , $colfilter[0] );
374 } elsif ( $colfilter[0]) {
375 $sth2->execute( $colfilter[0] );
376 } elsif ($barcodefilter) {
377 $sth2->execute($barcodefilter);
378 } else {
379 $sth2->execute();
381 my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
382 while ( my ($celvalue) = $sth2->fetchrow ) {
383 my %cell;
384 if (defined $celvalue and $celvalue ne '') {
385 if($colauthvals and $colauthvals->{$celvalue}) {
386 $cell{coltitle} = $colauthvals->{$celvalue};
387 } else {
388 $cell{coltitle} = $celvalue;
390 $cell{value} = $celvalue;
392 else {
393 $cell{coltitle} = "NULL";
394 $cell{value} = "zzEMPTY";
396 $cell{totalcol} = 0;
397 push @loopcol, \%cell;
400 my $i = 0;
401 my @totalcol;
402 my $hilighted = -1;
404 #Initialization of cell values.....
405 my %table;
407 foreach my $row (@loopline) {
408 foreach my $col (@loopcol) {
409 $table{ $row->{value} }->{ $col->{value} } = 0;
411 $table{ $row->{value} }->{totalrow} = 0;
414 # preparing calculation
415 my $select_cellvalue = " COUNT(*) ";
416 $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
417 my $strcalc = "
418 SELECT $linefield, $colfield, $select_cellvalue
419 FROM $itemstable
420 LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
421 LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
422 WHERE 1 ";
424 my @sqlargs;
426 if ($barcodefilter) {
427 $strcalc .= "AND barcode $not like ? ";
428 push @sqlargs, $barcodefilter;
431 if ( @$filters[0] ) {
432 $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
433 @$filters[0] =~ s/\*/%/g;
434 push @sqlargs, @$filters[0];
437 if ( @$filters[1] ) {
438 $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
439 @$filters[1] =~ s/\*/%/g;
440 push @sqlargs, @$filters[1];
443 if ( @$filters[2] ) {
444 $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
445 @$filters[2] =~ s/\*/%/g;
446 push @sqlargs, @$filters[2];
449 if ( @$filters[3] ) {
450 $strcalc .= " AND biblioitems.publishercode LIKE ? ";
451 @$filters[3] =~ s/\*/%/g;
452 @$filters[3] .= "%" unless @$filters[3] =~ /%/;
453 push @sqlargs, @$filters[3];
455 if ( @$filters[4] ) {
456 $strcalc .= " AND " .
457 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
458 . "> ? ";
459 @$filters[4] =~ s/\*/%/g;
460 push @sqlargs, @$filters[4];
462 if ( @$filters[5] ) {
463 @$filters[5] =~ s/\*/%/g;
464 $strcalc .= " AND " .
465 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
466 . "< ? ";
467 push @sqlargs, @$filters[5];
469 if ( @$filters[6] ) {
470 $strcalc .= " AND $itemstable.homebranch LIKE ? ";
471 @$filters[6] =~ s/\*/%/g;
472 push @sqlargs, @$filters[6];
474 if ( @$filters[7] ) {
475 $strcalc .= " AND $itemstable.location LIKE ? ";
476 @$filters[7] =~ s/\*/%/g;
477 push @sqlargs, @$filters[7];
479 if ( @$filters[8] ) {
480 $strcalc .= " AND $itemstable.ccode LIKE ? ";
481 @$filters[8] =~ s/\*/%/g;
482 push @sqlargs, @$filters[8];
484 if ( defined @$filters[9] and @$filters[9] ne '' ) {
485 $strcalc .= " AND $itemstable.notforloan LIKE ? ";
486 @$filters[9] =~ s/\*/%/g;
487 push @sqlargs, @$filters[9];
489 if ( defined @$filters[10] and @$filters[10] ne '' ) {
490 $strcalc .= " AND $itemstable.materials LIKE ? ";
491 @$filters[10] =~ s/\*/%/g;
492 push @sqlargs, @$filters[10];
494 if ( @$filters[13] ) {
495 $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
496 @$filters[13] =~ s/\*/%/g;
497 push @sqlargs, @$filters[13];
499 if ( @$filters[14] ) {
500 $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
501 @$filters[14] =~ s/\*/%/g;
502 push @sqlargs, @$filters[14];
504 if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
505 $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
506 @$filters[15] =~ s/\*/%/g;
507 push @sqlargs, @$filters[15];
509 if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
510 @$filters[16] =~ s/\*/%/g;
511 $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
512 push @sqlargs, @$filters[16];
514 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
515 $debug and warn "SQL: $strcalc";
516 my $dbcalc = $dbh->prepare($strcalc);
517 $dbcalc->execute(@sqlargs);
519 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
521 $col = "zzEMPTY" if ( !defined($col) );
522 $row = "zzEMPTY" if ( !defined($row) );
524 $table{$row}->{$col} += $value;
525 $table{$row}->{totalrow} += $value;
526 $grantotal += $value;
529 foreach my $row ( @loopline ) {
530 my @loopcell;
532 #@loopcol ensures the order for columns is common with column titles
533 # and the number matches the number of columns
534 foreach my $col (@loopcol) {
535 my $value = $table{$row->{value}}->{ $col->{value} };
536 push @loopcell, { value => $value };
538 push @looprow,
539 { 'rowtitle' => $row->{rowtitle},
540 'value' => $row->{value},
541 'loopcell' => \@loopcell,
542 'hilighted' => ( $hilighted *= -1 > 0 ),
543 'totalrow' => $table{$row->{value}}->{totalrow}
547 foreach my $col (@loopcol) {
548 my $total = 0;
549 foreach my $row (@looprow) {
550 $total += $table{ $row->{value} }->{ $col->{value} };
553 push @loopfooter, { 'totalcol' => $total };
556 # the header of the table
557 $globalline{loopfilter} = \@loopfilter;
559 # the core of the table
560 $globalline{looprow} = \@looprow;
561 $globalline{loopcol} = \@loopcol;
563 # the foot (totals by borrower type)
564 $globalline{loopfooter} = \@loopfooter;
565 $globalline{total} = $grantotal;
566 $globalline{line} = $line;
567 $globalline{column} = $column;
568 push @mainloop, \%globalline;
569 return \@mainloop;