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
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
25 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 my $podsp = $input->param("DisplayBy");
53 my $type = $input->param("PeriodTypeSel");
54 my $daysel = $input->param("PeriodDaySel");
55 my $monthsel = $input->param("PeriodMonthSel");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 my $del = $input->param("sep");
61 #warn "calcul : ".$calc;
62 my ($template, $borrowernumber, $cookie)
63 = get_template_and_user({template_name => $fullreportname,
67 flagsrequired => {reports => 1},
70 $template->param(do_it => $do_it,
71 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
74 my $itemtypes = GetItemTypes();
75 my $categoryloop = GetBorrowercategoryList;
77 my $ccodes = GetKohaAuthorisedValues("items.ccode");
78 my $locations = GetKohaAuthorisedValues("items.location");
80 my $Bsort1 = GetAuthorisedValues("Bsort1");
81 my $Bsort2 = GetAuthorisedValues("Bsort2");
82 my ($hassort1,$hassort2);
83 $hassort1=1 if $Bsort1;
84 $hassort2=1 if $Bsort2;
89 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
90 if ($output eq "screen"){
91 # Printing results to screen
92 $template->param(mainloop => $results);
93 output_html_with_http_headers $input, $cookie, $template->output;
96 # Printing to a csv file
97 print $input->header(-type => 'application/vnd.sun.xml.calc',
99 -attachment=>"$basename.csv",
100 -filename=>"$basename.csv" );
101 my $cols = @$results[0]->{loopcol};
102 my $lines = @$results[0]->{looprow};
104 $sep =C4::Context->preference("delimiter");
106 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
108 foreach my $col ( @$cols ) {
109 print $col->{coltitle}.$sep;
113 foreach my $line ( @$lines ) {
114 my $x = $line->{loopcell};
115 print $line->{rowtitle}.$sep;
116 foreach my $cell (@$x) {
117 print $cell->{value}.$sep;
119 print $line->{totalrow};
124 $cols = @$results[0]->{loopfooter};
125 foreach my $col ( @$cols ) {
126 print $sep.$col->{totalcol};
128 print $sep.@$results[0]->{total};
133 my $dbh = C4::Context->dbh;
138 # create itemtype arrayref for <select>.
140 for my $itype ( keys(%$itemtypes)) {
141 push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{description} } ;
144 my $branches=GetBranches();
146 foreach (keys %$branches) {
148 my %row = (branchcode => $_,
149 selected => ($thisbranch eq $_ ? 1 : 0),
150 code => $branches->{$_}->{'branchcode'},
151 description => $branches->{$_}->{'branchname'},
153 push @branchloop, \%row;
158 foreach (sort keys %$locations) {
159 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
163 foreach (keys %$ccodes) {
164 push @ccodes, { code => $_, description => $ccodes->{$_} };
168 my @mime = ( C4::Context->preference("MIME") );
170 my $CGIextChoice=CGI::scrolling_list(
177 my @dels = ( C4::Context->preference("delimiter") );
178 my $CGIsepChoice=CGI::scrolling_list(
186 categoryloop => $categoryloop,
187 itemtypeloop => \@itemtypeloop,
188 hassort1=> $hassort1,
189 hassort2=> $hassort2,
192 CGIextChoice => $CGIextChoice,
193 CGIsepChoice => $CGIsepChoice,
194 locationloop => \@locations,
195 ccodeloop => \@ccodes,
196 branchloop => \@branchloop,
198 output_html_with_http_headers $input, $cookie, $template->output;
205 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
214 my $dbh = C4::Context->dbh;
220 for (my $i=0;$i<=10;$i++) {
222 if ( @$filters[$i] ) {
223 if (($i==1) and (@$filters[$i-1])) {
224 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
226 # format the dates filters, otherwise just fill as is
228 $cell{filter} .= @$filters[$i];
230 $cell{filter} .= format_date(@$filters[$i]);
232 $cell{crit} .="Period From" if ($i==0);
233 $cell{crit} .="Period To" if ($i==1);
234 $cell{crit} .="Patron Category=" if ($i==2);
235 $cell{crit} .="Item Type=" if ($i==3);
236 $cell{crit} .="Library=" if ($i==4);
237 $cell{crit} .="Collection=" if ($i==5);
238 $cell{crit} .="Location=" if ($i==6);
239 $cell{crit} .="Item callnumber>=" if ($i==7);
240 $cell{crit} .="Item callnumber<" if ($i==8);
241 $cell{crit} .="sort1=" if ($i==9);
242 $cell{crit} .="sort2=" if ($i==10);
243 # FIXME - no translation mechanism !
244 push @loopfilter, \%cell;
247 push @loopfilter,{crit=>"Event",filter=>$type};
248 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
249 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
250 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
254 $debug and warn "filtres ". join "|", @filters;
255 my ($colsource, $linesource);
256 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
257 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
258 if ($line =~ /category/ ) {
259 $linefilter[0] = @$filters[2] ;
261 if ($line =~ /itemtype/ ) {
262 $linefilter[0] = @$filters[3] ;
264 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
265 if ($line =~ /ccode/ ) {
266 $linefilter[0] = @$filters[5] ;
267 $linesource = 'items';
269 if ($line =~ /location/ ) {
270 $linefilter[0] = @$filters[6] ;
271 $linesource = 'items';
273 $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
274 $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
277 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
278 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
279 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
280 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
281 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
282 if ($column =~ /ccode/ ) {
283 $colfilter[0] = @$filters[5] ;
284 $colsource = 'items';
286 if ($column =~ /location/ ) {
287 $colfilter[0] = @$filters[6] ;
288 $colsource = 'items';
290 $colfilter[0] = @$filters[9] if ($column =~ /sort1/ ) ;
291 $colfilter[0] = @$filters[10] if ($column =~ /sort2/ ) ;
294 if (($line =~/datetime/) and ($dsp == 1)) {
296 $linefield .="dayname($line)";
297 } elsif (($line=~/datetime/) and ($dsp == 2)) {
299 $linefield .="monthname($line) ";
300 } elsif (($line=~/datetime/) and ($dsp == 3)) {
302 $linefield .="Year($line)";
303 } elsif ($line=~/datetime/) {
304 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
308 my $lineorder = $linefield;
309 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
310 $lineorder = "month($line)" if $linefield =~ "^month";
311 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
314 $strsth .= "select distinctrow $linefield from statistics, ";
315 # get stats on items if ccode or location, otherwise borrowers.
316 $strsth .= ($linesource eq 'items' ) ?
317 "items where (statistics.itemnumber=items.itemnumber) "
318 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
319 $strsth .= " and $line is not null ";
321 if ($line=~/datetime/) {
322 if ($linefilter[1] and ($linefilter[0])){
323 $strsth .= " and $line between ? and ? " ;
324 } elsif ($linefilter[1]) {
325 $strsth .= " and $line < ? " ;
326 } elsif ($linefilter[0]) {
327 $strsth .= " and $line > ? " ;
329 $strsth .= " and type ='".$type."' " if $type;
330 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
331 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
332 } elsif ($linefilter[0]) {
333 $linefilter[0] =~ s/\*/%/g;
334 $strsth .= " and $line LIKE ? " ;
336 $strsth .=" group by $linefield";
337 $strsth .=" order by $lineorder";
338 $debug and warn $strsth;
340 my $sth = $dbh->prepare( $strsth );
341 if (( @linefilter ) and ($linefilter[1])){
342 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
343 } elsif ($linefilter[0]) {
344 $sth->execute($linefilter[0]);
349 while ( my ($celvalue) = $sth->fetchrow) {
351 if($line =~ /ccode/) {
352 $cell{rowtitle_display} = $ccodes->{$celvalue};
353 } elsif($line=~/location/) {
354 $cell{rowtitle_display} = $locations->{$celvalue};
355 } elsif($line=~/sort1/) {
356 for my $s (@$Bsort1) {
357 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
359 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
360 } elsif($line=~/sort2/) {
361 for my $s (@$Bsort2) {
362 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
364 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
365 } elsif($line=~/categorycode/) {
366 for my $s (@$categoryloop) {
367 $cell{rowtitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
369 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
370 } elsif($line=~/itemtype/) {
371 $cell{rowtitle_display} = $itemtypes->{$celvalue}->{description};
373 $cell{rowtitle_display} = $celvalue;
376 $cell{rowtitle} = $celvalue;
378 $cell{rowtitle} = "";
381 push @loopline, \%cell;
387 if (($column =~/datetime/) and ($dsp == 1)) {
389 $colfield .="dayname($column)";
390 } elsif (($column=~/datetime/) and ($dsp == 2)) {
392 $colfield .="monthname($column)";
393 } elsif (($column=~/datetime/) and ($dsp == 3)) {
395 $colfield .="Year($column)";
396 } elsif ($column=~/datetime/) {
397 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
399 $colfield .= $column;
401 $colorder = "weekday($line)" if $colfield =~ "^dayname";
402 $colorder = "month($line)" if $colfield =~ "^month";
403 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
406 $strsth2 .= "select distinctrow $colfield from statistics, ";
407 # get stats on items if ccode or location, otherwise borrowers.
408 $strsth2 .= ($colsource eq 'items' ) ?
409 "items where (statistics.itemnumber=items.itemnumber) "
410 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
411 $strsth2 .= " and $column is not null ";
413 if ($column=~/datetime/){
414 if (($colfilter[1]) and ($colfilter[0])){
415 $strsth2 .= " and $column between ? and ? " ;
416 } elsif ($colfilter[1]) {
417 $strsth2 .= " and $column < ? " ;
418 } elsif ($colfilter[0]) {
419 $strsth2 .= " and $column > ? " ;
421 $strsth2 .= " and type ='".$type."' " if $type;
422 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
423 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
424 } elsif ($colfilter[0]) {
425 $colfilter[0] =~ s/\*/%/g;
426 $strsth2 .= " and $column LIKE ? " ;
428 $strsth2 .=" group by $colfield";
429 $strsth2 .=" order by $colorder";
431 my $sth2 = $dbh->prepare( $strsth2 );
432 if (( @colfilter ) and ($colfilter[1])){
433 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
434 } elsif ($colfilter[0]) {
435 $sth2->execute($colfilter[0]);
441 while (my ($celvalue) = $sth2->fetchrow) {
444 if($column =~ /ccode/) {
445 $cell{coltitle_display} = $ccodes->{$celvalue};
446 } elsif($column=~/location/) {
447 $cell{coltitle_display} = $locations->{$celvalue};
448 } elsif($column=~/itemtype/) {
449 $cell{coltitle_display} = $itemtypes->{$celvalue}->{description};
450 } elsif($column=~/sort1/) {
451 for my $s (@$Bsort1) {
452 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
454 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
455 } elsif($column=~/sort2/) {
456 for my $s (@$Bsort2) {
457 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
459 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
460 } elsif($column=~/category/) {
461 for my $s (@$categoryloop) {
462 $cell{coltitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
464 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
466 $cell{coltitle_display} = $celvalue;
468 $cell{coltitle} = $celvalue;
469 # we leave this as 'coltitle' since we use it as hash key when filling the table, and add a title_display key.
471 push @loopcol, \%cell;
473 # warn "fin des titres colonnes";
478 #Initialization of cell values.....
481 foreach my $row ( @loopline ) {
482 foreach my $col ( @loopcol ) {
483 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
484 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
486 $table{$row->{rowtitle}}->{totalrow}=0;
489 # preparing calculation
492 $strcalc .= "SELECT $linefield, $colfield, ";
493 $strcalc .= "COUNT( * ) " if ($process ==1);
495 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
498 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
501 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
502 $rqbookcount->execute;
503 my ($bookcount) = $rqbookcount->fetchrow;
504 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
506 $strcalc .= "FROM statistics ";
507 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
508 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if ( ($colsource eq 'items') || @$filters[5] || @$filters[6] ||@$filters[7] || @$filters[8] );
510 $strcalc .= "WHERE 1=1 ";
511 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
512 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
513 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
514 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
515 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
516 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
517 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
518 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
519 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
520 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
521 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
522 $strcalc .= " AND items.ccode like '" . @$filters[5] ."'" if ( @$filters[5] );
523 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
524 $strcalc .= " AND items.location like '" . @$filters[6] ."'" if ( @$filters[6] );
525 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
526 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] ."'" if ( @$filters[7] );
527 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
528 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] ."'" if ( @$filters[8] );
529 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
530 $strcalc .= " AND borrowers.sort1 like '" . @$filters[9] ."'" if ( @$filters[9] );
531 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
532 $strcalc .= " AND borrowers.sort2 like '" . @$filters[10] ."'" if ( @$filters[10] );
533 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
534 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
535 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
537 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
538 ($debug) and warn "". $strcalc;
539 my $dbcalc = $dbh->prepare($strcalc);
541 # warn "filling table";
542 my ($emptycol,$emptyrow);
543 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
544 ($debug) and warn "filling table $row / $col / $value ";
553 $table{$row}->{$col}+=$value;
554 $table{$row}->{totalrow}+=$value;
555 $grantotal += $value;
557 push @loopcol,{coltitle => "NULL", coltitle_display => 'NULL'} if ($emptycol);
558 push @loopline,{rowtitle => "NULL", rowtitle_display => 'NULL'} if ($emptyrow);
560 foreach my $row (@loopline) {
562 #@loopcol ensures the order for columns is common with column titles
563 # and the number matches the number of columns
564 foreach my $col ( @loopcol ) {
565 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
566 push @loopcell, {value => $value } ;
568 push @looprow, { 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
569 'rowtitle_display' => ($row->{rowtitle_display} eq "NULL")?"NULL":$row->{rowtitle_display},
570 'loopcell' => \@loopcell,
571 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
574 # warn "footer processing";
575 for my $col ( @loopcol ) {
577 foreach my $row ( @looprow ) {
578 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
579 $debug and warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
581 # warn "summ for column ".$col->{coltitle}." = ".$total;
582 push @loopfooter, {'totalcol' => $total};
586 # the header of the table
587 $globalline{loopfilter}=\@loopfilter;
588 # the core of the table
589 $globalline{looprow} = \@looprow;
590 $globalline{loopcol} = \@loopcol;
591 # # the foot (totals by borrower type)
592 $globalline{loopfooter} = \@loopfooter;
593 $globalline{total}= $grantotal;
594 $globalline{line} = $line;
595 $globalline{column} = $column;
596 push @mainloop,\%globalline;