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/;
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 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 my $podsp = $input->param("DisplayBy");
53 my $type = $input->param("PeriodTypeSel");
54 my $daysel = $input->param("PeriodDaySel");
55 my $monthsel = $input->param("PeriodMonthSel");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 my $del = $input->param("sep");
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 => C4::Dates->DHTMLcalendar(),
73 my $itemtypes = GetItemTypes();
74 my $categoryloop = GetBorrowercategoryList;
76 my $ccodes = GetKohaAuthorisedValues("items.ccode");
77 my $locations = GetKohaAuthorisedValues("items.location");
79 my $Bsort1 = GetAuthorisedValues("Bsort1");
80 my $Bsort2 = GetAuthorisedValues("Bsort2");
81 my ($hassort1,$hassort2);
82 $hassort1=1 if $Bsort1;
83 $hassort2=1 if $Bsort2;
88 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
89 if ($output eq "screen"){
90 # Printing results to screen
91 $template->param(mainloop => $results);
92 output_html_with_http_headers $input, $cookie, $template->output;
95 # Printing to a csv file
96 print $input->header(-type => 'application/vnd.sun.xml.calc',
98 -attachment=>"$basename.csv",
99 -filename=>"$basename.csv" );
100 my $cols = @$results[0]->{loopcol};
101 my $lines = @$results[0]->{looprow};
103 $sep =C4::Context->preference("delimiter");
105 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
107 foreach my $col ( @$cols ) {
108 print $col->{coltitle}.$sep;
112 foreach my $line ( @$lines ) {
113 my $x = $line->{loopcell};
114 print $line->{rowtitle}.$sep;
115 foreach my $cell (@$x) {
116 print $cell->{value}.$sep;
118 print $line->{totalrow};
123 $cols = @$results[0]->{loopfooter};
124 foreach my $col ( @$cols ) {
125 print $sep.$col->{totalcol};
127 print $sep.@$results[0]->{total};
132 my $dbh = C4::Context->dbh;
137 # create itemtype arrayref for <select>.
139 for my $itype ( keys(%$itemtypes)) {
140 push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{description} } ;
143 my $branches=GetBranches();
145 foreach (keys %$branches) {
147 my %row = (branchcode => $_,
148 selected => ($thisbranch eq $_ ? 1 : 0),
149 code => $branches->{$_}->{'branchcode'},
150 description => $branches->{$_}->{'branchname'},
152 push @branchloop, \%row;
157 foreach (sort keys %$locations) {
158 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
162 foreach (keys %$ccodes) {
163 push @ccodes, { code => $_, description => $ccodes->{$_} };
167 my @mime = ( C4::Context->preference("MIME") );
169 my $CGIextChoice=CGI::scrolling_list(
176 my @dels = ( C4::Context->preference("delimiter") );
177 my $CGIsepChoice=CGI::scrolling_list(
185 categoryloop => $categoryloop,
186 itemtypeloop => \@itemtypeloop,
187 hassort1=> $hassort1,
188 hassort2=> $hassort2,
191 CGIextChoice => $CGIextChoice,
192 CGIsepChoice => $CGIsepChoice,
193 locationloop => \@locations,
194 ccodeloop => \@ccodes,
195 branchloop => \@branchloop,
197 output_html_with_http_headers $input, $cookie, $template->output;
204 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
213 my $dbh = C4::Context->dbh;
219 for (my $i=0;$i<=10;$i++) {
221 if ( @$filters[$i] ) {
222 if (($i==1) and (@$filters[$i-1])) {
223 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
225 # format the dates filters, otherwise just fill as is
227 $cell{filter} .= @$filters[$i];
229 $cell{filter} .= format_date(@$filters[$i]);
231 $cell{crit} .="Period From" if ($i==0);
232 $cell{crit} .="Period To" if ($i==1);
233 $cell{crit} .="Patron Category=" if ($i==2);
234 $cell{crit} .="Item Type=" if ($i==3);
235 $cell{crit} .="Library=" if ($i==4);
236 $cell{crit} .="Collection=" if ($i==5);
237 $cell{crit} .="Location=" if ($i==6);
238 $cell{crit} .="Item callnumber>=" if ($i==7);
239 $cell{crit} .="Item callnumber<" if ($i==8);
240 $cell{crit} .="sort1=" if ($i==9);
241 $cell{crit} .="sort2=" if ($i==10);
242 # FIXME - no translation mechanism !
243 push @loopfilter, \%cell;
246 push @loopfilter,{crit=>"Event",filter=>$type};
247 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
248 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
249 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
253 $debug and warn "filtres ". join "|", @filters;
254 my ($colsource, $linesource);
255 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
256 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
257 if ($line =~ /category/ ) {
258 $linefilter[0] = @$filters[2] ;
260 if ($line =~ /itemtype/ ) {
261 $linefilter[0] = @$filters[3] ;
263 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
264 if ($line =~ /ccode/ ) {
265 $linefilter[0] = @$filters[5] ;
266 $linesource = 'items';
268 if ($line =~ /location/ ) {
269 $linefilter[0] = @$filters[6] ;
270 $linesource = 'items';
272 $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
273 $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
276 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
277 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
278 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
279 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
280 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
281 if ($column =~ /ccode/ ) {
282 $colfilter[0] = @$filters[5] ;
283 $colsource = 'items';
285 if ($column =~ /location/ ) {
286 $colfilter[0] = @$filters[6] ;
287 $colsource = 'items';
289 $colfilter[0] = @$filters[9] if ($column =~ /sort1/ ) ;
290 $colfilter[0] = @$filters[10] if ($column =~ /sort2/ ) ;
293 if (($line =~/datetime/) and ($dsp == 1)) {
295 $linefield .="dayname($line)";
296 } elsif (($line=~/datetime/) and ($dsp == 2)) {
298 $linefield .="monthname($line) ";
299 } elsif (($line=~/datetime/) and ($dsp == 3)) {
301 $linefield .="Year($line)";
302 } elsif ($line=~/datetime/) {
303 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
307 my $lineorder = $linefield;
308 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
309 $lineorder = "month($line)" if $linefield =~ "^month";
310 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
313 $strsth .= "select distinctrow $linefield from statistics, ";
314 # get stats on items if ccode or location, otherwise borrowers.
315 $strsth .= ($linesource eq 'items' ) ?
316 "items where (statistics.itemnumber=items.itemnumber) "
317 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
318 $strsth .= " and $line is not null ";
320 if ($line=~/datetime/) {
321 if ($linefilter[1] and ($linefilter[0])){
322 $strsth .= " and $line between ? and ? " ;
323 } elsif ($linefilter[1]) {
324 $strsth .= " and $line < ? " ;
325 } elsif ($linefilter[0]) {
326 $strsth .= " and $line > ? " ;
328 $strsth .= " and type ='".$type."' " if $type;
329 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
330 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
331 } elsif ($linefilter[0]) {
332 $linefilter[0] =~ s/\*/%/g;
333 $strsth .= " and $line LIKE ? " ;
335 $strsth .=" group by $linefield";
336 $strsth .=" order by $lineorder";
337 $debug and warn $strsth;
338 my $sth = $dbh->prepare( $strsth );
339 if (( @linefilter ) and ($linefilter[1])){
340 $sth->execute($linefilter[0],$linefilter[1]);
341 } elsif ($linefilter[0]) {
342 $sth->execute($linefilter[0]);
347 while ( my ($celvalue) = $sth->fetchrow) {
349 if($line =~ /ccode/) {
350 $cell{rowtitle_display} = $ccodes->{$celvalue};
351 } elsif($line=~/location/) {
352 $cell{rowtitle_display} = $locations->{$celvalue};
353 } elsif($line=~/sort1/) {
354 for my $s (@$Bsort1) {
355 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
357 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
358 } elsif($line=~/sort2/) {
359 for my $s (@$Bsort2) {
360 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
362 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
363 } elsif($line=~/categorycode/) {
364 for my $s (@$categoryloop) {
365 $cell{rowtitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
367 $cell{rowtitle_display} = $celvalue unless $cell{rowtitle_display};
368 } elsif($line=~/itemtype/) {
369 $cell{rowtitle_display} = $itemtypes->{$celvalue}->{description};
371 $cell{rowtitle_display} = $celvalue;
374 $cell{rowtitle} = $celvalue;
376 $cell{rowtitle} = "";
379 push @loopline, \%cell;
385 if (($column =~/datetime/) and ($dsp == 1)) {
387 $colfield .="dayname($column)";
388 } elsif (($column=~/datetime/) and ($dsp == 2)) {
390 $colfield .="monthname($column)";
391 } elsif (($column=~/datetime/) and ($dsp == 3)) {
393 $colfield .="Year($column)";
394 } elsif ($column=~/datetime/) {
395 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
397 $colfield .= $column;
399 $colorder = "weekday($line)" if $colfield =~ "^dayname";
400 $colorder = "month($line)" if $colfield =~ "^month";
401 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
404 $strsth2 .= "select distinctrow $colfield from statistics, ";
405 # get stats on items if ccode or location, otherwise borrowers.
406 $strsth2 .= ($colsource eq 'items' ) ?
407 "items where (statistics.itemnumber=items.itemnumber) "
408 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
409 $strsth2 .= " 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";
429 my $sth2 = $dbh->prepare( $strsth2 );
430 if (( @colfilter ) and ($colfilter[1])){
431 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
432 } elsif ($colfilter[0]) {
433 $sth2->execute($colfilter[0]);
439 while (my ($celvalue) = $sth2->fetchrow) {
442 if($column =~ /ccode/) {
443 $cell{coltitle_display} = $ccodes->{$celvalue};
444 } elsif($column=~/location/) {
445 $cell{coltitle_display} = $locations->{$celvalue};
446 } elsif($column=~/itemtype/) {
447 $cell{coltitle_display} = $itemtypes->{$celvalue}->{description};
448 } elsif($column=~/sort1/) {
449 for my $s (@$Bsort1) {
450 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
452 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
453 } elsif($column=~/sort2/) {
454 for my $s (@$Bsort2) {
455 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
457 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
458 } elsif($column=~/category/) {
459 for my $s (@$categoryloop) {
460 $cell{coltitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
462 $cell{coltitle_display} = $celvalue unless $cell{coltitle_display};
464 $cell{coltitle_display} = $celvalue;
466 $cell{coltitle} = $celvalue;
467 # we leave this as 'coltitle' since we use it as hash key when filling the table, and add a title_display key.
469 push @loopcol, \%cell;
475 #Initialization of cell values.....
477 foreach my $row ( @loopline ) {
478 foreach my $col ( @loopcol ) {
479 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
480 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
482 $table{$row->{rowtitle}}->{totalrow}=0;
485 # preparing calculation
488 $strcalc .= "SELECT $linefield, $colfield, ";
489 $strcalc .= "COUNT( * ) " if ($process ==1);
491 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
494 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
497 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
498 $rqbookcount->execute;
499 my ($bookcount) = $rqbookcount->fetchrow;
500 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
502 $strcalc .= "FROM statistics ";
503 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
504 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if ( ($colsource eq 'items') || @$filters[5] || @$filters[6] ||@$filters[7] || @$filters[8] );
506 $strcalc .= "WHERE 1=1 ";
507 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
508 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
509 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
510 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
511 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
512 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
513 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
514 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
515 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
516 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
517 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
518 $strcalc .= " AND items.ccode like '" . @$filters[5] ."'" if ( @$filters[5] );
519 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
520 $strcalc .= " AND items.location like '" . @$filters[6] ."'" if ( @$filters[6] );
521 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
522 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] ."'" if ( @$filters[7] );
523 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
524 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] ."'" if ( @$filters[8] );
525 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
526 $strcalc .= " AND borrowers.sort1 like '" . @$filters[9] ."'" if ( @$filters[9] );
527 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
528 $strcalc .= " AND borrowers.sort2 like '" . @$filters[10] ."'" if ( @$filters[10] );
529 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
530 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
531 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
533 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
534 ($debug) and warn "". $strcalc;
535 my $dbcalc = $dbh->prepare($strcalc);
537 my ($emptycol,$emptyrow);
538 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
539 ($debug) and warn "filling table $row / $col / $value ";
548 $table{$row}->{$col}+=$value;
549 $table{$row}->{totalrow}+=$value;
550 $grantotal += $value;
552 push @loopcol,{coltitle => "NULL", coltitle_display => 'NULL'} if ($emptycol);
553 push @loopline,{rowtitle => "NULL", rowtitle_display => 'NULL'} if ($emptyrow);
555 foreach my $row (@loopline) {
557 #@loopcol ensures the order for columns is common with column titles
558 # and the number matches the number of columns
559 foreach my $col ( @loopcol ) {
560 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
561 push @loopcell, {value => $value } ;
563 push @looprow, { 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
564 'rowtitle_display' => ($row->{rowtitle_display} eq "NULL")?"NULL":$row->{rowtitle_display},
565 'loopcell' => \@loopcell,
566 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
569 for my $col ( @loopcol ) {
571 foreach my $row ( @looprow ) {
572 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
573 $debug and warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
575 push @loopfooter, {'totalcol' => $total};
579 # the header of the table
580 $globalline{loopfilter}=\@loopfilter;
581 # the core of the table
582 $globalline{looprow} = \@looprow;
583 $globalline{loopcol} = \@loopcol;
584 # # the foot (totals by borrower type)
585 $globalline{loopfooter} = \@loopfooter;
586 $globalline{total}= $grantotal;
587 $globalline{line} = $line;
588 $globalline{column} = $column;
589 push @mainloop,\%globalline;