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 C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Reports qw( GetDelimiterChoices );
27 use Koha::DateUtils qw( dt_from_string output_pref );
29 use Koha::Patron::Categories;
30 use Date::Calc qw( Delta_Days );
34 plugin that shows a stats on borrowers
41 my $do_it=$input->param('do_it');
42 my $fullreportname = "reports/issues_avg_stats.tt";
43 my $line = $input->param("Line");
44 my $column = $input->param("Column");
45 my @filters = $input->multi_param("Filter");
46 $filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
48 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
50 $filters[2] = eval { output_pref( { dt => dt_from_string( $filters[2]), dateonly => 1, dateformat => 'iso' } ); }
52 $filters[3] = eval { output_pref( { dt => dt_from_string( $filters[3]), dateonly => 1, dateformat => 'iso' } ); }
56 my $podsp = $input->param("IssueDisplay");
57 my $rodsp = $input->param("ReturnDisplay");
58 my $calc = $input->param("Cellvalue");
59 my $output = $input->param("output");
60 my $basename = $input->param("basename");
62 #warn "calcul : ".$calc;
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => $fullreportname,
67 flagsrequired => {reports => '*'},
69 our $sep = $input->param("sep");
70 $sep = "\t" if ($sep eq 'tabulation');
71 $template->param(do_it => $do_it,
75 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
76 if ($output eq "screen"){
77 # Printing results to screen
78 $template->param(mainloop => $results);
79 output_html_with_http_headers $input, $cookie, $template->output;
82 # Printing to a csv file
83 print $input->header(-type => 'application/vnd.sun.xml.calc',
85 -attachment=>"$basename.csv",
86 -filename=>"$basename.csv" );
87 my $cols = @$results[0]->{loopcol};
88 my $lines = @$results[0]->{looprow};
90 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
92 foreach my $col ( @$cols ) {
93 print $col->{coltitle}.$sep;
97 foreach my $line ( @$lines ) {
98 my $x = $line->{loopcell};
99 print $line->{rowtitle}.$sep;
100 foreach my $cell (@$x) {
101 print $cell->{value}.$sep;
103 print $line->{totalrow};
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col ( @$cols ) {
110 print $sep.$col->{totalcol};
112 print $sep.@$results[0]->{total};
117 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
119 my $itemtypes = Koha::ItemTypes->search_with_localization;
121 my $dbh = C4::Context->dbh;
122 my $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
126 while (my ($value) =$req->fetchrow) {
127 $hassort1 =1 if ($value);
128 push @selects1, $value;
131 values => \@selects1,
134 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
139 while (my ($value) =$req->fetchrow) {
140 $hassort2 =1 if ($value);
141 $hglghtsort2= !($hassort1);
142 push @selects2, $value;
145 values => \@selects2,
148 my $CGIsepChoice=GetDelimiterChoices;
151 patron_categories => $patron_categories,
152 itemtypes => $itemtypes,
153 hassort1 => $hassort1,
154 hassort2 => $hassort2,
155 HlghtSort2 => $hglghtsort2,
158 CGIsepChoice => $CGIsepChoice
160 output_html_with_http_headers $input, $cookie, $template->output;
167 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
175 my $itype = C4::Context->preference('item-level_itypes') ? "items.itype" : "biblioitems.itemtype";
177 my $dbh = C4::Context->dbh;
183 for (my $i=0;$i<=6;$i++) {
185 if ( @$filters[$i] ) {
186 if (($i==1) and (@$filters[$i-1])) {
187 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
189 # format the dates filters, otherwise just fill as is
191 $cell{filter} .= @$filters[$i];
193 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
194 if ( @$filters[$i] );
196 $cell{crit} .="Issue From" if ($i==0);
197 $cell{crit} .="Issue To" if ($i==1);
198 $cell{crit} .="Issue Month" if ($i==2);
199 $cell{crit} .="Issue Day" if ($i==3);
200 $cell{crit} .="Return From" if ($i==4);
201 $cell{crit} .="Return To" if ($i==5);
202 $cell{crit} .="Return Month" if ($i==6);
203 $cell{crit} .="Return Day" if ($i==7);
204 $cell{crit} .="Borrower Cat" if ($i==8);
205 $cell{crit} .="Doc Type" if ($i==9);
206 $cell{crit} .="Branch" if ($i==10);
207 $cell{crit} .="Sort1" if ($i==11);
208 $cell{crit} .="Sort2" if ($i==12);
209 push @loopfilter, \%cell;
212 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
213 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
218 # warn "filtres ".@filters[0];
219 # warn "filtres ".@filters[1];
220 # warn "filtres ".@filters[2];
221 # warn "filtres ".@filters[3];
222 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
223 if ( $line=~/itemtype/ ) { $line = $itype; }
224 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
225 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
226 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
227 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
228 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
229 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
230 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
231 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
232 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
233 $linefilter[0] = @$filters[9] if ($line eq $itype);
234 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
235 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
236 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
238 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
239 if ( $column=~/itemtype/ ) { $column = $itype; }
241 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
242 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
243 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
244 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
246 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
247 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
248 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
249 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
250 $colfilter[0] = @$filters[9] if ($column eq $itype);
251 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
252 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
253 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
258 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
260 $linefield .="dayname($line)";
261 $lineorder .="weekday($line)";
262 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
264 $linefield .="monthname($line)";
265 $lineorder .="month($line)";
266 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
268 $linefield .="Year($line)";
270 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
271 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
279 $strsth .= "select distinctrow $linefield
281 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
282 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
283 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
286 if (($line=~/timestamp/) or ($line=~/returndate/)){
287 if ($linefilter[1] and ($linefilter[0])){
288 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
289 } elsif ($linefilter[1]) {
290 $strsth .= " AND $line < \'$linefilter[1]\' " ;
291 } elsif ($linefilter[0]) {
292 $strsth .= " AND $line > \'$linefilter[0]\' " ;
295 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
298 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
300 } elsif ($linefilter[0]) {
301 $linefilter[0] =~ s/\*/%/g;
302 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
304 $strsth .=" GROUP BY $linefield";
305 $strsth .=" ORDER BY $lineorder";
307 my $sth = $dbh->prepare( $strsth );
310 while ( my ($celvalue) = $sth->fetchrow) {
313 $cell{rowtitle} = $celvalue;
315 $cell{rowtitle} = "";
318 push @loopline, \%cell;
324 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
326 $colfield .="dayname($column)";
327 $colorder .="weekday($column)";
328 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
330 $colfield .="monthname($column)";
331 $colorder .="month($column)";
332 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
334 $colfield .="Year($column)";
335 $colorder .= $column;
336 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
337 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
338 $colorder .= $column;
340 $colfield .= $column;
341 $colorder .= $column;
345 $strsth2 .= "SELECT distinctrow $colfield
347 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
348 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
349 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
352 if (($column=~/timestamp/) or ($column=~/returndate/)){
353 if ($colfilter[1] and ($colfilter[0])){
354 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
355 } elsif ($colfilter[1]) {
356 $strsth2 .= " AND $column < '$colfilter[1]' " ;
357 } elsif ($colfilter[0]) {
358 $strsth2 .= " AND $column > '$colfilter[0]' " ;
361 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
364 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
366 } elsif ($colfilter[0]) {
367 $colfilter[0] =~ s/\*/%/g;
368 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
370 $strsth2 .=" GROUP BY $colfield";
371 $strsth2 .=" ORDER BY $colorder";
373 my $sth2 = $dbh->prepare( $strsth2 );
377 while (my ($celvalue) = $sth2->fetchrow) {
380 # warn "coltitle :".$celvalue;
381 $cell{coltitle} = $celvalue;
383 push @loopcol, \%cell;
385 # warn "fin des titres colonnes";
390 #Initialization of cell values.....
396 foreach my $row ( @loopline ) {
397 foreach my $col ( @loopcol ) {
398 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
399 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
401 $table{$row->{rowtitle}}->{totalrow}=0;
404 # preparing calculation
407 # Processing average loanperiods
408 $strcalc .= "SELECT $linefield, $colfield, ";
409 $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";
411 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
412 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
413 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
414 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
415 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
416 $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
417 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
418 $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
419 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
420 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
421 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
422 $strcalc .= " AND $itype like '" . @$filters[9] ."'" if ( @$filters[9] );
423 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
424 $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
425 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
426 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
427 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
428 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
429 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
430 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
431 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
432 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
434 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
436 my $dbcalc = $dbh->prepare($strcalc);
438 # warn "filling table";
443 while (my @data = $dbcalc->fetchrow) {
444 my ($row, $col, $issuedate, $returndate, $weight)=@data;
445 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
446 $emptycol=1 if (!defined($col));
447 $col = "zzEMPTY" if (!defined($col));
448 $row = "zzEMPTY" if (!defined($row));
449 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
450 # and seconds between the two
451 $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
452 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
453 # warn "513 row :".$row." column :".$col;
454 $table{$row}->{$col}+=$weight*$loanlength;
455 # $table{$row}->{totalrow}+=$weight*$loanlength;
456 $cnttable{$row}->{$col}= 1;
457 $wgttable{$row}->{$col}+=$weight;
460 push @loopcol,{coltitle => "NULL"} if ($emptycol);
462 foreach my $row ( sort keys %table ) {
464 #@loopcol ensures the order for columns is common with column titles
465 # and the number matches the number of columns
467 foreach my $col ( @loopcol ) {
470 ( ( $col->{coltitle} eq 'NULL' )
471 or ( $col->{coltitle} eq q{} )
472 ) ? 'zzEMPTY' : $col->{coltitle}
475 $value = $table{$row}->{
476 ( ( $col->{coltitle} eq 'NULL' )
477 or ( $col->{coltitle} eq q{} )
478 ) ? 'zzEMPTY' : $col->{coltitle}
479 } / $wgttable{$row}->{
480 ( ( $col->{coltitle} eq 'NULL' )
481 or ( $col->{coltitle} eq q{} )
482 ) ? 'zzEMPTY' : $col->{coltitle}
485 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
486 $table{$row}->{totalrow}+=$value;
487 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
488 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
489 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
491 #warn "row : $row colcount:$colcount";
493 if ( $colcount > 0 ) {
494 $total = $table{$row}->{totalrow} / $colcount;
497 { 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
498 'loopcell' => \@loopcell,
499 'hilighted' => ( $hilighted > 0 ),
500 'totalrow' => ($total) ? sprintf( "%.2f", $total ) : 0
502 $hilighted = -$hilighted;
505 # # warn "footer processing";
506 foreach my $col ( @loopcol ) {
509 foreach my $row ( @looprow ) {
510 $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}};
511 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
512 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
514 # warn "summ for column ".$col->{coltitle}." = ".$total;
515 $total = $total/$nbrow if ($nbrow);
516 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
521 # the header of the table
522 $globalline{loopfilter}=\@loopfilter;
523 # the core of the table
524 $globalline{looprow} = \@looprow;
525 $globalline{loopcol} = \@loopcol;
526 # # the foot (totals by borrower type)
527 $globalline{loopfooter} = \@loopfooter;
528 $globalline{total}= $grantotal;
529 $globalline{line} = $line;
530 $globalline{column} = $column;
531 push @mainloop,\%globalline;