Fixing Bug : Not Opening the spreadsheet manager software on file exports.
[koha.git] / reports / issues_stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use HTML::Template;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39
40 =over2
41
42 =cut
43
44 my $input = new CGI;
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,
62                                 query => $input,
63                                 type => "intranet",
64                                 authnotrequired => 0,
65                                 flagsrequired => {editcatalogue => 1},
66                                 debug => 1,
67                                 });
68 $template->param(do_it => $do_it);
69 if ($do_it) {
70 # Displaying results
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;
76                 exit(1);
77         } else {
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};
84                 my $sep;
85                 $sep =C4::Context->preference("delimiter");
86 # header top-right
87                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 # Other header
89                 foreach my $col ( @$cols ) {
90                         print $col->{coltitle}.$sep;
91                 }
92                 print "Total\n";
93 # Table
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;
99                         }
100                         print $line->{totalrow};
101                         print "\n";
102                 }
103 # footer
104                 print "TOTAL";
105                 $cols = @$results[0]->{loopfooter};
106                 foreach my $col ( @$cols ) {
107                         print $sep.$col->{totalcol};
108                 }
109                 print $sep.@$results[0]->{total};
110                 exit(1);
111         }
112 # Displaying choices
113 } else {
114         my $dbh = C4::Context->dbh;
115         my @values;
116         my %labels;
117         my %select;
118         my $req;
119         $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
120         $req->execute;
121         my @select;
122         push @select,"";
123         $select{""}="";
124         while (my ($value, $desc) =$req->fetchrow) {
125                 push @select, $value;
126                 $select{$value}=$desc;
127         }
128         my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
129                                 -id => 'Filter',
130                                 -values   => \@select,
131                                 -labels   => \%select,
132                                 -size     => 1,
133                                 -multiple => 0 );
134         
135         $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
136         $req->execute;
137         undef @select;
138         undef %select;
139         push @select,"";
140         $select{""}="";
141         while (my ($value,$desc) =$req->fetchrow) {
142                 push @select, $value;
143                 $select{$value}=$desc;
144         }
145         my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
146                                 -id => 'Filter',
147                                 -values   => \@select,
148                                 -labels    => \%select,
149                                 -size     => 1,
150                                 -multiple => 0 );
151         
152         $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
153         $req->execute;
154         undef @select;
155         push @select,"";
156         my $hassort1;
157         while (my ($value) =$req->fetchrow) {
158                 $hassort1 =1 if ($value);
159                 push @select, $value;
160         }
161         my $branches=getbranches();
162         my @select_branch;
163         my %select_branches;
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'};
169         }
170         my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
171                                 -id => 'Filter',
172                                 -values   => \@select_branch,
173                                 -labels   => \%select_branches,
174                                 -size     => 1,
175                                 -multiple => 0 );
176         
177         my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
178                                 -id => 'Filter',
179                                 -values   => \@select,
180                                 -size     => 1,
181                                 -multiple => 0 );
182         
183         $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
184         $req->execute;
185         undef @select;
186         push @select,"";
187         my $hassort2;
188         my $hglghtsort2;
189         while (my ($value) =$req->fetchrow) {
190                 $hassort2 =1 if ($value);
191                 $hglghtsort2= !($hassort1);
192                 push @select, $value;
193         }
194         my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
195                                 -id => 'Filter',
196                                 -values   => \@select,
197                                 -size     => 1,
198                                 -multiple => 0 );
199         
200         my @mime = ( C4::Context->preference("MIME") );
201 #       foreach my $mime (@mime){
202 #               warn "".$mime;
203 #       }
204         
205         my $CGIextChoice=CGI::scrolling_list(
206                                 -name     => 'MIME',
207                                 -id       => 'MIME',
208                                 -values   => \@mime,
209                                 -size     => 1,
210                                 -multiple => 0 );
211         
212         my @dels = ( C4::Context->preference("delimiter") );
213         my $CGIsepChoice=CGI::scrolling_list(
214                                 -name     => 'sep',
215                                 -id       => 'sep',
216                                 -values   => \@dels,
217                                 -size     => 1,
218                                 -multiple => 0 );
219         
220         $template->param(
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
231                                         );
232 output_html_with_http_headers $input, $cookie, $template->output;
233 }
234
235
236
237
238 sub calculate {
239         my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
240         my @mainloop;
241         my @loopfooter;
242         my @loopcol;
243         my @loopline;
244         my @looprow;
245         my %globalline;
246         my $grantotal =0;
247 # extract parameters
248         my $dbh = C4::Context->dbh;
249
250 # Filters
251 # Checking filters
252 #
253         my @loopfilter;
254         for (my $i=0;$i<=6;$i++) {
255                 my %cell;
256                 if ( @$filters[$i] ) {
257                         if (($i==1) and (@$filters[$i-1])) {
258                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
259                         }
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;
269                 }
270         }
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);
275         
276         
277         my @linefilter;
278 #       warn "filtres ".@filters[0];
279 #       warn "filtres ".@filters[1];
280 #       warn "filtres ".@filters[2];
281 #       warn "filtres ".@filters[3];
282         
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];
292
293         my @colfilter ;
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];
302                                               
303 # 1st, loop rows.                             
304         my $linefield;                               
305         if (($line =~/datetime/) and ($dsp == 1)) {
306                 #Display by day
307                 $linefield .="dayname($line)";  
308         } elsif (($line=~/datetime/) and ($dsp == 2)) {
309                 #Display by Month
310                 $linefield .="monthname($line)";  
311         } elsif (($line=~/datetime/) and ($dsp == 3)) {
312                 #Display by Year
313                 $linefield .="Year($line)";
314         } elsif ($line=~/datetime/) {
315                 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
316         } else {
317                 $linefield .= $line;
318         }  
319         
320         my $strsth;
321         $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
322         
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 > ? " ;
330                 }
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 ? " ;
337         }
338         $strsth .=" group by $linefield";
339         $strsth .=" order by $linefield";
340 #       warn "". $strsth;
341         
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]);
347         } else {
348                 $sth->execute;
349         }
350         
351         while ( my ($celvalue) = $sth->fetchrow) {
352                 my %cell;
353                 if ($celvalue) {
354                         $cell{rowtitle} = $celvalue;
355                 } else {
356                         $cell{rowtitle} = "";
357                 }
358                 $cell{totalrow} = 0;
359                 push @loopline, \%cell;
360         }
361
362 # 2nd, loop cols.
363         my $colfield;                               
364         if (($column =~/datetime/) and ($dsp == 1)) {
365                 #Display by day
366                 $colfield .="dayname($column)";  
367         } elsif (($column=~/datetime/) and ($dsp == 2)) {
368                 #Display by Month
369                 $colfield .="monthname($column)";  
370         } elsif (($column=~/datetime/) and ($dsp == 3)) {
371                 #Display by Year
372                 $colfield .="Year($column)";
373         } elsif ($column=~/datetime/) {
374                 $colfield .='date_format(`datetime`,"%Y-%m-%d")';       
375         } else {
376                 $colfield .= $column;
377         }  
378         
379         my $strsth2;
380         $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
381         
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 > ? " ;
389                 }
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 ? " ;
396         }
397         $strsth2 .=" group by $colfield";
398         $strsth2 .=" order by $colfield";
399 #       warn "". $strsth2;
400         
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]);
406         } else {
407                 $sth2->execute;
408         }
409         
410
411         while (my ($celvalue) = $sth2->fetchrow) {
412                 my %cell;
413                 my %ft;
414 #               warn "coltitle :".$celvalue;
415                 $cell{coltitle} = $celvalue;
416                 $ft{totalcol} = 0;
417                 push @loopcol, \%cell;
418         }
419 #       warn "fin des titres colonnes";
420
421         my $i=0;
422         my @totalcol;
423         my $hilighted=-1;
424         
425         #Initialization of cell values.....
426         my %table;
427 #       warn "init table";
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;
432                 }
433                 $table{$row->{rowtitle}}->{totalrow}=0;
434         }
435
436 # preparing calculation
437         my $strcalc ;
438         $strcalc .= "SELECT $linefield, $colfield, ";
439         $strcalc .= "COUNT( * ) " if ($process ==1);
440         if ($process ==3){
441         my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
442         $rqbookcount->execute;
443         my ($bookcount) = $rqbookcount->fetchrow;
444         $strcalc .= "100*(COUNT(itemnumber))/ $bookcount " ;
445         }
446         $strcalc .= "FROM statistics,borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null and $line is not null ";
447
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 );
465         
466         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
467 #       warn "". $strcalc;
468         my $dbcalc = $dbh->prepare($strcalc);
469         $dbcalc->execute;
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;
476         }
477         
478         foreach my $row ( sort keys %table ) {
479                 my @loopcell;
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}}} ;
483                 }
484                 push @looprow,{ 'rowtitle' => $row,
485                                                 'loopcell' => \@loopcell,
486                                                 'hilighted' => 1 ,
487                                                 'totalrow' => $table{$row}->{totalrow}
488                                         };
489                 $hilighted = -$hilighted;
490         }
491         
492 #       warn "footer processing";
493         foreach my $col ( @loopcol ) {
494                 my $total=0;
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};
498                 }
499 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
500                 push @loopfooter, {'totalcol' => $total};
501         }
502                         
503
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;
515         return \@mainloop;
516 }
517
518 1;