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 $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
75 if ($output eq "screen"){
76 # Printing results to screen
77 $template->param(mainloop => $results);
78 output_html_with_http_headers $input, $cookie, $template->output;
81 # Printing to a csv file
82 print $input->header(-type => 'application/vnd.sun.xml.calc',
84 -attachment=>"$basename.csv",
85 -filename=>"$basename.csv" );
86 my $cols = @$results[0]->{loopcol};
87 my $lines = @$results[0]->{looprow};
89 $sep =C4::Context->preference("delimiter");
91 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
93 foreach my $col ( @$cols ) {
94 print $col->{coltitle}.$sep;
98 foreach my $line ( @$lines ) {
99 my $x = $line->{loopcell};
100 print $line->{rowtitle}.$sep;
101 foreach my $cell (@$x) {
102 print $cell->{value}.$sep;
104 print $line->{totalrow};
109 $cols = @$results[0]->{loopfooter};
110 foreach my $col ( @$cols ) {
111 print $sep.$col->{totalcol};
113 print $sep.@$results[0]->{total};
118 my $dbh = C4::Context->dbh;
123 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
128 while (my ($value, $desc) =$req->fetchrow) {
129 push @select, $value;
130 $select{$value}=$desc;
132 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
139 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
145 while (my ($value,$desc) =$req->fetchrow) {
146 push @select, $value;
147 $select{$value}=$desc;
149 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
156 my $branches=GetBranches();
158 foreach (keys %$branches) {
160 my %row = (branchcode => $_,
161 selected => ($thisbranch eq $_ ? 1 : 0),
162 code => $branches->{$_}->{'branchcode'},
163 description => $branches->{$_}->{'branchname'},
165 push @branchloop, \%row;
168 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
173 while (my ($value) =$req->fetchrow) {
174 $hassort1 =1 if ($value);
175 push @select, $value;
177 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
183 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
189 while (my ($value) =$req->fetchrow) {
190 $hassort2 =1 if ($value);
191 $hglghtsort2= !($hassort1);
192 push @select, $value;
194 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
200 my $locations = GetKohaAuthorisedValues("items.location");
202 foreach (sort keys %$locations) {
203 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
206 my $ccodes = GetKohaAuthorisedValues("items.ccode");
208 foreach (keys %$ccodes) {
209 push @ccodes, { code => $_, description => $ccodes->{$_} };
213 my @mime = ( C4::Context->preference("MIME") );
215 my $CGIextChoice=CGI::scrolling_list(
222 my @dels = ( C4::Context->preference("delimiter") );
223 my $CGIsepChoice=CGI::scrolling_list(
231 CGIBorCat => $CGIBorCat,
232 CGIItemType => $CGIItemTypes,
233 hassort1=> $hassort1,
234 hassort2=> $hassort2,
235 HlghtSort2 => $hglghtsort2,
236 CGISort1 => $CGISort1,
237 CGISort2 => $CGISort2,
238 CGIextChoice => $CGIextChoice,
239 CGIsepChoice => $CGIsepChoice,
240 locationloop => \@locations,
241 ccodeloop => \@ccodes,
242 branchloop => \@branchloop,
244 output_html_with_http_headers $input, $cookie, $template->output;
251 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
260 my $dbh = C4::Context->dbh;
266 for (my $i=0;$i<=9;$i++) {
268 if ( @$filters[$i] ) {
269 if (($i==1) and (@$filters[$i-1])) {
270 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
272 # format the dates filters, otherwise just fill as is
274 $cell{filter} .= @$filters[$i];
276 $cell{filter} .= format_date(@$filters[$i]);
278 $cell{crit} .="Period From" if ($i==0);
279 $cell{crit} .="Period To" if ($i==1);
280 $cell{crit} .="Borrower Cat=" if ($i==2);
281 $cell{crit} .="Doc Type=" if ($i==3);
282 $cell{crit} .="Branch=" if ($i==4);
283 $cell{crit} .="Location=" if ($i==5);
284 $cell{crit} .="Item callnumber>=" if ($i==6);
285 $cell{crit} .="Item callnumber<" if ($i==7);
286 $cell{crit} .="sort1=" if ($i==8);
287 $cell{crit} .="sort2=" if ($i==9);
289 push @loopfilter, \%cell;
292 push @loopfilter,{crit=>"Event",filter=>$type};
293 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
294 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
295 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
299 # warn "filtres ".@filters[0];
300 # warn "filtres ".@filters[1];
301 # warn "filtres ".@filters[2];
302 # warn "filtres ".@filters[3];
304 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
305 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
306 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
307 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
308 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
309 $linefilter[0] = @$filters[5] if ($line =~ /location/ ) ;
310 $linefilter[0] = @$filters[6] if ($line =~ /sort1/ ) ;
311 $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
314 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
315 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
316 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
317 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
318 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
319 $colfilter[0] = @$filters[5] if ($column =~ /location/ ) ;
320 $colfilter[0] = @$filters[6] if ($column =~ /sort1/ ) ;
321 $colfilter[0] = @$filters[7] if ($column =~ /sort2/ ) ;
324 if (($line =~/datetime/) and ($dsp == 1)) {
326 $linefield .="dayname($line)";
327 } elsif (($line=~/datetime/) and ($dsp == 2)) {
329 $linefield .="monthname($line) ";
330 } elsif (($line=~/datetime/) and ($dsp == 3)) {
332 $linefield .="Year($line)";
333 } elsif ($line=~/datetime/) {
334 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
338 my $lineorder = $linefield;
339 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
340 $lineorder = "month($line)" if $linefield =~ "^month";
341 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
344 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
346 if ($line=~/datetime/) {
347 if ($linefilter[1] and ($linefilter[0])){
348 $strsth .= " and $line between ? and ? " ;
349 } elsif ($linefilter[1]) {
350 $strsth .= " and $line < ? " ;
351 } elsif ($linefilter[0]) {
352 $strsth .= " and $line > ? " ;
354 $strsth .= " and type ='".$type."' " if $type;
355 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
356 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
357 } elsif ($linefilter[0]) {
358 $linefilter[0] =~ s/\*/%/g;
359 $strsth .= " and $line LIKE ? " ;
361 $strsth .=" group by $linefield";
362 $strsth .=" order by $lineorder";
363 $debug and warn $strsth;
365 my $sth = $dbh->prepare( $strsth );
366 if (( @linefilter ) and ($linefilter[1])){
367 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
368 } elsif ($linefilter[0]) {
369 $sth->execute($linefilter[0]);
374 while ( my ($celvalue) = $sth->fetchrow) {
377 $cell{rowtitle} = $celvalue;
379 $cell{rowtitle} = "";
382 push @loopline, \%cell;
388 if (($column =~/datetime/) and ($dsp == 1)) {
390 $colfield .="dayname($column)";
391 } elsif (($column=~/datetime/) and ($dsp == 2)) {
393 $colfield .="monthname($column)";
394 } elsif (($column=~/datetime/) and ($dsp == 3)) {
396 $colfield .="Year($column)";
397 } elsif ($column=~/datetime/) {
398 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
400 $colfield .= $column;
402 $colorder = "weekday($line)" if $colfield =~ "^dayname";
403 $colorder = "month($line)" if $colfield =~ "^month";
404 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
407 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
409 if ($column=~/datetime/){
410 if (($colfilter[1]) and ($colfilter[0])){
411 $strsth2 .= " and $column between ? and ? " ;
412 } elsif ($colfilter[1]) {
413 $strsth2 .= " and $column < ? " ;
414 } elsif ($colfilter[0]) {
415 $strsth2 .= " and $column > ? " ;
417 $strsth2 .= " and type ='".$type."' " if $type;
418 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
419 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
420 } elsif ($colfilter[0]) {
421 $colfilter[0] =~ s/\*/%/g;
422 $strsth2 .= " and $column LIKE ? " ;
424 $strsth2 .=" group by $colfield";
425 $strsth2 .=" order by $colorder";
428 my $sth2 = $dbh->prepare( $strsth2 );
429 if (( @colfilter ) and ($colfilter[1])){
430 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
431 } elsif ($colfilter[0]) {
432 $sth2->execute($colfilter[0]);
438 while (my ($celvalue) = $sth2->fetchrow) {
441 # $debug and warn "coltitle :".$celvalue;
442 $cell{coltitle} = $celvalue;
444 push @loopcol, \%cell;
446 # warn "fin des titres colonnes";
452 #Initialization of cell values.....
455 foreach my $row ( @loopline ) {
456 foreach my $col ( @loopcol ) {
457 $debug and warn " init table : $row->{rowtitle} / $col->{coltitle} ";
458 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
460 $table{$row->{rowtitle}}->{totalrow}=0;
463 # preparing calculation
466 $strcalc .= "SELECT $linefield, $colfield, ";
467 $strcalc .= "COUNT( * ) " if ($process ==1);
469 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
472 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
475 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
476 $rqbookcount->execute;
477 my ($bookcount) = $rqbookcount->fetchrow;
478 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
480 $strcalc .= "FROM statistics ";
481 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
482 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @$filters[5] or @$filters[6];
484 $strcalc .= "WHERE 1=1 ";
485 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
486 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
487 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
488 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
489 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
490 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
491 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
492 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
493 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
494 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
495 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
496 $strcalc .= " AND items.location like '" . @$filters[5] ."'" if ( @$filters[5] );
497 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
498 $strcalc .= " AND items.itemcallnumber >='" . @$filters[6] ."'" if ( @$filters[6] );
499 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
500 $strcalc .= " AND items.itemcallnumber <'" . @$filters[7] ."'" if ( @$filters[7] );
501 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
502 $strcalc .= " AND borrowers.sort1 like '" . @$filters[8] ."'" if ( @$filters[8] );
503 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
504 $strcalc .= " AND borrowers.sort2 like '" . @$filters[9] ."'" if ( @$filters[9] );
505 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
506 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
507 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
509 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
510 ($debug) and warn "". $strcalc;
511 my $dbcalc = $dbh->prepare($strcalc);
513 # warn "filling table";
515 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
516 ($debug) and warn "filling table $row / $col / $value ";
517 $emptycol = 1 if ($col eq undef);
518 $col = "zzEMPTY" if ($col eq undef);
519 $row = "zzEMPTY" if ($row eq undef);
521 $table{$row}->{$col}+=$value;
522 $table{$row}->{totalrow}+=$value;
523 $grantotal += $value;
525 push @loopcol,{coltitle => "NULL"} if ($emptycol);
527 foreach my $row (@loopline) {
529 #@loopcol ensures the order for columns is common with column titles
530 # and the number matches the number of columns
531 foreach my $col ( @loopcol ) {
532 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
533 push @loopcell, {value => $value } ;
535 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
536 'loopcell' => \@loopcell,
537 'hilighted' => ($hilighted >0),
538 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
540 $hilighted = -$hilighted;
543 # warn "footer processing";
544 foreach my $col ( @loopcol ) {
546 foreach my $row ( @looprow ) {
547 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
548 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
550 # warn "summ for column ".$col->{coltitle}." = ".$total;
551 push @loopfooter, {'totalcol' => $total};
555 # the header of the table
556 $globalline{loopfilter}=\@loopfilter;
557 # the core of the table
558 $globalline{looprow} = \@looprow;
559 $globalline{loopcol} = \@loopcol;
560 # # the foot (totals by borrower type)
561 $globalline{loopfooter} = \@loopfooter;
562 $globalline{total}= $grantotal;
563 $globalline{line} = $line;
564 $globalline{column} = $column;
565 push @mainloop,\%globalline;