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
30 use Date::Calc qw(Delta_Days);
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_avg_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 my $podsp = $input->param("IssueDisplay");
49 my $rodsp = $input->param("ReturnDisplay");
50 my $calc = $input->param("Cellvalue");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {reports => 1},
64 $template->param(do_it => $do_it,
65 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
66 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
67 IntranetNav => C4::Context->preference("IntranetNav"),
71 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results);
75 output_html_with_http_headers $input, $cookie, $template->output;
78 # Printing to a csv file
79 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, $rodsp, $podsp, $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} .="Issue From" if ($i==0);
263 $cell{crit} .="Issue To" if ($i==1);
264 $cell{crit} .="Issue Month" if ($i==2);
265 $cell{crit} .="Issue Day" if ($i==3);
266 $cell{crit} .="Return From" if ($i==4);
267 $cell{crit} .="Return To" if ($i==5);
268 $cell{crit} .="Return Month" if ($i==6);
269 $cell{crit} .="Return Day" if ($i==7);
270 $cell{crit} .="Borrower Cat" if ($i==8);
271 $cell{crit} .="Doc Type" if ($i==9);
272 $cell{crit} .="Branch" if ($i==10);
273 $cell{crit} .="Sort1" if ($i==11);
274 $cell{crit} .="Sort2" if ($i==12);
275 push @loopfilter, \%cell;
278 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
279 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
284 # warn "filtres ".@filters[0];
285 # warn "filtres ".@filters[1];
286 # warn "filtres ".@filters[2];
287 # warn "filtres ".@filters[3];
288 $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
289 $line = "biblioitems.".$line if $line=~/itemtype/;
291 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
292 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
293 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
294 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
295 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
296 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
297 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
298 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
299 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
300 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
301 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
302 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
303 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
304 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
305 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
307 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
308 $column = "biblioitems.".$column if $column=~/itemtype/;
310 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
311 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
312 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
313 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
314 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
315 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
316 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
317 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
318 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
319 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
320 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
321 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
322 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
323 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
324 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
329 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
331 $linefield .="dayname($line)";
332 $lineorder .="weekday($line)";
333 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
335 $linefield .="monthname($line)";
336 $lineorder .="month($line)";
337 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
339 $linefield .="Year($line)";
341 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
342 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
350 $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";
352 if (($line=~/timestamp/) or ($line=~/returndate/)){
353 if ($linefilter[1] and ($linefilter[0])){
354 $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
355 } elsif ($linefilter[1]) {
356 $strsth .= " and $line < \'$linefilter[1]\' " ;
357 } elsif ($linefilter[0]) {
358 $strsth .= " and $line > \'$linefilter[0]\' " ;
361 $strsth .= " and dayname($line) = '$linefilter[2]' " ;
364 $strsth .= " and monthname($line) = '$linefilter[3]' " ;
366 } elsif ($linefilter[0]) {
367 $linefilter[0] =~ s/\*/%/g;
368 $strsth .= " and $line LIKE '$linefilter[0]' " ;
370 $strsth .=" group by $linefield";
371 $strsth .=" order by $lineorder";
374 my $sth = $dbh->prepare( $strsth );
378 while ( my ($celvalue) = $sth->fetchrow) {
381 $cell{rowtitle} = $celvalue;
383 $cell{rowtitle} = "";
386 push @loopline, \%cell;
392 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
394 $colfield .="dayname($column)";
395 $colorder .="weekday($column)";
396 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
398 $colfield .="monthname($column)";
399 $colorder .="month($column)";
400 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
402 $colfield .="Year($column)";
403 $colorder .= $column;
404 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
405 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
406 $colorder .= $column;
408 $colfield .= $column;
409 $colorder .= $column;
413 $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";
415 if (($column=~/timestamp/) or ($column=~/returndate/)){
416 if ($colfilter[1] and ($colfilter[0])){
417 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
418 } elsif ($colfilter[1]) {
419 $strsth2 .= " and $column < '$colfilter[1]' " ;
420 } elsif ($colfilter[0]) {
421 $strsth2 .= " and $column > '$colfilter[0]' " ;
424 $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
427 $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
429 } elsif ($colfilter[0]) {
430 $colfilter[0] =~ s/\*/%/g;
431 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
433 $strsth2 .=" group by $colfield";
434 $strsth2 .=" order by $colorder";
437 my $sth2 = $dbh->prepare( $strsth2 );
438 if (( @colfilter ) and ($colfilter[1])){
439 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
440 } elsif ($colfilter[0]) {
441 $sth2->execute($colfilter[0]);
447 while (my ($celvalue) = $sth2->fetchrow) {
450 # warn "coltitle :".$celvalue;
451 $cell{coltitle} = $celvalue;
453 push @loopcol, \%cell;
455 # warn "fin des titres colonnes";
461 #Initialization of cell values.....
467 foreach my $row ( @loopline ) {
468 foreach my $col ( @loopcol ) {
469 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
470 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
472 $table{$row->{rowtitle}}->{totalrow}=0;
475 # preparing calculation
478 # Processing average loanperiods
479 $strcalc .= "SELECT $linefield, $colfield, ";
480 $strcalc .= " 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";
482 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
483 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
484 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
485 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
486 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
487 $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
488 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
489 $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
490 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
491 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
492 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
493 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
494 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
495 $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
496 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
497 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
498 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
499 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
500 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
501 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
502 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
503 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
505 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
506 warn "SQL :". $strcalc;
508 my $dbcalc = $dbh->prepare($strcalc);
510 # warn "filling table";
519 while (my @data = $dbcalc->fetchrow) {
520 my ($row, $col, $issuedate, $returndate, $weight)=@data;
521 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
522 $emptycol=1 if ($col eq undef);
523 $col = "zzEMPTY" if ($col eq undef);
524 $row = "zzEMPTY" if ($row eq undef);
525 # warn "506 row :".$row." column :".$col;
526 my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
527 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
528 # and seconds between the two
529 $loanlength = $result;
530 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
531 # warn "513 row :".$row." column :".$col;
532 $table{$row}->{$col}+=$weight*$loanlength;
533 # $table{$row}->{totalrow}+=$weight*$loanlength;
534 $cnttable{$row}->{$col}= 1;
535 $wgttable{$row}->{$col}+=$weight;
538 push @loopcol,{coltitle => "NULL"} if ($emptycol);
540 foreach my $row ( sort keys %table ) {
542 #@loopcol ensures the order for columns is common with column titles
543 # and the number matches the number of columns
545 foreach my $col ( @loopcol ) {
546 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}});
548 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
549 $table{$row}->{totalrow}+=$value;
550 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
551 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
552 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
554 #warn "row : $row colcount:$colcount";
555 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
556 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
557 'loopcell' => \@loopcell,
558 'hilighted' => ($hilighted >0),
559 'totalrow' => ($total)?sprintf("%.2f",$total):0
561 $hilighted = -$hilighted;
564 # # warn "footer processing";
565 foreach my $col ( @loopcol ) {
568 foreach my $row ( @looprow ) {
569 $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}};
570 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
571 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
573 # warn "summ for column ".$col->{coltitle}." = ".$total;
574 $total = $total/$nbrow if ($nbrow);
575 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
580 # the header of the table
581 $globalline{loopfilter}=\@loopfilter;
582 # the core of the table
583 $globalline{looprow} = \@looprow;
584 $globalline{loopcol} = \@loopcol;
585 # # the foot (totals by borrower type)
586 $globalline{loopfooter} = \@loopfooter;
587 $globalline{total}= $grantotal;
588 $globalline{line} = $line;
589 $globalline{column} = $column;
590 push @mainloop,\%globalline;