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
26 use C4::Branch; # GetBranches
29 use C4::Interface::CGI::Output;
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 my $podsp = $input->param("DisplayBy");
51 my $type = $input->param("PeriodTypeSel");
52 my $daysel = $input->param("PeriodDaySel");
53 my $monthsel = $input->param("PeriodMonthSel");
54 my $calc = $input->param("Cellvalue");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 my $del = $input->param("sep");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user({template_name => $fullreportname,
65 flagsrequired => {reports => 1},
68 $template->param(do_it => $do_it,
69 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
70 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
71 IntranetNav => C4::Context->preference("IntranetNav"),
75 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
76 if ($output eq "screen"){
77 # Printing results to screen
78 $template->param(mainloop => $results);
79 output_html_with_http_headers $input, $cookie, $template->output;
82 # Printing to a csv file
83 print $input->header(-type => 'application/vnd.sun.xml.calc',
85 -attachment=>"$basename.csv",
86 -filename=>"$basename.csv" );
87 my $cols = @$results[0]->{loopcol};
88 my $lines = @$results[0]->{looprow};
90 $sep =C4::Context->preference("delimiter");
92 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
94 foreach my $col ( @$cols ) {
95 print $col->{coltitle}.$sep;
99 foreach my $line ( @$lines ) {
100 my $x = $line->{loopcell};
101 print $line->{rowtitle}.$sep;
102 foreach my $cell (@$x) {
103 print $cell->{value}.$sep;
105 print $line->{totalrow};
110 $cols = @$results[0]->{loopfooter};
111 foreach my $col ( @$cols ) {
112 print $sep.$col->{totalcol};
114 print $sep.@$results[0]->{total};
119 my $dbh = C4::Context->dbh;
124 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
129 while (my ($value, $desc) =$req->fetchrow) {
130 push @select, $value;
131 $select{$value}=$desc;
133 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
140 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
146 while (my ($value,$desc) =$req->fetchrow) {
147 push @select, $value;
148 $select{$value}=$desc;
150 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
157 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
162 while (my ($value) =$req->fetchrow) {
163 $hassort1 =1 if ($value);
164 push @select, $value;
166 my $branches=GetBranches();
169 push @select_branch,"";
170 $select_branches{""} = "";
171 foreach my $branch (keys %$branches) {
172 push @select_branch, $branch;
173 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
175 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
177 -values => \@select_branch,
178 -labels => \%select_branches,
182 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
188 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
194 while (my ($value) =$req->fetchrow) {
195 $hassort2 =1 if ($value);
196 $hglghtsort2= !($hassort1);
197 push @select, $value;
199 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
205 $req = $dbh->prepare("select distinctrow location from items order by location");
209 while (my ($value) =$req->fetchrow) {
210 push @select, $value;
212 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
218 my @mime = ( C4::Context->preference("MIME") );
220 my $CGIextChoice=CGI::scrolling_list(
227 my @dels = ( C4::Context->preference("delimiter") );
228 my $CGIsepChoice=CGI::scrolling_list(
236 CGIBorCat => $CGIBorCat,
237 CGIItemType => $CGIItemTypes,
238 CGIBranch => $CGIBranch,
239 hassort1=> $hassort1,
240 hassort2=> $hassort2,
241 HlghtSort2 => $hglghtsort2,
242 CGISort1 => $CGISort1,
243 CGISort2 => $CGISort2,
244 CGIextChoice => $CGIextChoice,
245 CGIsepChoice => $CGIsepChoice,
246 CGILocation => $CGIlocation,
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<=9;$i++) {
272 if ( @$filters[$i] ) {
273 if (($i==1) and (@$filters[$i-1])) {
274 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
276 $cell{filter} .= @$filters[$i];
277 $cell{crit} .="Period From" if ($i==0);
278 $cell{crit} .="Period To" if ($i==1);
279 $cell{crit} .="Borrower Cat=" if ($i==2);
280 $cell{crit} .="Doc Type=" if ($i==3);
281 $cell{crit} .="Branch=" if ($i==4);
282 $cell{crit} .="Location=" if ($i==5);
283 $cell{crit} .="Item callnumber>=" if ($i==6);
284 $cell{crit} .="Item callnumber<" if ($i==7);
285 $cell{crit} .="sort1=" if ($i==8);
286 $cell{crit} .="sort2=" if ($i==9);
288 push @loopfilter, \%cell;
291 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
292 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
293 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
294 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
298 # warn "filtres ".@filters[0];
299 # warn "filtres ".@filters[1];
300 # warn "filtres ".@filters[2];
301 # warn "filtres ".@filters[3];
303 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
304 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
305 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
306 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
307 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
308 $linefilter[0] = @$filters[5] if ($line =~ /location/ ) ;
309 $linefilter[0] = @$filters[6] if ($line =~ /sort1/ ) ;
310 $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
313 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
314 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
315 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
316 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
317 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
318 $colfilter[0] = @$filters[5] if ($column =~ /location/ ) ;
319 $colfilter[0] = @$filters[6] if ($column =~ /sort1/ ) ;
320 $colfilter[0] = @$filters[7] if ($column =~ /sort2/ ) ;
323 if (($line =~/datetime/) and ($dsp == 1)) {
325 $linefield .="dayname($line)";
326 } elsif (($line=~/datetime/) and ($dsp == 2)) {
328 $linefield .="monthname($line)";
329 } elsif (($line=~/datetime/) and ($dsp == 3)) {
331 $linefield .="Year($line)";
332 } elsif ($line=~/datetime/) {
333 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
337 my $lineorder = $linefield;
338 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
339 $lineorder = "month($line)" if $linefield =~ "^month";
340 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
343 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
345 if ($line=~/datetime/) {
346 if ($linefilter[1] and ($linefilter[0])){
347 $strsth .= " and $line between ? and ? " ;
348 } elsif ($linefilter[1]) {
349 $strsth .= " and $line < ? " ;
350 } elsif ($linefilter[0]) {
351 $strsth .= " and $line > ? " ;
353 $strsth .= " and type ='".$type."' " if $type;
354 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
355 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
356 } elsif ($linefilter[0]) {
357 $linefilter[0] =~ s/\*/%/g;
358 $strsth .= " and $line LIKE ? " ;
360 $strsth .=" group by $linefield";
361 $strsth .=" order by $lineorder";
364 my $sth = $dbh->prepare( $strsth );
365 if (( @linefilter ) and ($linefilter[1])){
366 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
367 } elsif ($linefilter[0]) {
368 $sth->execute($linefilter[0]);
373 while ( my ($celvalue) = $sth->fetchrow) {
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, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
408 if ($column=~/datetime/){
409 if (($colfilter[1]) and ($colfilter[0])){
410 $strsth2 .= " and $column between ? and ? " ;
411 } elsif ($colfilter[1]) {
412 $strsth2 .= " and $column < ? " ;
413 } elsif ($colfilter[0]) {
414 $strsth2 .= " and $column > ? " ;
416 $strsth2 .= " and type ='".$type."' " if $type;
417 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
418 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
419 } elsif ($colfilter[0]) {
420 $colfilter[0] =~ s/\*/%/g;
421 $strsth2 .= " and $column LIKE ? " ;
423 $strsth2 .=" group by $colfield";
424 $strsth2 .=" order by $colorder";
427 my $sth2 = $dbh->prepare( $strsth2 );
428 if (( @colfilter ) and ($colfilter[1])){
429 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
430 } elsif ($colfilter[0]) {
431 $sth2->execute($colfilter[0]);
437 while (my ($celvalue) = $sth2->fetchrow) {
440 # warn "coltitle :".$celvalue;
441 $cell{coltitle} = $celvalue;
443 push @loopcol, \%cell;
445 # warn "fin des titres colonnes";
451 #Initialization of cell values.....
454 foreach my $row ( @loopline ) {
455 foreach my $col ( @loopcol ) {
456 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
457 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
459 $table{$row->{rowtitle}}->{totalrow}=0;
462 # preparing calculation
465 $strcalc .= "SELECT $linefield, $colfield, ";
466 $strcalc .= "COUNT( * ) " if ($process ==1);
468 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
471 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
474 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
475 $rqbookcount->execute;
476 my ($bookcount) = $rqbookcount->fetchrow;
477 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
479 $strcalc .= "FROM statistics ";
480 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
481 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @$filters[5] or @$filters[6];
483 $strcalc .= "WHERE 1=1 ";
484 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
485 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
486 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
487 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
488 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
489 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
490 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
491 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
492 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
493 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
494 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
495 $strcalc .= " AND items.location like '" . @$filters[5] ."'" if ( @$filters[5] );
496 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
497 $strcalc .= " AND items.itemcallnumber >='" . @$filters[6] ."'" if ( @$filters[6] );
498 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
499 $strcalc .= " AND items.itemcallnumber <'" . @$filters[7] ."'" if ( @$filters[7] );
500 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
501 $strcalc .= " AND borrowers.sort1 like '" . @$filters[8] ."'" if ( @$filters[8] );
502 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
503 $strcalc .= " AND borrowers.sort2 like '" . @$filters[9] ."'" if ( @$filters[9] );
504 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
505 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
506 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
508 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
510 my $dbcalc = $dbh->prepare($strcalc);
512 # warn "filling table";
514 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
515 warn "filling table $row / $col / $value ";
516 $emptycol = 1 if ($col eq undef);
517 $col = "zzEMPTY" if ($col eq undef);
518 $row = "zzEMPTY" if ($row eq undef);
520 $table{$row}->{$col}+=$value;
521 $table{$row}->{totalrow}+=$value;
522 $grantotal += $value;
524 push @loopcol,{coltitle => "NULL"} if ($emptycol);
526 foreach my $row (@loopline) {
528 #@loopcol ensures the order for columns is common with column titles
529 # and the number matches the number of columns
530 foreach my $col ( @loopcol ) {
531 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
532 push @loopcell, {value => $value } ;
534 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
535 'loopcell' => \@loopcell,
536 'hilighted' => ($hilighted >0),
537 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
539 $hilighted = -$hilighted;
542 # warn "footer processing";
543 foreach my $col ( @loopcol ) {
545 foreach my $row ( @looprow ) {
546 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
547 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
549 # warn "summ for column ".$col->{coltitle}." = ".$total;
550 push @loopfooter, {'totalcol' => $total};
554 # the header of the table
555 $globalline{loopfilter}=\@loopfilter;
556 # the core of the table
557 $globalline{looprow} = \@looprow;
558 $globalline{loopcol} = \@loopcol;
559 # # the foot (totals by borrower type)
560 $globalline{loopfooter} = \@loopfooter;
561 $globalline{total}= $grantotal;
562 $globalline{line} = $line;
563 $globalline{column} = $column;
564 push @mainloop,\%globalline;