5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Branch; # GetBranches
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 my $del = $input->param("sep");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user({template_name => $fullreportname,
66 flagsrequired => {reports => 1},
69 $template->param(do_it => $do_it,
70 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
74 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
75 if ($output eq "screen"){
76 # Printing results to screen
77 $template->param(mainloop => $results);
78 output_html_with_http_headers $input, $cookie, $template->output;
81 # Printing to a csv file
82 print $input->header(-type => 'application/vnd.sun.xml.calc',
84 -attachment=>"$basename.csv",
85 -filename=>"$basename.csv" );
86 my $cols = @$results[0]->{loopcol};
87 my $lines = @$results[0]->{looprow};
89 $sep =C4::Context->preference("delimiter");
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 @dels = ( C4::Context->preference("delimiter") );
217 my $CGIsepChoice=CGI::scrolling_list(
225 CGIBorCat => $CGIBorCat,
226 CGIItemType => $CGIItemTypes,
227 CGIBranch => $CGIBranch,
228 hassort1=> $hassort1,
229 hassort2=> $hassort2,
230 HlghtSort2 => $hglghtsort2,
231 CGISort1 => $CGISort1,
232 CGISort2 => $CGISort2,
233 CGIextChoice => $CGIextChoice,
234 CGIsepChoice => $CGIsepChoice
236 output_html_with_http_headers $input, $cookie, $template->output;
243 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
252 my $dbh = C4::Context->dbh;
258 for (my $i=0;$i<=6;$i++) {
260 if ( @$filters[$i] ) {
261 if (($i==1) and (@$filters[$i-1])) {
262 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
264 # format the dates filters, otherwise just fill as is
266 $cell{filter} .= @$filters[$i];
268 $cell{filter} .= format_date(@$filters[$i]);
270 $cell{crit} .="Issue From" if ($i==0);
271 $cell{crit} .="Issue To" if ($i==1);
272 $cell{crit} .="Issue Month" if ($i==2);
273 $cell{crit} .="Issue Day" if ($i==3);
274 $cell{crit} .="Return From" if ($i==4);
275 $cell{crit} .="Return To" if ($i==5);
276 $cell{crit} .="Return Month" if ($i==6);
277 $cell{crit} .="Return Day" if ($i==7);
278 $cell{crit} .="Borrower Cat" if ($i==8);
279 $cell{crit} .="Doc Type" if ($i==9);
280 $cell{crit} .="Branch" if ($i==10);
281 $cell{crit} .="Sort1" if ($i==11);
282 $cell{crit} .="Sort2" if ($i==12);
283 push @loopfilter, \%cell;
286 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
287 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
292 # warn "filtres ".@filters[0];
293 # warn "filtres ".@filters[1];
294 # warn "filtres ".@filters[2];
295 # warn "filtres ".@filters[3];
296 $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
297 $line = "biblioitems.".$line if $line=~/itemtype/;
299 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
300 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
301 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
302 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
303 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
304 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
305 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
306 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
307 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
308 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
309 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
310 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
311 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
312 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
313 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
315 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
316 $column = "biblioitems.".$column if $column=~/itemtype/;
318 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
319 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
320 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
321 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
322 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
323 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
324 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
325 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
326 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
327 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
328 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
329 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
330 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
331 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
332 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
337 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
339 $linefield .="dayname($line)";
340 $lineorder .="weekday($line)";
341 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
343 $linefield .="monthname($line)";
344 $lineorder .="month($line)";
345 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
347 $linefield .="Year($line)";
349 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
350 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
358 $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";
360 if (($line=~/timestamp/) or ($line=~/returndate/)){
361 if ($linefilter[1] and ($linefilter[0])){
362 $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
363 } elsif ($linefilter[1]) {
364 $strsth .= " and $line < \'$linefilter[1]\' " ;
365 } elsif ($linefilter[0]) {
366 $strsth .= " and $line > \'$linefilter[0]\' " ;
369 $strsth .= " and dayname($line) = '$linefilter[2]' " ;
372 $strsth .= " and monthname($line) = '$linefilter[3]' " ;
374 } elsif ($linefilter[0]) {
375 $linefilter[0] =~ s/\*/%/g;
376 $strsth .= " and $line LIKE '$linefilter[0]' " ;
378 $strsth .=" group by $linefield";
379 $strsth .=" order by $lineorder";
381 my $sth = $dbh->prepare( $strsth );
385 while ( my ($celvalue) = $sth->fetchrow) {
388 $cell{rowtitle} = $celvalue;
390 $cell{rowtitle} = "";
393 push @loopline, \%cell;
399 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
401 $colfield .="dayname($column)";
402 $colorder .="weekday($column)";
403 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
405 $colfield .="monthname($column)";
406 $colorder .="month($column)";
407 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
409 $colfield .="Year($column)";
410 $colorder .= $column;
411 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
412 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
413 $colorder .= $column;
415 $colfield .= $column;
416 $colorder .= $column;
420 $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";
422 if (($column=~/timestamp/) or ($column=~/returndate/)){
423 if ($colfilter[1] and ($colfilter[0])){
424 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
425 } elsif ($colfilter[1]) {
426 $strsth2 .= " and $column < '$colfilter[1]' " ;
427 } elsif ($colfilter[0]) {
428 $strsth2 .= " and $column > '$colfilter[0]' " ;
431 $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
434 $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
436 } elsif ($colfilter[0]) {
437 $colfilter[0] =~ s/\*/%/g;
438 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
440 $strsth2 .=" group by $colfield";
441 $strsth2 .=" order by $colorder";
444 my $sth2 = $dbh->prepare( $strsth2 );
445 if (( @colfilter ) and ($colfilter[1])){
446 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
447 } elsif ($colfilter[0]) {
448 $sth2->execute($colfilter[0]);
454 while (my ($celvalue) = $sth2->fetchrow) {
457 # warn "coltitle :".$celvalue;
458 $cell{coltitle} = $celvalue;
460 push @loopcol, \%cell;
462 # warn "fin des titres colonnes";
468 #Initialization of cell values.....
474 foreach my $row ( @loopline ) {
475 foreach my $col ( @loopcol ) {
476 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
477 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
479 $table{$row->{rowtitle}}->{totalrow}=0;
482 # preparing calculation
485 # Processing average loanperiods
486 $strcalc .= "SELECT $linefield, $colfield, ";
487 $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";
489 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
490 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
491 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
492 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
493 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
494 $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
495 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
496 $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
497 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
498 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
499 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
500 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
501 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
502 $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
503 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
504 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
505 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
506 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
507 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
508 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
509 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
510 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
512 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
513 warn "SQL :". $strcalc;
515 my $dbcalc = $dbh->prepare($strcalc);
517 # warn "filling table";
526 while (my @data = $dbcalc->fetchrow) {
527 my ($row, $col, $issuedate, $returndate, $weight)=@data;
528 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
529 $emptycol=1 if ($col eq undef);
530 $col = "zzEMPTY" if ($col eq undef);
531 $row = "zzEMPTY" if ($row eq undef);
532 # fill returndate to avoid an error with date calc (needed for all non returned issues)
533 $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
534 my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
535 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
536 # and seconds between the two
537 $loanlength = $result;
538 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
539 # warn "513 row :".$row." column :".$col;
540 $table{$row}->{$col}+=$weight*$loanlength;
541 # $table{$row}->{totalrow}+=$weight*$loanlength;
542 $cnttable{$row}->{$col}= 1;
543 $wgttable{$row}->{$col}+=$weight;
546 push @loopcol,{coltitle => "NULL"} if ($emptycol);
548 foreach my $row ( sort keys %table ) {
550 #@loopcol ensures the order for columns is common with column titles
551 # and the number matches the number of columns
553 foreach my $col ( @loopcol ) {
554 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}});
556 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
557 $table{$row}->{totalrow}+=$value;
558 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
559 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
560 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
562 #warn "row : $row colcount:$colcount";
563 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
564 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
565 'loopcell' => \@loopcell,
566 'hilighted' => ($hilighted >0),
567 'totalrow' => ($total)?sprintf("%.2f",$total):0
569 $hilighted = -$hilighted;
572 # # warn "footer processing";
573 foreach my $col ( @loopcol ) {
576 foreach my $row ( @looprow ) {
577 $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}};
578 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
579 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
581 # warn "summ for column ".$col->{coltitle}." = ".$total;
582 $total = $total/$nbrow if ($nbrow);
583 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
588 # the header of the table
589 $globalline{loopfilter}=\@loopfilter;
590 # the core of the table
591 $globalline{looprow} = \@looprow;
592 $globalline{loopcol} = \@loopcol;
593 # # the foot (totals by borrower type)
594 $globalline{loopfooter} = \@loopfooter;
595 $globalline{total}= $grantotal;
596 $globalline{line} = $line;
597 $globalline{column} = $column;
598 push @mainloop,\%globalline;