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
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_avg_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $podsp = $input->param("IssueDisplay");
50 my $rodsp = $input->param("ReturnDisplay");
51 my $calc = $input->param("Cellvalue");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
56 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {reports => 1},
65 $template->param(do_it => $do_it,
66 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
67 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
68 IntranetNav => C4::Context->preference("IntranetNav"),
72 my $results = calculate($line, $column, $rodsp, $podsp, $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',
82 -attachment=>"$basename.csv",
83 -filename=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
87 $sep =C4::Context->preference("delimiter");
89 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
91 foreach my $col ( @$cols ) {
92 print $col->{coltitle}.$sep;
96 foreach my $line ( @$lines ) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle}.$sep;
99 foreach my $cell (@$x) {
100 print $cell->{value}.$sep;
102 print $line->{totalrow};
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col ( @$cols ) {
109 print $sep.$col->{totalcol};
111 print $sep.@$results[0]->{total};
116 my $dbh = C4::Context->dbh;
121 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
126 while (my ($value, $desc) =$req->fetchrow) {
127 push @select, $value;
128 $select{$value}=$desc;
130 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
137 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
143 while (my ($value,$desc) =$req->fetchrow) {
144 push @select, $value;
145 $select{$value}=$desc;
147 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
154 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
159 while (my ($value) =$req->fetchrow) {
160 $hassort1 =1 if ($value);
161 push @select, $value;
163 my $branches=GetBranches();
166 push @select_branch,"";
167 $select_branches{""} = "";
168 foreach my $branch (keys %$branches) {
169 push @select_branch, $branch;
170 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
172 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
174 -values => \@select_branch,
175 -labels => \%select_branches,
179 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
185 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
191 while (my ($value) =$req->fetchrow) {
192 $hassort2 =1 if ($value);
193 $hglghtsort2= !($hassort1);
194 push @select, $value;
196 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
202 my @mime = ( C4::Context->preference("MIME") );
203 # foreach my $mime (@mime){
207 my $CGIextChoice=CGI::scrolling_list(
214 my @dels = ( C4::Context->preference("delimiter") );
215 my $CGIsepChoice=CGI::scrolling_list(
223 CGIBorCat => $CGIBorCat,
224 CGIItemType => $CGIItemTypes,
225 CGIBranch => $CGIBranch,
226 hassort1=> $hassort1,
227 hassort2=> $hassort2,
228 HlghtSort2 => $hglghtsort2,
229 CGISort1 => $CGISort1,
230 CGISort2 => $CGISort2,
231 CGIextChoice => $CGIextChoice,
232 CGIsepChoice => $CGIsepChoice
234 output_html_with_http_headers $input, $cookie, $template->output;
241 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
250 my $dbh = C4::Context->dbh;
256 for (my $i=0;$i<=6;$i++) {
258 if ( @$filters[$i] ) {
259 if (($i==1) and (@$filters[$i-1])) {
260 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
262 $cell{filter} .= @$filters[$i];
263 $cell{crit} .="Issue From" if ($i==0);
264 $cell{crit} .="Issue To" if ($i==1);
265 $cell{crit} .="Issue Month" if ($i==2);
266 $cell{crit} .="Issue Day" if ($i==3);
267 $cell{crit} .="Return From" if ($i==4);
268 $cell{crit} .="Return To" if ($i==5);
269 $cell{crit} .="Return Month" if ($i==6);
270 $cell{crit} .="Return Day" if ($i==7);
271 $cell{crit} .="Borrower Cat" if ($i==8);
272 $cell{crit} .="Doc Type" if ($i==9);
273 $cell{crit} .="Branch" if ($i==10);
274 $cell{crit} .="Sort1" if ($i==11);
275 $cell{crit} .="Sort2" if ($i==12);
276 push @loopfilter, \%cell;
279 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
280 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
285 # warn "filtres ".@filters[0];
286 # warn "filtres ".@filters[1];
287 # warn "filtres ".@filters[2];
288 # warn "filtres ".@filters[3];
289 $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
290 $line = "biblioitems.".$line if $line=~/itemtype/;
292 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
293 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
294 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
295 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
296 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
297 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
298 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
299 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
300 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
301 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
302 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
303 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
304 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
305 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
306 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
308 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
309 $column = "biblioitems.".$column if $column=~/itemtype/;
311 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
312 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
313 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
314 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
315 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
316 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
317 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
318 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
319 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
320 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
321 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
322 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
323 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
324 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
325 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
330 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
332 $linefield .="dayname($line)";
333 $lineorder .="weekday($line)";
334 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
336 $linefield .="monthname($line)";
337 $lineorder .="month($line)";
338 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
340 $linefield .="Year($line)";
342 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
343 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
351 $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";
353 if (($line=~/timestamp/) or ($line=~/returndate/)){
354 if ($linefilter[1] and ($linefilter[0])){
355 $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
356 } elsif ($linefilter[1]) {
357 $strsth .= " and $line < \'$linefilter[1]\' " ;
358 } elsif ($linefilter[0]) {
359 $strsth .= " and $line > \'$linefilter[0]\' " ;
362 $strsth .= " and dayname($line) = '$linefilter[2]' " ;
365 $strsth .= " and monthname($line) = '$linefilter[3]' " ;
367 } elsif ($linefilter[0]) {
368 $linefilter[0] =~ s/\*/%/g;
369 $strsth .= " and $line LIKE '$linefilter[0]' " ;
371 $strsth .=" group by $linefield";
372 $strsth .=" order by $lineorder";
375 my $sth = $dbh->prepare( $strsth );
379 while ( my ($celvalue) = $sth->fetchrow) {
382 $cell{rowtitle} = $celvalue;
384 $cell{rowtitle} = "";
387 push @loopline, \%cell;
393 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
395 $colfield .="dayname($column)";
396 $colorder .="weekday($column)";
397 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
399 $colfield .="monthname($column)";
400 $colorder .="month($column)";
401 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
403 $colfield .="Year($column)";
404 $colorder .= $column;
405 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
406 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
407 $colorder .= $column;
409 $colfield .= $column;
410 $colorder .= $column;
414 $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";
416 if (($column=~/timestamp/) or ($column=~/returndate/)){
417 if ($colfilter[1] and ($colfilter[0])){
418 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
419 } elsif ($colfilter[1]) {
420 $strsth2 .= " and $column < '$colfilter[1]' " ;
421 } elsif ($colfilter[0]) {
422 $strsth2 .= " and $column > '$colfilter[0]' " ;
425 $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
428 $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
430 } elsif ($colfilter[0]) {
431 $colfilter[0] =~ s/\*/%/g;
432 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
434 $strsth2 .=" group by $colfield";
435 $strsth2 .=" order by $colorder";
438 my $sth2 = $dbh->prepare( $strsth2 );
439 if (( @colfilter ) and ($colfilter[1])){
440 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
441 } elsif ($colfilter[0]) {
442 $sth2->execute($colfilter[0]);
448 while (my ($celvalue) = $sth2->fetchrow) {
451 # warn "coltitle :".$celvalue;
452 $cell{coltitle} = $celvalue;
454 push @loopcol, \%cell;
456 # warn "fin des titres colonnes";
462 #Initialization of cell values.....
468 foreach my $row ( @loopline ) {
469 foreach my $col ( @loopcol ) {
470 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
471 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
473 $table{$row->{rowtitle}}->{totalrow}=0;
476 # preparing calculation
479 # Processing average loanperiods
480 $strcalc .= "SELECT $linefield, $colfield, ";
481 $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";
483 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
484 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
485 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
486 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
487 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
488 $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
489 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
490 $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
491 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
492 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
493 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
494 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
495 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
496 $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
497 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
498 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
499 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
500 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
501 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
502 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
503 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
504 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
506 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
507 warn "SQL :". $strcalc;
509 my $dbcalc = $dbh->prepare($strcalc);
511 # warn "filling table";
520 while (my @data = $dbcalc->fetchrow) {
521 my ($row, $col, $issuedate, $returndate, $weight)=@data;
522 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
523 $emptycol=1 if ($col eq undef);
524 $col = "zzEMPTY" if ($col eq undef);
525 $row = "zzEMPTY" if ($row eq undef);
526 # warn "506 row :".$row." column :".$col;
527 my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
528 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
529 # and seconds between the two
530 $loanlength = $result;
531 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
532 # warn "513 row :".$row." column :".$col;
533 $table{$row}->{$col}+=$weight*$loanlength;
534 # $table{$row}->{totalrow}+=$weight*$loanlength;
535 $cnttable{$row}->{$col}= 1;
536 $wgttable{$row}->{$col}+=$weight;
539 push @loopcol,{coltitle => "NULL"} if ($emptycol);
541 foreach my $row ( sort keys %table ) {
543 #@loopcol ensures the order for columns is common with column titles
544 # and the number matches the number of columns
546 foreach my $col ( @loopcol ) {
547 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}});
549 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
550 $table{$row}->{totalrow}+=$value;
551 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
552 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
553 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
555 #warn "row : $row colcount:$colcount";
556 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
557 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
558 'loopcell' => \@loopcell,
559 'hilighted' => ($hilighted >0),
560 'totalrow' => ($total)?sprintf("%.2f",$total):0
562 $hilighted = -$hilighted;
565 # # warn "footer processing";
566 foreach my $col ( @loopcol ) {
569 foreach my $row ( @looprow ) {
570 $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}};
571 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
572 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
574 # warn "summ for column ".$col->{coltitle}." = ".$total;
575 $total = $total/$nbrow if ($nbrow);
576 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
581 # the header of the table
582 $globalline{loopfilter}=\@loopfilter;
583 # the core of the table
584 $globalline{looprow} = \@looprow;
585 $globalline{loopcol} = \@loopcol;
586 # # the foot (totals by borrower type)
587 $globalline{loopfooter} = \@loopfooter;
588 $globalline{total}= $grantotal;
589 $globalline{line} = $line;
590 $globalline{column} = $column;
591 push @mainloop,\%globalline;