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
31 use Koha
::Patron
::Categories
;
32 use Date
::Calc
qw(Delta_Days);
36 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_avg_stats.tt";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->multi_param("Filter");
48 $filters[0] = eval { output_pref
( { dt
=> dt_from_string
( $filters[0]), dateonly
=> 1, dateformat
=> 'iso' } ); }
50 $filters[1] = eval { output_pref
( { dt
=> dt_from_string
( $filters[1]), dateonly
=> 1, dateformat
=> 'iso' } ); }
52 $filters[2] = eval { output_pref
( { dt
=> dt_from_string
( $filters[2]), dateonly
=> 1, dateformat
=> 'iso' } ); }
54 $filters[3] = eval { output_pref
( { dt
=> dt_from_string
( $filters[3]), dateonly
=> 1, dateformat
=> 'iso' } ); }
58 my $podsp = $input->param("IssueDisplay");
59 my $rodsp = $input->param("ReturnDisplay");
60 my $calc = $input->param("Cellvalue");
61 my $output = $input->param("output");
62 my $basename = $input->param("basename");
63 #warn "calcul : ".$calc;
64 my ($template, $borrowernumber, $cookie)
65 = get_template_and_user
({template_name
=> $fullreportname,
69 flagsrequired
=> {reports
=> '*'},
72 our $sep = $input->param("sep");
73 $sep = "\t" if ($sep eq 'tabulation');
74 $template->param(do_it
=> $do_it,
78 my $results = calculate
($line, $column, $rodsp, $podsp, $calc, \
@filters);
79 if ($output eq "screen"){
80 # Printing results to screen
81 $template->param(mainloop
=> $results);
82 output_html_with_http_headers
$input, $cookie, $template->output;
85 # Printing to a csv file
86 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
88 -attachment
=>"$basename.csv",
89 -filename
=>"$basename.csv" );
90 my $cols = @
$results[0]->{loopcol
};
91 my $lines = @
$results[0]->{looprow
};
93 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
95 foreach my $col ( @
$cols ) {
96 print $col->{coltitle
}.$sep;
100 foreach my $line ( @
$lines ) {
101 my $x = $line->{loopcell
};
102 print $line->{rowtitle
}.$sep;
103 foreach my $cell (@
$x) {
104 print $cell->{value
}.$sep;
106 print $line->{totalrow
};
111 $cols = @
$results[0]->{loopfooter
};
112 foreach my $col ( @
$cols ) {
113 print $sep.$col->{totalcol
};
115 print $sep.@
$results[0]->{total
};
120 my $patron_categories = Koha
::Patron
::Categories
->search({}, {order_by
=> ['description']});
122 my $itemtypes = GetItemTypes
( style
=> 'array' );
124 my $dbh = C4
::Context
->dbh;
125 my $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
129 while (my ($value) =$req->fetchrow) {
130 $hassort1 =1 if ($value);
131 push @selects1, $value;
134 values => \
@selects1,
137 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
142 while (my ($value) =$req->fetchrow) {
143 $hassort2 =1 if ($value);
144 $hglghtsort2= !($hassort1);
145 push @selects2, $value;
148 values => \
@selects2,
151 my $CGIsepChoice=GetDelimiterChoices
;
154 patron_categories
=> $patron_categories,
155 itemtypes
=> $itemtypes,
156 hassort1
=> $hassort1,
157 hassort2
=> $hassort2,
158 HlghtSort2
=> $hglghtsort2,
161 CGIsepChoice
=> $CGIsepChoice
163 output_html_with_http_headers
$input, $cookie, $template->output;
170 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
179 my $dbh = C4
::Context
->dbh;
185 for (my $i=0;$i<=6;$i++) {
187 if ( @
$filters[$i] ) {
188 if (($i==1) and (@
$filters[$i-1])) {
189 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
191 # format the dates filters, otherwise just fill as is
193 $cell{filter
} .= @
$filters[$i];
195 $cell{filter
} .= eval { output_pref
( { dt
=> dt_from_string
( @
$filters[$i] ), dateonly
=> 1 }); }
196 if ( @
$filters[$i] );
198 $cell{crit
} .="Issue From" if ($i==0);
199 $cell{crit
} .="Issue To" if ($i==1);
200 $cell{crit
} .="Issue Month" if ($i==2);
201 $cell{crit
} .="Issue Day" if ($i==3);
202 $cell{crit
} .="Return From" if ($i==4);
203 $cell{crit
} .="Return To" if ($i==5);
204 $cell{crit
} .="Return Month" if ($i==6);
205 $cell{crit
} .="Return Day" if ($i==7);
206 $cell{crit
} .="Borrower Cat" if ($i==8);
207 $cell{crit
} .="Doc Type" if ($i==9);
208 $cell{crit
} .="Branch" if ($i==10);
209 $cell{crit
} .="Sort1" if ($i==11);
210 $cell{crit
} .="Sort2" if ($i==12);
211 push @loopfilter, \
%cell;
214 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
215 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
220 # warn "filtres ".@filters[0];
221 # warn "filtres ".@filters[1];
222 # warn "filtres ".@filters[2];
223 # warn "filtres ".@filters[3];
224 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
225 $line = "biblioitems.".$line if $line=~/itemtype/;
227 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
228 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
229 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
230 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
231 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
232 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
233 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
234 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
235 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
236 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
237 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
238 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
239 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
240 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
241 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
243 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
244 $column = "biblioitems.".$column if $column=~/itemtype/;
246 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
247 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
248 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
249 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
250 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
251 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
252 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
253 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
254 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
255 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
256 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
257 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
258 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
259 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
260 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
265 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
267 $linefield .="dayname($line)";
268 $lineorder .="weekday($line)";
269 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
271 $linefield .="monthname($line)";
272 $lineorder .="month($line)";
273 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
275 $linefield .="Year($line)";
277 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
278 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
286 $strsth .= "select distinctrow $linefield
288 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
289 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
290 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
291 LEFT JOIN issuingrules ON
292 (issuingrules.branchcode=old_issues.branchcode
293 AND issuingrules.itemtype=biblioitems.itemtype
294 AND issuingrules.categorycode=borrowers.categorycode)
297 if (($line=~/timestamp/) or ($line=~/returndate/)){
298 if ($linefilter[1] and ($linefilter[0])){
299 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
300 } elsif ($linefilter[1]) {
301 $strsth .= " AND $line < \'$linefilter[1]\' " ;
302 } elsif ($linefilter[0]) {
303 $strsth .= " AND $line > \'$linefilter[0]\' " ;
306 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
309 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
311 } elsif ($linefilter[0]) {
312 $linefilter[0] =~ s/\*/%/g;
313 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
315 $strsth .=" GROUP BY $linefield";
316 $strsth .=" ORDER BY $lineorder";
318 my $sth = $dbh->prepare( $strsth );
322 while ( my ($celvalue) = $sth->fetchrow) {
325 $cell{rowtitle
} = $celvalue;
327 $cell{rowtitle
} = "";
330 push @loopline, \
%cell;
336 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
338 $colfield .="dayname($column)";
339 $colorder .="weekday($column)";
340 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
342 $colfield .="monthname($column)";
343 $colorder .="month($column)";
344 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
346 $colfield .="Year($column)";
347 $colorder .= $column;
348 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
349 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
350 $colorder .= $column;
352 $colfield .= $column;
353 $colorder .= $column;
357 $strsth2 .= "SELECT distinctrow $colfield
359 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
360 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
361 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
362 LEFT JOIN issuingrules ON
363 (issuingrules.branchcode=old_issues.branchcode
364 AND issuingrules.itemtype=biblioitems.itemtype
365 AND issuingrules.categorycode=borrowers.categorycode)
368 if (($column=~/timestamp/) or ($column=~/returndate/)){
369 if ($colfilter[1] and ($colfilter[0])){
370 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
371 } elsif ($colfilter[1]) {
372 $strsth2 .= " AND $column < '$colfilter[1]' " ;
373 } elsif ($colfilter[0]) {
374 $strsth2 .= " AND $column > '$colfilter[0]' " ;
377 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
380 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
382 } elsif ($colfilter[0]) {
383 $colfilter[0] =~ s/\*/%/g;
384 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
386 $strsth2 .=" GROUP BY $colfield";
387 $strsth2 .=" ORDER BY $colorder";
389 my $sth2 = $dbh->prepare( $strsth2 );
390 if (( @colfilter ) and ($colfilter[1])){
391 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
392 } elsif ($colfilter[0]) {
393 $sth2->execute($colfilter[0]);
399 while (my ($celvalue) = $sth2->fetchrow) {
402 # warn "coltitle :".$celvalue;
403 $cell{coltitle
} = $celvalue;
405 push @loopcol, \
%cell;
407 # warn "fin des titres colonnes";
413 #Initialization of cell values.....
419 foreach my $row ( @loopline ) {
420 foreach my $col ( @loopcol ) {
421 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
422 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
424 $table{$row->{rowtitle
}}->{totalrow
}=0;
427 # preparing calculation
430 # Processing average loanperiods
431 $strcalc .= "SELECT $linefield, $colfield, ";
432 $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";
434 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
435 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
436 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
437 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
438 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
439 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
440 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
441 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
442 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
443 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
444 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
445 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
446 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
447 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
448 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
449 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
450 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
451 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
452 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
453 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
454 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
455 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
457 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
459 my $dbcalc = $dbh->prepare($strcalc);
461 # warn "filling table";
470 while (my @data = $dbcalc->fetchrow) {
471 my ($row, $col, $issuedate, $returndate, $weight)=@data;
472 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
473 $emptycol=1 if (!defined($col));
474 $col = "zzEMPTY" if (!defined($col));
475 $row = "zzEMPTY" if (!defined($row));
476 # fill returndate to avoid an error with date calc (needed for all non returned issues)
477 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
478 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
479 # and seconds between the two
480 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
481 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
482 # warn "513 row :".$row." column :".$col;
483 $table{$row}->{$col}+=$weight*$loanlength;
484 # $table{$row}->{totalrow}+=$weight*$loanlength;
485 $cnttable{$row}->{$col}= 1;
486 $wgttable{$row}->{$col}+=$weight;
489 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
491 foreach my $row ( sort keys %table ) {
493 #@loopcol ensures the order for columns is common with column titles
494 # and the number matches the number of columns
496 foreach my $col ( @loopcol ) {
499 ( ( $col->{coltitle
} eq 'NULL' )
500 or ( $col->{coltitle
} eq q{} )
501 ) ?
'zzEMPTY' : $col->{coltitle
}
504 $value = $table{$row}->{
505 ( ( $col->{coltitle
} eq 'NULL' )
506 or ( $col->{coltitle
} eq q{} )
507 ) ?
'zzEMPTY' : $col->{coltitle
}
508 } / $wgttable{$row}->{
509 ( ( $col->{coltitle
} eq 'NULL' )
510 or ( $col->{coltitle
} eq q{} )
511 ) ?
'zzEMPTY' : $col->{coltitle
}
514 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
515 $table{$row}->{totalrow
}+=$value;
516 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
517 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
518 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
520 #warn "row : $row colcount:$colcount";
522 if ( $colcount > 0 ) {
523 $total = $table{$row}->{totalrow
} / $colcount;
526 { 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
527 'loopcell' => \
@loopcell,
528 'hilighted' => ( $hilighted > 0 ),
529 'totalrow' => ($total) ?
sprintf( "%.2f", $total ) : 0
531 $hilighted = -$hilighted;
534 # # warn "footer processing";
535 foreach my $col ( @loopcol ) {
538 foreach my $row ( @looprow ) {
539 $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
}};
540 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
541 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
543 # warn "summ for column ".$col->{coltitle}." = ".$total;
544 $total = $total/$nbrow if ($nbrow);
545 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
550 # the header of the table
551 $globalline{loopfilter
}=\
@loopfilter;
552 # the core of the table
553 $globalline{looprow
} = \
@looprow;
554 $globalline{loopcol
} = \
@loopcol;
555 # # the foot (totals by borrower type)
556 $globalline{loopfooter
} = \
@loopfooter;
557 $globalline{total
}= $grantotal;
558 $globalline{line
} = $line;
559 $globalline{column
} = $column;
560 push @mainloop,\
%globalline;