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
29 use C4
::Dates qw
/format_date format_date_in_iso/;
30 use Date
::Calc
qw(Delta_Days);
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_avg_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso
($filters[0]);
49 $filters[1]=format_date_in_iso
($filters[1]);
50 $filters[2]=format_date_in_iso
($filters[2]);
51 $filters[3]=format_date_in_iso
($filters[3]);
52 my $podsp = $input->param("IssueDisplay");
53 my $rodsp = $input->param("ReturnDisplay");
54 my $calc = $input->param("Cellvalue");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 my $del = $input->param("sep");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user
({template_name
=> $fullreportname,
65 flagsrequired
=> {reports
=> 1},
68 $template->param(do_it
=> $do_it,
69 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
73 my $results = calculate
($line, $column, $rodsp, $podsp, $calc, \
@filters);
74 if ($output eq "screen"){
75 # Printing results to screen
76 $template->param(mainloop
=> $results);
77 output_html_with_http_headers
$input, $cookie, $template->output;
80 # Printing to a csv file
81 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
83 -attachment
=>"$basename.csv",
84 -filename
=>"$basename.csv" );
85 my $cols = @
$results[0]->{loopcol
};
86 my $lines = @
$results[0]->{looprow
};
88 $sep =C4
::Context
->preference("delimiter");
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 @dels = ( C4
::Context
->preference("delimiter") );
216 my $CGIsepChoice=CGI
::scrolling_list
(
224 CGIBorCat
=> $CGIBorCat,
225 CGIItemType
=> $CGIItemTypes,
226 CGIBranch
=> $CGIBranch,
227 hassort1
=> $hassort1,
228 hassort2
=> $hassort2,
229 HlghtSort2
=> $hglghtsort2,
230 CGISort1
=> $CGISort1,
231 CGISort2
=> $CGISort2,
232 CGIextChoice
=> $CGIextChoice,
233 CGIsepChoice
=> $CGIsepChoice
235 output_html_with_http_headers
$input, $cookie, $template->output;
242 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
251 my $dbh = C4
::Context
->dbh;
257 for (my $i=0;$i<=6;$i++) {
259 if ( @
$filters[$i] ) {
260 if (($i==1) and (@
$filters[$i-1])) {
261 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
263 # format the dates filters, otherwise just fill as is
265 $cell{filter
} .= @
$filters[$i];
267 $cell{filter
} .= format_date
(@
$filters[$i]);
269 $cell{crit
} .="Issue From" if ($i==0);
270 $cell{crit
} .="Issue To" if ($i==1);
271 $cell{crit
} .="Issue Month" if ($i==2);
272 $cell{crit
} .="Issue Day" if ($i==3);
273 $cell{crit
} .="Return From" if ($i==4);
274 $cell{crit
} .="Return To" if ($i==5);
275 $cell{crit
} .="Return Month" if ($i==6);
276 $cell{crit
} .="Return Day" if ($i==7);
277 $cell{crit
} .="Borrower Cat" if ($i==8);
278 $cell{crit
} .="Doc Type" if ($i==9);
279 $cell{crit
} .="Branch" if ($i==10);
280 $cell{crit
} .="Sort1" if ($i==11);
281 $cell{crit
} .="Sort2" if ($i==12);
282 push @loopfilter, \
%cell;
285 push @loopfilter,{crit
=>"Issue Display",filter
=>$rodsp} if ($rodsp);
286 push @loopfilter,{crit
=>"Return Display",filter
=>$podsp} if ($podsp);
291 # warn "filtres ".@filters[0];
292 # warn "filtres ".@filters[1];
293 # warn "filtres ".@filters[2];
294 # warn "filtres ".@filters[3];
295 $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
296 $line = "biblioitems.".$line if $line=~/itemtype/;
298 $linefilter[0] = @
$filters[0] if ($line =~ /timestamp/ ) ;
299 $linefilter[1] = @
$filters[1] if ($line =~ /timestamp/ ) ;
300 $linefilter[2] = @
$filters[2] if ($line =~ /timestamp/ ) ;
301 $linefilter[3] = @
$filters[3] if ($line =~ /timestamp/ ) ;
302 $linefilter[0] = @
$filters[4] if ($line =~ /returndate/ ) ;
303 $linefilter[1] = @
$filters[5] if ($line =~ /returndate/ ) ;
304 $linefilter[2] = @
$filters[6] if ($line =~ /returndate/ ) ;
305 $linefilter[3] = @
$filters[7] if ($line =~ /returndate/ ) ;
306 $linefilter[0] = @
$filters[8] if ($line =~ /category/ ) ;
307 $linefilter[0] = @
$filters[9] if ($line =~ /itemtype/ ) ;
308 $linefilter[0] = @
$filters[10] if ($line =~ /branch/ ) ;
309 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
310 $linefilter[0] = @
$filters[11] if ($line =~ /sort1/ ) ;
311 $linefilter[0] = @
$filters[12] if ($line =~ /sort2/ ) ;
312 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
314 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
315 $column = "biblioitems.".$column if $column=~/itemtype/;
317 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
318 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
319 $colfilter[2] = @
$filters[2] if ($column =~ /timestamp/ ) ;
320 $colfilter[3] = @
$filters[3] if ($column =~ /timestamp/ ) ;
321 $colfilter[0] = @
$filters[4] if ($column =~ /returndate/ ) ;
322 $colfilter[1] = @
$filters[5] if ($column =~ /returndate/ ) ;
323 $colfilter[2] = @
$filters[6] if ($column =~ /returndate/ ) ;
324 $colfilter[3] = @
$filters[7] if ($column =~ /returndate/ ) ;
325 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
326 $colfilter[0] = @
$filters[9] if ($column =~ /itemtype/ ) ;
327 $colfilter[0] = @
$filters[10] if ($column =~ /branch/ ) ;
328 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
329 $colfilter[0] = @
$filters[11] if ($column =~ /sort1/ ) ;
330 $colfilter[0] = @
$filters[12] if ($column =~ /sort2/ ) ;
331 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
336 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
338 $linefield .="dayname($line)";
339 $lineorder .="weekday($line)";
340 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
342 $linefield .="monthname($line)";
343 $lineorder .="month($line)";
344 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
346 $linefield .="Year($line)";
348 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
349 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
357 $strsth .= "select distinctrow $linefield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
359 if (($line=~/timestamp/) or ($line=~/returndate/)){
360 if ($linefilter[1] and ($linefilter[0])){
361 $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
362 } elsif ($linefilter[1]) {
363 $strsth .= " and $line < \'$linefilter[1]\' " ;
364 } elsif ($linefilter[0]) {
365 $strsth .= " and $line > \'$linefilter[0]\' " ;
368 $strsth .= " and dayname($line) = '$linefilter[2]' " ;
371 $strsth .= " and monthname($line) = '$linefilter[3]' " ;
373 } elsif ($linefilter[0]) {
374 $linefilter[0] =~ s/\*/%/g;
375 $strsth .= " and $line LIKE '$linefilter[0]' " ;
377 $strsth .=" group by $linefield";
378 $strsth .=" order by $lineorder";
380 my $sth = $dbh->prepare( $strsth );
384 while ( my ($celvalue) = $sth->fetchrow) {
387 $cell{rowtitle
} = $celvalue;
389 $cell{rowtitle
} = "";
392 push @loopline, \
%cell;
398 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
400 $colfield .="dayname($column)";
401 $colorder .="weekday($column)";
402 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
404 $colfield .="monthname($column)";
405 $colorder .="month($column)";
406 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
408 $colfield .="Year($column)";
409 $colorder .= $column;
410 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
411 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
412 $colorder .= $column;
414 $colfield .= $column;
415 $colorder .= $column;
419 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
421 if (($column=~/timestamp/) or ($column=~/returndate/)){
422 if ($colfilter[1] and ($colfilter[0])){
423 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
424 } elsif ($colfilter[1]) {
425 $strsth2 .= " and $column < '$colfilter[1]' " ;
426 } elsif ($colfilter[0]) {
427 $strsth2 .= " and $column > '$colfilter[0]' " ;
430 $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
433 $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
435 } elsif ($colfilter[0]) {
436 $colfilter[0] =~ s/\*/%/g;
437 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
439 $strsth2 .=" group by $colfield";
440 $strsth2 .=" order by $colorder";
443 my $sth2 = $dbh->prepare( $strsth2 );
444 if (( @colfilter ) and ($colfilter[1])){
445 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
446 } elsif ($colfilter[0]) {
447 $sth2->execute($colfilter[0]);
453 while (my ($celvalue) = $sth2->fetchrow) {
456 # warn "coltitle :".$celvalue;
457 $cell{coltitle
} = $celvalue;
459 push @loopcol, \
%cell;
461 # warn "fin des titres colonnes";
467 #Initialization of cell values.....
473 foreach my $row ( @loopline ) {
474 foreach my $col ( @loopcol ) {
475 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
476 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
478 $table{$row->{rowtitle
}}->{totalrow
}=0;
481 # preparing calculation
484 # Processing average loanperiods
485 $strcalc .= "SELECT $linefield, $colfield, ";
486 $strcalc .= " issuedate, returndate, timestamp, COUNT(*), date_due, issues.renewals, issuelength FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber AND returndate IS NOT NULL";
488 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
489 $strcalc .= " AND issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
490 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
491 $strcalc .= " AND issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
492 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
493 $strcalc .= " AND issues.returndate > '" . @
$filters[4] ."'" if ( @
$filters[4] );
494 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
495 $strcalc .= " AND issues.returndate < '" . @
$filters[5] ."'" if ( @
$filters[5] );
496 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
497 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[8] ."'" if ( @
$filters[8] );
498 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
499 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[9] ."'" if ( @
$filters[9] );
500 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
501 $strcalc .= " AND issues.branchcode like '" . @
$filters[10] ."'" if ( @
$filters[10] );
502 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
503 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[11] ."'" if ( @
$filters[11] );
504 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
505 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[12] ."'" if ( @
$filters[12] );
506 $strcalc .= " AND dayname(timestamp) like '" . @
$filters[2]."'" if (@
$filters[2]);
507 $strcalc .= " AND monthname(timestamp) like '" . @
$filters[3] ."'" if ( @
$filters[3] );
508 $strcalc .= " AND dayname(returndate) like '" . @
$filters[5]."'" if (@
$filters[5]);
509 $strcalc .= " AND monthname(returndate) like '" . @
$filters[6] ."'" if ( @
$filters[6] );
511 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
512 warn "SQL :". $strcalc;
514 my $dbcalc = $dbh->prepare($strcalc);
516 # warn "filling table";
525 while (my @data = $dbcalc->fetchrow) {
526 my ($row, $col, $issuedate, $returndate, $weight)=@data;
527 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
528 $emptycol=1 if ($col eq undef);
529 $col = "zzEMPTY" if ($col eq undef);
530 $row = "zzEMPTY" if ($row eq undef);
531 # fill returndate to avoid an error with date calc (needed for all non returned issues)
532 $returndate= join '-',Date
::Calc
::Today
if $returndate eq '0000-00-00';
533 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
534 # and seconds between the two
535 $loanlength = Delta_Days
(split(/-/,$issuedate),split (/-/,$returndate)) ;
536 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
537 # warn "513 row :".$row." column :".$col;
538 $table{$row}->{$col}+=$weight*$loanlength;
539 # $table{$row}->{totalrow}+=$weight*$loanlength;
540 $cnttable{$row}->{$col}= 1;
541 $wgttable{$row}->{$col}+=$weight;
544 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
546 foreach my $row ( sort keys %table ) {
548 #@loopcol ensures the order for columns is common with column titles
549 # and the number matches the number of columns
551 foreach my $col ( @loopcol ) {
552 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
}});
554 $table{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}} = $value;
555 $table{$row}->{totalrow
}+=$value;
556 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
557 $colcount+=$cnttable{$row}->{(($col->{coltitle
} eq "NULL")or ($col->{coltitle
} eq ""))?
"zzEMPTY":$col->{coltitle
}};
558 push @loopcell, {value
=> ($value)?
sprintf("%.2f",$value):0 } ;
560 #warn "row : $row colcount:$colcount";
561 my $total = $table{$row}->{totalrow
}/$colcount if ($colcount>0);
562 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
563 'loopcell' => \
@loopcell,
564 'hilighted' => ($hilighted >0),
565 'totalrow' => ($total)?
sprintf("%.2f",$total):0
567 $hilighted = -$hilighted;
570 # # warn "footer processing";
571 foreach my $col ( @loopcol ) {
574 foreach my $row ( @looprow ) {
575 $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
}};
576 $nbrow +=$cnttable{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};;
577 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
579 # warn "summ for column ".$col->{coltitle}." = ".$total;
580 $total = $total/$nbrow if ($nbrow);
581 push @loopfooter, {'totalcol' => ($total)?
sprintf("%.2f",$total):0};
586 # the header of the table
587 $globalline{loopfilter
}=\
@loopfilter;
588 # the core of the table
589 $globalline{looprow
} = \
@looprow;
590 $globalline{loopcol
} = \
@loopcol;
591 # # the foot (totals by borrower type)
592 $globalline{loopfooter
} = \
@loopfooter;
593 $globalline{total
}= $grantotal;
594 $globalline{line
} = $line;
595 $globalline{column
} = $column;
596 push @mainloop,\
%globalline;