fix for bug 2488: OPACItemsResultsDisplay/singlebranchmode
[koha.git] / reports / catalogue_stats.pl
blob3addbe77e0859a2db310390bea7480cf23d06491
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
30 =head1 NAME
32 plugin that shows a stats on borrowers
34 =head1 DESCRIPTION
36 =over 2
38 =cut
40 our $debug = 0;
41 my $input = new CGI;
42 my $fullreportname = "reports/catalogue_stats.tmpl";
43 my $do_it = $input->param('do_it');
44 my $line = $input->param("Line");
45 my $column = $input->param("Column");
46 my @filters = $input->param("Filter");
47 my $deweydigits = $input->param("deweydigits");
48 my $lccndigits = $input->param("lccndigits");
49 my $cotedigits = $input->param("cotedigits");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {reports => 1},
61 debug => 1,
62 });
63 $template->param(do_it => $do_it);
64 if ($do_it) {
65 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
66 if ($output eq "screen"){
67 $template->param(mainloop => $results);
68 output_html_with_http_headers $input, $cookie, $template->output;
69 exit(1);
70 } else {
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
72 -encoding => 'utf-8',
73 -attachment=>"$basename.csv",
74 -name=>"$basename.csv" );
75 my $cols = @$results[0]->{loopcol};
76 my $lines = @$results[0]->{looprow};
77 my $sep = C4::Context->preference("delimiter");
78 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
79 foreach my $col ( @$cols ) {
80 print $col->{coltitle}.$sep;
82 print "Total\n";
83 foreach my $line ( @$lines ) {
84 my $x = $line->{loopcell};
85 print $line->{rowtitle}.$sep;
86 foreach my $cell (@$x) {
87 print $cell->{value}.$sep;
89 print $line->{totalrow};
90 print "\n";
92 print "TOTAL";
93 $cols = @$results[0]->{loopfooter};
94 foreach my $col ( @$cols ) {
95 print $sep.$col->{totalcol};
97 print $sep.@$results[0]->{total};
98 exit(1);
100 } else {
101 my $dbh = C4::Context->dbh;
102 my @values;
103 my %labels;
104 my $count=0;
105 my $req;
106 my @select;
107 # FIXME: no such field "dewey"
108 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
109 # $req->execute;
110 my $hasdewey = 0;
112 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
113 # This is not the LC Classification. It's the Control Number.
114 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
116 my $haslccn = 0;
117 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
118 # $req->execute;
119 # my $hlghtlccn;
120 # while (my ($value) =$req->fetchrow) {
121 # $hlghtlccn = !($hasdewey);
122 # $haslccn =1 if (($value>2) and (! $haslccn));
123 # $count++ if (($value) and (! $haslccn));
124 # push @select, $value;
126 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
127 # -id => 'Filter',
128 # -values => \@select,
129 # -size => 1,
130 # -multiple => 0 );
132 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
133 # FIXME: Hardcoding to 5 chars on itemcallnum.
135 my $hascote = 1;
136 my $highcote = 5;
138 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
139 $req->execute;
140 my $CGIitemtype = $req->fetchall_arrayref({});
142 my $authvals = GetKohaAuthorisedValues("items.ccode");
143 my @authvals;
144 foreach (keys %$authvals) {
145 push @authvals, { code => $_, description => $authvals->{$_} };
149 my $branches=GetBranches();
150 my @branchloop;
151 foreach (keys %$branches) {
152 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
153 my %row = (branchcode => $_,
154 selected => ($thisbranch eq $_ ? 1 : 0),
155 branchname => $branches->{$_}->{'branchname'},
157 push @branchloop, \%row;
160 my $locations = GetKohaAuthorisedValues("items.location");
161 my @locations;
162 foreach (sort keys %$locations) {
163 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
166 my @mime = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
167 my @delim = ( map { +{delim=>$_} } (split //,C4::Context->preference("delimiter")) );
169 $template->param(hasdewey=>$hasdewey,
170 haslccn => $haslccn,
171 hascote => $hascote,
172 CGIItemType => $CGIitemtype,
173 CGIBranch => \@branchloop,
174 locationloop => \@locations,
175 authvals => \@authvals,
176 CGIextChoice => \@mime,
177 CGIsepChoice => \@delim,
181 output_html_with_http_headers $input, $cookie, $template->output;
183 ## End of Main Body
186 sub calculate {
187 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
188 my @mainloop;
189 my @loopfooter;
190 my @loopcol;
191 my @loopline;
192 my @looprow;
193 my %globalline;
194 my $grantotal =0;
195 # extract parameters
196 my $dbh = C4::Context->dbh;
198 # Filters
199 # Checking filters
201 my @loopfilter;
202 for (my $i=0;$i<=12;$i++) {
203 my %cell;
204 if ( @$filters[$i] ) {
205 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
206 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
208 $cell{filter} .= @$filters[$i];
209 $cell{crit} .=
210 ($i== 0) ? "Dewey Classification From" :
211 ($i== 1) ? "Dewey Classification To" :
212 ($i== 2) ? "Lccn Classification From" :
213 ($i== 3) ? "Lccn Classification To" :
214 ($i== 4) ? "Item CallNumber From" :
215 ($i== 5) ? "Item CallNumber To" :
216 ($i== 6) ? "Item type" :
217 ($i== 7) ? "Publisher" :
218 ($i== 8) ? "Publication year From" :
219 ($i== 9) ? "Publication year To" :
220 ($i==10) ? "Library :" :
221 ($i==11) ? "Shelving Location :" :
222 ($i==12) ? "Collection Code :" : '';
223 push @loopfilter, \%cell;
227 # warn map {"filtres $_\n"} @filters[0..3];
229 my @linefilter;
230 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
231 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
232 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
233 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
234 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
235 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
236 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
237 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
238 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
239 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
240 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
241 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
242 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
244 my @colfilter ;
245 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
246 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
247 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
248 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
249 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
250 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
251 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
252 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
253 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
254 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
255 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
256 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
257 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
259 # 1st, loop rows.
260 my $linefield;
261 if (($line =~/dewey/) and ($deweydigits)) {
262 $linefield .="left($line,$deweydigits)";
263 } elsif (($line=~/lccn/) and ($lccndigits)) {
264 $linefield .="left($line,$lccndigits)";
265 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
266 $linefield .="left($line,$cotedigits)";
267 }else {
268 $linefield .= $line;
271 my $strsth;
272 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
273 if ( @linefilter ) {
274 if ($linefilter[1]){
275 $strsth .= " and $line >= ? " ;
276 $strsth .= " and $line <= ? " ;
277 } elsif ($linefilter[0]) {
278 $linefilter[0] =~ s/\*/%/g;
279 $strsth .= " and $line LIKE ? " ;
282 $strsth .=" order by $linefield";
283 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
285 my $sth = $dbh->prepare( $strsth );
286 if (( @linefilter ) and ($linefilter[1])){
287 $sth->execute($linefilter[0],$linefilter[1]);
288 } elsif ($linefilter[0]) {
289 $sth->execute($linefilter[0]);
290 } else {
291 $sth->execute;
293 while ( my ($celvalue) = $sth->fetchrow) {
294 my %cell;
295 if ($celvalue) {
296 $cell{rowtitle} = $celvalue;
297 # } else {
298 # $cell{rowtitle} = "";
300 $cell{totalrow} = 0;
301 push @loopline, \%cell;
304 # 2nd, loop cols.
305 my $colfield;
306 if (($column =~/dewey/) and ($deweydigits)) {
307 $colfield = "left($column,$deweydigits)";
308 }elsif (($column=~/lccn/) and ($lccndigits)) {
309 $colfield = "left($column,$lccndigits)";
310 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
311 $colfield = "left($column,$cotedigits)";
312 }else {
313 $colfield = $column;
316 my $strsth2 = "
317 SELECT distinctrow $colfield
318 FROM biblioitems
319 LEFT JOIN items
320 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
321 WHERE $column IS NOT NULL ";
322 if (( @colfilter ) and ($colfilter[1])) {
323 $strsth2 .= " and $column> ? and $column< ?";
324 }elsif ($colfilter[0]){
325 $colfilter[0] =~ s/\*/%/g;
326 $strsth2 .= " and $column LIKE ? ";
328 $strsth2 .= " order by $colfield";
329 $debug and print STDERR "SQL: $strsth2";
330 my $sth2 = $dbh->prepare( $strsth2 );
331 if ((@colfilter) and ($colfilter[1])) {
332 $sth2->execute($colfilter[0],$colfilter[1]);
333 } elsif ($colfilter[0]){
334 $sth2->execute($colfilter[0]);
335 } else {
336 $sth2->execute;
338 while (my ($celvalue) = $sth2->fetchrow) {
339 my %cell;
340 my %ft;
341 if ($celvalue) {
342 $cell{coltitle} = $celvalue;
343 # } else {
344 # $cell{coltitle} = "";
346 $ft{totalcol} = 0;
347 push @loopcol, \%cell;
350 my $i=0;
351 my @totalcol;
352 my $hilighted=-1;
354 #Initialization of cell values.....
355 my %table;
356 # warn "init table";
357 foreach my $row ( @loopline ) {
358 foreach my $col ( @loopcol ) {
359 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
360 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
362 $table{$row->{rowtitle}}->{totalrow}=0;
365 # preparing calculation
366 my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
367 if (@$filters[0]){
368 @$filters[0]=~ s/\*/%/g;
369 $strcalc .= " AND dewey >" . @$filters[0];
371 if (@$filters[1]){
372 @$filters[1]=~ s/\*/%/g ;
373 $strcalc .= " AND dewey <" . @$filters[1];
375 if (@$filters[2]){
376 @$filters[2]=~ s/\*/%/g ;
377 $strcalc .= " AND lccn >" . @$filters[2];
379 if (@$filters[3]){
380 @$filters[3]=~ s/\*/%/g;
381 $strcalc .= " AND lccn <" . @$filters[3];
383 if (@$filters[4]){
384 @$filters[4]=~ s/\*/%/g ;
385 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
388 if (@$filters[5]){
389 @$filters[5]=~ s/\*/%/g;
390 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
393 if (@$filters[6]){
394 @$filters[6]=~ s/\*/%/g;
395 $strcalc .= " AND " .
396 (C4::Context::preference('Item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
397 . " LIKE '" . @$filters[6] ."'";
400 if (@$filters[7]){
401 @$filters[7]=~ s/\*/%/g;
402 @$filters[7].="%" unless @$filters[7]=~/%/;
403 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
405 if (@$filters[8]){
406 @$filters[8]=~ s/\*/%/g;
407 $strcalc .= " AND publicationyear >" . @$filters[8];
409 if (@$filters[9]){
410 @$filters[9]=~ s/\*/%/g;
411 $strcalc .= " AND publicationyear <" . @$filters[9];
413 if (@$filters[10]){
414 @$filters[10]=~ s/\*/%/g;
415 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
417 if (@$filters[11]){
418 @$filters[11]=~ s/\*/%/g;
419 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
421 if (@$filters[12]){
422 @$filters[12]=~ s/\*/%/g;
423 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
426 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
427 $debug and warn "SQL: $strcalc";
428 my $dbcalc = $dbh->prepare($strcalc);
429 $dbcalc->execute;
430 # warn "filling table";
432 my $emptycol;
433 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
434 # warn "filling table $row / $col / $value ";
435 $emptycol = 1 if (!defined($col));
436 $col = "zzEMPTY" if (!defined($col));
437 $row = "zzEMPTY" if (!defined($row));
439 $table{$row}->{$col}+=$value;
440 $table{$row}->{totalrow}+=$value;
441 $grantotal += $value;
444 # my %cell = {rowtitle => 'zzROWEMPTY'};
445 # push @loopline,\%cell;
446 # undef %cell;
447 # my %cell;
448 # %cell = {coltitle => "zzEMPTY"};
449 push @loopcol,{coltitle => "NULL"} if ($emptycol);
451 foreach my $row ( sort keys %table ) {
452 my @loopcell;
453 #@loopcol ensures the order for columns is common with column titles
454 # and the number matches the number of columns
455 foreach my $col ( @loopcol ) {
456 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
457 push @loopcell, {value => $value } ;
459 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
460 'loopcell' => \@loopcell,
461 'hilighted' => ($hilighted *= -1 > 0),
462 'totalrow' => $table{$row}->{totalrow}
466 # warn "footer processing";
467 foreach my $col ( @loopcol ) {
468 my $total=0;
469 foreach my $row ( @looprow ) {
470 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
471 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
473 # warn "summ for column ".$col->{coltitle}." = ".$total;
474 push @loopfooter, {'totalcol' => $total};
478 # the header of the table
479 $globalline{loopfilter}=\@loopfilter;
480 # the core of the table
481 $globalline{looprow} = \@looprow;
482 $globalline{loopcol} = \@loopcol;
483 # # the foot (totals by borrower type)
484 $globalline{loopfooter} = \@loopfooter;
485 $globalline{total}= $grantotal;
486 $globalline{line} = $line;
487 $globalline{column} = $column;
488 push @mainloop,\%globalline;
489 return \@mainloop;