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
23 my $do_it=$input->param('do_it');
24 my $fullreportname = "reports/issues_stats.tmpl";
25 my $line = $input->param("Line");
26 my $column = $input->param("Column");
27 my @filters = $input->param("Filter");
28 my $podsp = $input->param("DisplayBy");
29 my $type = $input->param("PeriodTypeSel");
30 my $daysel = $input->param("PeriodDaySel");
31 my $monthsel = $input->param("PeriodMonthSel");
32 my $calc = $input->param("Cellvalue");
33 my $output = $input->param("output");
34 my $basename = $input->param("basename");
35 my $mime = $input->param("MIME");
36 my $del = $input->param("sep");
37 #warn "calcul : ".$calc;
38 my ($template, $borrowernumber, $cookie)
39 = get_template_and_user({template_name => $fullreportname,
43 flagsrequired => {editcatalogue => 1},
46 $template->param(do_it => $do_it);
49 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
50 if ($output eq "screen"){
51 # Printing results to screen
52 $template->param(mainloop => $results);
53 output_html_with_http_headers $input, $cookie, $template->output;
56 # Printing to a csv file
57 print $input->header(-type => 'application/vnd.sun.xml.calc',
58 -attachment=>"$basename.csv",
59 -filename=>"$basename.csv" );
60 my $cols = @$results[0]->{loopcol};
61 my $lines = @$results[0]->{looprow};
63 $sep =C4::Context->preference("delimiter");
65 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
67 foreach my $col ( @$cols ) {
68 print $col->{coltitle}.$sep;
72 foreach my $line ( @$lines ) {
73 my $x = $line->{loopcell};
74 print $line->{rowtitle}.$sep;
75 foreach my $cell (@$x) {
76 print $cell->{value}.$sep;
78 print $line->{totalrow};
83 $cols = @$results[0]->{loopfooter};
84 foreach my $col ( @$cols ) {
85 print $sep.$col->{totalcol};
87 print $sep.@$results[0]->{total};
92 my $dbh = C4::Context->dbh;
97 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
102 while (my ($value, $desc) =$req->fetchrow) {
103 push @select, $value;
104 $select{$value}=$desc;
106 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
113 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
119 while (my ($value,$desc) =$req->fetchrow) {
120 push @select, $value;
121 $select{$value}=$desc;
123 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
130 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
135 while (my ($value) =$req->fetchrow) {
136 $hassort1 =1 if ($value);
137 push @select, $value;
139 my $branches=getbranches();
142 push @select_branch,"";
143 $select_branches{""} = "";
144 foreach my $branch (keys %$branches) {
145 push @select_branch, $branch;
146 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
148 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
150 -values => \@select_branch,
151 -labels => \%select_branches,
155 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
161 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
167 while (my ($value) =$req->fetchrow) {
168 $hassort2 =1 if ($value);
169 $hglghtsort2= !($hassort1);
170 push @select, $value;
172 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
178 my @mime = ( C4::Context->preference("MIME") );
179 # foreach my $mime (@mime){
183 my $CGIextChoice=CGI::scrolling_list(
190 my @dels = ( C4::Context->preference("delimiter") );
191 my $CGIsepChoice=CGI::scrolling_list(
199 CGIBorCat => $CGIBorCat,
200 CGIItemType => $CGIItemTypes,
201 CGIBranch => $CGIBranch,
202 hassort1=> $hassort1,
203 hassort2=> $hassort2,
204 HlghtSort2 => $hglghtsort2,
205 CGISort1 => $CGISort1,
206 CGISort2 => $CGISort2,
207 CGIextChoice => $CGIextChoice,
208 CGIsepChoice => $CGIsepChoice
210 output_html_with_http_headers $input, $cookie, $template->output;
217 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
226 my $dbh = C4::Context->dbh;
232 for (my $i=0;$i<=6;$i++) {
234 if ( @$filters[$i] ) {
235 if (($i==1) and (@$filters[$i-1])) {
236 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
238 $cell{filter} .= @$filters[$i];
239 $cell{crit} .="Period From" if ($i==0);
240 $cell{crit} .="Period To" if ($i==1);
241 $cell{crit} .="Borrower Cat" if ($i==2);
242 $cell{crit} .="Doc Type" if ($i==3);
243 $cell{crit} .="Branch" if ($i==4);
244 $cell{crit} .="Sort1" if ($i==5);
245 $cell{crit} .="Sort2" if ($i==6);
246 push @loopfilter, \%cell;
249 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
250 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
251 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
252 push @loopfilter,{crit=>"Select Month ",filter=>$daysel} if ($monthsel);
256 # warn "filtres ".@filters[0];
257 # warn "filtres ".@filters[1];
258 # warn "filtres ".@filters[2];
259 # warn "filtres ".@filters[3];
261 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
262 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
263 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
264 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
265 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
266 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
267 $linefilter[0] = @$filters[5] if ($line =~ /sort1/ ) ;
268 $linefilter[0] = @$filters[6] if ($line =~ /sort2/ ) ;
269 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
272 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
273 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
274 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
275 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
276 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
277 $colfilter[0] = @$filters[5] if ($column =~ /sort1/ ) ;
278 $colfilter[0] = @$filters[6] if ($column =~ /sort2/ ) ;
279 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
283 if (($line =~/datetime/) and ($dsp == 1)) {
285 $linefield .="dayname($line)";
286 } elsif (($line=~/datetime/) and ($dsp == 2)) {
288 $linefield .="monthname($line)";
289 } elsif (($line=~/datetime/) and ($dsp == 3)) {
291 $linefield .="Year($line)";
292 } elsif ($line=~/datetime/) {
293 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
299 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
301 if ($line=~/datetime/) {
302 if ($linefilter[1] and ($linefilter[0])){
303 $strsth .= " and $line between ? and ? " ;
304 } elsif ($linefilter[1]) {
305 $strsth .= " and $line < ? " ;
306 } elsif ($linefilter[0]) {
307 $strsth .= " and $line > ? " ;
309 $strsth .= " and type ='".$type."' " if $type;
310 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
311 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
312 } elsif ($linefilter[0]) {
313 $linefilter[0] =~ s/\*/%/g;
314 $strsth .= " and $line LIKE ? " ;
316 $strsth .=" group by $linefield";
317 $strsth .=" order by $linefield";
320 my $sth = $dbh->prepare( $strsth );
321 if (( @linefilter ) and ($linefilter[1])){
322 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
323 } elsif ($linefilter[0]) {
324 $sth->execute($linefilter[0]);
329 while ( my ($celvalue) = $sth->fetchrow) {
332 $cell{rowtitle} = $celvalue;
334 $cell{rowtitle} = "";
337 push @loopline, \%cell;
342 if (($column =~/datetime/) and ($dsp == 1)) {
344 $colfield .="dayname($column)";
345 } elsif (($column=~/datetime/) and ($dsp == 2)) {
347 $colfield .="monthname($column)";
348 } elsif (($column=~/datetime/) and ($dsp == 3)) {
350 $colfield .="Year($column)";
351 } elsif ($column=~/datetime/) {
352 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
354 $colfield .= $column;
358 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
360 if ($column=~/datetime/){
361 if (($colfilter[1]) and ($colfilter[0])){
362 $strsth2 .= " and $column between ? and ? " ;
363 } elsif ($colfilter[1]) {
364 $strsth2 .= " and $column < ? " ;
365 } elsif ($colfilter[0]) {
366 $strsth2 .= " and $column > ? " ;
368 $strsth2 .= " and type ='".$type."' " if $type;
369 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
370 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
371 } elsif ($colfilter[0]) {
372 $colfilter[0] =~ s/\*/%/g;
373 $strsth2 .= " and $column LIKE ? " ;
375 $strsth2 .=" group by $colfield";
376 $strsth2 .=" order by $colfield";
379 my $sth2 = $dbh->prepare( $strsth2 );
380 if (( @colfilter ) and ($colfilter[1])){
381 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
382 } elsif ($colfilter[0]) {
383 $sth2->execute($colfilter[0]);
389 while (my ($celvalue) = $sth2->fetchrow) {
392 # warn "coltitle :".$celvalue;
393 $cell{coltitle} = $celvalue;
395 push @loopcol, \%cell;
397 # warn "fin des titres colonnes";
403 #Initialization of cell values.....
406 foreach my $row ( @loopline ) {
407 foreach my $col ( @loopcol ) {
408 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
409 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
411 $table{$row->{rowtitle}}->{totalrow}=0;
414 # preparing calculation
417 $strcalc .= "SELECT $linefield, $colfield, ";
418 $strcalc .= "COUNT( * ) " if ($process ==1);
420 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
421 $rqbookcount->execute;
422 my ($bookcount) = $rqbookcount->fetchrow;
423 $strcalc .= "100*(COUNT(itemnumber))/ $bookcount " ;
425 $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
427 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
428 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
429 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
430 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
431 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
432 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
433 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
434 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
435 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
436 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
437 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
438 $strcalc .= " AND borrowers.sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
439 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
440 $strcalc .= " AND borrowers.sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
441 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
442 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
443 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
445 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
447 my $dbcalc = $dbh->prepare($strcalc);
449 # warn "filling table";
451 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
452 # warn "filling table $row / $col / $value ";
453 $emptycol = 1 if ($col eq undef);
454 $col = "zzEMPTY" if ($col eq undef);
455 $row = "zzEMPTY" if ($row eq undef);
457 $table{$row}->{$col}+=$value;
458 $table{$row}->{totalrow}+=$value;
459 $grantotal += $value;
461 push @loopcol,{coltitle => "NULL"} if ($emptycol);
463 foreach my $row ( sort keys %table ) {
465 #@loopcol ensures the order for columns is common with column titles
466 # and the number matches the number of columns
467 foreach my $col ( @loopcol ) {
468 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
469 push @loopcell, {value => $value } ;
471 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
472 'loopcell' => \@loopcell,
473 'hilighted' => ($hilighted >0),
474 'totalrow' => $table{$row}->{totalrow}
476 $hilighted = -$hilighted;
479 # warn "footer processing";
480 foreach my $col ( @loopcol ) {
482 foreach my $row ( @looprow ) {
483 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
484 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
486 # warn "summ for column ".$col->{coltitle}." = ".$total;
487 push @loopfooter, {'totalcol' => $total};
491 # the header of the table
492 $globalline{loopfilter}=\@loopfilter;
493 # the core of the table
494 $globalline{looprow} = \@looprow;
495 $globalline{loopcol} = \@loopcol;
496 # # the foot (totals by borrower type)
497 $globalline{loopfooter} = \@loopfooter;
498 $globalline{total}= $grantotal;
499 $globalline{line} = $line;
500 $globalline{column} = $column;
501 push @mainloop,\%globalline;