Bug 15468 - Search links on callnumbers with parentheses fails on OPAC results page
[koha.git] / reports / catalogue_stats.pl
blobc0b11da2cbbed052aef68f40106a9efe86158832
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::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Reports;
30 use C4::Circulation;
32 =head1 NAME
34 plugin that shows a stats on borrowers
36 =head1 DESCRIPTION
38 =over 2
40 =cut
42 our $debug = 0;
43 my $input = new CGI;
44 my $fullreportname = "reports/catalogue_stats.tt";
45 my $do_it = $input->param('do_it');
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $deweydigits = $input->param("deweydigits");
50 my $lccndigits = $input->param("lccndigits");
51 my $cotedigits = $input->param("cotedigits");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 our $sep = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 my $item_itype;
57 if(C4::Context->preference('item-level_itypes')) {
58 $item_itype = "items\.itype"
59 } else {
60 $item_itype = "itemtype";
62 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
63 $line = "copyrightdate";
65 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
66 $column = "copyrightdate";
69 my ($template, $borrowernumber, $cookie)
70 = get_template_and_user({template_name => $fullreportname,
71 query => $input,
72 type => "intranet",
73 authnotrequired => 0,
74 flagsrequired => {reports => '*'},
75 debug => 1,
76 });
77 $template->param(do_it => $do_it);
78 if ($do_it) {
79 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
80 if ($output eq "screen"){
81 $template->param(mainloop => $results);
82 output_html_with_http_headers $input, $cookie, $template->output;
83 exit;
84 } else {
85 print $input->header(-type => 'application/vnd.sun.xml.calc',
86 -encoding => 'utf-8',
87 -attachment=>"$basename.csv",
88 -name=>"$basename.csv" );
89 my $cols = @$results[0]->{loopcol};
90 my $lines = @$results[0]->{looprow};
91 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
92 foreach my $col ( @$cols ) {
93 print $col->{coltitle}.$sep;
95 print "Total\n";
96 foreach my $line ( @$lines ) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle}.$sep;
99 foreach my $cell (@$x) {
100 print $cell->{value}.$sep;
102 print $line->{totalrow};
103 print "\n";
105 print "TOTAL";
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col ( @$cols ) {
108 print $sep.$col->{totalcol};
110 print $sep.@$results[0]->{total};
111 exit;
113 } else {
114 my $dbh = C4::Context->dbh;
115 my @values;
116 my %labels;
117 my $count=0;
118 my $req;
119 my @select;
120 # FIXME: no such field "dewey"
121 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
122 # $req->execute;
123 my $hasdewey = 0;
125 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
126 # This is not the LC Classification. It's the Control Number.
127 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
129 my $haslccn = 0;
130 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
131 # $req->execute;
132 # my $hlghtlccn;
133 # while (my ($value) =$req->fetchrow) {
134 # $hlghtlccn = !($hasdewey);
135 # $haslccn =1 if (($value>2) and (! $haslccn));
136 # $count++ if (($value) and (! $haslccn));
137 # push @select, $value;
139 # my $CGIlccn = {
140 # values => \@select,
141 # };
143 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
144 # FIXME: Hardcoding to 5 chars on itemcallnum.
146 my $hascote = 1;
147 my $highcote = 5;
149 my $itemtypes = GetItemTypes( style => 'array' );
151 my $authvals = GetKohaAuthorisedValues("items.ccode");
152 my @authvals;
153 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
154 push @authvals, { code => $_, description => $authvals->{$_} };
157 my $locations = GetKohaAuthorisedValues("items.location");
158 my @locations;
159 foreach (sort keys %$locations) {
160 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
163 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
165 $template->param(hasdewey=>$hasdewey,
166 haslccn => $haslccn,
167 hascote => $hascote,
168 itemtypes => $itemtypes,
169 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
170 locationloop => \@locations,
171 authvals => \@authvals,
172 CGIextChoice => \@mime,
173 CGIsepChoice => GetDelimiterChoices,
174 item_itype => $item_itype
178 output_html_with_http_headers $input, $cookie, $template->output;
180 ## End of Main Body
183 sub calculate {
184 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
185 my @mainloop;
186 my @loopfooter;
187 my @loopcol;
188 my @loopline;
189 my @looprow;
190 my %globalline;
191 my $grantotal =0;
192 my $barcodelike = @$filters[13];
193 my $barcodefilter = @$filters[14];
194 my $not;
196 # extract parameters
197 my $dbh = C4::Context->dbh;
199 # if barcodefilter is empty set as %
200 if($barcodefilter){
201 # Check if barcodefilter is "like" or "not like"
202 if(!$barcodelike){
203 $not = "not";
205 # Change * to %
206 $barcodefilter =~ s/\*/%/g;
209 # Filters
210 # Checking filters
212 my @loopfilter;
213 for (my $i=0;$i<=12;$i++) {
214 my %cell;
215 if ( @$filters[$i] ) {
216 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
217 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
219 $cell{filter} .= @$filters[$i];
220 $cell{crit} .=
221 ($i== 0) ? "Dewey Classification From" :
222 ($i== 1) ? "Dewey Classification To" :
223 ($i== 2) ? "Lccn Classification From" :
224 ($i== 3) ? "Lccn Classification To" :
225 ($i== 4) ? "Item CallNumber From" :
226 ($i== 5) ? "Item CallNumber To" :
227 ($i== 6) ? "Item type" :
228 ($i== 7) ? "Publisher" :
229 ($i== 8) ? "Publication year From" :
230 ($i== 9) ? "Publication year To" :
231 ($i==10) ? "Library :" :
232 ($i==11) ? "Shelving Location :" :
233 ($i==12) ? "Collection Code :" : '';
234 push @loopfilter, \%cell;
238 # warn map {"filtres $_\n"} @filters[0..3];
240 my @linefilter;
241 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
242 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
243 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
244 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
245 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
246 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
247 if (C4::Context->preference('item-level_itypes')) {
248 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
249 } else {
250 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
252 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
253 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
254 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
255 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
256 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
257 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
259 my @colfilter ;
260 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
261 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
262 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
263 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
264 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
265 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
266 if (C4::Context->preference('item-level_itypes')) {
267 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
268 } else {
269 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
271 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
272 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
273 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
274 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
275 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
276 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
278 # 1st, loop rows.
279 my $linefield;
280 if (($line =~/dewey/) and ($deweydigits)) {
281 $linefield .="left($line,$deweydigits)";
282 } elsif (($line=~/lccn/) and ($lccndigits)) {
283 $linefield .="left($line,$lccndigits)";
284 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
285 $linefield .="left($line,$cotedigits)";
286 }else {
287 $linefield .= $line;
290 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
291 INNER JOIN items USING (biblioitemnumber)
292 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
293 WHERE $line IS NOT NULL ";
294 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
295 if ( @linefilter ) {
296 if ($linefilter[1]){
297 $strsth .= " AND $line >= ? " ;
298 $strsth .= " AND $line <= ? " ;
299 } elsif ($linefilter[0]) {
300 $linefilter[0] =~ s/\*/%/g;
301 $strsth .= " AND $line LIKE ? " ;
304 $strsth .=" ORDER BY $linefield";
305 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
307 my $sth = $dbh->prepare( $strsth );
308 if (( @linefilter ) and ($linefilter[1])){
309 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
310 } elsif ($barcodefilter,$linefilter[0]) {
311 $sth->execute($barcodefilter,$linefilter[0]);
312 } elsif ($barcodefilter) {
313 $sth->execute($barcodefilter);
314 }else{
315 $sth->execute();
317 while ( my ($celvalue) = $sth->fetchrow) {
318 my %cell;
319 if ($celvalue) {
320 $cell{rowtitle} = $celvalue;
321 # } else {
322 # $cell{rowtitle} = "";
324 $cell{totalrow} = 0;
325 push @loopline, \%cell;
328 # 2nd, loop cols.
329 my $colfield;
330 if (($column =~/dewey/) and ($deweydigits)) {
331 $colfield = "left($column,$deweydigits)";
332 }elsif (($column=~/lccn/) and ($lccndigits)) {
333 $colfield = "left($column,$lccndigits)";
334 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
335 $colfield = "left($column,$cotedigits)";
336 }else {
337 $colfield = $column;
340 my $strsth2 = "
341 SELECT distinctrow $colfield
342 FROM biblioitems
343 INNER JOIN items
344 USING (biblioitemnumber)
345 INNER JOIN biblio
346 ON (biblioitems.biblionumber = biblio.biblionumber)
347 WHERE $column IS NOT NULL ";
348 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
350 if (( @colfilter ) and ($colfilter[1])) {
351 $strsth2 .= " AND $column> ? AND $column< ?";
352 }elsif ($colfilter[0]){
353 $colfilter[0] =~ s/\*/%/g;
354 $strsth2 .= " AND $column LIKE ? ";
356 $strsth2 .= " ORDER BY $colfield";
357 $debug and print STDERR "SQL: $strsth2";
358 my $sth2 = $dbh->prepare( $strsth2 );
359 if ((@colfilter) and ($colfilter[1])) {
360 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
361 } elsif ($colfilter[0]){
362 $sth2->execute($barcodefilter,$colfilter[0]);
363 } elsif ($barcodefilter){
364 $sth2->execute($barcodefilter);
365 } else {
366 $sth2->execute();
368 while (my ($celvalue) = $sth2->fetchrow) {
369 my %cell;
370 my %ft;
371 if ($celvalue) {
372 $cell{coltitle} = $celvalue;
373 # } else {
374 # $cell{coltitle} = "";
376 $ft{totalcol} = 0;
377 push @loopcol, \%cell;
380 my $i=0;
381 my @totalcol;
382 my $hilighted=-1;
384 #Initialization of cell values.....
385 my %table;
386 # warn "init table";
387 foreach my $row ( @loopline ) {
388 foreach my $col ( @loopcol ) {
389 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
390 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
392 $table{$row->{rowtitle}}->{totalrow}=0;
395 # preparing calculation
396 my $strcalc = "
397 SELECT $linefield, $colfield, count(*)
398 FROM biblioitems
399 INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
400 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
401 WHERE 1 ";
402 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
404 if (@$filters[0]){
405 @$filters[0]=~ s/\*/%/g;
406 $strcalc .= " AND dewey >" . @$filters[0];
408 if (@$filters[1]){
409 @$filters[1]=~ s/\*/%/g ;
410 $strcalc .= " AND dewey <" . @$filters[1];
412 if (@$filters[2]){
413 @$filters[2]=~ s/\*/%/g ;
414 $strcalc .= " AND lccn >" . @$filters[2];
416 if (@$filters[3]){
417 @$filters[3]=~ s/\*/%/g;
418 $strcalc .= " AND lccn <" . @$filters[3];
420 if (@$filters[4]){
421 @$filters[4]=~ s/\*/%/g ;
422 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
425 if (@$filters[5]){
426 @$filters[5]=~ s/\*/%/g;
427 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
430 if (@$filters[6]){
431 @$filters[6]=~ s/\*/%/g;
432 $strcalc .= " AND " .
433 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
434 . " LIKE '" . @$filters[6] ."'";
437 if (@$filters[7]){
438 @$filters[7]=~ s/\*/%/g;
439 @$filters[7].="%" unless @$filters[7]=~/%/;
440 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
442 if (@$filters[8]){
443 @$filters[8]=~ s/\*/%/g;
444 $strcalc .= " AND " .
445 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
446 . ">" . @$filters[8];
448 if (@$filters[9]){
449 @$filters[9]=~ s/\*/%/g;
450 $strcalc .= " AND " .
451 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
452 . "<" . @$filters[9];
454 if (@$filters[10]){
455 @$filters[10]=~ s/\*/%/g;
456 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
458 if (@$filters[11]){
459 @$filters[11]=~ s/\*/%/g;
460 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
462 if (@$filters[12]){
463 @$filters[12]=~ s/\*/%/g;
464 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
467 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
468 $debug and warn "SQL: $strcalc";
469 my $dbcalc = $dbh->prepare($strcalc);
470 if($barcodefilter){
471 $dbcalc->execute($barcodefilter);
472 }else{
473 $dbcalc->execute();
475 # warn "filling table";
476 my $emptycol;
477 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
478 # warn "filling table $row / $col / $value ";
479 $emptycol = 1 if (!defined($col));
480 $col = "zzEMPTY" if (!defined($col));
481 $row = "zzEMPTY" if (!defined($row));
483 $table{$row}->{$col}+=$value;
484 $table{$row}->{totalrow}+=$value;
485 $grantotal += $value;
488 # my %cell = {rowtitle => 'zzROWEMPTY'};
489 # push @loopline,\%cell;
490 # undef %cell;
491 # my %cell;
492 # %cell = {coltitle => "zzEMPTY"};
493 push @loopcol,{coltitle => "NULL"} if ($emptycol);
495 foreach my $row ( sort keys %table ) {
496 my @loopcell;
497 #@loopcol ensures the order for columns is common with column titles
498 # and the number matches the number of columns
499 foreach my $col ( @loopcol ) {
500 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
501 push @loopcell, {value => $value } ;
503 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
504 'loopcell' => \@loopcell,
505 'hilighted' => ($hilighted *= -1 > 0),
506 'totalrow' => $table{$row}->{totalrow}
510 # warn "footer processing";
511 foreach my $col ( @loopcol ) {
512 my $total=0;
513 foreach my $row ( @looprow ) {
514 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
515 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
517 # warn "summ for column ".$col->{coltitle}." = ".$total;
518 push @loopfooter, {'totalcol' => $total};
522 # the header of the table
523 $globalline{loopfilter}=\@loopfilter;
524 # the core of the table
525 $globalline{looprow} = \@looprow;
526 $globalline{loopcol} = \@loopcol;
527 # # the foot (totals by borrower type)
528 $globalline{loopfooter} = \@loopfooter;
529 $globalline{total}= $grantotal;
530 $globalline{line} = $line;
531 $globalline{column} = $column;
532 push @mainloop,\%globalline;
533 return \@mainloop;