Update Catalog Stats report: / Remove lccn/loc filter, which seems to conflate marc21...
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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
20
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;
29
30 =head1 NAME
31
32 plugin that shows a stats on borrowers
33
34 =head1 DESCRIPTION
35
36 =over 2
37
38 =cut
39
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");
54
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;
81                 }
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;
88                         }
89                         print $line->{totalrow};
90                         print "\n";
91                 }
92                 print "TOTAL";
93                 $cols = @$results[0]->{loopfooter};
94                 foreach my $col ( @$cols ) {
95                         print $sep.$col->{totalcol};
96                 }
97                 print $sep.@$results[0]->{total};
98                 exit(1);
99         }
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;
111 #       push @select,"";
112 #       while (my ($value) =$req->fetchrow) {
113 #               if (($value>2) and (! $hasdewey)) {
114 #                       $hasdewey =1;
115 #                       $count++;
116 #               }
117 #               push @select, $value;
118 #       }
119 #       my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
120 #                               -id => 'Filter',
121 #                               -values   => \@select,
122 #                               -size     => 1,
123 #                               -multiple => 0 );
124
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.
128 #
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;
138 #       }
139 #       my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
140 #                               -id => 'Filter',
141 #                               -values   => \@select,
142 #                               -size     => 1,
143 #                               -multiple => 0 );
144
145 # No need to test for data here.  If you don't have itemcallnumbers, you probably know it.
146 # FIXME: Hardcoding to 5 chars on itemcallnum. 
147 #
148          my $hascote = 1;
149          my $highcote = 5;
150
151 #       $req = $dbh->prepare("select count(itemcallnumber) from items");
152 #       $req->execute;
153 #       my $hlghtcote;
154 #       while (my ($value) =$req->fetchrow) {
155 #               $hascote =1 if (($value>2) and (! $hascote));
156 #               $count++ if (($value) and (! $hascote));
157 #               $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
158 ##              push @select, $value;
159 #       }
160 #       my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
161 #                               -id => 'Filter',
162 #                               -values   => \@select,
163 #                               -size     => 1,
164 #                               -multiple => 0 );
165 #       my $hglghtDT  = ++$count % 2;
166 #       my $hglghtPub = ++$count % 2;
167 #       my $hglghtPY  = ++$count % 2;
168 #       my $hglghtHB  = ++$count % 2;
169 #       my $hglghtLOC = ++$count % 2;
170 #       warn "highlightDT "  .$hglghtDT;
171 #       warn "highlightPub " .$hglghtPub;
172 #       warn "highlightPY "  .$hglghtPY;
173 #       warn "highlightHB "  .$hglghtHB;
174 #       warn "highlightLOC " .$hglghtLOC;
175         
176         
177         $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
178         $req->execute;
179         my $CGIitemtype = $req->fetchall_arrayref({});
180
181         my $authvals = GetKohaAuthorisedValues("items.ccode");
182         my @authvals;
183         foreach (keys %$authvals) {
184                 push @authvals, { code => $_, description => $authvals->{$_} };
185         }
186         
187 #       $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
188 #       $req->execute;
189 #       undef @select;
190 #       push @select,"";
191 #       while (my ($value) =$req->fetchrow) {
192 #               push @select, $value;
193 #       }
194 #       my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
195 #                               -id => 'Filter',
196 #                               -values   => \@select,
197 #                               -size     => 1,
198 #                               -multiple => 0 );
199
200         my $branches=GetBranches();
201         my @branchloop;
202         foreach (keys %$branches) {
203                 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
204                 my %row = (branchcode => $_,
205                         selected => ($thisbranch eq $_ ? 1 : 0),
206                         branchname => $branches->{$_}->{'branchname'},
207                 );
208                 push @branchloop, \%row;
209         }
210
211         $req = $dbh->prepare("select distinctrow location from items order by location");
212         $req->execute;
213         my $locationloop = $req->fetchall_arrayref({});
214         my $locations = GetKohaAuthorisedValues("items.location");
215         my @locations;
216         foreach (sort keys %$locations) {
217                 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
218         }
219         
220         my @mime  = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
221         my @delim = ( map { +{delim=>$_} } (split //,C4::Context->preference("delimiter")) );
222         
223         $template->param(hasdewey=>$hasdewey,
224 #                                       CGIFromDeweyClass => $CGIdewey,
225 #                                       CGIToDeweyClass => $CGIdewey,
226                                         haslccn   => $haslccn,
227 #                                       hlghtlccn => $hlghtlccn,
228 #                                       CGIFromLoCClass => $CGIlccn,
229 #                                       CGIToLoCClass => $CGIlccn,
230                                         hascote   => $hascote,
231 #                                       hlghtcote => $hlghtcote,
232 #                                       hglghtDT  => $hglghtDT,
233 #                                       hglghtPub => $hglghtPub,
234 #                                       hglghtPY  => $hglghtPY,
235 #                                       hglghtHB  => $hglghtHB,
236 #                                       hglghtLOC => $hglghtLOC,
237 #                                       CGIFromCoteClass => $CGIcote,
238 #                                       CGIToCoteClass => $CGIcote,
239                                         CGIItemType => $CGIitemtype,
240 #                                       CGIFromPublicationYear => $CGIpublicationyear,
241 #                                       CGIToPublicationYear => $CGIpublicationyear,
242 #                                       CGIPublisher => $CGIpublisher,
243                                         CGIBranch    => \@branchloop,
244                                         locationloop => \@locations,
245                                         authvals     => \@authvals,
246                                         CGIextChoice => \@mime,
247                                         CGIsepChoice => \@delim,
248                                         );
249
250 }
251 output_html_with_http_headers $input, $cookie, $template->output;
252
253 ## End of Main Body
254
255
256 sub calculate {
257         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
258         my @mainloop;
259         my @loopfooter;
260         my @loopcol;
261         my @loopline;
262         my @looprow;
263         my %globalline;
264         my $grantotal =0;
265 # extract parameters
266         my $dbh = C4::Context->dbh;
267
268 # Filters
269 # Checking filters
270 #
271         my @loopfilter;
272         for (my $i=0;$i<=12;$i++) {
273                 my %cell;
274                 if ( @$filters[$i] ) {
275                         if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
276                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
277                         }
278                         $cell{filter} .= @$filters[$i];
279                         $cell{crit} .=
280                                 ($i== 0) ? "Dewey Classification From" :
281                                 ($i== 1) ? "Dewey Classification To"   :
282                                 ($i== 2) ? "Lccn Classification From"  :
283                                 ($i== 3) ? "Lccn Classification To"    :
284                                 ($i== 4) ? "Item CallNumber From"  :
285                                 ($i== 5) ? "Item CallNumber To"    :
286                                 ($i== 6) ? "Item type"             :
287                                 ($i== 7) ? "Publisher"                 :
288                                 ($i== 8) ? "Publication year From"     :
289                                 ($i== 9) ? "Publication year To"       :
290                                 ($i==10) ? "Library :"                  :
291                                 ($i==11) ? "Shelving Location :"                :
292                                 ($i==12) ? "Collection Code :"            : '';
293                         push @loopfilter, \%cell;
294                 }
295         }
296         
297 #       warn map {"filtres $_\n"} @filters[0..3];
298
299         my @linefilter;
300         $linefilter[0] = @$filters[0] if ($line =~ /dewey/ )  ;
301         $linefilter[1] = @$filters[1] if ($line =~ /dewey/ )  ;
302         $linefilter[0] = @$filters[2] if ($line =~ /lccn/ )  ;
303         $linefilter[1] = @$filters[3] if ($line =~ /lccn/ )  ;
304         $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ )  ;
305         $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ )  ;
306         $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
307         $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
308         $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
309         $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
310         $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
311         $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
312         $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
313
314         my @colfilter ;
315         $colfilter[0] = @$filters[0] if ($column =~ /dewey/ )  ;
316         $colfilter[1] = @$filters[1] if ($column =~ /dewey/ )  ;
317         $colfilter[0] = @$filters[2] if ($column =~ /lccn/ )  ;
318         $colfilter[1] = @$filters[3] if ($column =~ /lccn/ )  ;
319         $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ )  ;
320         $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ )  ;
321         $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
322         $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
323         $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
324         $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
325         $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
326         $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
327         $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
328
329 # 1st, loop rows.
330         my $linefield;
331         if (($line =~/dewey/)  and ($deweydigits)) {
332                 $linefield .="left($line,$deweydigits)";
333         } elsif (($line=~/lccn/) and ($lccndigits)) {
334                 $linefield .="left($line,$lccndigits)";
335         } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
336                 $linefield .="left($line,$cotedigits)";
337         }else {
338                 $linefield .= $line;
339         }
340
341         my $strsth;
342         $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
343         if ( @linefilter ) {
344                 if ($linefilter[1]){
345                         $strsth .= " and $line >= ? " ;
346                         $strsth .= " and $line <= ? " ;
347                 } elsif ($linefilter[0]) {
348                         $linefilter[0] =~ s/\*/%/g;
349                         $strsth .= " and $line LIKE ? " ;
350                 }
351         }
352         $strsth .=" order by $linefield";
353         $debug and print STDERR "catalogue_stats SQL: $strsth\n";
354         
355         my $sth = $dbh->prepare( $strsth );
356         if (( @linefilter ) and ($linefilter[1])){
357                 $sth->execute($linefilter[0],$linefilter[1]);
358         } elsif ($linefilter[0]) {
359                 $sth->execute($linefilter[0]);
360         } else {
361                 $sth->execute;
362         }
363         while ( my ($celvalue) = $sth->fetchrow) {
364                 my %cell;
365                 if ($celvalue) {
366                         $cell{rowtitle} = $celvalue;
367 #               } else {
368 #                       $cell{rowtitle} = "";
369                 }
370                 $cell{totalrow} = 0;
371                 push @loopline, \%cell;
372         }
373
374 # 2nd, loop cols.
375         my $colfield;
376         if (($column =~/dewey/)  and ($deweydigits)) {
377                 $colfield = "left($column,$deweydigits)";
378         }elsif (($column=~/lccn/) and ($lccndigits)) {
379                 $colfield = "left($column,$lccndigits)";
380         }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
381                 $colfield = "left($column,$cotedigits)";
382         }else {
383                 $colfield = $column;
384         }
385         
386         my $strsth2 = "
387         SELECT distinctrow $colfield
388         FROM   biblioitems
389         LEFT JOIN items
390                 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
391         WHERE  $column IS NOT NULL ";
392         if (( @colfilter ) and ($colfilter[1])) {
393                 $strsth2 .= " and $column> ? and $column< ?";
394         }elsif ($colfilter[0]){
395                 $colfilter[0] =~ s/\*/%/g;
396                 $strsth2 .= " and $column LIKE ? ";
397         } 
398         $strsth2 .= " order by $colfield";
399         $debug and print STDERR "SQL: $strsth2";
400         my $sth2 = $dbh->prepare( $strsth2 );
401         if ((@colfilter) and ($colfilter[1])) {
402                 $sth2->execute($colfilter[0],$colfilter[1]);
403         } elsif ($colfilter[0]){
404                 $sth2->execute($colfilter[0]);
405         } else {
406                 $sth2->execute;
407         }
408         while (my ($celvalue) = $sth2->fetchrow) {
409                 my %cell;
410                 my %ft;
411                 if ($celvalue) {
412                         $cell{coltitle} = $celvalue;
413 #               } else {
414 #                       $cell{coltitle} = "";
415                 }
416                 $ft{totalcol} = 0;
417                 push @loopcol, \%cell;
418         }
419         
420         my $i=0;
421         my @totalcol;
422         my $hilighted=-1;
423         
424         #Initialization of cell values.....
425         my %table;
426 #       warn "init table";
427         foreach my $row ( @loopline ) {
428                 foreach my $col ( @loopcol ) {
429 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
430                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
431                 }
432                 $table{$row->{rowtitle}}->{totalrow}=0;
433         }
434
435 # preparing calculation
436         my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
437         if (@$filters[0]){
438                 @$filters[0]=~ s/\*/%/g;
439                 $strcalc .= " AND dewey >" . @$filters[0];
440         }
441         if (@$filters[1]){
442                 @$filters[1]=~ s/\*/%/g ;
443                 $strcalc .= " AND dewey <" . @$filters[1];
444         }
445         if (@$filters[2]){
446                 @$filters[2]=~ s/\*/%/g ;
447                 $strcalc .= " AND lccn >" . @$filters[2];
448         }
449         if (@$filters[3]){
450                 @$filters[3]=~ s/\*/%/g;
451                 $strcalc .= " AND lccn <" . @$filters[3];
452         }
453         if (@$filters[4]){
454                 @$filters[4]=~ s/\*/%/g ;
455                 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
456         }
457         
458         if (@$filters[5]){
459                 @$filters[5]=~ s/\*/%/g;
460                 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
461         }
462         
463         if (@$filters[6]){
464                 @$filters[6]=~ s/\*/%/g;
465                 $strcalc .= " AND " . 
466                         (C4::Context::preference('Item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
467                         . " LIKE '" . @$filters[6] ."'";
468         }
469         
470         if (@$filters[7]){
471                 @$filters[7]=~ s/\*/%/g;
472                 @$filters[7].="%" unless @$filters[7]=~/%/;
473                 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
474         }
475         if (@$filters[8]){
476                 @$filters[8]=~ s/\*/%/g;
477                 $strcalc .= " AND publicationyear >" . @$filters[8];
478         }
479         if (@$filters[9]){
480                 @$filters[9]=~ s/\*/%/g;
481                 $strcalc .= " AND publicationyear <" . @$filters[9];
482         }
483         if (@$filters[10]){
484                 @$filters[10]=~ s/\*/%/g;
485                 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
486         }
487         if (@$filters[11]){
488                 @$filters[11]=~ s/\*/%/g;
489                 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
490         }
491         if (@$filters[12]){
492                 @$filters[12]=~ s/\*/%/g;
493                 $strcalc .= " AND items.ccode  LIKE '" . @$filters[12] ."'";
494         }
495         
496         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
497         $debug and warn "SQL: $strcalc";
498         my $dbcalc = $dbh->prepare($strcalc);
499         $dbcalc->execute;
500 #       warn "filling table";
501         
502         my $emptycol; 
503         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
504 #               warn "filling table $row / $col / $value ";
505                 $emptycol = 1    if ($col eq undef);
506                 $col = "zzEMPTY" if ($col eq undef);
507                 $row = "zzEMPTY" if ($row eq undef);
508                 
509                 $table{$row}->{$col}+=$value;
510                 $table{$row}->{totalrow}+=$value;
511                 $grantotal += $value;
512         }
513
514 #       my %cell = {rowtitle => 'zzROWEMPTY'};
515 #       push @loopline,\%cell;
516 #       undef %cell;
517 #       my %cell;
518 #       %cell = {coltitle => "zzEMPTY"};
519         push @loopcol,{coltitle => "NULL"} if ($emptycol);
520         
521         foreach my $row ( sort keys %table ) {
522                 my @loopcell;
523                 #@loopcol ensures the order for columns is common with column titles
524                 # and the number matches the number of columns
525                 foreach my $col ( @loopcol ) {
526                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
527                         push @loopcell, {value => $value  } ;
528                 }
529                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
530                                                 'loopcell' => \@loopcell,
531                                                 'hilighted' => ($hilighted *= -1 > 0),
532                                                 'totalrow' => $table{$row}->{totalrow}
533                                         };
534         }
535         
536 #       warn "footer processing";
537         foreach my $col ( @loopcol ) {
538                 my $total=0;
539                 foreach my $row ( @looprow ) {
540                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
541 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
542                 }
543 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
544                 push @loopfooter, {'totalcol' => $total};
545         }
546                         
547
548         # the header of the table
549         $globalline{loopfilter}=\@loopfilter;
550         # the core of the table
551         $globalline{looprow} = \@looprow;
552         $globalline{loopcol} = \@loopcol;
553 #       # the foot (totals by borrower type)
554         $globalline{loopfooter} = \@loopfooter;
555         $globalline{total}= $grantotal;
556         $globalline{line} = $line;
557         $globalline{column} = $column;
558         push @mainloop,\%globalline;
559         return \@mainloop;
560 }
561
562 1;