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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
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 => {editcatalogue => 1},
68 $template->param(do_it => $do_it);
71 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results);
75 output_html_with_http_headers $input, $cookie, $template->output;
78 # Printing to a csv file
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
87 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
89 foreach my $col ( @$cols ) {
90 print $col->{coltitle}.$sep;
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
114 my $dbh = C4::Context->dbh;
119 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
124 while (my ($value, $desc) =$req->fetchrow) {
125 push @select, $value;
126 $select{$value}=$desc;
128 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
135 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
141 while (my ($value,$desc) =$req->fetchrow) {
142 push @select, $value;
143 $select{$value}=$desc;
145 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
152 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
157 while (my ($value) =$req->fetchrow) {
158 $hassort1 =1 if ($value);
159 push @select, $value;
161 my $branches=getbranches();
164 push @select_branch,"";
165 $select_branches{""} = "";
166 foreach my $branch (keys %$branches) {
167 push @select_branch, $branch;
168 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
170 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
172 -values => \@select_branch,
173 -labels => \%select_branches,
177 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
183 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
189 while (my ($value) =$req->fetchrow) {
190 $hassort2 =1 if ($value);
191 $hglghtsort2= !($hassort1);
192 push @select, $value;
194 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
200 my @mime = ( C4::Context->preference("MIME") );
201 # foreach my $mime (@mime){
205 my $CGIextChoice=CGI::scrolling_list(
212 my @dels = ( C4::Context->preference("delimiter") );
213 my $CGIsepChoice=CGI::scrolling_list(
221 CGIBorCat => $CGIBorCat,
222 CGIItemType => $CGIItemTypes,
223 CGIBranch => $CGIBranch,
224 hassort1=> $hassort1,
225 hassort2=> $hassort2,
226 HlghtSort2 => $hglghtsort2,
227 CGISort1 => $CGISort1,
228 CGISort2 => $CGISort2,
229 CGIextChoice => $CGIextChoice,
230 CGIsepChoice => $CGIsepChoice
232 output_html_with_http_headers $input, $cookie, $template->output;
239 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
248 my $dbh = C4::Context->dbh;
254 for (my $i=0;$i<=6;$i++) {
256 if ( @$filters[$i] ) {
257 if (($i==1) and (@$filters[$i-1])) {
258 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
260 $cell{filter} .= @$filters[$i];
261 $cell{crit} .="Period From" if ($i==0);
262 $cell{crit} .="Period To" if ($i==1);
263 $cell{crit} .="Borrower Cat" if ($i==2);
264 $cell{crit} .="Doc Type" if ($i==3);
265 $cell{crit} .="Branch" if ($i==4);
266 $cell{crit} .="Sort1" if ($i==5);
267 $cell{crit} .="Sort2" if ($i==6);
268 push @loopfilter, \%cell;
271 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
272 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
273 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
274 push @loopfilter,{crit=>"Select Month ",filter=>$daysel} if ($monthsel);
278 # warn "filtres ".@filters[0];
279 # warn "filtres ".@filters[1];
280 # warn "filtres ".@filters[2];
281 # warn "filtres ".@filters[3];
283 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
284 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
285 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
286 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
287 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
288 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
289 $linefilter[0] = @$filters[5] if ($line =~ /sort1/ ) ;
290 $linefilter[0] = @$filters[6] if ($line =~ /sort2/ ) ;
291 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
294 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
295 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
296 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
297 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
298 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
299 $colfilter[0] = @$filters[5] if ($column =~ /sort1/ ) ;
300 $colfilter[0] = @$filters[6] if ($column =~ /sort2/ ) ;
301 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
305 if (($line =~/datetime/) and ($dsp == 1)) {
307 $linefield .="dayname($line)";
308 } elsif (($line=~/datetime/) and ($dsp == 2)) {
310 $linefield .="monthname($line)";
311 } elsif (($line=~/datetime/) and ($dsp == 3)) {
313 $linefield .="Year($line)";
314 } elsif ($line=~/datetime/) {
315 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
321 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
323 if ($line=~/datetime/) {
324 if ($linefilter[1] and ($linefilter[0])){
325 $strsth .= " and $line between ? and ? " ;
326 } elsif ($linefilter[1]) {
327 $strsth .= " and $line < ? " ;
328 } elsif ($linefilter[0]) {
329 $strsth .= " and $line > ? " ;
331 $strsth .= " and type ='".$type."' " if $type;
332 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
333 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
334 } elsif ($linefilter[0]) {
335 $linefilter[0] =~ s/\*/%/g;
336 $strsth .= " and $line LIKE ? " ;
338 $strsth .=" group by $linefield";
339 $strsth .=" order by $linefield";
342 my $sth = $dbh->prepare( $strsth );
343 if (( @linefilter ) and ($linefilter[1])){
344 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
345 } elsif ($linefilter[0]) {
346 $sth->execute($linefilter[0]);
351 while ( my ($celvalue) = $sth->fetchrow) {
354 $cell{rowtitle} = $celvalue;
356 $cell{rowtitle} = "";
359 push @loopline, \%cell;
364 if (($column =~/datetime/) and ($dsp == 1)) {
366 $colfield .="dayname($column)";
367 } elsif (($column=~/datetime/) and ($dsp == 2)) {
369 $colfield .="monthname($column)";
370 } elsif (($column=~/datetime/) and ($dsp == 3)) {
372 $colfield .="Year($column)";
373 } elsif ($column=~/datetime/) {
374 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
376 $colfield .= $column;
380 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
382 if ($column=~/datetime/){
383 if (($colfilter[1]) and ($colfilter[0])){
384 $strsth2 .= " and $column between ? and ? " ;
385 } elsif ($colfilter[1]) {
386 $strsth2 .= " and $column < ? " ;
387 } elsif ($colfilter[0]) {
388 $strsth2 .= " and $column > ? " ;
390 $strsth2 .= " and type ='".$type."' " if $type;
391 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
392 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
393 } elsif ($colfilter[0]) {
394 $colfilter[0] =~ s/\*/%/g;
395 $strsth2 .= " and $column LIKE ? " ;
397 $strsth2 .=" group by $colfield";
398 $strsth2 .=" order by $colfield";
401 my $sth2 = $dbh->prepare( $strsth2 );
402 if (( @colfilter ) and ($colfilter[1])){
403 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
404 } elsif ($colfilter[0]) {
405 $sth2->execute($colfilter[0]);
411 while (my ($celvalue) = $sth2->fetchrow) {
414 # warn "coltitle :".$celvalue;
415 $cell{coltitle} = $celvalue;
417 push @loopcol, \%cell;
419 # warn "fin des titres colonnes";
425 #Initialization of cell values.....
428 foreach my $row ( @loopline ) {
429 foreach my $col ( @loopcol ) {
430 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
431 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
433 $table{$row->{rowtitle}}->{totalrow}=0;
436 # preparing calculation
438 $strcalc .= "SELECT $linefield, $colfield, ";
439 $strcalc .= "COUNT( * ) " if ($process ==1);
441 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
442 $rqbookcount->execute;
443 my ($bookcount) = $rqbookcount->fetchrow;
444 $strcalc .= "100*(COUNT(itemnumber))/ $bookcount " ;
446 $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null and $line is not null ";
448 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
449 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
450 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
451 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
452 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
453 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
454 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
455 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
456 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
457 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
458 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
459 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
460 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
461 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
462 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
463 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
464 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
466 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
468 my $dbcalc = $dbh->prepare($strcalc);
470 # warn "filling table";
471 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
472 # warn "filling table $row / $col / $value ";
473 $table{$row}->{$col}=$value;
474 $table{$row}->{totalrow}+=$value;
475 $grantotal += $value;
478 foreach my $row ( sort keys %table ) {
480 #@loopcol ensures the order for columns is common with column titles
481 foreach my $col ( @loopcol ) {
482 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
484 push @looprow,{ 'rowtitle' => $row,
485 'loopcell' => \@loopcell,
487 'totalrow' => $table{$row}->{totalrow}
489 $hilighted = -$hilighted;
492 # warn "footer processing";
493 foreach my $col ( @loopcol ) {
495 foreach my $row ( @looprow ) {
496 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
497 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
499 # warn "summ for column ".$col->{coltitle}." = ".$total;
500 push @loopfooter, {'totalcol' => $total};
504 # the header of the table
505 $globalline{loopfilter}=\@loopfilter;
506 # the core of the table
507 $globalline{looprow} = \@looprow;
508 $globalline{loopcol} = \@loopcol;
509 # # the foot (totals by borrower type)
510 $globalline{loopfooter} = \@loopfooter;
511 $globalline{total}= $grantotal;
512 $globalline{line} = $line;
513 $globalline{column} = $column;
514 push @mainloop,\%globalline;