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
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] = eval { output_pref
( { dt
=> dt_from_string
( $filters[0]), dateonly
=> 1, dateformat
=> 'iso' } ); }
52 $filters[1] = eval { output_pref
( { dt
=> dt_from_string
( $filters[1]), dateonly
=> 1, dateformat
=> 'iso' } ); }
54 $filters[2] = eval { output_pref
( { dt
=> dt_from_string
( $filters[2]), dateonly
=> 1, dateformat
=> 'iso' } ); }
56 $filters[3] = eval { output_pref
( { dt
=> dt_from_string
( $filters[3]), dateonly
=> 1, dateformat
=> 'iso' } ); }
60 my $podsp = $input->param("IssueDisplay");
61 my $rodsp = $input->param("ReturnDisplay");
62 my $calc = $input->param("Cellvalue");
63 my $output = $input->param("output");
64 my $basename = $input->param("basename");
65 #warn "calcul : ".$calc;
66 my ($template, $borrowernumber, $cookie)
67 = get_template_and_user
({template_name
=> $fullreportname,
71 flagsrequired
=> {reports
=> '*'},
74 our $sep = $input->param("sep");
75 $sep = "\t" if ($sep eq 'tabulation');
76 $template->param(do_it
=> $do_it,
80 my $results = calculate
($line, $column, $rodsp, $podsp, $calc, \
@filters);
81 if ($output eq "screen"){
82 # Printing results to screen
83 $template->param(mainloop
=> $results);
84 output_html_with_http_headers
$input, $cookie, $template->output;
87 # Printing to a csv file
88 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
90 -attachment
=>"$basename.csv",
91 -filename
=>"$basename.csv" );
92 my $cols = @
$results[0]->{loopcol
};
93 my $lines = @
$results[0]->{looprow
};
95 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
97 foreach my $col ( @
$cols ) {
98 print $col->{coltitle
}.$sep;
102 foreach my $line ( @
$lines ) {
103 my $x = $line->{loopcell
};
104 print $line->{rowtitle
}.$sep;
105 foreach my $cell (@
$x) {
106 print $cell->{value
}.$sep;
108 print $line->{totalrow
};
113 $cols = @
$results[0]->{loopfooter
};
114 foreach my $col ( @
$cols ) {
115 print $sep.$col->{totalcol
};
117 print $sep.@
$results[0]->{total
};
122 my $dbh = C4
::Context
->dbh;
125 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
129 while (my ($value, $desc) =$req->fetchrow) {
130 push @selectc, $value;
131 $labelsc{$value} = $desc;
138 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
142 while (my ($value,$desc) =$req->fetchrow) {
143 push @selecti, $value;
144 $labelsi{$value}=$desc;
151 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
155 while (my ($value) =$req->fetchrow) {
156 $hassort1 =1 if ($value);
157 push @selects1, $value;
160 values => \
@selects1,
163 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
168 while (my ($value) =$req->fetchrow) {
169 $hassort2 =1 if ($value);
170 $hglghtsort2= !($hassort1);
171 push @selects2, $value;
174 values => \
@selects2,
177 my $CGIsepChoice=GetDelimiterChoices
;
181 ItemType
=> $ItemTypes,
182 branchloop
=> GetBranchesLoop
(),
183 hassort1
=> $hassort1,
184 hassort2
=> $hassort2,
185 HlghtSort2
=> $hglghtsort2,
188 CGIsepChoice
=> $CGIsepChoice
190 output_html_with_http_headers
$input, $cookie, $template->output;
197 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
206 my $dbh = C4
::Context
->dbh;
212 for (my $i=0;$i<=6;$i++) {
214 if ( @
$filters[$i] ) {
215 if (($i==1) and (@
$filters[$i-1])) {
216 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
218 # format the dates filters, otherwise just fill as is
220 $cell{filter
} .= @
$filters[$i];
222 $cell{filter
} .= eval { output_pref
( { dt
=> dt_from_string
( @
$filters[$i] ), dateonly
=> 1 }); }
223 if ( @
$filters[$i] );
225 $cell{crit
} .="Issue From" if ($i==0);
226 $cell{crit
} .="Issue To" if ($i==1);
227 $cell{crit
} .="Issue Month" if ($i==2);
228 $cell{crit
} .="Issue Day" if ($i==3);
229 $cell{crit
} .="Return From" if ($i==4);
230 $cell{crit
} .="Return To" if ($i==5);
231 $cell{crit
} .="Return Month" if ($i==6);
232 $cell{crit
} .="Return Day" if ($i==7);
233 $cell{crit
} .="Borrower Cat" if ($i==8);
234 $cell{crit
} .="Doc Type" if ($i==9);
235 $cell{crit
} .="Branch" if ($i==10);
236 $cell{crit
} .="Sort1" if ($i==11);
237 $cell{crit
} .="Sort2" if ($i==12);
238 push @loopfilter, \
%cell;
241 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
242 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
247 # warn "filtres ".@filters[0];
248 # warn "filtres ".@filters[1];
249 # warn "filtres ".@filters[2];
250 # warn "filtres ".@filters[3];
251 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
252 $line = "biblioitems.".$line if $line=~/itemtype/;
254 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
255 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
256 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
257 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
258 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
259 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
260 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
261 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
262 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
263 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
264 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
265 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
266 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
267 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
268 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
270 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
271 $column = "biblioitems.".$column if $column=~/itemtype/;
273 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
274 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
275 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
276 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
277 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
278 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
279 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
280 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
281 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
282 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
283 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
284 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
285 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
286 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
287 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
292 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
294 $linefield .="dayname($line)";
295 $lineorder .="weekday($line)";
296 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
298 $linefield .="monthname($line)";
299 $lineorder .="month($line)";
300 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
302 $linefield .="Year($line)";
304 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
305 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
313 $strsth .= "select distinctrow $linefield
315 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
316 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
317 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
318 LEFT JOIN issuingrules ON
319 (issuingrules.branchcode=old_issues.branchcode
320 AND issuingrules.itemtype=biblioitems.itemtype
321 AND issuingrules.categorycode=borrowers.categorycode)
324 if (($line=~/timestamp/) or ($line=~/returndate/)){
325 if ($linefilter[1] and ($linefilter[0])){
326 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
327 } elsif ($linefilter[1]) {
328 $strsth .= " AND $line < \'$linefilter[1]\' " ;
329 } elsif ($linefilter[0]) {
330 $strsth .= " AND $line > \'$linefilter[0]\' " ;
333 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
336 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
338 } elsif ($linefilter[0]) {
339 $linefilter[0] =~ s/\*/%/g;
340 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
342 $strsth .=" GROUP BY $linefield";
343 $strsth .=" ORDER BY $lineorder";
345 my $sth = $dbh->prepare( $strsth );
349 while ( my ($celvalue) = $sth->fetchrow) {
352 $cell{rowtitle
} = $celvalue;
354 $cell{rowtitle
} = "";
357 push @loopline, \
%cell;
363 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
365 $colfield .="dayname($column)";
366 $colorder .="weekday($column)";
367 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
369 $colfield .="monthname($column)";
370 $colorder .="month($column)";
371 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
373 $colfield .="Year($column)";
374 $colorder .= $column;
375 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
376 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
377 $colorder .= $column;
379 $colfield .= $column;
380 $colorder .= $column;
384 $strsth2 .= "SELECT distinctrow $colfield
386 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
387 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
388 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
389 LEFT JOIN issuingrules ON
390 (issuingrules.branchcode=old_issues.branchcode
391 AND issuingrules.itemtype=biblioitems.itemtype
392 AND issuingrules.categorycode=borrowers.categorycode)
395 if (($column=~/timestamp/) or ($column=~/returndate/)){
396 if ($colfilter[1] and ($colfilter[0])){
397 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
398 } elsif ($colfilter[1]) {
399 $strsth2 .= " AND $column < '$colfilter[1]' " ;
400 } elsif ($colfilter[0]) {
401 $strsth2 .= " AND $column > '$colfilter[0]' " ;
404 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
407 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
409 } elsif ($colfilter[0]) {
410 $colfilter[0] =~ s/\*/%/g;
411 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
413 $strsth2 .=" GROUP BY $colfield";
414 $strsth2 .=" ORDER BY $colorder";
416 my $sth2 = $dbh->prepare( $strsth2 );
417 if (( @colfilter ) and ($colfilter[1])){
418 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
419 } elsif ($colfilter[0]) {
420 $sth2->execute($colfilter[0]);
426 while (my ($celvalue) = $sth2->fetchrow) {
429 # warn "coltitle :".$celvalue;
430 $cell{coltitle
} = $celvalue;
432 push @loopcol, \
%cell;
434 # warn "fin des titres colonnes";
440 #Initialization of cell values.....
446 foreach my $row ( @loopline ) {
447 foreach my $col ( @loopcol ) {
448 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
449 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
451 $table{$row->{rowtitle
}}->{totalrow
}=0;
454 # preparing calculation
457 # Processing average loanperiods
458 $strcalc .= "SELECT $linefield, $colfield, ";
459 $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";
461 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
462 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
463 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
464 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
465 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
466 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
467 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
468 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
469 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
470 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
471 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
472 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
473 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
474 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
475 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
476 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
477 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
478 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
479 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
480 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
481 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
482 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
484 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
486 my $dbcalc = $dbh->prepare($strcalc);
488 # warn "filling table";
497 while (my @data = $dbcalc->fetchrow) {
498 my ($row, $col, $issuedate, $returndate, $weight)=@data;
499 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
500 $emptycol=1 if (!defined($col));
501 $col = "zzEMPTY" if (!defined($col));
502 $row = "zzEMPTY" if (!defined($row));
503 # fill returndate to avoid an error with date calc (needed for all non returned issues)
504 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
505 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
506 # and seconds between the two
507 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
508 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
509 # warn "513 row :".$row." column :".$col;
510 $table{$row}->{$col}+=$weight*$loanlength;
511 # $table{$row}->{totalrow}+=$weight*$loanlength;
512 $cnttable{$row}->{$col}= 1;
513 $wgttable{$row}->{$col}+=$weight;
516 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
518 foreach my $row ( sort keys %table ) {
520 #@loopcol ensures the order for columns is common with column titles
521 # and the number matches the number of columns
523 foreach my $col ( @loopcol ) {
526 ( ( $col->{coltitle
} eq 'NULL' )
527 or ( $col->{coltitle
} eq q{} )
528 ) ?
'zzEMPTY' : $col->{coltitle
}
531 $value = $table{$row}->{
532 ( ( $col->{coltitle
} eq 'NULL' )
533 or ( $col->{coltitle
} eq q{} )
534 ) ?
'zzEMPTY' : $col->{coltitle
}
535 } / $wgttable{$row}->{
536 ( ( $col->{coltitle
} eq 'NULL' )
537 or ( $col->{coltitle
} eq q{} )
538 ) ?
'zzEMPTY' : $col->{coltitle
}
541 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
542 $table{$row}->{totalrow
}+=$value;
543 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
544 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
545 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
547 #warn "row : $row colcount:$colcount";
549 if ( $colcount > 0 ) {
550 $total = $table{$row}->{totalrow
} / $colcount;
553 { 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
554 'loopcell' => \
@loopcell,
555 'hilighted' => ( $hilighted > 0 ),
556 'totalrow' => ($total) ?
sprintf( "%.2f", $total ) : 0
558 $hilighted = -$hilighted;
561 # # warn "footer processing";
562 foreach my $col ( @loopcol ) {
565 foreach my $row ( @looprow ) {
566 $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
}};
567 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
568 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
570 # warn "summ for column ".$col->{coltitle}." = ".$total;
571 $total = $total/$nbrow if ($nbrow);
572 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
577 # the header of the table
578 $globalline{loopfilter
}=\
@loopfilter;
579 # the core of the table
580 $globalline{looprow
} = \
@looprow;
581 $globalline{loopcol
} = \
@loopcol;
582 # # the foot (totals by borrower type)
583 $globalline{loopfooter
} = \
@loopfooter;
584 $globalline{total
}= $grantotal;
585 $globalline{line
} = $line;
586 $globalline{column
} = $column;
587 push @mainloop,\
%globalline;