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
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/issues_stats.tmpl";
48 my $line = $input->param("Line");
49 my $column = $input->param("Column");
50 my @filters = $input->param("Filter");
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 => {editcatalogue => 1},
69 $template->param(do_it => $do_it);
72 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
73 if ($output eq "screen"){
74 # Printing results to screen
75 $template->param(mainloop => $results);
76 output_html_with_http_headers $input, $cookie, $template->output;
79 # Printing to a csv file
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -attachment=>"$basename.csv",
82 -filename=>"$basename.csv" );
83 my $cols = @$results[0]->{loopcol};
84 my $lines = @$results[0]->{looprow};
86 $sep =C4::Context->preference("delimiter");
88 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
90 foreach my $col ( @$cols ) {
91 print $col->{coltitle}.$sep;
95 foreach my $line ( @$lines ) {
96 my $x = $line->{loopcell};
97 print $line->{rowtitle}.$sep;
98 foreach my $cell (@$x) {
99 print $cell->{value}.$sep;
101 print $line->{totalrow};
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col ( @$cols ) {
108 print $sep.$col->{totalcol};
110 print $sep.@$results[0]->{total};
115 my $dbh = C4::Context->dbh;
120 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
125 while (my ($value, $desc) =$req->fetchrow) {
126 push @select, $value;
127 $select{$value}=$desc;
129 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
136 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
142 while (my ($value,$desc) =$req->fetchrow) {
143 push @select, $value;
144 $select{$value}=$desc;
146 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
153 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
158 while (my ($value) =$req->fetchrow) {
159 $hassort1 =1 if ($value);
160 push @select, $value;
162 my $branches=getbranches();
165 push @select_branch,"";
166 $select_branches{""} = "";
167 foreach my $branch (keys %$branches) {
168 push @select_branch, $branch;
169 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
171 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
173 -values => \@select_branch,
174 -labels => \%select_branches,
178 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
184 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
190 while (my ($value) =$req->fetchrow) {
191 $hassort2 =1 if ($value);
192 $hglghtsort2= !($hassort1);
193 push @select, $value;
195 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
201 my @mime = ( C4::Context->preference("MIME") );
202 # foreach my $mime (@mime){
206 my $CGIextChoice=CGI::scrolling_list(
213 my @dels = ( C4::Context->preference("delimiter") );
214 my $CGIsepChoice=CGI::scrolling_list(
222 CGIBorCat => $CGIBorCat,
223 CGIItemType => $CGIItemTypes,
224 CGIBranch => $CGIBranch,
225 hassort1=> $hassort1,
226 hassort2=> $hassort2,
227 HlghtSort2 => $hglghtsort2,
228 CGISort1 => $CGISort1,
229 CGISort2 => $CGISort2,
230 CGIextChoice => $CGIextChoice,
231 CGIsepChoice => $CGIsepChoice
233 output_html_with_http_headers $input, $cookie, $template->output;
240 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
249 my $dbh = C4::Context->dbh;
255 for (my $i=0;$i<=6;$i++) {
257 if ( @$filters[$i] ) {
258 if (($i==1) and (@$filters[$i-1])) {
259 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
261 $cell{filter} .= @$filters[$i];
262 $cell{crit} .="Period From" if ($i==0);
263 $cell{crit} .="Period To" if ($i==1);
264 $cell{crit} .="Borrower Cat" if ($i==2);
265 $cell{crit} .="Doc Type" if ($i==3);
266 $cell{crit} .="Branch" if ($i==4);
267 $cell{crit} .="Sort1" if ($i==5);
268 $cell{crit} .="Sort2" if ($i==6);
269 push @loopfilter, \%cell;
272 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
273 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
274 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
275 push @loopfilter,{crit=>"Select Month ",filter=>$daysel} if ($monthsel);
279 # warn "filtres ".@filters[0];
280 # warn "filtres ".@filters[1];
281 # warn "filtres ".@filters[2];
282 # warn "filtres ".@filters[3];
284 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
285 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
286 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
287 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
288 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
289 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
290 $linefilter[0] = @$filters[5] if ($line =~ /sort1/ ) ;
291 $linefilter[0] = @$filters[6] if ($line =~ /sort2/ ) ;
292 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
295 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
296 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
297 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
298 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
299 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
300 $colfilter[0] = @$filters[5] if ($column =~ /sort1/ ) ;
301 $colfilter[0] = @$filters[6] if ($column =~ /sort2/ ) ;
302 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
306 if (($line =~/datetime/) and ($dsp == 1)) {
308 $linefield .="dayname($line)";
309 } elsif (($line=~/datetime/) and ($dsp == 2)) {
311 $linefield .="monthname($line)";
312 } elsif (($line=~/datetime/) and ($dsp == 3)) {
314 $linefield .="Year($line)";
315 } elsif ($line=~/datetime/) {
316 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
322 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
324 if ($line=~/datetime/) {
325 if ($linefilter[1] and ($linefilter[0])){
326 $strsth .= " and $line between ? and ? " ;
327 } elsif ($linefilter[1]) {
328 $strsth .= " and $line < ? " ;
329 } elsif ($linefilter[0]) {
330 $strsth .= " and $line > ? " ;
332 $strsth .= " and type ='".$type."' " if $type;
333 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
334 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
335 } elsif ($linefilter[0]) {
336 $linefilter[0] =~ s/\*/%/g;
337 $strsth .= " and $line LIKE ? " ;
339 $strsth .=" group by $linefield";
340 $strsth .=" order by $linefield";
343 my $sth = $dbh->prepare( $strsth );
344 if (( @linefilter ) and ($linefilter[1])){
345 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
346 } elsif ($linefilter[0]) {
347 $sth->execute($linefilter[0]);
352 while ( my ($celvalue) = $sth->fetchrow) {
355 $cell{rowtitle} = $celvalue;
357 $cell{rowtitle} = "";
360 push @loopline, \%cell;
365 if (($column =~/datetime/) and ($dsp == 1)) {
367 $colfield .="dayname($column)";
368 } elsif (($column=~/datetime/) and ($dsp == 2)) {
370 $colfield .="monthname($column)";
371 } elsif (($column=~/datetime/) and ($dsp == 3)) {
373 $colfield .="Year($column)";
374 } elsif ($column=~/datetime/) {
375 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
377 $colfield .= $column;
381 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
383 if ($column=~/datetime/){
384 if (($colfilter[1]) and ($colfilter[0])){
385 $strsth2 .= " and $column between ? and ? " ;
386 } elsif ($colfilter[1]) {
387 $strsth2 .= " and $column < ? " ;
388 } elsif ($colfilter[0]) {
389 $strsth2 .= " and $column > ? " ;
391 $strsth2 .= " and type ='".$type."' " if $type;
392 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
393 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
394 } elsif ($colfilter[0]) {
395 $colfilter[0] =~ s/\*/%/g;
396 $strsth2 .= " and $column LIKE ? " ;
398 $strsth2 .=" group by $colfield";
399 $strsth2 .=" order by $colfield";
402 my $sth2 = $dbh->prepare( $strsth2 );
403 if (( @colfilter ) and ($colfilter[1])){
404 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
405 } elsif ($colfilter[0]) {
406 $sth2->execute($colfilter[0]);
412 while (my ($celvalue) = $sth2->fetchrow) {
415 # warn "coltitle :".$celvalue;
416 $cell{coltitle} = $celvalue;
418 push @loopcol, \%cell;
420 # warn "fin des titres colonnes";
426 #Initialization of cell values.....
429 foreach my $row ( @loopline ) {
430 foreach my $col ( @loopcol ) {
431 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
432 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
434 $table{$row->{rowtitle}}->{totalrow}=0;
437 # preparing calculation
440 # Processing average loanperiods
441 $strcalc .= "SELECT $linefield, $colfield, ";
442 $strcalc .= " DATE_SUB(date_due, INTERVAL CAST(issues.renewals AS SIGNED INTEGER) * (CAST(issues.renewals AS SIGNED INTEGER)+1) DAY) AS issuedate, returndate, COUNT(*), date_due, issues.renewals, issuelength FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber ";
444 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
445 $strcalc .= " AND issues.returndate > '" . @$filters[0] ."'" if ( @$filters[0] );
446 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
447 $strcalc .= " AND issues.returndate < '" . @$filters[1] ."'" if ( @$filters[1] );
448 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
449 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
450 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
451 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
452 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
453 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
454 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
455 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
456 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
457 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
458 $strcalc .= " AND dayname(timestamp) like '" . $daysel ."'" if ( $daysel );
459 $strcalc .= " AND monthname(timestamp) like '" . $monthsel ."'" if ( $monthsel );
461 $strcalc .= " group by issuedate, returndate, $linefield, $colfield order by $linefield,$colfield";
463 my $dbcalc = $dbh->prepare($strcalc);
465 # warn "filling table";
468 while (my ($row, $col, $issuedate, $returndate) = $dbcalc->fetchrow) {
469 # warn "filling table $row / $col / $value ";
470 $emptycol = 1 if ($col eq undef);
471 $col = "zzEMPTY" if ($col eq undef);
472 $row = "zzEMPTY" if ($row eq undef);
474 $table{$row}->{$col}+=$value;
475 $table{$row}->{totalrow}+=$value;
476 $grantotal += $value;
479 push @loopcol,{coltitle => "NULL"} if ($emptycol);
481 foreach my $row ( sort keys %table ) {
483 #@loopcol ensures the order for columns is common with column titles
484 # and the number matches the number of columns
485 foreach my $col ( @loopcol ) {
486 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
487 push @loopcell, {value => $value } ;
489 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
490 'loopcell' => \@loopcell,
491 'hilighted' => ($hilighted >0),
492 'totalrow' => $table{$row}->{totalrow}
494 $hilighted = -$hilighted;
497 # warn "footer processing";
498 foreach my $col ( @loopcol ) {
500 foreach my $row ( @looprow ) {
501 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
502 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
504 # warn "summ for column ".$col->{coltitle}." = ".$total;
505 push @loopfooter, {'totalcol' => $total};
508 $strcalc .= "SELECT $linefield, $colfield, ";
509 $strcalc .= "COUNT( * ) " if ($process ==1);
511 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
512 $rqbookcount->execute;
513 my ($bookcount) = $rqbookcount->fetchrow;
514 $strcalc .= "100*(COUNT(itemnumber))/ $bookcount " ;
516 $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
518 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
519 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
520 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
521 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
522 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
523 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
524 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
525 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
526 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
527 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
528 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
529 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
530 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
531 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
532 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
533 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
534 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
536 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
538 my $dbcalc = $dbh->prepare($strcalc);
540 # warn "filling table";
542 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
543 # warn "filling table $row / $col / $value ";
544 $emptycol = 1 if ($col eq undef);
545 $col = "zzEMPTY" if ($col eq undef);
546 $row = "zzEMPTY" if ($row eq undef);
548 $table{$row}->{$col}+=$value;
549 $table{$row}->{totalrow}+=$value;
550 $grantotal += $value;
553 push @loopcol,{coltitle => "NULL"} if ($emptycol);
555 foreach my $row ( sort keys %table ) {
557 #@loopcol ensures the order for columns is common with column titles
558 # and the number matches the number of columns
559 foreach my $col ( @loopcol ) {
560 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
561 push @loopcell, {value => $value } ;
563 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
564 'loopcell' => \@loopcell,
565 'hilighted' => ($hilighted >0),
566 'totalrow' => $table{$row}->{totalrow}
568 $hilighted = -$hilighted;
571 # warn "footer processing";
572 foreach my $col ( @loopcol ) {
574 foreach my $row ( @looprow ) {
575 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
576 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
578 # warn "summ for column ".$col->{coltitle}." = ".$total;
579 push @loopfooter, {'totalcol' => $total};
583 # the header of the table
584 $globalline{loopfilter}=\@loopfilter;
585 # the core of the table
586 $globalline{looprow} = \@looprow;
587 $globalline{loopcol} = \@loopcol;
588 # # the foot (totals by borrower type)
589 $globalline{loopfooter} = \@loopfooter;
590 $globalline{total}= $grantotal;
591 $globalline{line} = $line;
592 $globalline{column} = $column;
593 push @mainloop,\%globalline;