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