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_avg_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("IssueDisplay");
52 my $rodsp = $input->param("ReturnDisplay");
53 my $calc = $input->param("Cellvalue");
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 my $del = $input->param("sep");
58 #warn "calcul : ".$calc;
59 my ($template, $borrowernumber, $cookie)
60 = get_template_and_user({template_name => $fullreportname,
64 flagsrequired => {editcatalogue => 1},
67 $template->param(do_it => $do_it);
70 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
71 if ($output eq "screen"){
72 # Printing results to screen
73 $template->param(mainloop => $results);
74 output_html_with_http_headers $input, $cookie, $template->output;
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
84 $sep =C4::Context->preference("delimiter");
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 foreach my $col ( @$cols ) {
89 print $col->{coltitle}.$sep;
93 foreach my $line ( @$lines ) {
94 my $x = $line->{loopcell};
95 print $line->{rowtitle}.$sep;
96 foreach my $cell (@$x) {
97 print $cell->{value}.$sep;
99 print $line->{totalrow};
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
113 my $dbh = C4::Context->dbh;
118 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
123 while (my ($value, $desc) =$req->fetchrow) {
124 push @select, $value;
125 $select{$value}=$desc;
127 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
134 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
140 while (my ($value,$desc) =$req->fetchrow) {
141 push @select, $value;
142 $select{$value}=$desc;
144 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
151 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
156 while (my ($value) =$req->fetchrow) {
157 $hassort1 =1 if ($value);
158 push @select, $value;
160 my $branches=getbranches();
163 push @select_branch,"";
164 $select_branches{""} = "";
165 foreach my $branch (keys %$branches) {
166 push @select_branch, $branch;
167 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
169 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
171 -values => \@select_branch,
172 -labels => \%select_branches,
176 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
182 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
188 while (my ($value) =$req->fetchrow) {
189 $hassort2 =1 if ($value);
190 $hglghtsort2= !($hassort1);
191 push @select, $value;
193 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
199 my @mime = ( C4::Context->preference("MIME") );
200 # foreach my $mime (@mime){
204 my $CGIextChoice=CGI::scrolling_list(
211 my @dels = ( C4::Context->preference("delimiter") );
212 my $CGIsepChoice=CGI::scrolling_list(
220 CGIBorCat => $CGIBorCat,
221 CGIItemType => $CGIItemTypes,
222 CGIBranch => $CGIBranch,
223 hassort1=> $hassort1,
224 hassort2=> $hassort2,
225 HlghtSort2 => $hglghtsort2,
226 CGISort1 => $CGISort1,
227 CGISort2 => $CGISort2,
228 CGIextChoice => $CGIextChoice,
229 CGIsepChoice => $CGIsepChoice
231 output_html_with_http_headers $input, $cookie, $template->output;
238 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
247 my $dbh = C4::Context->dbh;
253 for (my $i=0;$i<=6;$i++) {
255 if ( @$filters[$i] ) {
256 if (($i==1) and (@$filters[$i-1])) {
257 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
259 $cell{filter} .= @$filters[$i];
260 $cell{crit} .="Issue From" if ($i==0);
261 $cell{crit} .="Issue To" if ($i==1);
262 $cell{crit} .="Issue Month" if ($i==2);
263 $cell{crit} .="Issue Day" if ($i==3);
264 $cell{crit} .="Return From" if ($i==4);
265 $cell{crit} .="Return To" if ($i==5);
266 $cell{crit} .="Return Month" if ($i==6);
267 $cell{crit} .="Return Day" if ($i==7);
268 $cell{crit} .="Borrower Cat" if ($i==8);
269 $cell{crit} .="Doc Type" if ($i==9);
270 $cell{crit} .="Branch" if ($i==10);
271 $cell{crit} .="Sort1" if ($i==11);
272 $cell{crit} .="Sort2" if ($i==12);
273 push @loopfilter, \%cell;
276 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
277 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
282 # warn "filtres ".@filters[0];
283 # warn "filtres ".@filters[1];
284 # warn "filtres ".@filters[2];
285 # warn "filtres ".@filters[3];
286 $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
287 $line = "biblioitems.".$line if $line=~/itemtype/;
289 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
290 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
291 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
292 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
293 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
294 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
295 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
296 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
297 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
298 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
299 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
300 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
301 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
302 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
303 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
305 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
306 $column = "biblioitems.".$column if $column=~/itemtype/;
308 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
309 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
310 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
311 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
312 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
313 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
314 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
315 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
316 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
317 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
318 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
319 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
320 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
321 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
322 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
327 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
329 $linefield .="dayname($line)";
330 $lineorder .="weekday($line)";
331 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
333 $linefield .="monthname($line)";
334 $lineorder .="month($line)";
335 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
337 $linefield .="Year($line)";
339 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
340 $linefield .= "date_format('$line',\"%Y-%m-%d\")";
348 $strsth .= "select distinctrow $linefield 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 and returndate is not null";
350 if (($line=~/timestamp/) or ($line=~/returndate/)){
351 if ($linefilter[1] and ($linefilter[0])){
352 $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
353 } elsif ($linefilter[1]) {
354 $strsth .= " and $line < '$linefilter[1]' " ;
355 } elsif ($linefilter[0]) {
356 $strsth .= " and $line > '$linefilter[0]' " ;
359 $strsth .= " and dayname($line) = '$linefilter[2]' " ;
362 $strsth .= " and monthname($line) = '$linefilter[3]' " ;
364 } elsif ($linefilter[0]) {
365 $linefilter[0] =~ s/\*/%/g;
366 $strsth .= " and $line LIKE '$linefilter[0]' " ;
368 $strsth .=" group by $linefield";
369 $strsth .=" order by $lineorder";
372 my $sth = $dbh->prepare( $strsth );
376 while ( my ($celvalue) = $sth->fetchrow) {
379 $cell{rowtitle} = $celvalue;
381 $cell{rowtitle} = "";
384 push @loopline, \%cell;
390 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
392 $colfield .="dayname($column)";
393 $colorder .="weekday($column)";
394 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
396 $colfield .="monthname($column)";
397 $colorder .="month($column)";
398 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
400 $colfield .="Year($column)";
401 $colorder .= $column;
402 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
403 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
404 $colorder .= $column;
406 $colfield .= $column;
407 $colorder .= $column;
411 $strsth2 .= "select distinctrow $colfield 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 and returndate is not null";
413 if (($column=~/timestamp/) or ($column=~/returndate/)){
414 if ($colfilter[1] and ($colfilter[0])){
415 $strsth .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
416 } elsif ($colfilter[1]) {
417 $strsth .= " and $column < '$colfilter[1]' " ;
418 } elsif ($colfilter[0]) {
419 $strsth .= " and $column > '$colfilter[0]' " ;
422 $strsth .= " and dayname($column) = '$colfilter[2]' " ;
425 $strsth .= " and monthname($column) = '$colfilter[3]' " ;
427 } elsif ($colfilter[0]) {
428 $colfilter[0] =~ s/\*/%/g;
429 $strsth .= " and $column LIKE '$colfilter[0]' " ;
431 $strsth2 .=" group by $colfield";
432 $strsth2 .=" order by $colorder";
435 my $sth2 = $dbh->prepare( $strsth2 );
436 if (( @colfilter ) and ($colfilter[1])){
437 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
438 } elsif ($colfilter[0]) {
439 $sth2->execute($colfilter[0]);
445 while (my ($celvalue) = $sth2->fetchrow) {
448 # warn "coltitle :".$celvalue;
449 $cell{coltitle} = $celvalue;
451 push @loopcol, \%cell;
453 # warn "fin des titres colonnes";
459 #Initialization of cell values.....
465 foreach my $row ( @loopline ) {
466 foreach my $col ( @loopcol ) {
467 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
468 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
470 $table{$row->{rowtitle}}->{totalrow}=0;
473 # preparing calculation
476 # Processing average loanperiods
477 $strcalc .= "SELECT $linefield, $colfield, ";
478 $strcalc .= " DATE_SUB(date_due, INTERVAL CAST(issuingrules.issuelength 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 and returndate is not null";
480 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
481 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
482 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
483 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
484 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
485 $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
486 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
487 $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
488 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
489 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
490 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
491 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
492 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
493 $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
494 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
495 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
496 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
497 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
498 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
499 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
500 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
501 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
503 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
504 warn "SQL :". $strcalc;
506 my $dbcalc = $dbh->prepare($strcalc);
508 # warn "filling table";
517 while (my @data = $dbcalc->fetchrow) {
518 my ($row, $col, $issuedate, $returndate, $weight)=@data;
519 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
520 $emptycol=1 if ($col eq undef);
521 $col = "zzEMPTY" if ($col eq undef);
522 $row = "zzEMPTY" if ($row eq undef);
523 # warn "506 row :".$row." column :".$col;
524 my @result =split /:/,DateCalc($returndate,$issuedate) ;
525 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
526 # and seconds between the two
527 $loanlength = $result[2]*7+$result[3];
528 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
529 # warn "513 row :".$row." column :".$col;
530 $table{$row}->{$col}+=$weight*$loanlength;
531 # $table{$row}->{totalrow}+=$weight*$loanlength;
532 $cnttable{$row}->{$col}= 1;
533 $wgttable{$row}->{$col}+=$weight;
536 push @loopcol,{coltitle => "NULL"} if ($emptycol);
538 foreach my $row ( sort keys %table ) {
540 #@loopcol ensures the order for columns is common with column titles
541 # and the number matches the number of columns
543 foreach my $col ( @loopcol ) {
544 my $value =$table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} / $wgttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} if ($table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}});
546 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
547 $table{$row}->{totalrow}+=$value;
548 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
549 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
550 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
552 #warn "row : $row colcount:$colcount";
553 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
554 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
555 'loopcell' => \@loopcell,
556 'hilighted' => ($hilighted >0),
557 'totalrow' => ($total)?sprintf("%.2f",$total):0
559 $hilighted = -$hilighted;
562 # # warn "footer processing";
563 foreach my $col ( @loopcol ) {
566 foreach my $row ( @looprow ) {
567 $total += $cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}}*$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
568 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
569 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
571 # warn "summ for column ".$col->{coltitle}." = ".$total;
572 $total = $total/$nbrow if ($nbrow);
573 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
578 # the header of the table
579 $globalline{loopfilter}=\@loopfilter;
580 # the core of the table
581 $globalline{looprow} = \@looprow;
582 $globalline{loopcol} = \@loopcol;
583 # # the foot (totals by borrower type)
584 $globalline{loopfooter} = \@loopfooter;
585 $globalline{total}= $grantotal;
586 $globalline{line} = $line;
587 $globalline{column} = $column;
588 push @mainloop,\%globalline;