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;
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;
164 my $CGISort1=CGI
::scrolling_list
( -name
=> 'Filter',
170 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
176 while (my ($value) =$req->fetchrow) {
177 $hassort2 =1 if ($value);
178 $hglghtsort2= !($hassort1);
179 push @select, $value;
181 my $CGISort2=CGI
::scrolling_list
( -name
=> 'Filter',
187 my $CGIextChoice=CGI
::scrolling_list
(
190 -values => ['CSV'], # FIXME translation
194 my $CGIsepChoice=GetDelimiterChoices
;
197 CGIBorCat
=> $CGIBorCat,
198 CGIItemType
=> $CGIItemTypes,
199 branchloop
=> GetBranchesLoop
(),
200 hassort1
=> $hassort1,
201 hassort2
=> $hassort2,
202 HlghtSort2
=> $hglghtsort2,
203 CGISort1
=> $CGISort1,
204 CGISort2
=> $CGISort2,
205 CGIextChoice
=> $CGIextChoice,
206 CGIsepChoice
=> $CGIsepChoice
208 output_html_with_http_headers
$input, $cookie, $template->output;
215 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
224 my $dbh = C4
::Context
->dbh;
230 for (my $i=0;$i<=6;$i++) {
232 if ( @
$filters[$i] ) {
233 if (($i==1) and (@
$filters[$i-1])) {
234 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
236 # format the dates filters, otherwise just fill as is
238 $cell{filter
} .= @
$filters[$i];
240 $cell{filter
} .= format_date
(@
$filters[$i]);
242 $cell{crit
} .="Issue From" if ($i==0);
243 $cell{crit
} .="Issue To" if ($i==1);
244 $cell{crit
} .="Issue Month" if ($i==2);
245 $cell{crit
} .="Issue Day" if ($i==3);
246 $cell{crit
} .="Return From" if ($i==4);
247 $cell{crit
} .="Return To" if ($i==5);
248 $cell{crit
} .="Return Month" if ($i==6);
249 $cell{crit
} .="Return Day" if ($i==7);
250 $cell{crit
} .="Borrower Cat" if ($i==8);
251 $cell{crit
} .="Doc Type" if ($i==9);
252 $cell{crit
} .="Branch" if ($i==10);
253 $cell{crit
} .="Sort1" if ($i==11);
254 $cell{crit
} .="Sort2" if ($i==12);
255 push @loopfilter, \
%cell;
258 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
259 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
264 # warn "filtres ".@filters[0];
265 # warn "filtres ".@filters[1];
266 # warn "filtres ".@filters[2];
267 # warn "filtres ".@filters[3];
268 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
269 $line = "biblioitems.".$line if $line=~/itemtype/;
271 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
272 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
273 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
274 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
275 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
276 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
277 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
278 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
279 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
280 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
281 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
282 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
283 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
284 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
285 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
287 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
288 $column = "biblioitems.".$column if $column=~/itemtype/;
290 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
291 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
292 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
293 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
294 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
295 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
296 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
297 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
298 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
299 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
300 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
301 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
302 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
303 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
304 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
309 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
311 $linefield .="dayname($line)";
312 $lineorder .="weekday($line)";
313 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
315 $linefield .="monthname($line)";
316 $lineorder .="month($line)";
317 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
319 $linefield .="Year($line)";
321 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
322 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
330 $strsth .= "select distinctrow $linefield
332 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
333 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
334 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
335 LEFT JOIN issuingrules ON
336 (issuingrules.branchcode=old_issues.branchcode
337 AND issuingrules.itemtype=biblioitems.itemtype
338 AND issuingrules.categorycode=borrowers.categorycode)
341 if (($line=~/timestamp/) or ($line=~/returndate/)){
342 if ($linefilter[1] and ($linefilter[0])){
343 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
344 } elsif ($linefilter[1]) {
345 $strsth .= " AND $line < \'$linefilter[1]\' " ;
346 } elsif ($linefilter[0]) {
347 $strsth .= " AND $line > \'$linefilter[0]\' " ;
350 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
353 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
355 } elsif ($linefilter[0]) {
356 $linefilter[0] =~ s/\*/%/g;
357 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
359 $strsth .=" GROUP BY $linefield";
360 $strsth .=" ORDER BY $lineorder";
362 my $sth = $dbh->prepare( $strsth );
366 while ( my ($celvalue) = $sth->fetchrow) {
369 $cell{rowtitle
} = $celvalue;
371 $cell{rowtitle
} = "";
374 push @loopline, \
%cell;
380 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
382 $colfield .="dayname($column)";
383 $colorder .="weekday($column)";
384 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
386 $colfield .="monthname($column)";
387 $colorder .="month($column)";
388 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
390 $colfield .="Year($column)";
391 $colorder .= $column;
392 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
393 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
394 $colorder .= $column;
396 $colfield .= $column;
397 $colorder .= $column;
401 $strsth2 .= "SELECT distinctrow $colfield
403 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
404 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
405 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
406 LEFT JOIN issuingrules ON
407 (issuingrules.branchcode=old_issues.branchcode
408 AND issuingrules.itemtype=biblioitems.itemtype
409 AND issuingrules.categorycode=borrowers.categorycode)
412 if (($column=~/timestamp/) or ($column=~/returndate/)){
413 if ($colfilter[1] and ($colfilter[0])){
414 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
415 } elsif ($colfilter[1]) {
416 $strsth2 .= " AND $column < '$colfilter[1]' " ;
417 } elsif ($colfilter[0]) {
418 $strsth2 .= " AND $column > '$colfilter[0]' " ;
421 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
424 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
426 } elsif ($colfilter[0]) {
427 $colfilter[0] =~ s/\*/%/g;
428 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
430 $strsth2 .=" GROUP BY $colfield";
431 $strsth2 .=" ORDER BY $colorder";
433 my $sth2 = $dbh->prepare( $strsth2 );
434 if (( @colfilter ) and ($colfilter[1])){
435 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
436 } elsif ($colfilter[0]) {
437 $sth2->execute($colfilter[0]);
443 while (my ($celvalue) = $sth2->fetchrow) {
446 # warn "coltitle :".$celvalue;
447 $cell{coltitle
} = $celvalue;
449 push @loopcol, \
%cell;
451 # warn "fin des titres colonnes";
457 #Initialization of cell values.....
463 foreach my $row ( @loopline ) {
464 foreach my $col ( @loopcol ) {
465 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
466 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
468 $table{$row->{rowtitle
}}->{totalrow
}=0;
471 # preparing calculation
474 # Processing average loanperiods
475 $strcalc .= "SELECT $linefield, $colfield, ";
476 $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";
478 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
479 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
480 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
481 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
482 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
483 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
484 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
485 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
486 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
487 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
488 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
489 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
490 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
491 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
492 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
493 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
494 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
495 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
496 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
497 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
498 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
499 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
501 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
503 my $dbcalc = $dbh->prepare($strcalc);
505 # warn "filling table";
514 while (my @data = $dbcalc->fetchrow) {
515 my ($row, $col, $issuedate, $returndate, $weight)=@data;
516 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
517 $emptycol=1 if (!defined($col));
518 $col = "zzEMPTY" if (!defined($col));
519 $row = "zzEMPTY" if (!defined($row));
520 # fill returndate to avoid an error with date calc (needed for all non returned issues)
521 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
522 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
523 # and seconds between the two
524 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
525 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
526 # warn "513 row :".$row." column :".$col;
527 $table{$row}->{$col}+=$weight*$loanlength;
528 # $table{$row}->{totalrow}+=$weight*$loanlength;
529 $cnttable{$row}->{$col}= 1;
530 $wgttable{$row}->{$col}+=$weight;
533 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
535 foreach my $row ( sort keys %table ) {
537 #@loopcol ensures the order for columns is common with column titles
538 # and the number matches the number of columns
540 foreach my $col ( @loopcol ) {
543 ( ( $col->{coltitle
} eq 'NULL' )
544 or ( $col->{coltitle
} eq q{} )
545 ) ?
'zzEMPTY' : $col->{coltitle
}
548 $value = $table{$row}->{
549 ( ( $col->{coltitle
} eq 'NULL' )
550 or ( $col->{coltitle
} eq q{} )
551 ) ?
'zzEMPTY' : $col->{coltitle
}
552 } / $wgttable{$row}->{
553 ( ( $col->{coltitle
} eq 'NULL' )
554 or ( $col->{coltitle
} eq q{} )
555 ) ?
'zzEMPTY' : $col->{coltitle
}
558 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
559 $table{$row}->{totalrow
}+=$value;
560 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
561 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
562 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
564 #warn "row : $row colcount:$colcount";
566 if ( $colcount > 0 ) {
567 $total = $table{$row}->{totalrow
} / $colcount;
570 { 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
571 'loopcell' => \
@loopcell,
572 'hilighted' => ( $hilighted > 0 ),
573 'totalrow' => ($total) ?
sprintf( "%.2f", $total ) : 0
575 $hilighted = -$hilighted;
578 # # warn "footer processing";
579 foreach my $col ( @loopcol ) {
582 foreach my $row ( @looprow ) {
583 $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
}};
584 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
585 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
587 # warn "summ for column ".$col->{coltitle}." = ".$total;
588 $total = $total/$nbrow if ($nbrow);
589 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
594 # the header of the table
595 $globalline{loopfilter
}=\
@loopfilter;
596 # the core of the table
597 $globalline{looprow
} = \
@looprow;
598 $globalline{loopcol
} = \
@loopcol;
599 # # the foot (totals by borrower type)
600 $globalline{loopfooter
} = \
@loopfooter;
601 $globalline{total
}= $grantotal;
602 $globalline{line
} = $line;
603 $globalline{column
} = $column;
604 push @mainloop,\
%globalline;