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 #warn "calcul : ".$calc;
59 my ($template, $borrowernumber, $cookie)
60 = get_template_and_user({template_name => $fullreportname,
64 flagsrequired => {reports => 1},
67 our $sep = $input->param("sep");
68 $sep = "\t" if ($sep eq 'tabulation');
69 $template->param(do_it => $do_it,
70 DHTMLcalendar_dateformat => C4::Dates->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 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
91 foreach my $col ( @$cols ) {
92 print $col->{coltitle}.$sep;
96 foreach my $line ( @$lines ) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle}.$sep;
99 foreach my $cell (@$x) {
100 print $cell->{value}.$sep;
102 print $line->{totalrow};
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col ( @$cols ) {
109 print $sep.$col->{totalcol};
111 print $sep.@$results[0]->{total};
116 my $dbh = C4::Context->dbh;
121 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
126 while (my ($value, $desc) =$req->fetchrow) {
127 push @select, $value;
128 $select{$value}=$desc;
130 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
137 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
143 while (my ($value,$desc) =$req->fetchrow) {
144 push @select, $value;
145 $select{$value}=$desc;
147 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
154 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
159 while (my ($value) =$req->fetchrow) {
160 $hassort1 =1 if ($value);
161 push @select, $value;
163 my $branches=GetBranches();
166 push @select_branch,"";
167 $select_branches{""} = "";
168 foreach my $branch (keys %$branches) {
169 push @select_branch, $branch;
170 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
172 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
174 -values => \@select_branch,
175 -labels => \%select_branches,
179 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
185 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
191 while (my ($value) =$req->fetchrow) {
192 $hassort2 =1 if ($value);
193 $hglghtsort2= !($hassort1);
194 push @select, $value;
196 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
202 my @mime = ( C4::Context->preference("MIME") );
203 # foreach my $mime (@mime){
207 my $CGIextChoice=CGI::scrolling_list(
214 my $CGIsepChoice=GetDelimiterChoices;
217 CGIBorCat => $CGIBorCat,
218 CGIItemType => $CGIItemTypes,
219 CGIBranch => $CGIBranch,
220 hassort1=> $hassort1,
221 hassort2=> $hassort2,
222 HlghtSort2 => $hglghtsort2,
223 CGISort1 => $CGISort1,
224 CGISort2 => $CGISort2,
225 CGIextChoice => $CGIextChoice,
226 CGIsepChoice => $CGIsepChoice
228 output_html_with_http_headers $input, $cookie, $template->output;
235 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
244 my $dbh = C4::Context->dbh;
250 for (my $i=0;$i<=6;$i++) {
252 if ( @$filters[$i] ) {
253 if (($i==1) and (@$filters[$i-1])) {
254 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
256 # format the dates filters, otherwise just fill as is
258 $cell{filter} .= @$filters[$i];
260 $cell{filter} .= format_date(@$filters[$i]);
262 $cell{crit} .="Issue From" if ($i==0);
263 $cell{crit} .="Issue To" if ($i==1);
264 $cell{crit} .="Issue Month" if ($i==2);
265 $cell{crit} .="Issue Day" if ($i==3);
266 $cell{crit} .="Return From" if ($i==4);
267 $cell{crit} .="Return To" if ($i==5);
268 $cell{crit} .="Return Month" if ($i==6);
269 $cell{crit} .="Return Day" if ($i==7);
270 $cell{crit} .="Borrower Cat" if ($i==8);
271 $cell{crit} .="Doc Type" if ($i==9);
272 $cell{crit} .="Branch" if ($i==10);
273 $cell{crit} .="Sort1" if ($i==11);
274 $cell{crit} .="Sort2" if ($i==12);
275 push @loopfilter, \%cell;
278 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
279 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
284 # warn "filtres ".@filters[0];
285 # warn "filtres ".@filters[1];
286 # warn "filtres ".@filters[2];
287 # warn "filtres ".@filters[3];
288 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
289 $line = "biblioitems.".$line if $line=~/itemtype/;
291 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
292 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
293 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
294 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
295 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
296 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
297 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
298 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
299 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
300 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
301 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
302 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
303 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
304 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
305 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
307 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
308 $column = "biblioitems.".$column if $column=~/itemtype/;
310 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
311 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
312 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
313 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
314 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
315 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
316 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
317 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
318 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
319 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
320 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
321 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
322 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
323 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
324 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
329 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
331 $linefield .="dayname($line)";
332 $lineorder .="weekday($line)";
333 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
335 $linefield .="monthname($line)";
336 $lineorder .="month($line)";
337 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
339 $linefield .="Year($line)";
341 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
342 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
350 $strsth .= "select distinctrow $linefield
352 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
353 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
354 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
355 LEFT JOIN issuingrules ON
356 (issuingrules.branchcode=old_issues.branchcode
357 AND issuingrules.itemtype=biblioitems.itemtype
358 AND issuingrules.categorycode=borrowers.categorycode)
361 if (($line=~/timestamp/) or ($line=~/returndate/)){
362 if ($linefilter[1] and ($linefilter[0])){
363 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
364 } elsif ($linefilter[1]) {
365 $strsth .= " AND $line < \'$linefilter[1]\' " ;
366 } elsif ($linefilter[0]) {
367 $strsth .= " AND $line > \'$linefilter[0]\' " ;
370 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
373 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
375 } elsif ($linefilter[0]) {
376 $linefilter[0] =~ s/\*/%/g;
377 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
379 $strsth .=" GROUP BY $linefield";
380 $strsth .=" ORDER BY $lineorder";
382 my $sth = $dbh->prepare( $strsth );
386 while ( my ($celvalue) = $sth->fetchrow) {
389 $cell{rowtitle} = $celvalue;
391 $cell{rowtitle} = "";
394 push @loopline, \%cell;
400 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
402 $colfield .="dayname($column)";
403 $colorder .="weekday($column)";
404 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
406 $colfield .="monthname($column)";
407 $colorder .="month($column)";
408 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
410 $colfield .="Year($column)";
411 $colorder .= $column;
412 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
413 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
414 $colorder .= $column;
416 $colfield .= $column;
417 $colorder .= $column;
421 $strsth2 .= "SELECT distinctrow $colfield
423 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
424 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
425 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
426 LEFT JOIN issuingrules ON
427 (issuingrules.branchcode=old_issues.branchcode
428 AND issuingrules.itemtype=biblioitems.itemtype
429 AND issuingrules.categorycode=borrowers.categorycode)
432 if (($column=~/timestamp/) or ($column=~/returndate/)){
433 if ($colfilter[1] and ($colfilter[0])){
434 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
435 } elsif ($colfilter[1]) {
436 $strsth2 .= " AND $column < '$colfilter[1]' " ;
437 } elsif ($colfilter[0]) {
438 $strsth2 .= " AND $column > '$colfilter[0]' " ;
441 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
444 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
446 } elsif ($colfilter[0]) {
447 $colfilter[0] =~ s/\*/%/g;
448 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
450 $strsth2 .=" GROUP BY $colfield";
451 $strsth2 .=" ORDER BY $colorder";
454 my $sth2 = $dbh->prepare( $strsth2 );
455 if (( @colfilter ) and ($colfilter[1])){
456 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
457 } elsif ($colfilter[0]) {
458 $sth2->execute($colfilter[0]);
464 while (my ($celvalue) = $sth2->fetchrow) {
467 # warn "coltitle :".$celvalue;
468 $cell{coltitle} = $celvalue;
470 push @loopcol, \%cell;
472 # warn "fin des titres colonnes";
478 #Initialization of cell values.....
484 foreach my $row ( @loopline ) {
485 foreach my $col ( @loopcol ) {
486 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
487 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
489 $table{$row->{rowtitle}}->{totalrow}=0;
492 # preparing calculation
495 # Processing average loanperiods
496 $strcalc .= "SELECT $linefield, $colfield, ";
497 $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";
499 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
500 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
501 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
502 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
503 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
504 $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
505 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
506 $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
507 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
508 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
509 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
510 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
511 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
512 $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
513 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
514 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
515 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
516 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
517 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
518 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
519 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
520 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
522 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
523 warn "SQL :". $strcalc;
525 my $dbcalc = $dbh->prepare($strcalc);
527 # warn "filling table";
536 while (my @data = $dbcalc->fetchrow) {
537 my ($row, $col, $issuedate, $returndate, $weight)=@data;
538 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
539 $emptycol=1 if (!defined($col));
540 $col = "zzEMPTY" if (!defined($col));
541 $row = "zzEMPTY" if (!defined($row));
542 # fill returndate to avoid an error with date calc (needed for all non returned issues)
543 $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
544 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
545 # and seconds between the two
546 $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
547 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
548 # warn "513 row :".$row." column :".$col;
549 $table{$row}->{$col}+=$weight*$loanlength;
550 # $table{$row}->{totalrow}+=$weight*$loanlength;
551 $cnttable{$row}->{$col}= 1;
552 $wgttable{$row}->{$col}+=$weight;
555 push @loopcol,{coltitle => "NULL"} if ($emptycol);
557 foreach my $row ( sort keys %table ) {
559 #@loopcol ensures the order for columns is common with column titles
560 # and the number matches the number of columns
562 foreach my $col ( @loopcol ) {
563 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}});
565 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
566 $table{$row}->{totalrow}+=$value;
567 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
568 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
569 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
571 #warn "row : $row colcount:$colcount";
572 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
573 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
574 'loopcell' => \@loopcell,
575 'hilighted' => ($hilighted >0),
576 'totalrow' => ($total)?sprintf("%.2f",$total):0
578 $hilighted = -$hilighted;
581 # # warn "footer processing";
582 foreach my $col ( @loopcol ) {
585 foreach my $row ( @looprow ) {
586 $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}};
587 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
588 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
590 # warn "summ for column ".$col->{coltitle}." = ".$total;
591 $total = $total/$nbrow if ($nbrow);
592 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
597 # the header of the table
598 $globalline{loopfilter}=\@loopfilter;
599 # the core of the table
600 $globalline{looprow} = \@looprow;
601 $globalline{loopcol} = \@loopcol;
602 # # the foot (totals by borrower type)
603 $globalline{loopfooter} = \@loopfooter;
604 $globalline{total}= $grantotal;
605 $globalline{line} = $line;
606 $globalline{column} = $column;
607 push @mainloop,\%globalline;