5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
36 plugin that shows a stats on borrowers
48 my $do_it=$input->param('do_it');
49 my $fullreportname = "reports/issues_stats.tmpl";
50 my $line = $input->param("Line");
51 my $column = $input->param("Column");
52 my @filters = $input->param("Filter");
53 my $podsp = $input->param("DisplayBy");
54 my $type = $input->param("PeriodTypeSel");
55 my $daysel = $input->param("PeriodDaySel");
56 my $monthsel = $input->param("PeriodMonthSel");
57 my $calc = $input->param("Cellvalue");
58 my $output = $input->param("output");
59 my $basename = $input->param("basename");
60 my $mime = $input->param("MIME");
61 my $del = $input->param("sep");
62 #warn "calcul : ".$calc;
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => $fullreportname,
68 flagsrequired => {editcatalogue => 1},
71 $template->param(do_it => $do_it,
72 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
73 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
74 IntranetNav => C4::Context->preference("IntranetNav"),
78 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
79 if ($output eq "screen"){
80 # Printing results to screen
81 $template->param(mainloop => $results);
82 output_html_with_http_headers $input, $cookie, $template->output;
85 # Printing to a csv file
86 print $input->header(-type => 'application/vnd.sun.xml.calc',
87 -attachment=>"$basename.csv",
88 -filename=>"$basename.csv" );
89 my $cols = @$results[0]->{loopcol};
90 my $lines = @$results[0]->{looprow};
92 $sep =C4::Context->preference("delimiter");
94 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
96 foreach my $col ( @$cols ) {
97 print $col->{coltitle}.$sep;
101 foreach my $line ( @$lines ) {
102 my $x = $line->{loopcell};
103 print $line->{rowtitle}.$sep;
104 foreach my $cell (@$x) {
105 print $cell->{value}.$sep;
107 print $line->{totalrow};
112 $cols = @$results[0]->{loopfooter};
113 foreach my $col ( @$cols ) {
114 print $sep.$col->{totalcol};
116 print $sep.@$results[0]->{total};
121 my $dbh = C4::Context->dbh;
126 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
131 while (my ($value, $desc) =$req->fetchrow) {
132 push @select, $value;
133 $select{$value}=$desc;
135 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
142 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
148 while (my ($value,$desc) =$req->fetchrow) {
149 push @select, $value;
150 $select{$value}=$desc;
152 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
159 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
164 while (my ($value) =$req->fetchrow) {
165 $hassort1 =1 if ($value);
166 push @select, $value;
168 my $branches=GetBranches();
171 push @select_branch,"";
172 $select_branches{""} = "";
173 foreach my $branch (keys %$branches) {
174 push @select_branch, $branch;
175 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
177 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
179 -values => \@select_branch,
180 -labels => \%select_branches,
184 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
190 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
196 while (my ($value) =$req->fetchrow) {
197 $hassort2 =1 if ($value);
198 $hglghtsort2= !($hassort1);
199 push @select, $value;
201 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
207 my @mime = ( C4::Context->preference("MIME") );
208 # foreach my $mime (@mime){
212 my $CGIextChoice=CGI::scrolling_list(
219 my @dels = ( C4::Context->preference("delimiter") );
220 my $CGIsepChoice=CGI::scrolling_list(
228 CGIBorCat => $CGIBorCat,
229 CGIItemType => $CGIItemTypes,
230 CGIBranch => $CGIBranch,
231 hassort1=> $hassort1,
232 hassort2=> $hassort2,
233 HlghtSort2 => $hglghtsort2,
234 CGISort1 => $CGISort1,
235 CGISort2 => $CGISort2,
236 CGIextChoice => $CGIextChoice,
237 CGIsepChoice => $CGIsepChoice
239 output_html_with_http_headers $input, $cookie, $template->output;
246 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
255 my $dbh = C4::Context->dbh;
261 for (my $i=0;$i<=6;$i++) {
263 if ( @$filters[$i] ) {
264 if (($i==1) and (@$filters[$i-1])) {
265 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
267 $cell{filter} .= @$filters[$i];
268 $cell{crit} .="Period From" if ($i==0);
269 $cell{crit} .="Period To" if ($i==1);
270 $cell{crit} .="Borrower Cat" if ($i==2);
271 $cell{crit} .="Doc Type" if ($i==3);
272 $cell{crit} .="Branch" if ($i==4);
273 $cell{crit} .="Sort1" if ($i==5);
274 $cell{crit} .="Sort2" if ($i==6);
275 push @loopfilter, \%cell;
278 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
279 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
280 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
281 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
285 # warn "filtres ".@filters[0];
286 # warn "filtres ".@filters[1];
287 # warn "filtres ".@filters[2];
288 # warn "filtres ".@filters[3];
290 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
291 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
292 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
293 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
294 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
295 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
296 $linefilter[0] = @$filters[5] if ($line =~ /sort1/ ) ;
297 $linefilter[0] = @$filters[6] if ($line =~ /sort2/ ) ;
298 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
301 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
302 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
303 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
304 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
305 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
306 $colfilter[0] = @$filters[5] if ($column =~ /sort1/ ) ;
307 $colfilter[0] = @$filters[6] if ($column =~ /sort2/ ) ;
308 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
312 if (($line =~/datetime/) and ($dsp == 1)) {
314 $linefield .="dayname($line)";
315 } elsif (($line=~/datetime/) and ($dsp == 2)) {
317 $linefield .="monthname($line)";
318 } elsif (($line=~/datetime/) and ($dsp == 3)) {
320 $linefield .="Year($line)";
321 } elsif ($line=~/datetime/) {
322 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
326 my $lineorder = $linefield;
327 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
328 $lineorder = "month($line)" if $linefield =~ "^month";
329 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
332 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
334 if ($line=~/datetime/) {
335 if ($linefilter[1] and ($linefilter[0])){
336 $strsth .= " and $line between ? and ? " ;
337 } elsif ($linefilter[1]) {
338 $strsth .= " and $line < ? " ;
339 } elsif ($linefilter[0]) {
340 $strsth .= " and $line > ? " ;
342 $strsth .= " and type ='".$type."' " if $type;
343 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
344 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
345 } elsif ($linefilter[0]) {
346 $linefilter[0] =~ s/\*/%/g;
347 $strsth .= " and $line LIKE ? " ;
349 $strsth .=" group by $linefield";
350 $strsth .=" order by $lineorder";
353 my $sth = $dbh->prepare( $strsth );
354 if (( @linefilter ) and ($linefilter[1])){
355 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
356 } elsif ($linefilter[0]) {
357 $sth->execute($linefilter[0]);
362 while ( my ($celvalue) = $sth->fetchrow) {
365 $cell{rowtitle} = $celvalue;
367 $cell{rowtitle} = "";
370 push @loopline, \%cell;
376 if (($column =~/datetime/) and ($dsp == 1)) {
378 $colfield .="dayname($column)";
379 } elsif (($column=~/datetime/) and ($dsp == 2)) {
381 $colfield .="monthname($column)";
382 } elsif (($column=~/datetime/) and ($dsp == 3)) {
384 $colfield .="Year($column)";
385 } elsif ($column=~/datetime/) {
386 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
388 $colfield .= $column;
390 $colorder = "weekday($line)" if $colfield =~ "^dayname";
391 $colorder = "month($line)" if $colfield =~ "^month";
392 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
395 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
397 if ($column=~/datetime/){
398 if (($colfilter[1]) and ($colfilter[0])){
399 $strsth2 .= " and $column between ? and ? " ;
400 } elsif ($colfilter[1]) {
401 $strsth2 .= " and $column < ? " ;
402 } elsif ($colfilter[0]) {
403 $strsth2 .= " and $column > ? " ;
405 $strsth2 .= " and type ='".$type."' " if $type;
406 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
407 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
408 } elsif ($colfilter[0]) {
409 $colfilter[0] =~ s/\*/%/g;
410 $strsth2 .= " and $column LIKE ? " ;
412 $strsth2 .=" group by $colfield";
413 $strsth2 .=" order by $colorder";
416 my $sth2 = $dbh->prepare( $strsth2 );
417 if (( @colfilter ) and ($colfilter[1])){
418 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
419 } elsif ($colfilter[0]) {
420 $sth2->execute($colfilter[0]);
426 while (my ($celvalue) = $sth2->fetchrow) {
429 # warn "coltitle :".$celvalue;
430 $cell{coltitle} = $celvalue;
432 push @loopcol, \%cell;
434 # warn "fin des titres colonnes";
440 #Initialization of cell values.....
443 foreach my $row ( @loopline ) {
444 foreach my $col ( @loopcol ) {
445 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
446 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
448 $table{$row->{rowtitle}}->{totalrow}=0;
451 # preparing calculation
454 $strcalc .= "SELECT $linefield, $colfield, ";
455 $strcalc .= "COUNT( * ) " if ($process ==1);
457 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
460 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
463 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
464 $rqbookcount->execute;
465 my ($bookcount) = $rqbookcount->fetchrow;
466 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
468 $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
470 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
471 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
472 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
473 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
474 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
475 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
476 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
477 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
478 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
479 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
480 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
481 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
482 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
483 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
484 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
485 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
486 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
488 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
490 my $dbcalc = $dbh->prepare($strcalc);
492 # warn "filling table";
494 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
495 warn "filling table $row / $col / $value ";
496 $emptycol = 1 if ($col eq undef);
497 $col = "zzEMPTY" if ($col eq undef);
498 $row = "zzEMPTY" if ($row eq undef);
500 $table{$row}->{$col}+=$value;
501 $table{$row}->{totalrow}+=$value;
502 $grantotal += $value;
504 push @loopcol,{coltitle => "NULL"} if ($emptycol);
506 foreach my $row (@loopline) {
508 #@loopcol ensures the order for columns is common with column titles
509 # and the number matches the number of columns
510 foreach my $col ( @loopcol ) {
511 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
512 push @loopcell, {value => $value } ;
514 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
515 'loopcell' => \@loopcell,
516 'hilighted' => ($hilighted >0),
517 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
519 $hilighted = -$hilighted;
522 # warn "footer processing";
523 foreach my $col ( @loopcol ) {
525 foreach my $row ( @looprow ) {
526 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
527 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
529 # warn "summ for column ".$col->{coltitle}." = ".$total;
530 push @loopfooter, {'totalcol' => $total};
534 # the header of the table
535 $globalline{loopfilter}=\@loopfilter;
536 # the core of the table
537 $globalline{looprow} = \@looprow;
538 $globalline{loopcol} = \@loopcol;
539 # # the foot (totals by borrower type)
540 $globalline{loopfooter} = \@loopfooter;
541 $globalline{total}= $grantotal;
542 $globalline{line} = $line;
543 $globalline{column} = $column;
544 push @mainloop,\%globalline;