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/;
34 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 my $podsp = $input->param("DisplayBy");
52 my $type = $input->param("PeriodTypeSel");
53 my $daysel = $input->param("PeriodDaySel");
54 my $monthsel = $input->param("PeriodMonthSel");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 my $del = $input->param("sep");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user({template_name => $fullreportname,
66 flagsrequired => {reports => 1},
69 $template->param(do_it => $do_it,
70 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
74 my $ccodes = GetKohaAuthorisedValues("items.ccode");
75 my $locations = GetKohaAuthorisedValues("items.location");
79 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
80 if ($output eq "screen"){
81 # Printing results to screen
82 $template->param(mainloop => $results);
83 output_html_with_http_headers $input, $cookie, $template->output;
86 # Printing to a csv file
87 print $input->header(-type => 'application/vnd.sun.xml.calc',
89 -attachment=>"$basename.csv",
90 -filename=>"$basename.csv" );
91 my $cols = @$results[0]->{loopcol};
92 my $lines = @$results[0]->{looprow};
94 $sep =C4::Context->preference("delimiter");
96 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
98 foreach my $col ( @$cols ) {
99 print $col->{coltitle}.$sep;
103 foreach my $line ( @$lines ) {
104 my $x = $line->{loopcell};
105 print $line->{rowtitle}.$sep;
106 foreach my $cell (@$x) {
107 print $cell->{value}.$sep;
109 print $line->{totalrow};
114 $cols = @$results[0]->{loopfooter};
115 foreach my $col ( @$cols ) {
116 print $sep.$col->{totalcol};
118 print $sep.@$results[0]->{total};
123 my $dbh = C4::Context->dbh;
128 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
133 while (my ($value, $desc) =$req->fetchrow) {
134 push @select, $value;
135 $select{$value}=$desc;
137 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
144 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
150 while (my ($value,$desc) =$req->fetchrow) {
151 push @select, $value;
152 $select{$value}=$desc;
154 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
161 my $branches=GetBranches();
163 foreach (keys %$branches) {
165 my %row = (branchcode => $_,
166 selected => ($thisbranch eq $_ ? 1 : 0),
167 code => $branches->{$_}->{'branchcode'},
168 description => $branches->{$_}->{'branchname'},
170 push @branchloop, \%row;
173 #FIXME - we have an auth val for these now.
174 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
179 while (my ($value) =$req->fetchrow) {
180 $hassort1 =1 if ($value);
181 push @select, $value;
183 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
189 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
195 while (my ($value) =$req->fetchrow) {
196 $hassort2 =1 if ($value);
197 $hglghtsort2= !($hassort1);
198 push @select, $value;
200 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
207 foreach (sort keys %$locations) {
208 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
212 foreach (keys %$ccodes) {
213 push @ccodes, { code => $_, description => $ccodes->{$_} };
217 my @mime = ( C4::Context->preference("MIME") );
219 my $CGIextChoice=CGI::scrolling_list(
226 my @dels = ( C4::Context->preference("delimiter") );
227 my $CGIsepChoice=CGI::scrolling_list(
235 CGIBorCat => $CGIBorCat,
236 CGIItemType => $CGIItemTypes,
237 hassort1=> $hassort1,
238 hassort2=> $hassort2,
239 HlghtSort2 => $hglghtsort2,
240 CGISort1 => $CGISort1,
241 CGISort2 => $CGISort2,
242 CGIextChoice => $CGIextChoice,
243 CGIsepChoice => $CGIsepChoice,
244 locationloop => \@locations,
245 ccodeloop => \@ccodes,
246 branchloop => \@branchloop,
248 output_html_with_http_headers $input, $cookie, $template->output;
255 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
264 my $dbh = C4::Context->dbh;
270 for (my $i=0;$i<=10;$i++) {
272 if ( @$filters[$i] ) {
273 if (($i==1) and (@$filters[$i-1])) {
274 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
276 # format the dates filters, otherwise just fill as is
278 $cell{filter} .= @$filters[$i];
280 $cell{filter} .= format_date(@$filters[$i]);
282 $cell{crit} .="Period From" if ($i==0);
283 $cell{crit} .="Period To" if ($i==1);
284 $cell{crit} .="Patron Category=" if ($i==2);
285 $cell{crit} .="Item Type=" if ($i==3);
286 $cell{crit} .="Library=" if ($i==4);
287 $cell{crit} .="Collection=" if ($i==5);
288 $cell{crit} .="Location=" if ($i==6);
289 $cell{crit} .="Item callnumber>=" if ($i==7);
290 $cell{crit} .="Item callnumber<" if ($i==8);
291 $cell{crit} .="sort1=" if ($i==9);
292 $cell{crit} .="sort2=" if ($i==10);
293 # FIXME - no translation mechanism !
294 push @loopfilter, \%cell;
297 push @loopfilter,{crit=>"Event",filter=>$type};
298 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
299 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
300 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
304 $debug and warn "filtres ". join "|", @filters;
305 my ($colsource, $linesource);
306 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
307 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
308 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
309 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
310 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
311 if ($line =~ /ccode/ ) {
312 $linefilter[0] = @$filters[5] ;
313 $linesource = 'items';
315 if ($line =~ /location/ ) {
316 $linefilter[0] = @$filters[6] ;
317 $linesource = 'items';
319 $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
320 $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
323 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
324 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
325 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
326 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
327 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
328 if ($column =~ /ccode/ ) {
329 $colfilter[0] = @$filters[5] ;
330 $colsource = 'items';
332 if ($column =~ /location/ ) {
333 $colfilter[0] = @$filters[6] ;
334 $colsource = 'items';
336 $colfilter[0] = @$filters[9] if ($column =~ /sort1/ ) ;
337 $colfilter[0] = @$filters[10] if ($column =~ /sort2/ ) ;
340 if (($line =~/datetime/) and ($dsp == 1)) {
342 $linefield .="dayname($line)";
343 } elsif (($line=~/datetime/) and ($dsp == 2)) {
345 $linefield .="monthname($line) ";
346 } elsif (($line=~/datetime/) and ($dsp == 3)) {
348 $linefield .="Year($line)";
349 } elsif ($line=~/datetime/) {
350 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
354 my $lineorder = $linefield;
355 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
356 $lineorder = "month($line)" if $linefield =~ "^month";
357 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
360 $strsth .= "select distinctrow $linefield from statistics, ";
361 # get stats on items if ccode or location, otherwise borrowers.
362 $strsth .= ($linesource eq 'items' ) ?
363 "items where (statistics.itemnumber=items.itemnumber) "
364 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
365 $strsth .= " and $line is not null ";
367 if ($line=~/datetime/) {
368 if ($linefilter[1] and ($linefilter[0])){
369 $strsth .= " and $line between ? and ? " ;
370 } elsif ($linefilter[1]) {
371 $strsth .= " and $line < ? " ;
372 } elsif ($linefilter[0]) {
373 $strsth .= " and $line > ? " ;
375 $strsth .= " and type ='".$type."' " if $type;
376 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
377 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
378 } elsif ($linefilter[0]) {
379 $linefilter[0] =~ s/\*/%/g;
380 $strsth .= " and $line LIKE ? " ;
382 $strsth .=" group by $linefield";
383 $strsth .=" order by $lineorder";
384 $debug and warn $strsth;
386 my $sth = $dbh->prepare( $strsth );
387 if (( @linefilter ) and ($linefilter[1])){
388 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
389 } elsif ($linefilter[0]) {
390 $sth->execute($linefilter[0]);
395 while ( my ($celvalue) = $sth->fetchrow) {
397 if($line =~ /ccode/) {
398 $cell{rowtitle_display} = $ccodes->{$celvalue};
399 } elsif($line=~/location/) {
400 $cell{rowtitle_display} = $locations->{$celvalue};
402 $cell{rowtitle_display} = $celvalue;
405 $cell{rowtitle} = $celvalue;
407 $cell{rowtitle} = "";
410 push @loopline, \%cell;
416 if (($column =~/datetime/) and ($dsp == 1)) {
418 $colfield .="dayname($column)";
419 } elsif (($column=~/datetime/) and ($dsp == 2)) {
421 $colfield .="monthname($column)";
422 } elsif (($column=~/datetime/) and ($dsp == 3)) {
424 $colfield .="Year($column)";
425 } elsif ($column=~/datetime/) {
426 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
428 $colfield .= $column;
430 $colorder = "weekday($line)" if $colfield =~ "^dayname";
431 $colorder = "month($line)" if $colfield =~ "^month";
432 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
435 $strsth2 .= "select distinctrow $colfield from statistics, ";
436 # get stats on items if ccode or location, otherwise borrowers.
437 $strsth2 .= ($colsource eq 'items' ) ?
438 "items where (statistics.itemnumber=items.itemnumber) "
439 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
440 $strsth2 .= " and $column is not null ";
442 if ($column=~/datetime/){
443 if (($colfilter[1]) and ($colfilter[0])){
444 $strsth2 .= " and $column between ? and ? " ;
445 } elsif ($colfilter[1]) {
446 $strsth2 .= " and $column < ? " ;
447 } elsif ($colfilter[0]) {
448 $strsth2 .= " and $column > ? " ;
450 $strsth2 .= " and type ='".$type."' " if $type;
451 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
452 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
453 } elsif ($colfilter[0]) {
454 $colfilter[0] =~ s/\*/%/g;
455 $strsth2 .= " and $column LIKE ? " ;
457 $strsth2 .=" group by $colfield";
458 $strsth2 .=" order by $colorder";
460 my $sth2 = $dbh->prepare( $strsth2 );
461 if (( @colfilter ) and ($colfilter[1])){
462 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
463 } elsif ($colfilter[0]) {
464 $sth2->execute($colfilter[0]);
470 while (my ($celvalue) = $sth2->fetchrow) {
473 if($column =~ /ccode/) {
474 $cell{coltitle_display} = $ccodes->{$celvalue};
475 } elsif($column=~/location/) {
476 $cell{coltitle_display} = $locations->{$celvalue};
478 $cell{coltitle_display} = $celvalue;
480 $cell{coltitle} = $celvalue;
481 # we leave this as 'coltitle' since we use it as hash key when filling the table, and add a title_display key.
483 push @loopcol, \%cell;
485 # warn "fin des titres colonnes";
491 #Initialization of cell values.....
494 foreach my $row ( @loopline ) {
495 foreach my $col ( @loopcol ) {
496 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
497 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
499 $table{$row->{rowtitle}}->{totalrow}=0;
502 # preparing calculation
505 $strcalc .= "SELECT $linefield, $colfield, ";
506 $strcalc .= "COUNT( * ) " if ($process ==1);
508 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
511 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
514 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
515 $rqbookcount->execute;
516 my ($bookcount) = $rqbookcount->fetchrow;
517 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
519 $strcalc .= "FROM statistics ";
520 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
521 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if ( ($colsource eq 'items') || @$filters[5] || @$filters[6] ||@$filters[7] || @$filters[8] );
523 $strcalc .= "WHERE 1=1 ";
524 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
525 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
526 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
527 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
528 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
529 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
530 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
531 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
532 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
533 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
534 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
535 $strcalc .= " AND items.ccode like '" . @$filters[5] ."'" if ( @$filters[5] );
536 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
537 $strcalc .= " AND items.location like '" . @$filters[6] ."'" if ( @$filters[6] );
538 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
539 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] ."'" if ( @$filters[7] );
540 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
541 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] ."'" if ( @$filters[8] );
542 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
543 $strcalc .= " AND borrowers.sort1 like '" . @$filters[9] ."'" if ( @$filters[9] );
544 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
545 $strcalc .= " AND borrowers.sort2 like '" . @$filters[10] ."'" if ( @$filters[10] );
546 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
547 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
548 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
550 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
551 ($debug) and warn "". $strcalc;
552 my $dbcalc = $dbh->prepare($strcalc);
554 # warn "filling table";
556 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
557 ($debug) and warn "filling table $row / $col / $value ";
558 $emptycol = 1 if ($col eq undef);
559 $col = "zzEMPTY" if ($col eq undef);
560 $row = "zzEMPTY" if ($row eq undef);
562 $table{$row}->{$col}+=$value;
563 $table{$row}->{totalrow}+=$value;
564 $grantotal += $value;
566 push @loopcol,{coltitle => "NULL"} if ($emptycol);
568 foreach my $row (@loopline) {
570 #@loopcol ensures the order for columns is common with column titles
571 # and the number matches the number of columns
572 foreach my $col ( @loopcol ) {
573 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
574 push @loopcell, {value => $value } ;
576 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
577 'rowtitle_display' => ($row->{rowtitle_display} eq "NULL")?"zzEMPTY":$row->{rowtitle_display},
578 'loopcell' => \@loopcell,
579 'hilighted' => ($hilighted >0),
580 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
582 $hilighted = -$hilighted;
585 # warn "footer processing";
586 foreach my $col ( @loopcol ) {
588 foreach my $row ( @looprow ) {
589 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
590 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
592 # warn "summ for column ".$col->{coltitle}." = ".$total;
593 push @loopfooter, {'totalcol' => $total};
597 # the header of the table
598 $globalline{loopfilter}=\@loopfilter;
599 # the core of the table
600 $globalline{looprow} = \@looprow;
601 $globalline{loopcol} = \@loopcol;
602 # # the foot (totals by borrower type)
603 $globalline{loopfooter} = \@loopfooter;
604 $globalline{total}= $grantotal;
605 $globalline{line} = $line;
606 $globalline{column} = $column;
607 push @mainloop,\%globalline;