Bug 9002 - Remove Problematic Logic from Patron Messaging Preferences Form
[koha.git] / reports / catalogue_stats.pl
blobc8e21940f494058fd4a7645e81c20f670876bee5
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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Reports;
30 use C4::Circulation;
32 =head1 NAME
34 plugin that shows a stats on borrowers
36 =head1 DESCRIPTION
38 =over 2
40 =cut
42 our $debug = 0;
43 my $input = new CGI;
44 my $fullreportname = "reports/catalogue_stats.tmpl";
45 my $do_it = $input->param('do_it');
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $deweydigits = $input->param("deweydigits");
50 my $lccndigits = $input->param("lccndigits");
51 my $cotedigits = $input->param("cotedigits");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 our $sep = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 my $item_itype;
57 if(C4::Context->preference('item-level_itypes')) {
58 $item_itype = "items\.itype"
59 } else {
60 $item_itype = "itemtype";
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => $fullreportname,
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => {reports => '*'},
69 debug => 1,
70 });
71 $template->param(do_it => $do_it);
72 if ($do_it) {
73 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
74 if ($output eq "screen"){
75 $template->param(mainloop => $results);
76 output_html_with_http_headers $input, $cookie, $template->output;
77 exit;
78 } else {
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -encoding => 'utf-8',
81 -attachment=>"$basename.csv",
82 -name=>"$basename.csv" );
83 my $cols = @$results[0]->{loopcol};
84 my $lines = @$results[0]->{looprow};
85 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
89 print "Total\n";
90 foreach my $line ( @$lines ) {
91 my $x = $line->{loopcell};
92 print $line->{rowtitle}.$sep;
93 foreach my $cell (@$x) {
94 print $cell->{value}.$sep;
96 print $line->{totalrow};
97 print "\n";
99 print "TOTAL";
100 $cols = @$results[0]->{loopfooter};
101 foreach my $col ( @$cols ) {
102 print $sep.$col->{totalcol};
104 print $sep.@$results[0]->{total};
105 exit;
107 } else {
108 my $dbh = C4::Context->dbh;
109 my @values;
110 my %labels;
111 my $count=0;
112 my $req;
113 my @select;
114 # FIXME: no such field "dewey"
115 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
116 # $req->execute;
117 my $hasdewey = 0;
119 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
120 # This is not the LC Classification. It's the Control Number.
121 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
123 my $haslccn = 0;
124 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
125 # $req->execute;
126 # my $hlghtlccn;
127 # while (my ($value) =$req->fetchrow) {
128 # $hlghtlccn = !($hasdewey);
129 # $haslccn =1 if (($value>2) and (! $haslccn));
130 # $count++ if (($value) and (! $haslccn));
131 # push @select, $value;
133 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
134 # -id => 'Filter',
135 # -values => \@select,
136 # -size => 1,
137 # -multiple => 0 );
139 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
140 # FIXME: Hardcoding to 5 chars on itemcallnum.
142 my $hascote = 1;
143 my $highcote = 5;
145 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
146 $req->execute;
147 my $CGIitemtype = $req->fetchall_arrayref({});
149 my $authvals = GetKohaAuthorisedValues("items.ccode");
150 my @authvals;
151 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
152 push @authvals, { code => $_, description => $authvals->{$_} };
155 my $locations = GetKohaAuthorisedValues("items.location");
156 my @locations;
157 foreach (sort keys %$locations) {
158 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
161 my @mime = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
163 $template->param(hasdewey=>$hasdewey,
164 haslccn => $haslccn,
165 hascote => $hascote,
166 CGIItemType => $CGIitemtype,
167 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
168 locationloop => \@locations,
169 authvals => \@authvals,
170 CGIextChoice => \@mime,
171 CGIsepChoice => GetDelimiterChoices,
172 item_itype => $item_itype
176 output_html_with_http_headers $input, $cookie, $template->output;
178 ## End of Main Body
181 sub calculate {
182 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
183 my @mainloop;
184 my @loopfooter;
185 my @loopcol;
186 my @loopline;
187 my @looprow;
188 my %globalline;
189 my $grantotal =0;
190 my $barcodelike = @$filters[13];
191 my $barcodefilter = @$filters[14];
192 my $not;
194 # extract parameters
195 my $dbh = C4::Context->dbh;
197 # if barcodefilter is empty set as %
198 if($barcodefilter){
199 # Check if barcodefilter is "like" or "not like"
200 if(!$barcodelike){
201 $not = "not";
203 # Change * to %
204 $barcodefilter =~ s/\*/%/g;
207 # Filters
208 # Checking filters
210 my @loopfilter;
211 for (my $i=0;$i<=12;$i++) {
212 my %cell;
213 if ( @$filters[$i] ) {
214 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
215 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
217 $cell{filter} .= @$filters[$i];
218 $cell{crit} .=
219 ($i== 0) ? "Dewey Classification From" :
220 ($i== 1) ? "Dewey Classification To" :
221 ($i== 2) ? "Lccn Classification From" :
222 ($i== 3) ? "Lccn Classification To" :
223 ($i== 4) ? "Item CallNumber From" :
224 ($i== 5) ? "Item CallNumber To" :
225 ($i== 6) ? "Item type" :
226 ($i== 7) ? "Publisher" :
227 ($i== 8) ? "Publication year From" :
228 ($i== 9) ? "Publication year To" :
229 ($i==10) ? "Library :" :
230 ($i==11) ? "Shelving Location :" :
231 ($i==12) ? "Collection Code :" : '';
232 push @loopfilter, \%cell;
236 # warn map {"filtres $_\n"} @filters[0..3];
238 my @linefilter;
239 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
240 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
241 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
242 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
243 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
244 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
245 if (C4::Context->preference('item-level_itypes')) {
246 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
247 } else {
248 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
250 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
251 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
252 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
253 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
254 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
255 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
257 my @colfilter ;
258 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
259 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
260 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
261 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
262 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
263 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
264 if (C4::Context->preference('item-level_itypes')) {
265 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
266 } else {
267 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
269 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
270 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
271 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
272 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
273 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
274 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
276 # 1st, loop rows.
277 my $linefield;
278 if (($line =~/dewey/) and ($deweydigits)) {
279 $linefield .="left($line,$deweydigits)";
280 } elsif (($line=~/lccn/) and ($lccndigits)) {
281 $linefield .="left($line,$lccndigits)";
282 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
283 $linefield .="left($line,$cotedigits)";
284 }else {
285 $linefield .= $line;
288 my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems
289 INNER JOIN items USING (biblioitemnumber)
290 WHERE $line IS NOT NULL ";
291 $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
292 if ( @linefilter ) {
293 if ($linefilter[1]){
294 $strsth .= " AND $line >= ? " ;
295 $strsth .= " AND $line <= ? " ;
296 } elsif ($linefilter[0]) {
297 $linefilter[0] =~ s/\*/%/g;
298 $strsth .= " AND $line LIKE ? " ;
301 $strsth .=" ORDER BY $linefield";
302 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
304 my $sth = $dbh->prepare( $strsth );
305 if (( @linefilter ) and ($linefilter[1])){
306 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
307 } elsif ($barcodefilter,$linefilter[0]) {
308 $sth->execute($barcodefilter,$linefilter[0]);
309 } elsif ($barcodefilter) {
310 $sth->execute($barcodefilter);
311 }else{
312 $sth->execute();
314 while ( my ($celvalue) = $sth->fetchrow) {
315 my %cell;
316 if ($celvalue) {
317 $cell{rowtitle} = $celvalue;
318 # } else {
319 # $cell{rowtitle} = "";
321 $cell{totalrow} = 0;
322 push @loopline, \%cell;
325 # 2nd, loop cols.
326 my $colfield;
327 if (($column =~/dewey/) and ($deweydigits)) {
328 $colfield = "left($column,$deweydigits)";
329 }elsif (($column=~/lccn/) and ($lccndigits)) {
330 $colfield = "left($column,$lccndigits)";
331 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
332 $colfield = "left($column,$cotedigits)";
333 }else {
334 $colfield = $column;
337 my $strsth2 = "
338 SELECT distinctrow $colfield
339 FROM biblioitems
340 INNER JOIN items
341 USING (biblioitemnumber)
342 WHERE $column IS NOT NULL ";
343 $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
345 if (( @colfilter ) and ($colfilter[1])) {
346 $strsth2 .= " AND $column> ? AND $column< ?";
347 }elsif ($colfilter[0]){
348 $colfilter[0] =~ s/\*/%/g;
349 $strsth2 .= " AND $column LIKE ? ";
351 $strsth2 .= " ORDER BY $colfield";
352 $debug and print STDERR "SQL: $strsth2";
353 my $sth2 = $dbh->prepare( $strsth2 );
354 if ((@colfilter) and ($colfilter[1])) {
355 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
356 } elsif ($colfilter[0]){
357 $sth2->execute($barcodefilter,$colfilter[0]);
358 } elsif ($barcodefilter){
359 $sth2->execute($barcodefilter);
360 } else {
361 $sth2->execute();
363 while (my ($celvalue) = $sth2->fetchrow) {
364 my %cell;
365 my %ft;
366 if ($celvalue) {
367 $cell{coltitle} = $celvalue;
368 # } else {
369 # $cell{coltitle} = "";
371 $ft{totalcol} = 0;
372 push @loopcol, \%cell;
375 my $i=0;
376 my @totalcol;
377 my $hilighted=-1;
379 #Initialization of cell values.....
380 my %table;
381 # warn "init table";
382 foreach my $row ( @loopline ) {
383 foreach my $col ( @loopcol ) {
384 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
385 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
387 $table{$row->{rowtitle}}->{totalrow}=0;
390 # preparing calculation
391 my $strcalc = "SELECT $linefield, $colfield, count(*) FROM biblioitems INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 ";
392 $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
394 if (@$filters[0]){
395 @$filters[0]=~ s/\*/%/g;
396 $strcalc .= " AND dewey >" . @$filters[0];
398 if (@$filters[1]){
399 @$filters[1]=~ s/\*/%/g ;
400 $strcalc .= " AND dewey <" . @$filters[1];
402 if (@$filters[2]){
403 @$filters[2]=~ s/\*/%/g ;
404 $strcalc .= " AND lccn >" . @$filters[2];
406 if (@$filters[3]){
407 @$filters[3]=~ s/\*/%/g;
408 $strcalc .= " AND lccn <" . @$filters[3];
410 if (@$filters[4]){
411 @$filters[4]=~ s/\*/%/g ;
412 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
415 if (@$filters[5]){
416 @$filters[5]=~ s/\*/%/g;
417 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
420 if (@$filters[6]){
421 @$filters[6]=~ s/\*/%/g;
422 $strcalc .= " AND " .
423 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
424 . " LIKE '" . @$filters[6] ."'";
427 if (@$filters[7]){
428 @$filters[7]=~ s/\*/%/g;
429 @$filters[7].="%" unless @$filters[7]=~/%/;
430 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
432 if (@$filters[8]){
433 @$filters[8]=~ s/\*/%/g;
434 $strcalc .= " AND publicationyear >" . @$filters[8];
436 if (@$filters[9]){
437 @$filters[9]=~ s/\*/%/g;
438 $strcalc .= " AND publicationyear <" . @$filters[9];
440 if (@$filters[10]){
441 @$filters[10]=~ s/\*/%/g;
442 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
444 if (@$filters[11]){
445 @$filters[11]=~ s/\*/%/g;
446 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
448 if (@$filters[12]){
449 @$filters[12]=~ s/\*/%/g;
450 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
453 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
454 $debug and warn "SQL: $strcalc";
455 my $dbcalc = $dbh->prepare($strcalc);
456 if($barcodefilter){
457 $dbcalc->execute($barcodefilter);
458 }else{
459 $dbcalc->execute();
461 # warn "filling table";
463 my $emptycol;
464 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
465 # warn "filling table $row / $col / $value ";
466 $emptycol = 1 if (!defined($col));
467 $col = "zzEMPTY" if (!defined($col));
468 $row = "zzEMPTY" if (!defined($row));
470 $table{$row}->{$col}+=$value;
471 $table{$row}->{totalrow}+=$value;
472 $grantotal += $value;
475 # my %cell = {rowtitle => 'zzROWEMPTY'};
476 # push @loopline,\%cell;
477 # undef %cell;
478 # my %cell;
479 # %cell = {coltitle => "zzEMPTY"};
480 push @loopcol,{coltitle => "NULL"} if ($emptycol);
482 foreach my $row ( sort keys %table ) {
483 my @loopcell;
484 #@loopcol ensures the order for columns is common with column titles
485 # and the number matches the number of columns
486 foreach my $col ( @loopcol ) {
487 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
488 push @loopcell, {value => $value } ;
490 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
491 'loopcell' => \@loopcell,
492 'hilighted' => ($hilighted *= -1 > 0),
493 'totalrow' => $table{$row}->{totalrow}
497 # warn "footer processing";
498 foreach my $col ( @loopcol ) {
499 my $total=0;
500 foreach my $row ( @looprow ) {
501 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
502 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
504 # warn "summ for column ".$col->{coltitle}." = ".$total;
505 push @loopfooter, {'totalcol' => $total};
509 # the header of the table
510 $globalline{loopfilter}=\@loopfilter;
511 # the core of the table
512 $globalline{looprow} = \@looprow;
513 $globalline{loopcol} = \@loopcol;
514 # # the foot (totals by borrower type)
515 $globalline{loopfooter} = \@loopfooter;
516 $globalline{total}= $grantotal;
517 $globalline{line} = $line;
518 $globalline{column} = $column;
519 push @mainloop,\%globalline;
520 return \@mainloop;