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
34 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $podsp = $input->param("DisplayBy");
50 my $type = $input->param("PeriodTypeSel");
51 my $daysel = $input->param("PeriodDaySel");
52 my $monthsel = $input->param("PeriodMonthSel");
53 my $calc = $input->param("Cellvalue");
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 my $del = $input->param("sep");
58 #warn "calcul : ".$calc;
59 my ($template, $borrowernumber, $cookie)
60 = get_template_and_user({template_name => $fullreportname,
64 flagsrequired => {reports => 1},
67 $template->param(do_it => $do_it,
68 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
69 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
70 IntranetNav => C4::Context->preference("IntranetNav"),
74 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $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 $req = $dbh->prepare("select distinctrow location from items order by location");
208 while (my ($value) =$req->fetchrow) {
209 push @select, $value;
211 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
217 my @mime = ( C4::Context->preference("MIME") );
219 my $CGIextChoice=CGI::scrolling_list(
226 my @dels = ( C4::Context->preference("delimiter") );
227 my $CGIsepChoice=CGI::scrolling_list(
235 CGIBorCat => $CGIBorCat,
236 CGIItemType => $CGIItemTypes,
237 CGIBranch => $CGIBranch,
238 hassort1=> $hassort1,
239 hassort2=> $hassort2,
240 HlghtSort2 => $hglghtsort2,
241 CGISort1 => $CGISort1,
242 CGISort2 => $CGISort2,
243 CGIextChoice => $CGIextChoice,
244 CGIsepChoice => $CGIsepChoice,
245 CGILocation => $CGIlocation,
247 output_html_with_http_headers $input, $cookie, $template->output;
254 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
263 my $dbh = C4::Context->dbh;
269 for (my $i=0;$i<=9;$i++) {
271 if ( @$filters[$i] ) {
272 if (($i==1) and (@$filters[$i-1])) {
273 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
275 $cell{filter} .= @$filters[$i];
276 $cell{crit} .="Period From" if ($i==0);
277 $cell{crit} .="Period To" if ($i==1);
278 $cell{crit} .="Borrower Cat=" if ($i==2);
279 $cell{crit} .="Doc Type=" if ($i==3);
280 $cell{crit} .="Branch=" if ($i==4);
281 $cell{crit} .="Location=" if ($i==5);
282 $cell{crit} .="Item callnumber>=" if ($i==6);
283 $cell{crit} .="Item callnumber<" if ($i==7);
284 $cell{crit} .="sort1=" if ($i==8);
285 $cell{crit} .="sort2=" if ($i==9);
287 push @loopfilter, \%cell;
290 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
291 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
292 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
293 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
297 # warn "filtres ".@filters[0];
298 # warn "filtres ".@filters[1];
299 # warn "filtres ".@filters[2];
300 # warn "filtres ".@filters[3];
302 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
303 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
304 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
305 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
306 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
307 $linefilter[0] = @$filters[5] if ($line =~ /location/ ) ;
308 $linefilter[0] = @$filters[6] if ($line =~ /sort1/ ) ;
309 $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
312 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
313 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
314 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
315 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
316 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
317 $colfilter[0] = @$filters[5] if ($column =~ /location/ ) ;
318 $colfilter[0] = @$filters[6] if ($column =~ /sort1/ ) ;
319 $colfilter[0] = @$filters[7] if ($column =~ /sort2/ ) ;
322 if (($line =~/datetime/) and ($dsp == 1)) {
324 $linefield .="dayname($line)";
325 } elsif (($line=~/datetime/) and ($dsp == 2)) {
327 $linefield .="monthname($line)";
328 } elsif (($line=~/datetime/) and ($dsp == 3)) {
330 $linefield .="Year($line)";
331 } elsif ($line=~/datetime/) {
332 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
336 my $lineorder = $linefield;
337 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
338 $lineorder = "month($line)" if $linefield =~ "^month";
339 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
342 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
344 if ($line=~/datetime/) {
345 if ($linefilter[1] and ($linefilter[0])){
346 $strsth .= " and $line between ? and ? " ;
347 } elsif ($linefilter[1]) {
348 $strsth .= " and $line < ? " ;
349 } elsif ($linefilter[0]) {
350 $strsth .= " and $line > ? " ;
352 $strsth .= " and type ='".$type."' " if $type;
353 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
354 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
355 } elsif ($linefilter[0]) {
356 $linefilter[0] =~ s/\*/%/g;
357 $strsth .= " and $line LIKE ? " ;
359 $strsth .=" group by $linefield";
360 $strsth .=" order by $lineorder";
363 my $sth = $dbh->prepare( $strsth );
364 if (( @linefilter ) and ($linefilter[1])){
365 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
366 } elsif ($linefilter[0]) {
367 $sth->execute($linefilter[0]);
372 while ( my ($celvalue) = $sth->fetchrow) {
375 $cell{rowtitle} = $celvalue;
377 $cell{rowtitle} = "";
380 push @loopline, \%cell;
386 if (($column =~/datetime/) and ($dsp == 1)) {
388 $colfield .="dayname($column)";
389 } elsif (($column=~/datetime/) and ($dsp == 2)) {
391 $colfield .="monthname($column)";
392 } elsif (($column=~/datetime/) and ($dsp == 3)) {
394 $colfield .="Year($column)";
395 } elsif ($column=~/datetime/) {
396 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
398 $colfield .= $column;
400 $colorder = "weekday($line)" if $colfield =~ "^dayname";
401 $colorder = "month($line)" if $colfield =~ "^month";
402 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
405 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
407 if ($column=~/datetime/){
408 if (($colfilter[1]) and ($colfilter[0])){
409 $strsth2 .= " and $column between ? and ? " ;
410 } elsif ($colfilter[1]) {
411 $strsth2 .= " and $column < ? " ;
412 } elsif ($colfilter[0]) {
413 $strsth2 .= " and $column > ? " ;
415 $strsth2 .= " and type ='".$type."' " if $type;
416 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
417 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
418 } elsif ($colfilter[0]) {
419 $colfilter[0] =~ s/\*/%/g;
420 $strsth2 .= " and $column LIKE ? " ;
422 $strsth2 .=" group by $colfield";
423 $strsth2 .=" order by $colorder";
426 my $sth2 = $dbh->prepare( $strsth2 );
427 if (( @colfilter ) and ($colfilter[1])){
428 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
429 } elsif ($colfilter[0]) {
430 $sth2->execute($colfilter[0]);
436 while (my ($celvalue) = $sth2->fetchrow) {
439 # warn "coltitle :".$celvalue;
440 $cell{coltitle} = $celvalue;
442 push @loopcol, \%cell;
444 # warn "fin des titres colonnes";
450 #Initialization of cell values.....
453 foreach my $row ( @loopline ) {
454 foreach my $col ( @loopcol ) {
455 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
456 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
458 $table{$row->{rowtitle}}->{totalrow}=0;
461 # preparing calculation
464 $strcalc .= "SELECT $linefield, $colfield, ";
465 $strcalc .= "COUNT( * ) " if ($process ==1);
467 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
470 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
473 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
474 $rqbookcount->execute;
475 my ($bookcount) = $rqbookcount->fetchrow;
476 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
478 $strcalc .= "FROM statistics ";
479 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
480 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @$filters[5] or @$filters[6];
482 $strcalc .= "WHERE 1=1 ";
483 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
484 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
485 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
486 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
487 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
488 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
489 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
490 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
491 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
492 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
493 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
494 $strcalc .= " AND items.location like '" . @$filters[5] ."'" if ( @$filters[5] );
495 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
496 $strcalc .= " AND items.itemcallnumber >='" . @$filters[6] ."'" if ( @$filters[6] );
497 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
498 $strcalc .= " AND items.itemcallnumber <'" . @$filters[7] ."'" if ( @$filters[7] );
499 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
500 $strcalc .= " AND borrowers.sort1 like '" . @$filters[8] ."'" if ( @$filters[8] );
501 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
502 $strcalc .= " AND borrowers.sort2 like '" . @$filters[9] ."'" if ( @$filters[9] );
503 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
504 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
505 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
507 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
509 my $dbcalc = $dbh->prepare($strcalc);
511 # warn "filling table";
513 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
514 warn "filling table $row / $col / $value ";
515 $emptycol = 1 if ($col eq undef);
516 $col = "zzEMPTY" if ($col eq undef);
517 $row = "zzEMPTY" if ($row eq undef);
519 $table{$row}->{$col}+=$value;
520 $table{$row}->{totalrow}+=$value;
521 $grantotal += $value;
523 push @loopcol,{coltitle => "NULL"} if ($emptycol);
525 foreach my $row (@loopline) {
527 #@loopcol ensures the order for columns is common with column titles
528 # and the number matches the number of columns
529 foreach my $col ( @loopcol ) {
530 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
531 push @loopcell, {value => $value } ;
533 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
534 'loopcell' => \@loopcell,
535 'hilighted' => ($hilighted >0),
536 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
538 $hilighted = -$hilighted;
541 # warn "footer processing";
542 foreach my $col ( @loopcol ) {
544 foreach my $row ( @looprow ) {
545 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
546 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
548 # warn "summ for column ".$col->{coltitle}." = ".$total;
549 push @loopfooter, {'totalcol' => $total};
553 # the header of the table
554 $globalline{loopfilter}=\@loopfilter;
555 # the core of the table
556 $globalline{looprow} = \@looprow;
557 $globalline{loopcol} = \@loopcol;
558 # # the foot (totals by borrower type)
559 $globalline{loopfooter} = \@loopfooter;
560 $globalline{total}= $grantotal;
561 $globalline{line} = $line;
562 $globalline{column} = $column;
563 push @mainloop,\%globalline;