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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use C4
::Branch
; # GetBranches
30 use C4
::Dates qw
/format_date format_date_in_iso/;
31 use Date
::Calc
qw(Delta_Days);
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_avg_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 $filters[0]=format_date_in_iso
($filters[0]);
50 $filters[1]=format_date_in_iso
($filters[1]);
51 $filters[2]=format_date_in_iso
($filters[2]);
52 $filters[3]=format_date_in_iso
($filters[3]);
53 my $podsp = $input->param("IssueDisplay");
54 my $rodsp = $input->param("ReturnDisplay");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user
({template_name
=> $fullreportname,
65 flagsrequired
=> {reports
=> '*'},
68 our $sep = $input->param("sep");
69 $sep = "\t" if ($sep eq 'tabulation');
70 $template->param(do_it
=> $do_it,
71 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
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 $dbh = C4
::Context
->dbh;
122 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
127 while (my ($value, $desc) =$req->fetchrow) {
128 push @select, $value;
129 $select{$value}=$desc;
131 my $CGIBorCat=CGI
::scrolling_list
( -name
=> 'Filter',
138 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
144 while (my ($value,$desc) =$req->fetchrow) {
145 push @select, $value;
146 $select{$value}=$desc;
148 my $CGIItemTypes=CGI
::scrolling_list
( -name
=> 'Filter',
155 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
160 while (my ($value) =$req->fetchrow) {
161 $hassort1 =1 if ($value);
162 push @select, $value;
164 my $branches=GetBranches
();
167 push @select_branch,"";
168 $select_branches{""} = "";
169 foreach my $branch (keys %$branches) {
170 push @select_branch, $branch;
171 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
173 my $CGIBranch=CGI
::scrolling_list
( -name
=> 'Filter',
175 -values => \
@select_branch,
176 -labels
=> \
%select_branches,
180 my $CGISort1=CGI
::scrolling_list
( -name
=> 'Filter',
186 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
192 while (my ($value) =$req->fetchrow) {
193 $hassort2 =1 if ($value);
194 $hglghtsort2= !($hassort1);
195 push @select, $value;
197 my $CGISort2=CGI
::scrolling_list
( -name
=> 'Filter',
203 my @mime = ( C4
::Context
->preference("MIME") );
204 # foreach my $mime (@mime){
208 my $CGIextChoice=CGI
::scrolling_list
(
215 my $CGIsepChoice=GetDelimiterChoices
;
218 CGIBorCat
=> $CGIBorCat,
219 CGIItemType
=> $CGIItemTypes,
220 CGIBranch
=> $CGIBranch,
221 hassort1
=> $hassort1,
222 hassort2
=> $hassort2,
223 HlghtSort2
=> $hglghtsort2,
224 CGISort1
=> $CGISort1,
225 CGISort2
=> $CGISort2,
226 CGIextChoice
=> $CGIextChoice,
227 CGIsepChoice
=> $CGIsepChoice
229 output_html_with_http_headers
$input, $cookie, $template->output;
236 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
245 my $dbh = C4
::Context
->dbh;
251 for (my $i=0;$i<=6;$i++) {
253 if ( @
$filters[$i] ) {
254 if (($i==1) and (@
$filters[$i-1])) {
255 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
257 # format the dates filters, otherwise just fill as is
259 $cell{filter
} .= @
$filters[$i];
261 $cell{filter
} .= format_date
(@
$filters[$i]);
263 $cell{crit
} .="Issue From" if ($i==0);
264 $cell{crit
} .="Issue To" if ($i==1);
265 $cell{crit
} .="Issue Month" if ($i==2);
266 $cell{crit
} .="Issue Day" if ($i==3);
267 $cell{crit
} .="Return From" if ($i==4);
268 $cell{crit
} .="Return To" if ($i==5);
269 $cell{crit
} .="Return Month" if ($i==6);
270 $cell{crit
} .="Return Day" if ($i==7);
271 $cell{crit
} .="Borrower Cat" if ($i==8);
272 $cell{crit
} .="Doc Type" if ($i==9);
273 $cell{crit
} .="Branch" if ($i==10);
274 $cell{crit
} .="Sort1" if ($i==11);
275 $cell{crit
} .="Sort2" if ($i==12);
276 push @loopfilter, \
%cell;
279 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
280 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
285 # warn "filtres ".@filters[0];
286 # warn "filtres ".@filters[1];
287 # warn "filtres ".@filters[2];
288 # warn "filtres ".@filters[3];
289 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
290 $line = "biblioitems.".$line if $line=~/itemtype/;
292 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
293 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
294 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
295 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
296 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
297 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
298 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
299 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
300 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
301 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
302 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
303 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
304 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
305 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
306 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
308 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
309 $column = "biblioitems.".$column if $column=~/itemtype/;
311 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
312 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
313 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
314 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
315 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
316 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
317 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
318 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
319 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
320 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
321 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
322 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
323 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
324 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
325 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
330 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
332 $linefield .="dayname($line)";
333 $lineorder .="weekday($line)";
334 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
336 $linefield .="monthname($line)";
337 $lineorder .="month($line)";
338 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
340 $linefield .="Year($line)";
342 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
343 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
351 $strsth .= "select distinctrow $linefield
353 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
354 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
355 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
356 LEFT JOIN issuingrules ON
357 (issuingrules.branchcode=old_issues.branchcode
358 AND issuingrules.itemtype=biblioitems.itemtype
359 AND issuingrules.categorycode=borrowers.categorycode)
362 if (($line=~/timestamp/) or ($line=~/returndate/)){
363 if ($linefilter[1] and ($linefilter[0])){
364 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
365 } elsif ($linefilter[1]) {
366 $strsth .= " AND $line < \'$linefilter[1]\' " ;
367 } elsif ($linefilter[0]) {
368 $strsth .= " AND $line > \'$linefilter[0]\' " ;
371 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
374 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
376 } elsif ($linefilter[0]) {
377 $linefilter[0] =~ s/\*/%/g;
378 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
380 $strsth .=" GROUP BY $linefield";
381 $strsth .=" ORDER BY $lineorder";
383 my $sth = $dbh->prepare( $strsth );
387 while ( my ($celvalue) = $sth->fetchrow) {
390 $cell{rowtitle
} = $celvalue;
392 $cell{rowtitle
} = "";
395 push @loopline, \
%cell;
401 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
403 $colfield .="dayname($column)";
404 $colorder .="weekday($column)";
405 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
407 $colfield .="monthname($column)";
408 $colorder .="month($column)";
409 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
411 $colfield .="Year($column)";
412 $colorder .= $column;
413 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
414 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
415 $colorder .= $column;
417 $colfield .= $column;
418 $colorder .= $column;
422 $strsth2 .= "SELECT distinctrow $colfield
424 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
425 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
426 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
427 LEFT JOIN issuingrules ON
428 (issuingrules.branchcode=old_issues.branchcode
429 AND issuingrules.itemtype=biblioitems.itemtype
430 AND issuingrules.categorycode=borrowers.categorycode)
433 if (($column=~/timestamp/) or ($column=~/returndate/)){
434 if ($colfilter[1] and ($colfilter[0])){
435 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
436 } elsif ($colfilter[1]) {
437 $strsth2 .= " AND $column < '$colfilter[1]' " ;
438 } elsif ($colfilter[0]) {
439 $strsth2 .= " AND $column > '$colfilter[0]' " ;
442 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
445 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
447 } elsif ($colfilter[0]) {
448 $colfilter[0] =~ s/\*/%/g;
449 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
451 $strsth2 .=" GROUP BY $colfield";
452 $strsth2 .=" ORDER BY $colorder";
455 my $sth2 = $dbh->prepare( $strsth2 );
456 if (( @colfilter ) and ($colfilter[1])){
457 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
458 } elsif ($colfilter[0]) {
459 $sth2->execute($colfilter[0]);
465 while (my ($celvalue) = $sth2->fetchrow) {
468 # warn "coltitle :".$celvalue;
469 $cell{coltitle
} = $celvalue;
471 push @loopcol, \
%cell;
473 # warn "fin des titres colonnes";
479 #Initialization of cell values.....
485 foreach my $row ( @loopline ) {
486 foreach my $col ( @loopcol ) {
487 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
488 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
490 $table{$row->{rowtitle
}}->{totalrow
}=0;
493 # preparing calculation
496 # Processing average loanperiods
497 $strcalc .= "SELECT $linefield, $colfield, ";
498 $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";
500 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
501 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
502 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
503 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
504 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
505 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
506 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
507 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
508 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
509 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
510 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
511 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
512 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
513 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
514 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
515 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
516 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
517 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
518 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
519 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
520 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
521 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
523 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
524 warn "SQL :". $strcalc;
526 my $dbcalc = $dbh->prepare($strcalc);
528 # warn "filling table";
537 while (my @data = $dbcalc->fetchrow) {
538 my ($row, $col, $issuedate, $returndate, $weight)=@data;
539 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
540 $emptycol=1 if (!defined($col));
541 $col = "zzEMPTY" if (!defined($col));
542 $row = "zzEMPTY" if (!defined($row));
543 # fill returndate to avoid an error with date calc (needed for all non returned issues)
544 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
545 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
546 # and seconds between the two
547 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
548 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
549 # warn "513 row :".$row." column :".$col;
550 $table{$row}->{$col}+=$weight*$loanlength;
551 # $table{$row}->{totalrow}+=$weight*$loanlength;
552 $cnttable{$row}->{$col}= 1;
553 $wgttable{$row}->{$col}+=$weight;
556 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
558 foreach my $row ( sort keys %table ) {
560 #@loopcol ensures the order for columns is common with column titles
561 # and the number matches the number of columns
563 foreach my $col ( @loopcol ) {
564 my $value =$table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} / $wgttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} if ($table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}});
566 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
567 $table{$row}->{totalrow
}+=$value;
568 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
569 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
570 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
572 #warn "row : $row colcount:$colcount";
573 my $total = $table{$row}->{totalrow
}/$colcount if ($colcount>0);
574 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
575 'loopcell' => \
@loopcell,
576 'hilighted' => ($hilighted >0),
577 'totalrow' => ($total)?
sprintf("%.2f",$total):0
579 $hilighted = -$hilighted;
582 # # warn "footer processing";
583 foreach my $col ( @loopcol ) {
586 foreach my $row ( @looprow ) {
587 $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
}};
588 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
589 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
591 # warn "summ for column ".$col->{coltitle}." = ".$total;
592 $total = $total/$nbrow if ($nbrow);
593 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
598 # the header of the table
599 $globalline{loopfilter
}=\
@loopfilter;
600 # the core of the table
601 $globalline{looprow
} = \
@looprow;
602 $globalline{loopcol
} = \
@loopcol;
603 # # the foot (totals by borrower type)
604 $globalline{loopfooter
} = \
@loopfooter;
605 $globalline{total
}= $grantotal;
606 $globalline{line
} = $line;
607 $globalline{column
} = $column;
608 push @mainloop,\
%globalline;