New XML API
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2 ## Will not work. Requires a complete re-write for ZEBRA
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Search;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Interface::CGI::Output;
30 use C4::Circulation::Circ2;
31
32 =head1 NAME
33
34 plugin that shows a stats on catalogue
35
36 =head1 DESCRIPTION
37
38
39 =over2
40
41 =cut
42
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/catalogue_stats.tmpl";
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 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
56
57 my ($template, $borrowernumber, $cookie)
58         = get_template_and_user({template_name => $fullreportname,
59                                 query => $input,
60                                 type => "intranet",
61                                 authnotrequired => 0,
62                                 flagsrequired => {editcatalogue => 1},
63                                 debug => 1,
64                                 });
65 $template->param(do_it => $do_it);
66 if ($do_it) {
67         my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
68         if ($output eq "screen"){
69                 $template->param(mainloop => $results);
70                 output_html_with_http_headers $input, $cookie, $template->output;
71                 exit(1);
72         } else {
73                 print $input->header(-type => 'application/vnd.sun.xml.calc',
74                                                          -attachment=>"$basename.csv",
75                                                          -name=>"$basename.csv" );
76                 my $cols = @$results[0]->{loopcol};
77                 my $lines = @$results[0]->{looprow};
78                 my $sep;
79                 $sep =C4::Context->preference("delimiter");
80                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
81                 foreach my $col ( @$cols ) {
82                         print $col->{coltitle}.$sep;
83                 }
84                 print "Total\n";
85                 foreach my $line ( @$lines ) {
86                         my $x = $line->{loopcell};
87                         print $line->{rowtitle}.$sep;
88                         foreach my $cell (@$x) {
89                                 print $cell->{value}.$sep;
90                         }
91                         print $line->{totalrow};
92                         print "\n";
93                 }
94                 print "TOTAL";
95                 $cols = @$results[0]->{loopfooter};
96                 foreach my $col ( @$cols ) {
97                         print $sep.$col->{totalcol};
98                 }
99                 print $sep.@$results[0]->{total};
100                 exit(1);
101         }
102 } else {
103         my $dbh = C4::Context->dbh;
104         my @values;
105         my %labels;
106         my $count=0;
107         my $req;
108 ###Use mfield of koha_attr instead of dewey
109 my $sth = $dbh->prepare("select authorised_value from authorised_values where category='mfield' order by lib");
110         $sth->execute;
111         my @authorised_values;
112         #push @authorised_values,"";
113         while ((my $category) = $sth->fetchrow_array) {
114                 push @authorised_values, $category;
115         }
116 my $hasdewey;
117         my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
118                                 -id => 'Filter',
119                                 -values   => \@authorised_values,
120                                 -size     => 1,
121                                 -multiple => 0 );
122         
123
124         my $haslccn=1;
125         my $hlghtlccn=1;
126         my @select;
127         for my $value ("A".."Z") {
128                 push @select, $value;
129         }
130         my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
131                                 -id => 'Filter',
132                                 -values   => \@select,
133                                 -size     => 1,
134                                 -multiple => 0 );
135         
136         my $hascote;
137         my $hlghtcote;
138         $count++;
139         my $hglghtDT =$count % 2;
140         $count++;
141         my $hglghtPub =$count % 2;
142         $count++;
143         my $hglghtPY =$count % 2;
144         $count++;
145         my $hglghtHB =$count % 2;
146         $count++;
147         my $hglghtLOC =$count % 2;
148         my $hglghtSTACK =$count % 2;
149         
150         my $itemtypes = GetItemTypes;
151         undef @select;
152         push @select,"";
153         my %select_item;
154         $select_item{""} = "";
155 foreach my $thisitemtype (sort keys %$itemtypes) {
156         push @select,$thisitemtype;
157         $select_item{$thisitemtype}=$itemtypes->{$thisitemtype}->{'description'};
158
159 }
160
161         my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
162                                 -id => 'itemtype',
163                                 -values   => \@select,
164                                 -labels=>\%select_item,
165                                 -size     => 1,
166                                 -multiple => 0 );
167         
168
169         undef @select;
170         push @select,"";
171         my $branches=GetBranches();
172         my %select_branches;
173         $select_branches{""} = "";
174         foreach my $branch (keys %$branches) {
175                 push @select, $branch;
176                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
177         }
178         my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
179                                 -id => 'branch',
180                                 -values   => \@select,
181                                 -labels   => \%select_branches,
182                                 -size     => 1,
183                                 -multiple => 0 );
184
185         my $CGIholdingbranch=CGI::scrolling_list( -name     => 'Filter',
186                                 -id => 'holdingbranch',
187                                 -values   => \@select,
188                                 -labels   => \%select_branches,
189                                 -size     => 1,
190                                 -multiple => 0 );
191         $req = $dbh->prepare("select authorised_value,lib from authorised_values where category='sections'");
192         $req->execute;
193         undef @select;
194         push @select,"";
195         my %desc;
196         $desc{""}="";
197         while (my ($value,$desc) =$req->fetchrow) {
198                 push @select, $value;
199                 $desc{$value}=$desc;
200         }
201
202         my $CGISTACK=CGI::scrolling_list( -name     => 'Filter',
203                                 -id => 'shelf',
204                                 -values   => \@select,
205                                 -labels =>\%desc,
206                                 -size     => 1,
207                                 -multiple => 0 );
208         
209         my @mime = ( C4::Context->preference("MIME") );
210         foreach my $mime (@mime){
211 #               warn "".$mime;
212         }
213         
214         my $CGIextChoice=CGI::scrolling_list(
215                                 -name => 'MIME',
216                                 -id => 'MIME',
217                                 -values   => \@mime,
218                                 -size     => 1,
219                                 -multiple => 0 );
220         
221         my @dels = ( C4::Context->preference("delimiter") );
222         my $CGIsepChoice=CGI::scrolling_list(
223                                 -name => 'sep',
224                                 -id => 'sep',
225                                 -values   => \@dels,
226                                 -size     => 1,
227                                 -multiple => 0 );
228         
229         $template->param(hasdewey=>$hasdewey,
230                                         CGIFromDeweyClass => $CGIdewey,
231                                         CGIToDeweyClass => $CGIdewey,
232                                         haslccn=> $haslccn,
233                                         hlghtlccn => $hlghtlccn,
234                                         CGIFromLoCClass => $CGIlccn,
235                                         CGIToLoCClass => $CGIlccn,
236                                         hascote=> $hascote,
237                                         hlghtcote => $hlghtcote,
238                                         hglghtDT => $hglghtDT,
239                                         hglghtPub => $hglghtPub,
240                                         hglghtPY => $hglghtPY,
241                                         hglghtHB => $hglghtHB,
242                                         hglghtLOC => $hglghtLOC,
243                                         hglghtSTACK => $hglghtSTACK,
244                                         CGIItemType => $CGIitemtype,
245                                         CGIBranch => $CGIbranch,
246                                         CGILocation => $CGIbranch,
247                                         CGISTACK => $CGISTACK,
248                                         CGIextChoice => $CGIextChoice,
249                                         CGIsepChoice => $CGIsepChoice
250                                         );
251
252 }
253 output_html_with_http_headers $input, $cookie, $template->output;
254
255
256
257 sub calculate {
258         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
259         my @mainloop;
260         my @loopfooter;
261         my @loopcol;
262         my @loopline;
263         my @looprow;
264         my %globalline;
265         my $grantotal =0;
266 # extract parameters
267         my $dbh = C4::Context->dbh;
268
269 # Filters
270 # Checking filters
271 #
272         my @loopfilter;
273         for (my $i=0;$i<=11;$i++) {
274                 my %cell;
275                 if ( @$filters[$i] ) {
276                         if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
277                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
278                         }
279                         $cell{filter} .= @$filters[$i];
280                         $cell{crit} .="Dewey Classification From" if ($i==0);
281                         $cell{crit} .="Dewey Classification To" if ($i==1);
282                         $cell{crit} .="Classification From" if ($i==2);
283                         $cell{crit} .="Classification To" if ($i==3);
284                         $cell{crit} .="Call Number From" if ($i==4);
285                         $cell{crit} .="Call Number To" if ($i==5);
286                         $cell{crit} .="Document type" if ($i==6);
287                         $cell{crit} .="Publisher" if ($i==7);
288                         $cell{crit} .="Publication year From" if ($i==8);
289                         $cell{crit} .="Publication year To" if ($i==9);
290                         $cell{crit} .="Branch :" if ($i==10);
291                         $cell{crit} .="Location:" if ($i==11);
292                         $cell{crit} .="Shelving:" if ($i==12);
293                         push @loopfilter, \%cell;
294                 }
295         }
296         
297         my @linefilter;
298 #       warn "filtres ".@filters[0];
299 #       warn "filtres ".@filters[1];
300 #       warn "filtres ".@filters[2];
301 #       warn "filtres ".@filters[3];
302         
303         $linefilter[0] = @$filters[0] if ($line =~ /dewey/ )  ;
304         $linefilter[1] = @$filters[1] if ($line =~ /dewey/ )  ;
305         $linefilter[0] = @$filters[2] if ($line =~ /classification/ )  ;
306         $linefilter[1] = @$filters[3] if ($line =~ /classification/ )  ;
307         $linefilter[0] = @$filters[4] if ($line =~ /itemcallnumber/ )  ;
308         $linefilter[1] = @$filters[5] if ($line =~ /itemcallnumber/ )  ;
309         $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
310         $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
311         $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
312         $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
313         $linefilter[0] = @$filters[10] if ($line =~ /homebranch/ ) ;
314         $linefilter[0] = @$filters[11] if ($line =~ /holdingbranch/ ) ;
315         $linefilter[0] = @$filters[12] if ($line =~ /shelf/ ) ;
316
317         my @colfilter ;
318         $colfilter[0] = @$filters[0] if ($column =~ /dewey/ )  ;
319         $colfilter[1] = @$filters[1] if ($column =~ /dewey/ )  ;
320         $colfilter[0] = @$filters[2] if ($column =~ /classification/ )  ;
321         $colfilter[1] = @$filters[3] if ($column =~ /classification/ )  ;
322         $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ )  ;
323         $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ )  ;
324         $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
325         $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
326         $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
327         $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
328         $colfilter[0] = @$filters[10] if ($column =~ /homebranch/ ) ;
329         $colfilter[0] = @$filters[11] if ($column =~ /holdingbranch/ ) ;
330         $colfilter[0] = @$filters[12] if ($column =~ /shelf/ ) ;
331 # 1st, loop rows.
332         my $linefield;
333         if (($line =~/dewey/)  and ($deweydigits)) {
334                 $linefield .="left($line,$deweydigits)";
335         } elsif (($line=~/classification/) and ($lccndigits)) {
336                 $linefield .="left($line,$lccndigits)";
337         } elsif (($line=~/itemcallnumber/) and ($cotedigits)) {
338                 $linefield .="left($line,$cotedigits)";
339         }else {
340                 $linefield .= $line;
341         }
342         
343 warn $linefield,$colfilter[0],$linefilter[0],$line;     
344         my $strsth;
345         $strsth .= "select distinctrow $linefield from biblio left join items on (items.biblionumber = biblio.biblionumber) where $line is not null ";
346         if ( @linefilter ) {
347                 if ($linefilter[1]){
348                         $strsth .= " and $line >= ? " ;
349                         $strsth .= " and $line <= ? " ;
350                 } elsif ($linefilter[0]) {
351                         $linefilter[0] =~ s/\*/%/g;
352                         $strsth .= " and $line LIKE ? " ;
353                 }
354         }
355         $strsth .=" order by $linefield";
356         warn "". $strsth;
357         
358         my $sth = $dbh->prepare( $strsth );
359         if (( @linefilter ) and ($linefilter[1])){
360                 $sth->execute($linefilter[0],$linefilter[1]);
361         } elsif ($linefilter[0]) {
362                 $sth->execute($linefilter[0]);
363         } else {
364                 $sth->execute;
365         }
366         while ( my ($celvalue) = $sth->fetchrow) {
367                 my %cell;
368                 if ($celvalue) {
369                         $cell{rowtitle} = $celvalue;
370 #               } else {
371 #                       $cell{rowtitle} = "";
372                 }
373                 $cell{totalrow} = 0;
374                 push @loopline, \%cell;
375         }
376
377 # 2nd, loop cols.
378         my $colfield;
379         if (($column =~/dewey/)  and ($deweydigits)) {
380                 $colfield .="left($column,$deweydigits)";
381         }elsif (($column=~/classification/) and ($lccndigits)) {
382                 $colfield .="left($column,$lccndigits)";
383         }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
384                 $colfield .="left($column,$cotedigits)";
385         }else {
386                 $colfield .= $column;
387         }
388         
389         my $strsth2;
390         $strsth2 .= "select distinctrow $colfield from biblio left join items on (items.biblionumber = biblio.biblionumber) where $column is not null ";
391         if (( @colfilter ) and ($colfilter[1])) {
392                 $strsth2 .= " and $column> ? and $column< ?";
393         }elsif ($colfilter[0]){
394                 $colfilter[0] =~ s/\*/%/g;
395                 $strsth2 .= " and $column LIKE ? ";
396         } 
397         $strsth2 .= " order by $colfield";
398         warn "". $strsth2;
399         my $sth2 = $dbh->prepare( $strsth2 );
400         if ((@colfilter) and ($colfilter[1])) {
401                 $sth2->execute($colfilter[0],$colfilter[1]);
402         } elsif ($colfilter[0]){
403                 $sth2->execute($colfilter[0]);
404         } else {
405                 $sth2->execute;
406         }
407         while (my ($celvalue) = $sth2->fetchrow) {
408                 my %cell;
409                 my %ft;
410                 if ($celvalue) {
411                         $cell{coltitle} = $celvalue;
412 #               } else {
413 #                       $cell{coltitle} = "";
414                 }
415                 $ft{totalcol} = 0;
416                 push @loopcol, \%cell;
417         }
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 my @kohafield;
435 my @values;
436 my @and_or;
437 my @relations;
438 # preparing calculation in zebra
439         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
440         if (@$filters[0]){
441                 @$filters[0]=~ s/\*/%/g;
442                 $strcalc .= " AND dewey >" . @$filters[0] ."";
443                 push @and_or, "\@and";
444                 push @relations "\@attr 2=5";
445                 push @kohafield, "dewey";
446                 push @values,  @$filters[0] ;
447
448         }
449         if (@$filters[1]){
450                 @$filters[1]=~ s/\*/%/g ;
451                 $strcalc .= " AND dewey <" . @$filters[1] ."";
452                 push @and_or, "\@and";
453                 push @relations "\@attr 2=1";
454                 push @kohafield, "dewey";
455                 push @values,  @$filters[1] ;
456
457                 
458         }
459         if (@$filters[2]){
460                 @$filters[2]=~ s/\*/%/g ;
461                 $strcalc .= " AND classification >=" .$dbh->quote(@$filters[2]) ."" ;
462                 push @and_or, "\@and";
463                 push @relations "\@attr 2=4";
464                 push @kohafield, "classification";
465                 push @values,  @$filters[2];
466
467         }
468         if (@$filters[3]){
469                 @$filters[3]=~ s/\*/%/g;
470                 $strcalc .= " AND classification <=" . $dbh->quote(@$filters[3]) ."" ;
471                 push @and_or, "\@and";
472                 push @relations "\@attr 2=2";
473                 push @kohafield, "classification";
474                 push @values,  @$filters[3] ;
475         }
476         if (@$filters[4]){
477                 @$filters[4]=~ s/\*/%/g ;
478                 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
479                 push @and_or, "\@and";
480                 push @relations "\@attr 2=4";
481                 push @kohafield, "itemcallnumber";
482                 push @values,  @$filters[4] ;
483         }
484         
485         if (@$filters[5]){
486                 @$filters[5]=~ s/\*/%/g;
487                 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
488                 push @and_or, "\@and";
489                 push @relations "\@attr 2=2";
490                 push @kohafield, "itemcallnumber";
491                 push @values,  @$filters[5] ;
492         }
493         
494         if (@$filters[6]){
495                 @$filters[6]=~ s/\*/%/g;
496                 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
497                 push @and_or, "\@and";
498                 push @relations "\@attr 2=3";
499                 push @kohafield, "itemtype";
500                 push @values,  @$filters[6] ;
501         }
502         
503         if (@$filters[7]){
504                 @$filters[7]=~ s/\*/%/g;
505                 @$filters[7].="%" unless @$filters[7]=~/%/;
506                 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
507                 push @and_or, "\@and";
508                 push @relations "\@attr 2=3";
509                 push @kohafield, "publishercode";
510                 push @values,  @$filters[7]; 
511         }
512         if (@$filters[8]){
513                 @$filters[8]=~ s/\*/%/g;
514                 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
515         }
516         if (@$filters[9]){
517                 @$filters[9]=~ s/\*/%/g;
518                 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
519         }
520         if (@$filters[10]){
521                 @$filters[10]=~ s/\*/%/g;
522                 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
523         }
524         if (@$filters[11]){
525                 @$filters[11]=~ s/\*/%/g;
526                 $strcalc .= " AND items.holdingbranch like '" . @$filters[11] ."'" if ( @$filters[11] );
527         }
528         if (@$filters[12]){
529                 @$filters[12]=~ s/\*/%/g;
530                 $strcalc .= " AND items.stack like '" . @$filters[12] ."'" if ( @$filters[12] );
531         }
532         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
533         warn "". $strcalc;
534         my $dbcalc = $dbh->prepare($strcalc);
535 #       $dbcalc->execute;
536 #       warn "filling table";
537         
538         my $emptycol; 
539         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
540 #               warn "filling table $row / $col / $value ";
541                 $emptycol = 1 if ($col eq undef);
542                 $col = "zzEMPTY" if ($col eq undef);
543                 $row = "zzEMPTY" if ($row eq undef);
544                 
545                 $table{$row}->{$col}+=$value;
546                 $table{$row}->{totalrow}+=$value;
547                 $grantotal += $value;
548         }
549
550         push @loopcol,{coltitle => "NULL"} if ($emptycol);
551         
552         foreach my $row ( sort keys %table ) {
553                 my @loopcell;
554                 #@loopcol ensures the order for columns is common with column titles
555                 # and the number matches the number of columns
556                 foreach my $col ( @loopcol ) {
557                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
558                         push @loopcell, {value => $value  } ;
559                 }
560                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
561                                                 'loopcell' => \@loopcell,
562                                                 'hilighted' => ($hilighted >0),
563                                                 'totalrow' => $table{$row}->{totalrow}
564                                         };
565                 $hilighted = -$hilighted;
566         }
567         
568 #       warn "footer processing";
569         foreach my $col ( @loopcol ) {
570                 my $total=0;
571                 foreach my $row ( @looprow ) {
572                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
573 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
574                 }
575 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
576                 push @loopfooter, {'totalcol' => $total};
577         }
578                         
579
580         # the header of the table
581         $globalline{loopfilter}=\@loopfilter;
582         # the core of the table
583         $globalline{looprow} = \@looprow;
584         $globalline{loopcol} = \@loopcol;
585 #       # the foot (totals by borrower type)
586         $globalline{loopfooter} = \@loopfooter;
587         $globalline{total}= $grantotal;
588         $globalline{line} = $line;
589         $globalline{column} = $column;
590         push @mainloop,\%globalline;
591         return \@mainloop;
592 }
593
594 1;