Bug 11759: Add links to the new page
[koha.git] / reports / catalogue_stats.pl
blob5e7848d879eaff2d29006698ae9e072a413859d6
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 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
150 $req->execute;
151 my $CGIitemtype = $req->fetchall_arrayref({});
153 my $authvals = GetKohaAuthorisedValues("items.ccode");
154 my @authvals;
155 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
156 push @authvals, { code => $_, description => $authvals->{$_} };
159 my $locations = GetKohaAuthorisedValues("items.location");
160 my @locations;
161 foreach (sort keys %$locations) {
162 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
165 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
167 $template->param(hasdewey=>$hasdewey,
168 haslccn => $haslccn,
169 hascote => $hascote,
170 CGIItemType => $CGIitemtype,
171 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
172 locationloop => \@locations,
173 authvals => \@authvals,
174 CGIextChoice => \@mime,
175 CGIsepChoice => GetDelimiterChoices,
176 item_itype => $item_itype
180 output_html_with_http_headers $input, $cookie, $template->output;
182 ## End of Main Body
185 sub calculate {
186 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
187 my @mainloop;
188 my @loopfooter;
189 my @loopcol;
190 my @loopline;
191 my @looprow;
192 my %globalline;
193 my $grantotal =0;
194 my $barcodelike = @$filters[13];
195 my $barcodefilter = @$filters[14];
196 my $not;
198 # extract parameters
199 my $dbh = C4::Context->dbh;
201 # if barcodefilter is empty set as %
202 if($barcodefilter){
203 # Check if barcodefilter is "like" or "not like"
204 if(!$barcodelike){
205 $not = "not";
207 # Change * to %
208 $barcodefilter =~ s/\*/%/g;
211 # Filters
212 # Checking filters
214 my @loopfilter;
215 for (my $i=0;$i<=12;$i++) {
216 my %cell;
217 if ( @$filters[$i] ) {
218 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
219 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
221 $cell{filter} .= @$filters[$i];
222 $cell{crit} .=
223 ($i== 0) ? "Dewey Classification From" :
224 ($i== 1) ? "Dewey Classification To" :
225 ($i== 2) ? "Lccn Classification From" :
226 ($i== 3) ? "Lccn Classification To" :
227 ($i== 4) ? "Item CallNumber From" :
228 ($i== 5) ? "Item CallNumber To" :
229 ($i== 6) ? "Item type" :
230 ($i== 7) ? "Publisher" :
231 ($i== 8) ? "Publication year From" :
232 ($i== 9) ? "Publication year To" :
233 ($i==10) ? "Library :" :
234 ($i==11) ? "Shelving Location :" :
235 ($i==12) ? "Collection Code :" : '';
236 push @loopfilter, \%cell;
240 # warn map {"filtres $_\n"} @filters[0..3];
242 my @linefilter;
243 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
244 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
245 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
246 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
247 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
248 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
249 if (C4::Context->preference('item-level_itypes')) {
250 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
251 } else {
252 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
254 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
255 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
256 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
257 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
258 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
259 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
261 my @colfilter ;
262 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
263 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
264 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
265 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
266 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
267 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
268 if (C4::Context->preference('item-level_itypes')) {
269 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
270 } else {
271 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
273 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
274 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
275 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
276 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
277 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
278 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
280 # 1st, loop rows.
281 my $linefield;
282 if (($line =~/dewey/) and ($deweydigits)) {
283 $linefield .="left($line,$deweydigits)";
284 } elsif (($line=~/lccn/) and ($lccndigits)) {
285 $linefield .="left($line,$lccndigits)";
286 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
287 $linefield .="left($line,$cotedigits)";
288 }else {
289 $linefield .= $line;
292 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
293 INNER JOIN items USING (biblioitemnumber)
294 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
295 WHERE $line IS NOT NULL ";
296 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
297 if ( @linefilter ) {
298 if ($linefilter[1]){
299 $strsth .= " AND $line >= ? " ;
300 $strsth .= " AND $line <= ? " ;
301 } elsif ($linefilter[0]) {
302 $linefilter[0] =~ s/\*/%/g;
303 $strsth .= " AND $line LIKE ? " ;
306 $strsth .=" ORDER BY $linefield";
307 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
309 my $sth = $dbh->prepare( $strsth );
310 if (( @linefilter ) and ($linefilter[1])){
311 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
312 } elsif ($barcodefilter,$linefilter[0]) {
313 $sth->execute($barcodefilter,$linefilter[0]);
314 } elsif ($barcodefilter) {
315 $sth->execute($barcodefilter);
316 }else{
317 $sth->execute();
319 while ( my ($celvalue) = $sth->fetchrow) {
320 my %cell;
321 if ($celvalue) {
322 $cell{rowtitle} = $celvalue;
323 # } else {
324 # $cell{rowtitle} = "";
326 $cell{totalrow} = 0;
327 push @loopline, \%cell;
330 # 2nd, loop cols.
331 my $colfield;
332 if (($column =~/dewey/) and ($deweydigits)) {
333 $colfield = "left($column,$deweydigits)";
334 }elsif (($column=~/lccn/) and ($lccndigits)) {
335 $colfield = "left($column,$lccndigits)";
336 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
337 $colfield = "left($column,$cotedigits)";
338 }else {
339 $colfield = $column;
342 my $strsth2 = "
343 SELECT distinctrow $colfield
344 FROM biblioitems
345 INNER JOIN items
346 USING (biblioitemnumber)
347 INNER JOIN biblio
348 ON (biblioitems.biblionumber = biblio.biblionumber)
349 WHERE $column IS NOT NULL ";
350 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
352 if (( @colfilter ) and ($colfilter[1])) {
353 $strsth2 .= " AND $column> ? AND $column< ?";
354 }elsif ($colfilter[0]){
355 $colfilter[0] =~ s/\*/%/g;
356 $strsth2 .= " AND $column LIKE ? ";
358 $strsth2 .= " ORDER BY $colfield";
359 $debug and print STDERR "SQL: $strsth2";
360 my $sth2 = $dbh->prepare( $strsth2 );
361 if ((@colfilter) and ($colfilter[1])) {
362 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
363 } elsif ($colfilter[0]){
364 $sth2->execute($barcodefilter,$colfilter[0]);
365 } elsif ($barcodefilter){
366 $sth2->execute($barcodefilter);
367 } else {
368 $sth2->execute();
370 while (my ($celvalue) = $sth2->fetchrow) {
371 my %cell;
372 my %ft;
373 if ($celvalue) {
374 $cell{coltitle} = $celvalue;
375 # } else {
376 # $cell{coltitle} = "";
378 $ft{totalcol} = 0;
379 push @loopcol, \%cell;
382 my $i=0;
383 my @totalcol;
384 my $hilighted=-1;
386 #Initialization of cell values.....
387 my %table;
388 # warn "init table";
389 foreach my $row ( @loopline ) {
390 foreach my $col ( @loopcol ) {
391 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
392 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
394 $table{$row->{rowtitle}}->{totalrow}=0;
397 # preparing calculation
398 my $strcalc = "
399 SELECT $linefield, $colfield, count(*)
400 FROM biblioitems
401 INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
402 INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
403 WHERE 1 ";
404 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
406 if (@$filters[0]){
407 @$filters[0]=~ s/\*/%/g;
408 $strcalc .= " AND dewey >" . @$filters[0];
410 if (@$filters[1]){
411 @$filters[1]=~ s/\*/%/g ;
412 $strcalc .= " AND dewey <" . @$filters[1];
414 if (@$filters[2]){
415 @$filters[2]=~ s/\*/%/g ;
416 $strcalc .= " AND lccn >" . @$filters[2];
418 if (@$filters[3]){
419 @$filters[3]=~ s/\*/%/g;
420 $strcalc .= " AND lccn <" . @$filters[3];
422 if (@$filters[4]){
423 @$filters[4]=~ s/\*/%/g ;
424 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
427 if (@$filters[5]){
428 @$filters[5]=~ s/\*/%/g;
429 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
432 if (@$filters[6]){
433 @$filters[6]=~ s/\*/%/g;
434 $strcalc .= " AND " .
435 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
436 . " LIKE '" . @$filters[6] ."'";
439 if (@$filters[7]){
440 @$filters[7]=~ s/\*/%/g;
441 @$filters[7].="%" unless @$filters[7]=~/%/;
442 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
444 if (@$filters[8]){
445 @$filters[8]=~ s/\*/%/g;
446 $strcalc .= " AND " .
447 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
448 . ">" . @$filters[8];
450 if (@$filters[9]){
451 @$filters[9]=~ s/\*/%/g;
452 $strcalc .= " AND " .
453 (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
454 . "<" . @$filters[9];
456 if (@$filters[10]){
457 @$filters[10]=~ s/\*/%/g;
458 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
460 if (@$filters[11]){
461 @$filters[11]=~ s/\*/%/g;
462 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
464 if (@$filters[12]){
465 @$filters[12]=~ s/\*/%/g;
466 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
469 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
470 $debug and warn "SQL: $strcalc";
471 my $dbcalc = $dbh->prepare($strcalc);
472 if($barcodefilter){
473 $dbcalc->execute($barcodefilter);
474 }else{
475 $dbcalc->execute();
477 # warn "filling table";
478 my $emptycol;
479 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
480 # warn "filling table $row / $col / $value ";
481 $emptycol = 1 if (!defined($col));
482 $col = "zzEMPTY" if (!defined($col));
483 $row = "zzEMPTY" if (!defined($row));
485 $table{$row}->{$col}+=$value;
486 $table{$row}->{totalrow}+=$value;
487 $grantotal += $value;
490 # my %cell = {rowtitle => 'zzROWEMPTY'};
491 # push @loopline,\%cell;
492 # undef %cell;
493 # my %cell;
494 # %cell = {coltitle => "zzEMPTY"};
495 push @loopcol,{coltitle => "NULL"} if ($emptycol);
497 foreach my $row ( sort keys %table ) {
498 my @loopcell;
499 #@loopcol ensures the order for columns is common with column titles
500 # and the number matches the number of columns
501 foreach my $col ( @loopcol ) {
502 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
503 push @loopcell, {value => $value } ;
505 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
506 'loopcell' => \@loopcell,
507 'hilighted' => ($hilighted *= -1 > 0),
508 'totalrow' => $table{$row}->{totalrow}
512 # warn "footer processing";
513 foreach my $col ( @loopcol ) {
514 my $total=0;
515 foreach my $row ( @looprow ) {
516 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
517 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
519 # warn "summ for column ".$col->{coltitle}." = ".$total;
520 push @loopfooter, {'totalcol' => $total};
524 # the header of the table
525 $globalline{loopfilter}=\@loopfilter;
526 # the core of the table
527 $globalline{looprow} = \@looprow;
528 $globalline{loopcol} = \@loopcol;
529 # # the foot (totals by borrower type)
530 $globalline{loopfooter} = \@loopfooter;
531 $globalline{total}= $grantotal;
532 $globalline{line} = $line;
533 $globalline{column} = $column;
534 push @mainloop,\%globalline;
535 return \@mainloop;