4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 #use warnings; FIXME - Bug 2505
26 use C4
::Branch
; # GetBranches
31 use C4
::Dates qw
/format_date format_date_in_iso/;
32 use Date
::Calc
qw(Delta_Days);
36 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_avg_stats.tt";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso
($filters[0]);
51 $filters[1]=format_date_in_iso
($filters[1]);
52 $filters[2]=format_date_in_iso
($filters[2]);
53 $filters[3]=format_date_in_iso
($filters[3]);
54 my $podsp = $input->param("IssueDisplay");
55 my $rodsp = $input->param("ReturnDisplay");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user
({template_name
=> $fullreportname,
65 flagsrequired
=> {reports
=> '*'},
68 our $sep = $input->param("sep");
69 $sep = "\t" if ($sep eq 'tabulation');
70 $template->param(do_it
=> $do_it,
74 my $results = calculate
($line, $column, $rodsp, $podsp, $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',
84 -attachment
=>"$basename.csv",
85 -filename
=>"$basename.csv" );
86 my $cols = @
$results[0]->{loopcol
};
87 my $lines = @
$results[0]->{looprow
};
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;
119 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
123 while (my ($value, $desc) =$req->fetchrow) {
124 push @selectc, $value;
125 $labelsc{$value} = $desc;
132 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
136 while (my ($value,$desc) =$req->fetchrow) {
137 push @selecti, $value;
138 $labelsi{$value}=$desc;
145 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
149 while (my ($value) =$req->fetchrow) {
150 $hassort1 =1 if ($value);
151 push @selects1, $value;
154 values => \
@selects1,
157 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
162 while (my ($value) =$req->fetchrow) {
163 $hassort2 =1 if ($value);
164 $hglghtsort2= !($hassort1);
165 push @selects2, $value;
168 values => \
@selects2,
171 my $CGIsepChoice=GetDelimiterChoices
;
175 ItemType
=> $ItemTypes,
176 branchloop
=> GetBranchesLoop
(),
177 hassort1
=> $hassort1,
178 hassort2
=> $hassort2,
179 HlghtSort2
=> $hglghtsort2,
182 CGIsepChoice
=> $CGIsepChoice
184 output_html_with_http_headers
$input, $cookie, $template->output;
191 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
200 my $dbh = C4
::Context
->dbh;
206 for (my $i=0;$i<=6;$i++) {
208 if ( @
$filters[$i] ) {
209 if (($i==1) and (@
$filters[$i-1])) {
210 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
212 # format the dates filters, otherwise just fill as is
214 $cell{filter
} .= @
$filters[$i];
216 $cell{filter
} .= format_date
(@
$filters[$i]);
218 $cell{crit
} .="Issue From" if ($i==0);
219 $cell{crit
} .="Issue To" if ($i==1);
220 $cell{crit
} .="Issue Month" if ($i==2);
221 $cell{crit
} .="Issue Day" if ($i==3);
222 $cell{crit
} .="Return From" if ($i==4);
223 $cell{crit
} .="Return To" if ($i==5);
224 $cell{crit
} .="Return Month" if ($i==6);
225 $cell{crit
} .="Return Day" if ($i==7);
226 $cell{crit
} .="Borrower Cat" if ($i==8);
227 $cell{crit
} .="Doc Type" if ($i==9);
228 $cell{crit
} .="Branch" if ($i==10);
229 $cell{crit
} .="Sort1" if ($i==11);
230 $cell{crit
} .="Sort2" if ($i==12);
231 push @loopfilter, \
%cell;
234 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
235 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
240 # warn "filtres ".@filters[0];
241 # warn "filtres ".@filters[1];
242 # warn "filtres ".@filters[2];
243 # warn "filtres ".@filters[3];
244 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
245 $line = "biblioitems.".$line if $line=~/itemtype/;
247 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
248 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
249 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
250 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
251 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
252 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
253 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
254 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
255 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
256 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
257 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
258 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
259 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
260 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
261 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
263 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
264 $column = "biblioitems.".$column if $column=~/itemtype/;
266 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
267 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
268 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
269 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
270 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
271 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
272 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
273 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
274 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
275 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
276 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
277 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
278 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
279 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
280 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
285 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
287 $linefield .="dayname($line)";
288 $lineorder .="weekday($line)";
289 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
291 $linefield .="monthname($line)";
292 $lineorder .="month($line)";
293 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
295 $linefield .="Year($line)";
297 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
298 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
306 $strsth .= "select distinctrow $linefield
308 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
309 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
310 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
311 LEFT JOIN issuingrules ON
312 (issuingrules.branchcode=old_issues.branchcode
313 AND issuingrules.itemtype=biblioitems.itemtype
314 AND issuingrules.categorycode=borrowers.categorycode)
317 if (($line=~/timestamp/) or ($line=~/returndate/)){
318 if ($linefilter[1] and ($linefilter[0])){
319 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
320 } elsif ($linefilter[1]) {
321 $strsth .= " AND $line < \'$linefilter[1]\' " ;
322 } elsif ($linefilter[0]) {
323 $strsth .= " AND $line > \'$linefilter[0]\' " ;
326 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
329 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
331 } elsif ($linefilter[0]) {
332 $linefilter[0] =~ s/\*/%/g;
333 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
335 $strsth .=" GROUP BY $linefield";
336 $strsth .=" ORDER BY $lineorder";
338 my $sth = $dbh->prepare( $strsth );
342 while ( my ($celvalue) = $sth->fetchrow) {
345 $cell{rowtitle
} = $celvalue;
347 $cell{rowtitle
} = "";
350 push @loopline, \
%cell;
356 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
358 $colfield .="dayname($column)";
359 $colorder .="weekday($column)";
360 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
362 $colfield .="monthname($column)";
363 $colorder .="month($column)";
364 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
366 $colfield .="Year($column)";
367 $colorder .= $column;
368 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
369 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
370 $colorder .= $column;
372 $colfield .= $column;
373 $colorder .= $column;
377 $strsth2 .= "SELECT distinctrow $colfield
379 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
380 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
381 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
382 LEFT JOIN issuingrules ON
383 (issuingrules.branchcode=old_issues.branchcode
384 AND issuingrules.itemtype=biblioitems.itemtype
385 AND issuingrules.categorycode=borrowers.categorycode)
388 if (($column=~/timestamp/) or ($column=~/returndate/)){
389 if ($colfilter[1] and ($colfilter[0])){
390 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
391 } elsif ($colfilter[1]) {
392 $strsth2 .= " AND $column < '$colfilter[1]' " ;
393 } elsif ($colfilter[0]) {
394 $strsth2 .= " AND $column > '$colfilter[0]' " ;
397 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
400 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
402 } elsif ($colfilter[0]) {
403 $colfilter[0] =~ s/\*/%/g;
404 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
406 $strsth2 .=" GROUP BY $colfield";
407 $strsth2 .=" ORDER BY $colorder";
409 my $sth2 = $dbh->prepare( $strsth2 );
410 if (( @colfilter ) and ($colfilter[1])){
411 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
412 } elsif ($colfilter[0]) {
413 $sth2->execute($colfilter[0]);
419 while (my ($celvalue) = $sth2->fetchrow) {
422 # warn "coltitle :".$celvalue;
423 $cell{coltitle
} = $celvalue;
425 push @loopcol, \
%cell;
427 # warn "fin des titres colonnes";
433 #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 # Processing average loanperiods
451 $strcalc .= "SELECT $linefield, $colfield, ";
452 $strcalc .= " issuedate, returndate, old_issues.timestamp, COUNT(*), date_due, old_issues.renewals, issuelength FROM `old_issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=categorycode) WHERE old_issues.itemnumber=items.itemnumber AND old_issues.borrowernumber=borrowers.borrowernumber";
454 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
455 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
456 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
457 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
458 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
459 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
460 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
461 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
462 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
463 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
464 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
465 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
466 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
467 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
468 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
469 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
470 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
471 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
472 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
473 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
474 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
475 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
477 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
479 my $dbcalc = $dbh->prepare($strcalc);
481 # warn "filling table";
490 while (my @data = $dbcalc->fetchrow) {
491 my ($row, $col, $issuedate, $returndate, $weight)=@data;
492 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
493 $emptycol=1 if (!defined($col));
494 $col = "zzEMPTY" if (!defined($col));
495 $row = "zzEMPTY" if (!defined($row));
496 # fill returndate to avoid an error with date calc (needed for all non returned issues)
497 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
498 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
499 # and seconds between the two
500 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
501 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
502 # warn "513 row :".$row." column :".$col;
503 $table{$row}->{$col}+=$weight*$loanlength;
504 # $table{$row}->{totalrow}+=$weight*$loanlength;
505 $cnttable{$row}->{$col}= 1;
506 $wgttable{$row}->{$col}+=$weight;
509 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
511 foreach my $row ( sort keys %table ) {
513 #@loopcol ensures the order for columns is common with column titles
514 # and the number matches the number of columns
516 foreach my $col ( @loopcol ) {
519 ( ( $col->{coltitle
} eq 'NULL' )
520 or ( $col->{coltitle
} eq q{} )
521 ) ?
'zzEMPTY' : $col->{coltitle
}
524 $value = $table{$row}->{
525 ( ( $col->{coltitle
} eq 'NULL' )
526 or ( $col->{coltitle
} eq q{} )
527 ) ?
'zzEMPTY' : $col->{coltitle
}
528 } / $wgttable{$row}->{
529 ( ( $col->{coltitle
} eq 'NULL' )
530 or ( $col->{coltitle
} eq q{} )
531 ) ?
'zzEMPTY' : $col->{coltitle
}
534 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
535 $table{$row}->{totalrow
}+=$value;
536 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
537 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
538 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
540 #warn "row : $row colcount:$colcount";
542 if ( $colcount > 0 ) {
543 $total = $table{$row}->{totalrow
} / $colcount;
546 { 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
547 'loopcell' => \
@loopcell,
548 'hilighted' => ( $hilighted > 0 ),
549 'totalrow' => ($total) ?
sprintf( "%.2f", $total ) : 0
551 $hilighted = -$hilighted;
554 # # warn "footer processing";
555 foreach my $col ( @loopcol ) {
558 foreach my $row ( @looprow ) {
559 $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
}};
560 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
561 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
563 # warn "summ for column ".$col->{coltitle}." = ".$total;
564 $total = $total/$nbrow if ($nbrow);
565 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
570 # the header of the table
571 $globalline{loopfilter
}=\
@loopfilter;
572 # the core of the table
573 $globalline{looprow
} = \
@looprow;
574 $globalline{loopcol
} = \
@loopcol;
575 # # the foot (totals by borrower type)
576 $globalline{loopfooter
} = \
@loopfooter;
577 $globalline{total
}= $grantotal;
578 $globalline{line
} = $line;
579 $globalline{column
} = $column;
580 push @mainloop,\
%globalline;