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 Koha::Patron::Categories;
33 use Date::Calc qw(Delta_Days);
37 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_avg_stats.tt";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->multi_param("Filter");
49 $filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
51 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
53 $filters[2] = eval { output_pref( { dt => dt_from_string( $filters[2]), dateonly => 1, dateformat => 'iso' } ); }
55 $filters[3] = eval { output_pref( { dt => dt_from_string( $filters[3]), dateonly => 1, dateformat => 'iso' } ); }
59 my $podsp = $input->param("IssueDisplay");
60 my $rodsp = $input->param("ReturnDisplay");
61 my $calc = $input->param("Cellvalue");
62 my $output = $input->param("output");
63 my $basename = $input->param("basename");
64 #warn "calcul : ".$calc;
65 my ($template, $borrowernumber, $cookie)
66 = get_template_and_user({template_name => $fullreportname,
70 flagsrequired => {reports => '*'},
73 our $sep = $input->param("sep");
74 $sep = "\t" if ($sep eq 'tabulation');
75 $template->param(do_it => $do_it,
79 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
80 if ($output eq "screen"){
81 # Printing results to screen
82 $template->param(mainloop => $results);
83 output_html_with_http_headers $input, $cookie, $template->output;
86 # Printing to a csv file
87 print $input->header(-type => 'application/vnd.sun.xml.calc',
89 -attachment=>"$basename.csv",
90 -filename=>"$basename.csv" );
91 my $cols = @$results[0]->{loopcol};
92 my $lines = @$results[0]->{looprow};
94 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
96 foreach my $col ( @$cols ) {
97 print $col->{coltitle}.$sep;
101 foreach my $line ( @$lines ) {
102 my $x = $line->{loopcell};
103 print $line->{rowtitle}.$sep;
104 foreach my $cell (@$x) {
105 print $cell->{value}.$sep;
107 print $line->{totalrow};
112 $cols = @$results[0]->{loopfooter};
113 foreach my $col ( @$cols ) {
114 print $sep.$col->{totalcol};
116 print $sep.@$results[0]->{total};
121 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
123 my $itemtypes = GetItemTypes( style => 'array' );
125 my $dbh = C4::Context->dbh;
126 my $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
130 while (my ($value) =$req->fetchrow) {
131 $hassort1 =1 if ($value);
132 push @selects1, $value;
135 values => \@selects1,
138 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
143 while (my ($value) =$req->fetchrow) {
144 $hassort2 =1 if ($value);
145 $hglghtsort2= !($hassort1);
146 push @selects2, $value;
149 values => \@selects2,
152 my $CGIsepChoice=GetDelimiterChoices;
155 patron_categories => $patron_categories,
156 itemtypes => $itemtypes,
157 branchloop => GetBranchesLoop(),
158 hassort1 => $hassort1,
159 hassort2 => $hassort2,
160 HlghtSort2 => $hglghtsort2,
163 CGIsepChoice => $CGIsepChoice
165 output_html_with_http_headers $input, $cookie, $template->output;
172 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
181 my $dbh = C4::Context->dbh;
187 for (my $i=0;$i<=6;$i++) {
189 if ( @$filters[$i] ) {
190 if (($i==1) and (@$filters[$i-1])) {
191 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
193 # format the dates filters, otherwise just fill as is
195 $cell{filter} .= @$filters[$i];
197 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
198 if ( @$filters[$i] );
200 $cell{crit} .="Issue From" if ($i==0);
201 $cell{crit} .="Issue To" if ($i==1);
202 $cell{crit} .="Issue Month" if ($i==2);
203 $cell{crit} .="Issue Day" if ($i==3);
204 $cell{crit} .="Return From" if ($i==4);
205 $cell{crit} .="Return To" if ($i==5);
206 $cell{crit} .="Return Month" if ($i==6);
207 $cell{crit} .="Return Day" if ($i==7);
208 $cell{crit} .="Borrower Cat" if ($i==8);
209 $cell{crit} .="Doc Type" if ($i==9);
210 $cell{crit} .="Branch" if ($i==10);
211 $cell{crit} .="Sort1" if ($i==11);
212 $cell{crit} .="Sort2" if ($i==12);
213 push @loopfilter, \%cell;
216 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
217 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
222 # warn "filtres ".@filters[0];
223 # warn "filtres ".@filters[1];
224 # warn "filtres ".@filters[2];
225 # warn "filtres ".@filters[3];
226 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
227 $line = "biblioitems.".$line if $line=~/itemtype/;
229 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
230 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
231 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
232 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
233 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
234 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
235 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
236 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
237 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
238 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
239 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
240 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
241 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
242 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
243 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
245 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
246 $column = "biblioitems.".$column if $column=~/itemtype/;
248 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
249 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
250 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
251 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
252 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
253 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
254 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
255 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
256 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
257 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
258 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
259 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
260 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
261 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
262 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
267 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
269 $linefield .="dayname($line)";
270 $lineorder .="weekday($line)";
271 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
273 $linefield .="monthname($line)";
274 $lineorder .="month($line)";
275 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
277 $linefield .="Year($line)";
279 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
280 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
288 $strsth .= "select distinctrow $linefield
290 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
291 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
292 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
293 LEFT JOIN issuingrules ON
294 (issuingrules.branchcode=old_issues.branchcode
295 AND issuingrules.itemtype=biblioitems.itemtype
296 AND issuingrules.categorycode=borrowers.categorycode)
299 if (($line=~/timestamp/) or ($line=~/returndate/)){
300 if ($linefilter[1] and ($linefilter[0])){
301 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
302 } elsif ($linefilter[1]) {
303 $strsth .= " AND $line < \'$linefilter[1]\' " ;
304 } elsif ($linefilter[0]) {
305 $strsth .= " AND $line > \'$linefilter[0]\' " ;
308 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
311 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
313 } elsif ($linefilter[0]) {
314 $linefilter[0] =~ s/\*/%/g;
315 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
317 $strsth .=" GROUP BY $linefield";
318 $strsth .=" ORDER BY $lineorder";
320 my $sth = $dbh->prepare( $strsth );
324 while ( my ($celvalue) = $sth->fetchrow) {
327 $cell{rowtitle} = $celvalue;
329 $cell{rowtitle} = "";
332 push @loopline, \%cell;
338 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
340 $colfield .="dayname($column)";
341 $colorder .="weekday($column)";
342 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
344 $colfield .="monthname($column)";
345 $colorder .="month($column)";
346 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
348 $colfield .="Year($column)";
349 $colorder .= $column;
350 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
351 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
352 $colorder .= $column;
354 $colfield .= $column;
355 $colorder .= $column;
359 $strsth2 .= "SELECT distinctrow $colfield
361 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
362 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
363 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
364 LEFT JOIN issuingrules ON
365 (issuingrules.branchcode=old_issues.branchcode
366 AND issuingrules.itemtype=biblioitems.itemtype
367 AND issuingrules.categorycode=borrowers.categorycode)
370 if (($column=~/timestamp/) or ($column=~/returndate/)){
371 if ($colfilter[1] and ($colfilter[0])){
372 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
373 } elsif ($colfilter[1]) {
374 $strsth2 .= " AND $column < '$colfilter[1]' " ;
375 } elsif ($colfilter[0]) {
376 $strsth2 .= " AND $column > '$colfilter[0]' " ;
379 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
382 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
384 } elsif ($colfilter[0]) {
385 $colfilter[0] =~ s/\*/%/g;
386 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
388 $strsth2 .=" GROUP BY $colfield";
389 $strsth2 .=" ORDER BY $colorder";
391 my $sth2 = $dbh->prepare( $strsth2 );
392 if (( @colfilter ) and ($colfilter[1])){
393 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
394 } elsif ($colfilter[0]) {
395 $sth2->execute($colfilter[0]);
401 while (my ($celvalue) = $sth2->fetchrow) {
404 # warn "coltitle :".$celvalue;
405 $cell{coltitle} = $celvalue;
407 push @loopcol, \%cell;
409 # warn "fin des titres colonnes";
415 #Initialization of cell values.....
421 foreach my $row ( @loopline ) {
422 foreach my $col ( @loopcol ) {
423 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
424 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
426 $table{$row->{rowtitle}}->{totalrow}=0;
429 # preparing calculation
432 # Processing average loanperiods
433 $strcalc .= "SELECT $linefield, $colfield, ";
434 $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";
436 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
437 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
438 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
439 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
440 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
441 $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
442 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
443 $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
444 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
445 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
446 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
447 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
448 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
449 $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
450 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
451 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
452 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
453 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
454 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
455 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
456 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
457 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
459 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
461 my $dbcalc = $dbh->prepare($strcalc);
463 # warn "filling table";
472 while (my @data = $dbcalc->fetchrow) {
473 my ($row, $col, $issuedate, $returndate, $weight)=@data;
474 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
475 $emptycol=1 if (!defined($col));
476 $col = "zzEMPTY" if (!defined($col));
477 $row = "zzEMPTY" if (!defined($row));
478 # fill returndate to avoid an error with date calc (needed for all non returned issues)
479 $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
480 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
481 # and seconds between the two
482 $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
483 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
484 # warn "513 row :".$row." column :".$col;
485 $table{$row}->{$col}+=$weight*$loanlength;
486 # $table{$row}->{totalrow}+=$weight*$loanlength;
487 $cnttable{$row}->{$col}= 1;
488 $wgttable{$row}->{$col}+=$weight;
491 push @loopcol,{coltitle => "NULL"} if ($emptycol);
493 foreach my $row ( sort keys %table ) {
495 #@loopcol ensures the order for columns is common with column titles
496 # and the number matches the number of columns
498 foreach my $col ( @loopcol ) {
501 ( ( $col->{coltitle} eq 'NULL' )
502 or ( $col->{coltitle} eq q{} )
503 ) ? 'zzEMPTY' : $col->{coltitle}
506 $value = $table{$row}->{
507 ( ( $col->{coltitle} eq 'NULL' )
508 or ( $col->{coltitle} eq q{} )
509 ) ? 'zzEMPTY' : $col->{coltitle}
510 } / $wgttable{$row}->{
511 ( ( $col->{coltitle} eq 'NULL' )
512 or ( $col->{coltitle} eq q{} )
513 ) ? 'zzEMPTY' : $col->{coltitle}
516 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
517 $table{$row}->{totalrow}+=$value;
518 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
519 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
520 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
522 #warn "row : $row colcount:$colcount";
524 if ( $colcount > 0 ) {
525 $total = $table{$row}->{totalrow} / $colcount;
528 { 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
529 'loopcell' => \@loopcell,
530 'hilighted' => ( $hilighted > 0 ),
531 'totalrow' => ($total) ? sprintf( "%.2f", $total ) : 0
533 $hilighted = -$hilighted;
536 # # warn "footer processing";
537 foreach my $col ( @loopcol ) {
540 foreach my $row ( @looprow ) {
541 $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}};
542 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
543 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
545 # warn "summ for column ".$col->{coltitle}." = ".$total;
546 $total = $total/$nbrow if ($nbrow);
547 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
552 # the header of the table
553 $globalline{loopfilter}=\@loopfilter;
554 # the core of the table
555 $globalline{looprow} = \@looprow;
556 $globalline{loopcol} = \@loopcol;
557 # # the foot (totals by borrower type)
558 $globalline{loopfooter} = \@loopfooter;
559 $globalline{total}= $grantotal;
560 $globalline{line} = $line;
561 $globalline{column} = $column;
562 push @mainloop,\%globalline;