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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
26 use C4
::Branch
; # GetBranches
31 use C4
::Dates qw
/format_date format_date_in_iso/;
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.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso
($filters[0]);
51 $filters[1]=format_date_in_iso
($filters[1]);
52 $filters[2]=format_date_in_iso
($filters[2]);
53 $filters[3]=format_date_in_iso
($filters[3]);
54 my $podsp = $input->param("IssueDisplay");
55 my $rodsp = $input->param("ReturnDisplay");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user
({template_name
=> $fullreportname,
66 flagsrequired
=> {reports
=> '*'},
69 our $sep = $input->param("sep");
70 $sep = "\t" if ($sep eq 'tabulation');
71 $template->param(do_it
=> $do_it,
72 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
76 my $results = calculate
($line, $column, $rodsp, $podsp, $calc, \
@filters);
77 if ($output eq "screen"){
78 # Printing results to screen
79 $template->param(mainloop
=> $results);
80 output_html_with_http_headers
$input, $cookie, $template->output;
83 # Printing to a csv file
84 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
86 -attachment
=>"$basename.csv",
87 -filename
=>"$basename.csv" );
88 my $cols = @
$results[0]->{loopcol
};
89 my $lines = @
$results[0]->{looprow
};
91 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
93 foreach my $col ( @
$cols ) {
94 print $col->{coltitle
}.$sep;
98 foreach my $line ( @
$lines ) {
99 my $x = $line->{loopcell
};
100 print $line->{rowtitle
}.$sep;
101 foreach my $cell (@
$x) {
102 print $cell->{value
}.$sep;
104 print $line->{totalrow
};
109 $cols = @
$results[0]->{loopfooter
};
110 foreach my $col ( @
$cols ) {
111 print $sep.$col->{totalcol
};
113 print $sep.@
$results[0]->{total
};
118 my $dbh = C4
::Context
->dbh;
123 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
128 while (my ($value, $desc) =$req->fetchrow) {
129 push @select, $value;
130 $select{$value}=$desc;
132 my $CGIBorCat=CGI
::scrolling_list
( -name
=> 'Filter',
139 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
145 while (my ($value,$desc) =$req->fetchrow) {
146 push @select, $value;
147 $select{$value}=$desc;
149 my $CGIItemTypes=CGI
::scrolling_list
( -name
=> 'Filter',
156 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
161 while (my ($value) =$req->fetchrow) {
162 $hassort1 =1 if ($value);
163 push @select, $value;
165 my $branches=GetBranches
();
168 push @select_branch,"";
169 $select_branches{""} = "";
170 foreach my $branch (keys %$branches) {
171 push @select_branch, $branch;
172 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
174 my $CGIBranch=CGI
::scrolling_list
( -name
=> 'Filter',
176 -values => \
@select_branch,
177 -labels
=> \
%select_branches,
181 my $CGISort1=CGI
::scrolling_list
( -name
=> 'Filter',
187 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
193 while (my ($value) =$req->fetchrow) {
194 $hassort2 =1 if ($value);
195 $hglghtsort2= !($hassort1);
196 push @select, $value;
198 my $CGISort2=CGI
::scrolling_list
( -name
=> 'Filter',
204 my @mime = ( C4
::Context
->preference("MIME") );
205 # foreach my $mime (@mime){
209 my $CGIextChoice=CGI
::scrolling_list
(
216 my $CGIsepChoice=GetDelimiterChoices
;
219 CGIBorCat
=> $CGIBorCat,
220 CGIItemType
=> $CGIItemTypes,
221 CGIBranch
=> $CGIBranch,
222 hassort1
=> $hassort1,
223 hassort2
=> $hassort2,
224 HlghtSort2
=> $hglghtsort2,
225 CGISort1
=> $CGISort1,
226 CGISort2
=> $CGISort2,
227 CGIextChoice
=> $CGIextChoice,
228 CGIsepChoice
=> $CGIsepChoice
230 output_html_with_http_headers
$input, $cookie, $template->output;
237 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
246 my $dbh = C4
::Context
->dbh;
252 for (my $i=0;$i<=6;$i++) {
254 if ( @
$filters[$i] ) {
255 if (($i==1) and (@
$filters[$i-1])) {
256 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
258 # format the dates filters, otherwise just fill as is
260 $cell{filter
} .= @
$filters[$i];
262 $cell{filter
} .= format_date
(@
$filters[$i]);
264 $cell{crit
} .="Issue From" if ($i==0);
265 $cell{crit
} .="Issue To" if ($i==1);
266 $cell{crit
} .="Issue Month" if ($i==2);
267 $cell{crit
} .="Issue Day" if ($i==3);
268 $cell{crit
} .="Return From" if ($i==4);
269 $cell{crit
} .="Return To" if ($i==5);
270 $cell{crit
} .="Return Month" if ($i==6);
271 $cell{crit
} .="Return Day" if ($i==7);
272 $cell{crit
} .="Borrower Cat" if ($i==8);
273 $cell{crit
} .="Doc Type" if ($i==9);
274 $cell{crit
} .="Branch" if ($i==10);
275 $cell{crit
} .="Sort1" if ($i==11);
276 $cell{crit
} .="Sort2" if ($i==12);
277 push @loopfilter, \
%cell;
280 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
281 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
286 # warn "filtres ".@filters[0];
287 # warn "filtres ".@filters[1];
288 # warn "filtres ".@filters[2];
289 # warn "filtres ".@filters[3];
290 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
291 $line = "biblioitems.".$line if $line=~/itemtype/;
293 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
294 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
295 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
296 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
297 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
298 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
299 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
300 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
301 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
302 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
303 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
304 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
305 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
306 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
307 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
309 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
310 $column = "biblioitems.".$column if $column=~/itemtype/;
312 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
313 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
314 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
315 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
316 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
317 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
318 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
319 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
320 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
321 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
322 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
323 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
324 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
325 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
326 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
331 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
333 $linefield .="dayname($line)";
334 $lineorder .="weekday($line)";
335 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
337 $linefield .="monthname($line)";
338 $lineorder .="month($line)";
339 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
341 $linefield .="Year($line)";
343 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
344 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
352 $strsth .= "select distinctrow $linefield
354 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
355 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
356 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
357 LEFT JOIN issuingrules ON
358 (issuingrules.branchcode=old_issues.branchcode
359 AND issuingrules.itemtype=biblioitems.itemtype
360 AND issuingrules.categorycode=borrowers.categorycode)
363 if (($line=~/timestamp/) or ($line=~/returndate/)){
364 if ($linefilter[1] and ($linefilter[0])){
365 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
366 } elsif ($linefilter[1]) {
367 $strsth .= " AND $line < \'$linefilter[1]\' " ;
368 } elsif ($linefilter[0]) {
369 $strsth .= " AND $line > \'$linefilter[0]\' " ;
372 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
375 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
377 } elsif ($linefilter[0]) {
378 $linefilter[0] =~ s/\*/%/g;
379 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
381 $strsth .=" GROUP BY $linefield";
382 $strsth .=" ORDER BY $lineorder";
384 my $sth = $dbh->prepare( $strsth );
388 while ( my ($celvalue) = $sth->fetchrow) {
391 $cell{rowtitle
} = $celvalue;
393 $cell{rowtitle
} = "";
396 push @loopline, \
%cell;
402 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
404 $colfield .="dayname($column)";
405 $colorder .="weekday($column)";
406 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
408 $colfield .="monthname($column)";
409 $colorder .="month($column)";
410 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
412 $colfield .="Year($column)";
413 $colorder .= $column;
414 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
415 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
416 $colorder .= $column;
418 $colfield .= $column;
419 $colorder .= $column;
423 $strsth2 .= "SELECT distinctrow $colfield
425 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
426 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
427 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
428 LEFT JOIN issuingrules ON
429 (issuingrules.branchcode=old_issues.branchcode
430 AND issuingrules.itemtype=biblioitems.itemtype
431 AND issuingrules.categorycode=borrowers.categorycode)
434 if (($column=~/timestamp/) or ($column=~/returndate/)){
435 if ($colfilter[1] and ($colfilter[0])){
436 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
437 } elsif ($colfilter[1]) {
438 $strsth2 .= " AND $column < '$colfilter[1]' " ;
439 } elsif ($colfilter[0]) {
440 $strsth2 .= " AND $column > '$colfilter[0]' " ;
443 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
446 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
448 } elsif ($colfilter[0]) {
449 $colfilter[0] =~ s/\*/%/g;
450 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
452 $strsth2 .=" GROUP BY $colfield";
453 $strsth2 .=" ORDER BY $colorder";
456 my $sth2 = $dbh->prepare( $strsth2 );
457 if (( @colfilter ) and ($colfilter[1])){
458 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
459 } elsif ($colfilter[0]) {
460 $sth2->execute($colfilter[0]);
466 while (my ($celvalue) = $sth2->fetchrow) {
469 # warn "coltitle :".$celvalue;
470 $cell{coltitle
} = $celvalue;
472 push @loopcol, \
%cell;
474 # warn "fin des titres colonnes";
480 #Initialization of cell values.....
486 foreach my $row ( @loopline ) {
487 foreach my $col ( @loopcol ) {
488 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
489 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
491 $table{$row->{rowtitle
}}->{totalrow
}=0;
494 # preparing calculation
497 # Processing average loanperiods
498 $strcalc .= "SELECT $linefield, $colfield, ";
499 $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";
501 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
502 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
503 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
504 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
505 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
506 $strcalc .= " AND old_issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
507 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
508 $strcalc .= " AND old_issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
509 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
510 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
511 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
512 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
513 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
514 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
515 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
516 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
517 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
518 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
519 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
520 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
521 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
522 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
524 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
525 warn "SQL :". $strcalc;
527 my $dbcalc = $dbh->prepare($strcalc);
529 # warn "filling table";
538 while (my @data = $dbcalc->fetchrow) {
539 my ($row, $col, $issuedate, $returndate, $weight)=@data;
540 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
541 $emptycol=1 if (!defined($col));
542 $col = "zzEMPTY" if (!defined($col));
543 $row = "zzEMPTY" if (!defined($row));
544 # fill returndate to avoid an error with date calc (needed for all non returned issues)
545 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
546 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
547 # and seconds between the two
548 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
549 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
550 # warn "513 row :".$row." column :".$col;
551 $table{$row}->{$col}+=$weight*$loanlength;
552 # $table{$row}->{totalrow}+=$weight*$loanlength;
553 $cnttable{$row}->{$col}= 1;
554 $wgttable{$row}->{$col}+=$weight;
557 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
559 foreach my $row ( sort keys %table ) {
561 #@loopcol ensures the order for columns is common with column titles
562 # and the number matches the number of columns
564 foreach my $col ( @loopcol ) {
565 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
}});
567 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
568 $table{$row}->{totalrow
}+=$value;
569 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
570 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
571 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
573 #warn "row : $row colcount:$colcount";
574 my $total = $table{$row}->{totalrow
}/$colcount if ($colcount>0);
575 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
576 'loopcell' => \
@loopcell,
577 'hilighted' => ($hilighted >0),
578 'totalrow' => ($total)?
sprintf("%.2f",$total):0
580 $hilighted = -$hilighted;
583 # # warn "footer processing";
584 foreach my $col ( @loopcol ) {
587 foreach my $row ( @looprow ) {
588 $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
}};
589 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
590 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
592 # warn "summ for column ".$col->{coltitle}." = ".$total;
593 $total = $total/$nbrow if ($nbrow);
594 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
599 # the header of the table
600 $globalline{loopfilter
}=\
@loopfilter;
601 # the core of the table
602 $globalline{looprow
} = \
@looprow;
603 $globalline{loopcol
} = \
@loopcol;
604 # # the foot (totals by borrower type)
605 $globalline{loopfooter
} = \
@loopfooter;
606 $globalline{total
}= $grantotal;
607 $globalline{line
} = $line;
608 $globalline{column
} = $column;
609 push @mainloop,\
%globalline;