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);
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',
83 -attachment=>"$basename.csv",
84 -filename=>"$basename.csv" );
85 my $cols = @$results[0]->{loopcol};
86 my $lines = @$results[0]->{looprow};
88 $sep =C4::Context->preference("delimiter");
90 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
92 foreach my $col ( @$cols ) {
93 print $col->{coltitle}.$sep;
97 foreach my $line ( @$lines ) {
98 my $x = $line->{loopcell};
99 print $line->{rowtitle}.$sep;
100 foreach my $cell (@$x) {
101 print $cell->{value}.$sep;
103 print $line->{totalrow};
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col ( @$cols ) {
110 print $sep.$col->{totalcol};
112 print $sep.@$results[0]->{total};
117 my $dbh = C4::Context->dbh;
122 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
127 while (my ($value, $desc) =$req->fetchrow) {
128 push @select, $value;
129 $select{$value}=$desc;
131 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
138 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
144 while (my ($value,$desc) =$req->fetchrow) {
145 push @select, $value;
146 $select{$value}=$desc;
148 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
155 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
160 while (my ($value) =$req->fetchrow) {
161 $hassort1 =1 if ($value);
162 push @select, $value;
164 my $branches=getbranches();
167 push @select_branch,"";
168 $select_branches{""} = "";
169 foreach my $branch (keys %$branches) {
170 push @select_branch, $branch;
171 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
173 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
175 -values => \@select_branch,
176 -labels => \%select_branches,
180 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
186 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
192 while (my ($value) =$req->fetchrow) {
193 $hassort2 =1 if ($value);
194 $hglghtsort2= !($hassort1);
195 push @select, $value;
197 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
203 my @mime = ( C4::Context->preference("MIME") );
204 # foreach my $mime (@mime){
208 my $CGIextChoice=CGI::scrolling_list(
215 my @dels = ( C4::Context->preference("delimiter") );
216 my $CGIsepChoice=CGI::scrolling_list(
224 CGIBorCat => $CGIBorCat,
225 CGIItemType => $CGIItemTypes,
226 CGIBranch => $CGIBranch,
227 hassort1=> $hassort1,
228 hassort2=> $hassort2,
229 HlghtSort2 => $hglghtsort2,
230 CGISort1 => $CGISort1,
231 CGISort2 => $CGISort2,
232 CGIextChoice => $CGIextChoice,
233 CGIsepChoice => $CGIsepChoice
235 output_html_with_http_headers $input, $cookie, $template->output;
242 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
251 my $dbh = C4::Context->dbh;
257 for (my $i=0;$i<=6;$i++) {
259 if ( @$filters[$i] ) {
260 if (($i==1) and (@$filters[$i-1])) {
261 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
263 $cell{filter} .= @$filters[$i];
264 $cell{crit} .="Period From" if ($i==0);
265 $cell{crit} .="Period To" if ($i==1);
266 $cell{crit} .="Borrower Cat" if ($i==2);
267 $cell{crit} .="Doc Type" if ($i==3);
268 $cell{crit} .="Branch" if ($i==4);
269 $cell{crit} .="Sort1" if ($i==5);
270 $cell{crit} .="Sort2" if ($i==6);
271 push @loopfilter, \%cell;
274 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
275 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
276 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
277 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
281 # warn "filtres ".@filters[0];
282 # warn "filtres ".@filters[1];
283 # warn "filtres ".@filters[2];
284 # warn "filtres ".@filters[3];
286 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
287 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
288 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
289 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
290 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
291 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
292 $linefilter[0] = @$filters[5] if ($line =~ /sort1/ ) ;
293 $linefilter[0] = @$filters[6] if ($line =~ /sort2/ ) ;
294 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
297 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
298 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
299 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
300 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
301 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
302 $colfilter[0] = @$filters[5] if ($column =~ /sort1/ ) ;
303 $colfilter[0] = @$filters[6] if ($column =~ /sort2/ ) ;
304 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
308 if (($line =~/datetime/) and ($dsp == 1)) {
310 $linefield .="dayname($line)";
311 } elsif (($line=~/datetime/) and ($dsp == 2)) {
313 $linefield .="monthname($line)";
314 } elsif (($line=~/datetime/) and ($dsp == 3)) {
316 $linefield .="Year($line)";
317 } elsif ($line=~/datetime/) {
318 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
322 my $lineorder = $linefield;
323 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
324 $lineorder = "month($line)" if $linefield =~ "^month";
325 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
328 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
330 if ($line=~/datetime/) {
331 if ($linefilter[1] and ($linefilter[0])){
332 $strsth .= " and $line between ? and ? " ;
333 } elsif ($linefilter[1]) {
334 $strsth .= " and $line < ? " ;
335 } elsif ($linefilter[0]) {
336 $strsth .= " and $line > ? " ;
338 $strsth .= " and type ='".$type."' " if $type;
339 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
340 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
341 } elsif ($linefilter[0]) {
342 $linefilter[0] =~ s/\*/%/g;
343 $strsth .= " and $line LIKE ? " ;
345 $strsth .=" group by $linefield";
346 $strsth .=" order by $lineorder";
349 my $sth = $dbh->prepare( $strsth );
350 if (( @linefilter ) and ($linefilter[1])){
351 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
352 } elsif ($linefilter[0]) {
353 $sth->execute($linefilter[0]);
358 while ( my ($celvalue) = $sth->fetchrow) {
361 $cell{rowtitle} = $celvalue;
363 $cell{rowtitle} = "";
366 push @loopline, \%cell;
372 if (($column =~/datetime/) and ($dsp == 1)) {
374 $colfield .="dayname($column)";
375 } elsif (($column=~/datetime/) and ($dsp == 2)) {
377 $colfield .="monthname($column)";
378 } elsif (($column=~/datetime/) and ($dsp == 3)) {
380 $colfield .="Year($column)";
381 } elsif ($column=~/datetime/) {
382 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
384 $colfield .= $column;
386 $colorder = "weekday($line)" if $colfield =~ "^dayname";
387 $colorder = "month($line)" if $colfield =~ "^month";
388 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
391 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
393 if ($column=~/datetime/){
394 if (($colfilter[1]) and ($colfilter[0])){
395 $strsth2 .= " and $column between ? and ? " ;
396 } elsif ($colfilter[1]) {
397 $strsth2 .= " and $column < ? " ;
398 } elsif ($colfilter[0]) {
399 $strsth2 .= " and $column > ? " ;
401 $strsth2 .= " and type ='".$type."' " if $type;
402 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
403 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
404 } elsif ($colfilter[0]) {
405 $colfilter[0] =~ s/\*/%/g;
406 $strsth2 .= " and $column LIKE ? " ;
408 $strsth2 .=" group by $colfield";
409 $strsth2 .=" order by $colorder";
412 my $sth2 = $dbh->prepare( $strsth2 );
413 if (( @colfilter ) and ($colfilter[1])){
414 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
415 } elsif ($colfilter[0]) {
416 $sth2->execute($colfilter[0]);
422 while (my ($celvalue) = $sth2->fetchrow) {
425 # warn "coltitle :".$celvalue;
426 $cell{coltitle} = $celvalue;
428 push @loopcol, \%cell;
430 # warn "fin des titres colonnes";
436 #Initialization of cell values.....
439 foreach my $row ( @loopline ) {
440 foreach my $col ( @loopcol ) {
441 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
442 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
444 $table{$row->{rowtitle}}->{totalrow}=0;
447 # preparing calculation
450 $strcalc .= "SELECT $linefield, $colfield, ";
451 $strcalc .= "COUNT( * ) " if ($process ==1);
453 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
456 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
459 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
460 $rqbookcount->execute;
461 my ($bookcount) = $rqbookcount->fetchrow;
462 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
464 $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
466 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
467 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
468 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
469 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
470 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
471 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
472 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
473 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
474 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
475 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
476 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
477 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
478 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
479 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
480 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
481 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
482 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
484 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
486 my $dbcalc = $dbh->prepare($strcalc);
488 # warn "filling table";
490 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
491 warn "filling table $row / $col / $value ";
492 $emptycol = 1 if ($col eq undef);
493 $col = "zzEMPTY" if ($col eq undef);
494 $row = "zzEMPTY" if ($row eq undef);
496 $table{$row}->{$col}+=$value;
497 $table{$row}->{totalrow}+=$value;
498 $grantotal += $value;
500 push @loopcol,{coltitle => "NULL"} if ($emptycol);
502 foreach my $row (@loopline) {
504 #@loopcol ensures the order for columns is common with column titles
505 # and the number matches the number of columns
506 foreach my $col ( @loopcol ) {
507 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
508 push @loopcell, {value => $value } ;
510 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
511 'loopcell' => \@loopcell,
512 'hilighted' => ($hilighted >0),
513 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
515 $hilighted = -$hilighted;
518 # warn "footer processing";
519 foreach my $col ( @loopcol ) {
521 foreach my $row ( @looprow ) {
522 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
523 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
525 # warn "summ for column ".$col->{coltitle}." = ".$total;
526 push @loopfooter, {'totalcol' => $total};
530 # the header of the table
531 $globalline{loopfilter}=\@loopfilter;
532 # the core of the table
533 $globalline{looprow} = \@looprow;
534 $globalline{loopcol} = \@loopcol;
535 # # the foot (totals by borrower type)
536 $globalline{loopfooter} = \@loopfooter;
537 $globalline{total}= $grantotal;
538 $globalline{line} = $line;
539 $globalline{column} = $column;
540 push @mainloop,\%globalline;