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->multi_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");
66 #warn "calcul : ".$calc;
67 my ($template, $borrowernumber, $cookie)
68 = get_template_and_user({template_name => $fullreportname,
72 flagsrequired => {reports => '*'},
75 our $sep = $input->param("sep");
76 $sep = "\t" if ($sep eq 'tabulation');
77 $template->param(do_it => $do_it,
81 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
82 if ($output eq "screen"){
83 # Printing results to screen
84 $template->param(mainloop => $results);
85 output_html_with_http_headers $input, $cookie, $template->output;
88 # Printing to a csv file
89 print $input->header(-type => 'application/vnd.sun.xml.calc',
91 -attachment=>"$basename.csv",
92 -filename=>"$basename.csv" );
93 my $cols = @$results[0]->{loopcol};
94 my $lines = @$results[0]->{looprow};
96 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
98 foreach my $col ( @$cols ) {
99 print $col->{coltitle}.$sep;
103 foreach my $line ( @$lines ) {
104 my $x = $line->{loopcell};
105 print $line->{rowtitle}.$sep;
106 foreach my $cell (@$x) {
107 print $cell->{value}.$sep;
109 print $line->{totalrow};
114 $cols = @$results[0]->{loopfooter};
115 foreach my $col ( @$cols ) {
116 print $sep.$col->{totalcol};
118 print $sep.@$results[0]->{total};
123 my $dbh = C4::Context->dbh;
126 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
130 while (my ($value, $desc) =$req->fetchrow) {
131 push @selectc, $value;
132 $labelsc{$value} = $desc;
139 my $itemtypes = GetItemTypes( style => 'array' );
141 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
145 while (my ($value) =$req->fetchrow) {
146 $hassort1 =1 if ($value);
147 push @selects1, $value;
150 values => \@selects1,
153 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
158 while (my ($value) =$req->fetchrow) {
159 $hassort2 =1 if ($value);
160 $hglghtsort2= !($hassort1);
161 push @selects2, $value;
164 values => \@selects2,
167 my $CGIsepChoice=GetDelimiterChoices;
171 itemtypes => $itemtypes,
172 branchloop => GetBranchesLoop(),
173 hassort1 => $hassort1,
174 hassort2 => $hassort2,
175 HlghtSort2 => $hglghtsort2,
178 CGIsepChoice => $CGIsepChoice
180 output_html_with_http_headers $input, $cookie, $template->output;
187 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
195 my $itype = C4::Context->preference('item-level_itypes') ? "items.itype" : "biblioitems.itemtype";
197 my $dbh = C4::Context->dbh;
203 for (my $i=0;$i<=6;$i++) {
205 if ( @$filters[$i] ) {
206 if (($i==1) and (@$filters[$i-1])) {
207 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
209 # format the dates filters, otherwise just fill as is
211 $cell{filter} .= @$filters[$i];
213 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
214 if ( @$filters[$i] );
216 $cell{crit} .="Issue From" if ($i==0);
217 $cell{crit} .="Issue To" if ($i==1);
218 $cell{crit} .="Issue Month" if ($i==2);
219 $cell{crit} .="Issue Day" if ($i==3);
220 $cell{crit} .="Return From" if ($i==4);
221 $cell{crit} .="Return To" if ($i==5);
222 $cell{crit} .="Return Month" if ($i==6);
223 $cell{crit} .="Return Day" if ($i==7);
224 $cell{crit} .="Borrower Cat" if ($i==8);
225 $cell{crit} .="Doc Type" if ($i==9);
226 $cell{crit} .="Branch" if ($i==10);
227 $cell{crit} .="Sort1" if ($i==11);
228 $cell{crit} .="Sort2" if ($i==12);
229 push @loopfilter, \%cell;
232 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
233 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
238 # warn "filtres ".@filters[0];
239 # warn "filtres ".@filters[1];
240 # warn "filtres ".@filters[2];
241 # warn "filtres ".@filters[3];
242 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
243 if ( $line=~/itemtype/ ) { $line = $itype; }
244 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
245 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
246 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
247 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
248 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
249 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
250 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
251 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
252 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
253 $linefilter[0] = @$filters[9] if ($line eq $itype);
254 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
255 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
256 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
258 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
259 if ( $column=~/itemtype/ ) { $column = $itype; }
261 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
262 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
263 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
264 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
265 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
266 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
267 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
268 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
269 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
270 $colfilter[0] = @$filters[9] if ($column eq $itype);
271 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
272 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
273 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
278 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
280 $linefield .="dayname($line)";
281 $lineorder .="weekday($line)";
282 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
284 $linefield .="monthname($line)";
285 $lineorder .="month($line)";
286 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
288 $linefield .="Year($line)";
290 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
291 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
299 $strsth .= "select distinctrow $linefield
301 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
302 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
303 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
306 if (($line=~/timestamp/) or ($line=~/returndate/)){
307 if ($linefilter[1] and ($linefilter[0])){
308 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
309 } elsif ($linefilter[1]) {
310 $strsth .= " AND $line < \'$linefilter[1]\' " ;
311 } elsif ($linefilter[0]) {
312 $strsth .= " AND $line > \'$linefilter[0]\' " ;
315 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
318 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
320 } elsif ($linefilter[0]) {
321 $linefilter[0] =~ s/\*/%/g;
322 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
324 $strsth .=" GROUP BY $linefield";
325 $strsth .=" ORDER BY $lineorder";
327 my $sth = $dbh->prepare( $strsth );
330 while ( my ($celvalue) = $sth->fetchrow) {
333 $cell{rowtitle} = $celvalue;
335 $cell{rowtitle} = "";
338 push @loopline, \%cell;
344 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
346 $colfield .="dayname($column)";
347 $colorder .="weekday($column)";
348 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
350 $colfield .="monthname($column)";
351 $colorder .="month($column)";
352 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
354 $colfield .="Year($column)";
355 $colorder .= $column;
356 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
357 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
358 $colorder .= $column;
360 $colfield .= $column;
361 $colorder .= $column;
365 $strsth2 .= "SELECT distinctrow $colfield
367 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
368 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
369 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
372 if (($column=~/timestamp/) or ($column=~/returndate/)){
373 if ($colfilter[1] and ($colfilter[0])){
374 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
375 } elsif ($colfilter[1]) {
376 $strsth2 .= " AND $column < '$colfilter[1]' " ;
377 } elsif ($colfilter[0]) {
378 $strsth2 .= " AND $column > '$colfilter[0]' " ;
381 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
384 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
386 } elsif ($colfilter[0]) {
387 $colfilter[0] =~ s/\*/%/g;
388 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
390 $strsth2 .=" GROUP BY $colfield";
391 $strsth2 .=" ORDER BY $colorder";
393 my $sth2 = $dbh->prepare( $strsth2 );
397 while (my ($celvalue) = $sth2->fetchrow) {
400 # warn "coltitle :".$celvalue;
401 $cell{coltitle} = $celvalue;
403 push @loopcol, \%cell;
405 # warn "fin des titres colonnes";
411 #Initialization of cell values.....
417 foreach my $row ( @loopline ) {
418 foreach my $col ( @loopcol ) {
419 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
420 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
422 $table{$row->{rowtitle}}->{totalrow}=0;
425 # preparing calculation
428 # Processing average loanperiods
429 $strcalc .= "SELECT $linefield, $colfield, ";
430 $strcalc .= " issuedate, returndate, COUNT(*) FROM `old_issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE old_issues.itemnumber=items.itemnumber AND old_issues.borrowernumber=borrowers.borrowernumber";
432 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
433 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
434 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
435 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
436 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
437 $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
438 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
439 $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
440 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
441 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
442 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
443 $strcalc .= " AND $itype like '" . @$filters[9] ."'" if ( @$filters[9] );
444 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
445 $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
446 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
447 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
448 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
449 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
450 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
451 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
452 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
453 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
455 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
457 my $dbcalc = $dbh->prepare($strcalc);
459 # warn "filling table";
468 while (my @data = $dbcalc->fetchrow) {
469 my ($row, $col, $issuedate, $returndate, $weight)=@data;
470 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
471 $emptycol=1 if (!defined($col));
472 $col = "zzEMPTY" if (!defined($col));
473 $row = "zzEMPTY" if (!defined($row));
474 # fill returndate to avoid an error with date calc (needed for all non returned issues)
475 $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
476 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
477 # and seconds between the two
478 $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
479 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
480 # warn "513 row :".$row." column :".$col;
481 $table{$row}->{$col}+=$weight*$loanlength;
482 # $table{$row}->{totalrow}+=$weight*$loanlength;
483 $cnttable{$row}->{$col}= 1;
484 $wgttable{$row}->{$col}+=$weight;
487 push @loopcol,{coltitle => "NULL"} if ($emptycol);
489 foreach my $row ( sort keys %table ) {
491 #@loopcol ensures the order for columns is common with column titles
492 # and the number matches the number of columns
494 foreach my $col ( @loopcol ) {
497 ( ( $col->{coltitle} eq 'NULL' )
498 or ( $col->{coltitle} eq q{} )
499 ) ? 'zzEMPTY' : $col->{coltitle}
502 $value = $table{$row}->{
503 ( ( $col->{coltitle} eq 'NULL' )
504 or ( $col->{coltitle} eq q{} )
505 ) ? 'zzEMPTY' : $col->{coltitle}
506 } / $wgttable{$row}->{
507 ( ( $col->{coltitle} eq 'NULL' )
508 or ( $col->{coltitle} eq q{} )
509 ) ? 'zzEMPTY' : $col->{coltitle}
512 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
513 $table{$row}->{totalrow}+=$value;
514 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
515 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
516 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
518 #warn "row : $row colcount:$colcount";
520 if ( $colcount > 0 ) {
521 $total = $table{$row}->{totalrow} / $colcount;
524 { 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
525 'loopcell' => \@loopcell,
526 'hilighted' => ( $hilighted > 0 ),
527 'totalrow' => ($total) ? sprintf( "%.2f", $total ) : 0
529 $hilighted = -$hilighted;
532 # # warn "footer processing";
533 foreach my $col ( @loopcol ) {
536 foreach my $row ( @looprow ) {
537 $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}};
538 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
539 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
541 # warn "summ for column ".$col->{coltitle}." = ".$total;
542 $total = $total/$nbrow if ($nbrow);
543 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
548 # the header of the table
549 $globalline{loopfilter}=\@loopfilter;
550 # the core of the table
551 $globalline{looprow} = \@looprow;
552 $globalline{loopcol} = \@loopcol;
553 # # the foot (totals by borrower type)
554 $globalline{loopfooter} = \@loopfooter;
555 $globalline{total}= $grantotal;
556 $globalline{line} = $line;
557 $globalline{column} = $column;
558 push @mainloop,\%globalline;