overdue_notices.pl send no email directly to patron
[koha.git] / reports / catalogue_stats.pl
blob220157c98478493033c43a187deb429801e0eab5
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 # use warnings; # FIXME
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 my $mime = $input->param("MIME");
55 our $sep = $input->param("sep");
56 $sep = "\t" if ($sep eq 'tabulation');
57 my $item_itype;
58 if(C4::Context->preference('item-level_itypes')) {
59 $item_itype = "items\.itype"
60 } else {
61 $item_itype = "itemtype";
64 my ($template, $borrowernumber, $cookie)
65 = get_template_and_user({template_name => $fullreportname,
66 query => $input,
67 type => "intranet",
68 authnotrequired => 0,
69 flagsrequired => {reports => '*'},
70 debug => 1,
71 });
72 $template->param(do_it => $do_it);
73 if ($do_it) {
74 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
75 if ($output eq "screen"){
76 $template->param(mainloop => $results);
77 output_html_with_http_headers $input, $cookie, $template->output;
78 exit(1);
79 } else {
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -encoding => 'utf-8',
82 -attachment=>"$basename.csv",
83 -name=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
90 print "Total\n";
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
98 print "\n";
100 print "TOTAL";
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
106 exit(1);
108 } else {
109 my $dbh = C4::Context->dbh;
110 my @values;
111 my %labels;
112 my $count=0;
113 my $req;
114 my @select;
115 # FIXME: no such field "dewey"
116 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
117 # $req->execute;
118 my $hasdewey = 0;
120 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
121 # This is not the LC Classification. It's the Control Number.
122 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
124 my $haslccn = 0;
125 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
126 # $req->execute;
127 # my $hlghtlccn;
128 # while (my ($value) =$req->fetchrow) {
129 # $hlghtlccn = !($hasdewey);
130 # $haslccn =1 if (($value>2) and (! $haslccn));
131 # $count++ if (($value) and (! $haslccn));
132 # push @select, $value;
134 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
135 # -id => 'Filter',
136 # -values => \@select,
137 # -size => 1,
138 # -multiple => 0 );
140 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
141 # FIXME: Hardcoding to 5 chars on itemcallnum.
143 my $hascote = 1;
144 my $highcote = 5;
146 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
147 $req->execute;
148 my $CGIitemtype = $req->fetchall_arrayref({});
150 my $authvals = GetKohaAuthorisedValues("items.ccode");
151 my @authvals;
152 foreach (sort {$authvals->{$a} cmp $authvals->{$b} || $a cmp $b} keys %$authvals) {
153 push @authvals, { code => $_, description => $authvals->{$_} };
156 my $locations = GetKohaAuthorisedValues("items.location");
157 my @locations;
158 foreach (sort keys %$locations) {
159 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
162 my @mime = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
164 $template->param(hasdewey=>$hasdewey,
165 haslccn => $haslccn,
166 hascote => $hascote,
167 CGIItemType => $CGIitemtype,
168 CGIBranch => GetBranchesLoop(C4::Context->userenv->{'branch'}),
169 locationloop => \@locations,
170 authvals => \@authvals,
171 CGIextChoice => \@mime,
172 CGIsepChoice => GetDelimiterChoices,
173 item_itype => $item_itype
177 output_html_with_http_headers $input, $cookie, $template->output;
179 ## End of Main Body
182 sub calculate {
183 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
184 my @mainloop;
185 my @loopfooter;
186 my @loopcol;
187 my @loopline;
188 my @looprow;
189 my %globalline;
190 my $grantotal =0;
191 my $barcodelike = @$filters[13];
192 my $barcodefilter = @$filters[14];
193 my $not;
195 # extract parameters
196 my $dbh = C4::Context->dbh;
198 # if barcodefilter is empty set as %
199 if($barcodefilter){
200 # Check if barcodefilter is "like" or "not like"
201 if(!$barcodelike){
202 $not = "not";
204 # Change * to %
205 $barcodefilter =~ s/\*/%/g;
206 }else{
207 $barcodefilter = "%";
210 # Filters
211 # Checking filters
213 my @loopfilter;
214 for (my $i=0;$i<=12;$i++) {
215 my %cell;
216 if ( @$filters[$i] ) {
217 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
218 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
220 $cell{filter} .= @$filters[$i];
221 $cell{crit} .=
222 ($i== 0) ? "Dewey Classification From" :
223 ($i== 1) ? "Dewey Classification To" :
224 ($i== 2) ? "Lccn Classification From" :
225 ($i== 3) ? "Lccn Classification To" :
226 ($i== 4) ? "Item CallNumber From" :
227 ($i== 5) ? "Item CallNumber To" :
228 ($i== 6) ? "Item type" :
229 ($i== 7) ? "Publisher" :
230 ($i== 8) ? "Publication year From" :
231 ($i== 9) ? "Publication year To" :
232 ($i==10) ? "Library :" :
233 ($i==11) ? "Shelving Location :" :
234 ($i==12) ? "Collection Code :" : '';
235 push @loopfilter, \%cell;
239 # warn map {"filtres $_\n"} @filters[0..3];
241 my @linefilter;
242 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
243 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
244 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
245 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
246 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
247 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
248 if (C4::Context->preference('item-level_itypes')) {
249 $linefilter[0] = @$filters[6] if ($line =~ /items\.itype/ ) ;
250 } else {
251 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
253 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
254 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
255 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
256 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
257 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
258 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
260 my @colfilter ;
261 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
262 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
263 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
264 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
265 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
266 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
267 if (C4::Context->preference('item-level_itypes')) {
268 $colfilter[0] = @$filters[6] if ($column =~ /items\.itype/ ) ;
269 } else {
270 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
272 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
273 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
274 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
275 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
276 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
277 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
279 # 1st, loop rows.
280 my $linefield;
281 if (($line =~/dewey/) and ($deweydigits)) {
282 $linefield .="left($line,$deweydigits)";
283 } elsif (($line=~/lccn/) and ($lccndigits)) {
284 $linefield .="left($line,$lccndigits)";
285 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
286 $linefield .="left($line,$cotedigits)";
287 }else {
288 $linefield .= $line;
291 my $strsth;
292 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where barcode $not LIKE ? AND $line is not null ";
293 if ( @linefilter ) {
294 if ($linefilter[1]){
295 $strsth .= " and $line >= ? " ;
296 $strsth .= " and $line <= ? " ;
297 } elsif ($linefilter[0]) {
298 $linefilter[0] =~ s/\*/%/g;
299 $strsth .= " and $line LIKE ? " ;
302 $strsth .=" order by $linefield";
303 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
305 my $sth = $dbh->prepare( $strsth );
306 if (( @linefilter ) and ($linefilter[1])){
307 $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
308 } elsif ($barcodefilter,$linefilter[0]) {
309 $sth->execute($barcodefilter,$linefilter[0]);
310 } else {
311 $sth->execute($barcodefilter);
313 while ( my ($celvalue) = $sth->fetchrow) {
314 my %cell;
315 if ($celvalue) {
316 $cell{rowtitle} = $celvalue;
317 # } else {
318 # $cell{rowtitle} = "";
320 $cell{totalrow} = 0;
321 push @loopline, \%cell;
324 # 2nd, loop cols.
325 my $colfield;
326 if (($column =~/dewey/) and ($deweydigits)) {
327 $colfield = "left($column,$deweydigits)";
328 }elsif (($column=~/lccn/) and ($lccndigits)) {
329 $colfield = "left($column,$lccndigits)";
330 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
331 $colfield = "left($column,$cotedigits)";
332 }else {
333 $colfield = $column;
336 my $strsth2 = "
337 SELECT distinctrow $colfield
338 FROM biblioitems
339 LEFT JOIN items
340 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
341 WHERE barcode $not LIKE ? AND $column IS NOT NULL ";
342 if (( @colfilter ) and ($colfilter[1])) {
343 $strsth2 .= " and $column> ? and $column< ?";
344 }elsif ($colfilter[0]){
345 $colfilter[0] =~ s/\*/%/g;
346 $strsth2 .= " and $column LIKE ? ";
348 $strsth2 .= " order by $colfield";
349 $debug and print STDERR "SQL: $strsth2";
350 my $sth2 = $dbh->prepare( $strsth2 );
351 if ((@colfilter) and ($colfilter[1])) {
352 $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
353 } elsif ($colfilter[0]){
354 $sth2->execute($barcodefilter,$colfilter[0]);
355 } else {
356 $sth2->execute($barcodefilter);
358 while (my ($celvalue) = $sth2->fetchrow) {
359 my %cell;
360 my %ft;
361 if ($celvalue) {
362 $cell{coltitle} = $celvalue;
363 # } else {
364 # $cell{coltitle} = "";
366 $ft{totalcol} = 0;
367 push @loopcol, \%cell;
370 my $i=0;
371 my @totalcol;
372 my $hilighted=-1;
374 #Initialization of cell values.....
375 my %table;
376 # warn "init table";
377 foreach my $row ( @loopline ) {
378 foreach my $col ( @loopcol ) {
379 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
380 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
382 $table{$row->{rowtitle}}->{totalrow}=0;
385 # preparing calculation
386 my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 AND barcode $not like ? ";
387 if (@$filters[0]){
388 @$filters[0]=~ s/\*/%/g;
389 $strcalc .= " AND dewey >" . @$filters[0];
391 if (@$filters[1]){
392 @$filters[1]=~ s/\*/%/g ;
393 $strcalc .= " AND dewey <" . @$filters[1];
395 if (@$filters[2]){
396 @$filters[2]=~ s/\*/%/g ;
397 $strcalc .= " AND lccn >" . @$filters[2];
399 if (@$filters[3]){
400 @$filters[3]=~ s/\*/%/g;
401 $strcalc .= " AND lccn <" . @$filters[3];
403 if (@$filters[4]){
404 @$filters[4]=~ s/\*/%/g ;
405 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
408 if (@$filters[5]){
409 @$filters[5]=~ s/\*/%/g;
410 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
413 if (@$filters[6]){
414 @$filters[6]=~ s/\*/%/g;
415 $strcalc .= " AND " .
416 (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
417 . " LIKE '" . @$filters[6] ."'";
420 if (@$filters[7]){
421 @$filters[7]=~ s/\*/%/g;
422 @$filters[7].="%" unless @$filters[7]=~/%/;
423 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
425 if (@$filters[8]){
426 @$filters[8]=~ s/\*/%/g;
427 $strcalc .= " AND publicationyear >" . @$filters[8];
429 if (@$filters[9]){
430 @$filters[9]=~ s/\*/%/g;
431 $strcalc .= " AND publicationyear <" . @$filters[9];
433 if (@$filters[10]){
434 @$filters[10]=~ s/\*/%/g;
435 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
437 if (@$filters[11]){
438 @$filters[11]=~ s/\*/%/g;
439 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
441 if (@$filters[12]){
442 @$filters[12]=~ s/\*/%/g;
443 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
446 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
447 $debug and warn "SQL: $strcalc";
448 my $dbcalc = $dbh->prepare($strcalc);
449 $dbcalc->execute($barcodefilter);
450 # warn "filling table";
452 my $emptycol;
453 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
454 # warn "filling table $row / $col / $value ";
455 $emptycol = 1 if (!defined($col));
456 $col = "zzEMPTY" if (!defined($col));
457 $row = "zzEMPTY" if (!defined($row));
459 $table{$row}->{$col}+=$value;
460 $table{$row}->{totalrow}+=$value;
461 $grantotal += $value;
464 # my %cell = {rowtitle => 'zzROWEMPTY'};
465 # push @loopline,\%cell;
466 # undef %cell;
467 # my %cell;
468 # %cell = {coltitle => "zzEMPTY"};
469 push @loopcol,{coltitle => "NULL"} if ($emptycol);
471 foreach my $row ( sort keys %table ) {
472 my @loopcell;
473 #@loopcol ensures the order for columns is common with column titles
474 # and the number matches the number of columns
475 foreach my $col ( @loopcol ) {
476 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
477 push @loopcell, {value => $value } ;
479 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
480 'loopcell' => \@loopcell,
481 'hilighted' => ($hilighted *= -1 > 0),
482 'totalrow' => $table{$row}->{totalrow}
486 # warn "footer processing";
487 foreach my $col ( @loopcol ) {
488 my $total=0;
489 foreach my $row ( @looprow ) {
490 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
491 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
493 # warn "summ for column ".$col->{coltitle}." = ".$total;
494 push @loopfooter, {'totalcol' => $total};
498 # the header of the table
499 $globalline{loopfilter}=\@loopfilter;
500 # the core of the table
501 $globalline{looprow} = \@looprow;
502 $globalline{loopcol} = \@loopcol;
503 # # the foot (totals by borrower type)
504 $globalline{loopfooter} = \@loopfooter;
505 $globalline{total}= $grantotal;
506 $globalline{line} = $line;
507 $globalline{column} = $column;
508 push @mainloop,\%globalline;
509 return \@mainloop;