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/;
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_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 my $podsp = $input->param("DisplayBy");
51 my $type = $input->param("PeriodTypeSel");
52 my $daysel = $input->param("PeriodDaySel");
53 my $monthsel = $input->param("PeriodMonthSel");
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, $podsp, $type, $daysel, $monthsel, $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 $req = $dbh->prepare("select distinctrow location from items order by location");
207 while (my ($value) =$req->fetchrow) {
208 push @select, $value;
210 my $CGIlocation=CGI
::scrolling_list
( -name
=> 'Filter',
216 my @mime = ( C4
::Context
->preference("MIME") );
218 my $CGIextChoice=CGI
::scrolling_list
(
225 my @dels = ( C4
::Context
->preference("delimiter") );
226 my $CGIsepChoice=CGI
::scrolling_list
(
234 CGIBorCat
=> $CGIBorCat,
235 CGIItemType
=> $CGIItemTypes,
236 CGIBranch
=> $CGIBranch,
237 hassort1
=> $hassort1,
238 hassort2
=> $hassort2,
239 HlghtSort2
=> $hglghtsort2,
240 CGISort1
=> $CGISort1,
241 CGISort2
=> $CGISort2,
242 CGIextChoice
=> $CGIextChoice,
243 CGIsepChoice
=> $CGIsepChoice,
244 CGILocation
=> $CGIlocation,
246 output_html_with_http_headers
$input, $cookie, $template->output;
253 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
262 my $dbh = C4
::Context
->dbh;
268 for (my $i=0;$i<=9;$i++) {
270 if ( @
$filters[$i] ) {
271 if (($i==1) and (@
$filters[$i-1])) {
272 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
274 # format the dates filters, otherwise just fill as is
276 $cell{filter
} .= @
$filters[$i];
278 $cell{filter
} .= format_date
(@
$filters[$i]);
280 $cell{crit
} .="Period From" if ($i==0);
281 $cell{crit
} .="Period To" if ($i==1);
282 $cell{crit
} .="Borrower Cat=" if ($i==2);
283 $cell{crit
} .="Doc Type=" if ($i==3);
284 $cell{crit
} .="Branch=" if ($i==4);
285 $cell{crit
} .="Location=" if ($i==5);
286 $cell{crit
} .="Item callnumber>=" if ($i==6);
287 $cell{crit
} .="Item callnumber<" if ($i==7);
288 $cell{crit
} .="sort1=" if ($i==8);
289 $cell{crit
} .="sort2=" if ($i==9);
291 push @loopfilter, \
%cell;
294 push @loopfilter,{crit
=>"Issue|Return ",filter
=>$type};
295 push @loopfilter,{crit
=>"Display by ",filter
=>$dsp} if ($dsp);
296 push @loopfilter,{crit
=>"Select Day ",filter
=>$daysel} if ($daysel);
297 push @loopfilter,{crit
=>"Select Month ",filter
=>$monthsel} if ($monthsel);
301 # warn "filtres ".@filters[0];
302 # warn "filtres ".@filters[1];
303 # warn "filtres ".@filters[2];
304 # warn "filtres ".@filters[3];
306 $linefilter[0] = @
$filters[0] if ($line =~ /datetime/ ) ;
307 $linefilter[1] = @
$filters[1] if ($line =~ /datetime/ ) ;
308 $linefilter[0] = @
$filters[2] if ($line =~ /category/ ) ;
309 $linefilter[0] = @
$filters[3] if ($line =~ /itemtype/ ) ;
310 $linefilter[0] = @
$filters[4] if ($line =~ /branch/ ) ;
311 $linefilter[0] = @
$filters[5] if ($line =~ /location/ ) ;
312 $linefilter[0] = @
$filters[6] if ($line =~ /sort1/ ) ;
313 $linefilter[0] = @
$filters[7] if ($line =~ /sort2/ ) ;
316 $colfilter[0] = @
$filters[0] if ($column =~ /datetime/) ;
317 $colfilter[1] = @
$filters[1] if ($column =~ /datetime/) ;
318 $colfilter[0] = @
$filters[2] if ($column =~ /category/) ;
319 $colfilter[0] = @
$filters[3] if ($column =~ /itemtype/) ;
320 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
321 $colfilter[0] = @
$filters[5] if ($column =~ /location/ ) ;
322 $colfilter[0] = @
$filters[6] if ($column =~ /sort1/ ) ;
323 $colfilter[0] = @
$filters[7] if ($column =~ /sort2/ ) ;
326 if (($line =~/datetime/) and ($dsp == 1)) {
328 $linefield .="dayname($line)";
329 } elsif (($line=~/datetime/) and ($dsp == 2)) {
331 $linefield .="monthname($line)";
332 } elsif (($line=~/datetime/) and ($dsp == 3)) {
334 $linefield .="Year($line)";
335 } elsif ($line=~/datetime/) {
336 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
340 my $lineorder = $linefield;
341 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
342 $lineorder = "month($line)" if $linefield =~ "^month";
343 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
346 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
348 if ($line=~/datetime/) {
349 if ($linefilter[1] and ($linefilter[0])){
350 $strsth .= " and $line between ? and ? " ;
351 } elsif ($linefilter[1]) {
352 $strsth .= " and $line < ? " ;
353 } elsif ($linefilter[0]) {
354 $strsth .= " and $line > ? " ;
356 $strsth .= " and type ='".$type."' " if $type;
357 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
358 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
359 } elsif ($linefilter[0]) {
360 $linefilter[0] =~ s/\*/%/g;
361 $strsth .= " and $line LIKE ? " ;
363 $strsth .=" group by $linefield";
364 $strsth .=" order by $lineorder";
367 my $sth = $dbh->prepare( $strsth );
368 if (( @linefilter ) and ($linefilter[1])){
369 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
370 } elsif ($linefilter[0]) {
371 $sth->execute($linefilter[0]);
376 while ( my ($celvalue) = $sth->fetchrow) {
379 $cell{rowtitle
} = $celvalue;
381 $cell{rowtitle
} = "";
384 push @loopline, \
%cell;
390 if (($column =~/datetime/) and ($dsp == 1)) {
392 $colfield .="dayname($column)";
393 } elsif (($column=~/datetime/) and ($dsp == 2)) {
395 $colfield .="monthname($column)";
396 } elsif (($column=~/datetime/) and ($dsp == 3)) {
398 $colfield .="Year($column)";
399 } elsif ($column=~/datetime/) {
400 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
402 $colfield .= $column;
404 $colorder = "weekday($line)" if $colfield =~ "^dayname";
405 $colorder = "month($line)" if $colfield =~ "^month";
406 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
409 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
411 if ($column=~/datetime/){
412 if (($colfilter[1]) and ($colfilter[0])){
413 $strsth2 .= " and $column between ? and ? " ;
414 } elsif ($colfilter[1]) {
415 $strsth2 .= " and $column < ? " ;
416 } elsif ($colfilter[0]) {
417 $strsth2 .= " and $column > ? " ;
419 $strsth2 .= " and type ='".$type."' " if $type;
420 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
421 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
422 } elsif ($colfilter[0]) {
423 $colfilter[0] =~ s/\*/%/g;
424 $strsth2 .= " and $column LIKE ? " ;
426 $strsth2 .=" group by $colfield";
427 $strsth2 .=" order by $colorder";
430 my $sth2 = $dbh->prepare( $strsth2 );
431 if (( @colfilter ) and ($colfilter[1])){
432 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
433 } elsif ($colfilter[0]) {
434 $sth2->execute($colfilter[0]);
440 while (my ($celvalue) = $sth2->fetchrow) {
443 # warn "coltitle :".$celvalue;
444 $cell{coltitle
} = $celvalue;
446 push @loopcol, \
%cell;
448 # warn "fin des titres colonnes";
454 #Initialization of cell values.....
457 foreach my $row ( @loopline ) {
458 foreach my $col ( @loopcol ) {
459 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
460 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
462 $table{$row->{rowtitle
}}->{totalrow
}=0;
465 # preparing calculation
468 $strcalc .= "SELECT $linefield, $colfield, ";
469 $strcalc .= "COUNT( * ) " if ($process ==1);
471 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
474 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
477 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
478 $rqbookcount->execute;
479 my ($bookcount) = $rqbookcount->fetchrow;
480 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
482 $strcalc .= "FROM statistics ";
483 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
484 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @
$filters[5] or @
$filters[6];
486 $strcalc .= "WHERE 1=1 ";
487 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
488 $strcalc .= " AND statistics.datetime > '" . @
$filters[0] ."'" if ( @
$filters[0] );
489 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
490 $strcalc .= " AND statistics.datetime < '" . @
$filters[1] ."'" if ( @
$filters[1] );
491 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
492 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[2] ."'" if ( @
$filters[2] );
493 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
494 $strcalc .= " AND statistics.itemtype like '" . @
$filters[3] ."'" if ( @
$filters[3] );
495 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
496 $strcalc .= " AND statistics.branch like '" . @
$filters[4] ."'" if ( @
$filters[4] );
497 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
498 $strcalc .= " AND items.location like '" . @
$filters[5] ."'" if ( @
$filters[5] );
499 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
500 $strcalc .= " AND items.itemcallnumber >='" . @
$filters[6] ."'" if ( @
$filters[6] );
501 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
502 $strcalc .= " AND items.itemcallnumber <'" . @
$filters[7] ."'" if ( @
$filters[7] );
503 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
504 $strcalc .= " AND borrowers.sort1 like '" . @
$filters[8] ."'" if ( @
$filters[8] );
505 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
506 $strcalc .= " AND borrowers.sort2 like '" . @
$filters[9] ."'" if ( @
$filters[9] );
507 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
508 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
509 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
511 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
513 my $dbcalc = $dbh->prepare($strcalc);
515 # warn "filling table";
517 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
518 # warn "filling table $row / $col / $value ";
519 $emptycol = 1 if ($col eq undef);
520 $col = "zzEMPTY" if ($col eq undef);
521 $row = "zzEMPTY" if ($row eq undef);
523 $table{$row}->{$col}+=$value;
524 $table{$row}->{totalrow
}+=$value;
525 $grantotal += $value;
527 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
529 foreach my $row (@loopline) {
531 #@loopcol ensures the order for columns is common with column titles
532 # and the number matches the number of columns
533 foreach my $col ( @loopcol ) {
534 my $value =$table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
535 push @loopcell, {value
=> $value } ;
537 push @looprow,{ 'rowtitle' => ($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
},
538 'loopcell' => \
@loopcell,
539 'hilighted' => ($hilighted >0),
540 'totalrow' => $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{totalrow
}
542 $hilighted = -$hilighted;
545 # warn "footer processing";
546 foreach my $col ( @loopcol ) {
548 foreach my $row ( @looprow ) {
549 $total += $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
550 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
552 # warn "summ for column ".$col->{coltitle}." = ".$total;
553 push @loopfooter, {'totalcol' => $total};
557 # the header of the table
558 $globalline{loopfilter
}=\
@loopfilter;
559 # the core of the table
560 $globalline{looprow
} = \
@looprow;
561 $globalline{loopcol
} = \
@loopcol;
562 # # the foot (totals by borrower type)
563 $globalline{loopfooter
} = \
@loopfooter;
564 $globalline{total
}= $grantotal;
565 $globalline{line
} = $line;
566 $globalline{column
} = $column;
567 push @mainloop,\
%globalline;