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
35 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 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 my $podsp = $input->param("DisplayBy");
52 my $type = $input->param("PeriodTypeSel");
53 my $daysel = $input->param("PeriodDaySel");
54 my $monthsel = $input->param("PeriodMonthSel");
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, $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 # format the dates filters, otherwise just fill as is
277 $cell{filter} .= @$filters[$i];
279 $cell{filter} .= format_date(@$filters[$i]);
281 $cell{crit} .="Period From" if ($i==0);
282 $cell{crit} .="Period To" if ($i==1);
283 $cell{crit} .="Borrower Cat=" if ($i==2);
284 $cell{crit} .="Doc Type=" if ($i==3);
285 $cell{crit} .="Branch=" if ($i==4);
286 $cell{crit} .="Location=" if ($i==5);
287 $cell{crit} .="Item callnumber>=" if ($i==6);
288 $cell{crit} .="Item callnumber<" if ($i==7);
289 $cell{crit} .="sort1=" if ($i==8);
290 $cell{crit} .="sort2=" if ($i==9);
292 push @loopfilter, \%cell;
295 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
296 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
297 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
298 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
302 # warn "filtres ".@filters[0];
303 # warn "filtres ".@filters[1];
304 # warn "filtres ".@filters[2];
305 # warn "filtres ".@filters[3];
307 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
308 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
309 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
310 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
311 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
312 $linefilter[0] = @$filters[5] if ($line =~ /location/ ) ;
313 $linefilter[0] = @$filters[6] if ($line =~ /sort1/ ) ;
314 $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
317 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
318 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
319 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
320 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
321 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
322 $colfilter[0] = @$filters[5] if ($column =~ /location/ ) ;
323 $colfilter[0] = @$filters[6] if ($column =~ /sort1/ ) ;
324 $colfilter[0] = @$filters[7] if ($column =~ /sort2/ ) ;
327 if (($line =~/datetime/) and ($dsp == 1)) {
329 $linefield .="dayname($line)";
330 } elsif (($line=~/datetime/) and ($dsp == 2)) {
332 $linefield .="monthname($line)";
333 } elsif (($line=~/datetime/) and ($dsp == 3)) {
335 $linefield .="Year($line)";
336 } elsif ($line=~/datetime/) {
337 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
341 my $lineorder = $linefield;
342 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
343 $lineorder = "month($line)" if $linefield =~ "^month";
344 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
347 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
349 if ($line=~/datetime/) {
350 if ($linefilter[1] and ($linefilter[0])){
351 $strsth .= " and $line between ? and ? " ;
352 } elsif ($linefilter[1]) {
353 $strsth .= " and $line < ? " ;
354 } elsif ($linefilter[0]) {
355 $strsth .= " and $line > ? " ;
357 $strsth .= " and type ='".$type."' " if $type;
358 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
359 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
360 } elsif ($linefilter[0]) {
361 $linefilter[0] =~ s/\*/%/g;
362 $strsth .= " and $line LIKE ? " ;
364 $strsth .=" group by $linefield";
365 $strsth .=" order by $lineorder";
368 my $sth = $dbh->prepare( $strsth );
369 if (( @linefilter ) and ($linefilter[1])){
370 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
371 } elsif ($linefilter[0]) {
372 $sth->execute($linefilter[0]);
377 while ( my ($celvalue) = $sth->fetchrow) {
380 $cell{rowtitle} = $celvalue;
382 $cell{rowtitle} = "";
385 push @loopline, \%cell;
391 if (($column =~/datetime/) and ($dsp == 1)) {
393 $colfield .="dayname($column)";
394 } elsif (($column=~/datetime/) and ($dsp == 2)) {
396 $colfield .="monthname($column)";
397 } elsif (($column=~/datetime/) and ($dsp == 3)) {
399 $colfield .="Year($column)";
400 } elsif ($column=~/datetime/) {
401 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
403 $colfield .= $column;
405 $colorder = "weekday($line)" if $colfield =~ "^dayname";
406 $colorder = "month($line)" if $colfield =~ "^month";
407 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
410 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
412 if ($column=~/datetime/){
413 if (($colfilter[1]) and ($colfilter[0])){
414 $strsth2 .= " and $column between ? and ? " ;
415 } elsif ($colfilter[1]) {
416 $strsth2 .= " and $column < ? " ;
417 } elsif ($colfilter[0]) {
418 $strsth2 .= " and $column > ? " ;
420 $strsth2 .= " and type ='".$type."' " if $type;
421 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
422 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
423 } elsif ($colfilter[0]) {
424 $colfilter[0] =~ s/\*/%/g;
425 $strsth2 .= " and $column LIKE ? " ;
427 $strsth2 .=" group by $colfield";
428 $strsth2 .=" order by $colorder";
431 my $sth2 = $dbh->prepare( $strsth2 );
432 if (( @colfilter ) and ($colfilter[1])){
433 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
434 } elsif ($colfilter[0]) {
435 $sth2->execute($colfilter[0]);
441 while (my ($celvalue) = $sth2->fetchrow) {
444 # warn "coltitle :".$celvalue;
445 $cell{coltitle} = $celvalue;
447 push @loopcol, \%cell;
449 # warn "fin des titres colonnes";
455 #Initialization of cell values.....
458 foreach my $row ( @loopline ) {
459 foreach my $col ( @loopcol ) {
460 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
461 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
463 $table{$row->{rowtitle}}->{totalrow}=0;
466 # preparing calculation
469 $strcalc .= "SELECT $linefield, $colfield, ";
470 $strcalc .= "COUNT( * ) " if ($process ==1);
472 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
475 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
478 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
479 $rqbookcount->execute;
480 my ($bookcount) = $rqbookcount->fetchrow;
481 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
483 $strcalc .= "FROM statistics ";
484 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
485 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @$filters[5] or @$filters[6];
487 $strcalc .= "WHERE 1=1 ";
488 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
489 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
490 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
491 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
492 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
493 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
494 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
495 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
496 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
497 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
498 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
499 $strcalc .= " AND items.location like '" . @$filters[5] ."'" if ( @$filters[5] );
500 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
501 $strcalc .= " AND items.itemcallnumber >='" . @$filters[6] ."'" if ( @$filters[6] );
502 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
503 $strcalc .= " AND items.itemcallnumber <'" . @$filters[7] ."'" if ( @$filters[7] );
504 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
505 $strcalc .= " AND borrowers.sort1 like '" . @$filters[8] ."'" if ( @$filters[8] );
506 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
507 $strcalc .= " AND borrowers.sort2 like '" . @$filters[9] ."'" if ( @$filters[9] );
508 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
509 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
510 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
512 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
514 my $dbcalc = $dbh->prepare($strcalc);
516 # warn "filling table";
518 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
519 # warn "filling table $row / $col / $value ";
520 $emptycol = 1 if ($col eq undef);
521 $col = "zzEMPTY" if ($col eq undef);
522 $row = "zzEMPTY" if ($row eq undef);
524 $table{$row}->{$col}+=$value;
525 $table{$row}->{totalrow}+=$value;
526 $grantotal += $value;
528 push @loopcol,{coltitle => "NULL"} if ($emptycol);
530 foreach my $row (@loopline) {
532 #@loopcol ensures the order for columns is common with column titles
533 # and the number matches the number of columns
534 foreach my $col ( @loopcol ) {
535 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
536 push @loopcell, {value => $value } ;
538 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
539 'loopcell' => \@loopcell,
540 'hilighted' => ($hilighted >0),
541 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
543 $hilighted = -$hilighted;
546 # warn "footer processing";
547 foreach my $col ( @loopcol ) {
549 foreach my $row ( @looprow ) {
550 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
551 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
553 # warn "summ for column ".$col->{coltitle}." = ".$total;
554 push @loopfooter, {'totalcol' => $total};
558 # the header of the table
559 $globalline{loopfilter}=\@loopfilter;
560 # the core of the table
561 $globalline{looprow} = \@looprow;
562 $globalline{loopcol} = \@loopcol;
563 # # the foot (totals by borrower type)
564 $globalline{loopfooter} = \@loopfooter;
565 $globalline{total}= $grantotal;
566 $globalline{line} = $line;
567 $globalline{column} = $column;
568 push @mainloop,\%globalline;