Improving top lists display
[koha.git] / reports / acquisitions_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/acquisitions_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("PlacedOnDisplay");
51 my $rodsp = $input->param("ReceivedOnDisplay");
52 my $calc = $input->param("Cellvalue");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
57 #warn "calcul : ".$calc;
58 my ($template, $borrowernumber, $cookie)
59         = get_template_and_user({template_name => $fullreportname,
60                                 query => $input,
61                                 type => "intranet",
62                                 authnotrequired => 0,
63                                 flagsrequired => {editcatalogue => 1},
64                                 debug => 1,
65                                 });
66 $template->param(do_it => $do_it);
67 if ($do_it) {
68         my $results = calculate($line, $column, $podsp, $rodsp, $calc, \@filters);
69         if ($output eq "screen"){
70                 $template->param(mainloop => $results);
71                 output_html_with_http_headers $input, $cookie, $template->output;
72                 exit(1);
73         } else {
74                 print $input->header(-type => 'application/vnd.sun.xml.calc', 
75                                                          -attachment=>"$basename.csv",
76                                                          -name=>"$basename.csv" );
77                 my $cols = @$results[0]->{loopcol};
78                 my $lines = @$results[0]->{looprow};
79                 my $sep;
80                 $sep =C4::Context->preference("delimiter");
81                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82                 foreach my $col ( @$cols ) {
83                         print $col->{coltitle}.$sep;
84                 }
85                 print "Total\n";
86                 foreach my $line ( @$lines ) {
87                         my $x = $line->{loopcell};
88                         print $line->{rowtitle}.$sep;
89                         foreach my $cell (@$x) {
90                                 print $cell->{value}.$sep;
91                         }
92                         print $line->{totalrow};
93                         print "\n";
94                 }
95                 print "TOTAL";
96                 $cols = @$results[0]->{loopfooter};
97                 foreach my $col ( @$cols ) {
98                         print $sep.$col->{totalcol};
99                 }
100                 print $sep.@$results[0]->{total};
101                 exit(1);
102         }
103 } else {
104         my $dbh = C4::Context->dbh;
105         my @values;
106         my %labels;
107         my %select;
108         my $req;
109         $req = $dbh->prepare("select distinctrow id,name from aqbooksellers order by name");
110         $req->execute;
111         my @select;
112         push @select,"";
113 #       $select{""}="";
114         while (my ($value, $desc) =$req->fetchrow) {
115                 push @select, $desc;
116 #               $select{$value}=$desc;
117         }
118         my $CGIBookSellers=CGI::scrolling_list( -name     => 'Filter',
119                                 -id => 'Filter',
120                                 -values   => \@select,
121 #                               -labels   => \%select,
122                                 -size     => 1,
123                                 -multiple => 0 );
124         
125         $req = $dbh->prepare( "select distinctrow bookfundid,bookfundname from aqbookfund order by bookfundname");
126         $req->execute;
127         undef @select;
128         undef %select;
129         push @select,"";
130         $select{""}="";
131         while (my ($value,$desc) =$req->fetchrow) {
132                 push @select, $value;
133                 $select{$value}=$desc;
134         }
135         my $CGIBudget=CGI::scrolling_list( -name     => 'Filter',
136                                 -id => 'Filter',
137                                 -values   => \@select,
138                                 -labels    => \%select,
139                                 -size     => 1,
140                                 -multiple => 0 );
141         
142         $req = $dbh->prepare("select distinctrow sort1 from aqorders where sort1 is not null order by sort1");
143         $req->execute;
144         undef @select;
145         push @select,"";
146         my $hassort1;
147         while (my ($value) =$req->fetchrow) {
148                 $hassort1 =1 if ($value);
149                 push @select, $value;
150         }
151         my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
152                                 -id => 'Filter',
153                                 -values   => \@select,
154                                 -size     => 1,
155                                 -multiple => 0 );
156         
157         $req = $dbh->prepare("select distinctrow sort2 from aqorders where sort2 is not null order by sort2");
158         $req->execute;
159         undef @select;
160         push @select,"";
161         my $hassort2;
162         my $hglghtsort2;
163         while (my ($value) =$req->fetchrow) {
164                 $hassort2 =1 if ($value);
165                 $hglghtsort2= !($hassort1);
166                 push @select, $value;
167         }
168         my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
169                                 -id => 'Filter',
170                                 -values   => \@select,
171                                 -size     => 1,
172                                 -multiple => 0 );
173         
174         my @mime = ( C4::Context->preference("MIME") );
175         foreach my $mime (@mime){
176 #               warn "".$mime;
177         }
178         
179         my $CGIextChoice=CGI::scrolling_list(
180                                 -name     => 'MIME',
181                                 -id       => 'MIME',
182                                 -values   => \@mime,
183                                 -size     => 1,
184                                 -multiple => 0 );
185         
186         my @dels = ( C4::Context->preference("delimiter") );
187         my $CGIsepChoice=CGI::scrolling_list(
188                                 -name     => 'sep',
189                                 -id       => 'sep',
190                                 -values   => \@dels,
191                                 -size     => 1,
192                                 -multiple => 0 );
193         
194         $template->param(
195                                         CGIBookSeller => $CGIBookSellers,
196                                         CGIBudget => $CGIBudget,
197                                         hassort1=> $hassort1,
198                                         hassort2=> $hassort2,
199                                         HlghtSort2 => $hglghtsort2,
200                                         CGISort1 => $CGISort1,
201                                         CGISort2 => $CGISort2,
202                                         CGIextChoice => $CGIextChoice,
203                                         CGIsepChoice => $CGIsepChoice
204                                         );
205
206 }
207 output_html_with_http_headers $input, $cookie, $template->output;
208
209
210
211 sub calculate {
212         my ($line, $column, $podsp, $rodsp, ,$process, $filters) = @_;
213         my @mainloop;
214         my @loopfooter;
215         my @loopcol;
216         my @loopline;
217         my @looprow;
218         my %globalline;
219         my $grantotal =0;
220 # extract parameters
221         my $dbh = C4::Context->dbh;
222
223 # Filters
224 # Checking filters
225 #
226         my @loopfilter;
227         for (my $i=0;$i<=7;$i++) {
228                 my %cell;
229                 if ( @$filters[$i] ) {
230                         if ((($i==1) or ($i==3)) and (@$filters[$i-1])) {
231                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
232                         }
233                         $cell{filter} .= @$filters[$i];
234                         $cell{crit} .="Placed On From" if ($i==0);
235                         $cell{crit} .="Placed On To" if ($i==1);
236                         $cell{crit} .="Received On From" if ($i==2);
237                         $cell{crit} .="Received On To" if ($i==3);
238                         $cell{crit} .="BookSeller" if ($i==4);
239                         $cell{crit} .="Budget" if ($i==5);
240                         $cell{crit} .="Sort1" if ($i==6);
241                         $cell{crit} .="Sort2" if ($i==7);
242                         push @loopfilter, \%cell;
243                 }
244         }
245         
246         my @linefilter;
247 #       warn "filtres ".@filters[0];
248 #       warn "filtres ".@filters[1];
249 #       warn "filtres ".@filters[2];
250 #       warn "filtres ".@filters[3];
251         
252         $linefilter[0] = @$filters[0] if ($line =~ /closedate/ )  ;
253         $linefilter[1] = @$filters[1] if ($line =~ /closedate/ )  ;
254         $linefilter[0] = @$filters[2] if ($line =~ /received/ )  ;
255         $linefilter[1] = @$filters[3] if ($line =~ /received/ )  ;
256         $linefilter[0] = @$filters[4] if ($line =~ /bookseller/ )  ;
257         $linefilter[0] = @$filters[5] if ($line =~ /bookfund/ )  ;
258         $linefilter[0] = @$filters[6] if ($line =~ /sort1/ )  ;
259         $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
260 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
261
262         my @colfilter ;
263         $colfilter[0] = @$filters[0] if ($column =~ /closedate/ ) ;
264         $colfilter[1] = @$filters[1] if ($column =~ /closedate/ ) ;
265         $colfilter[0] = @$filters[2] if ($column =~ /received/ )  ;
266         $colfilter[1] = @$filters[3] if ($column =~ /received/ )  ;
267         $colfilter[0] = @$filters[4] if ($column =~ /bookseller/ );
268         $colfilter[0] = @$filters[5] if ($column =~ /bookfund/ )  ;
269         $colfilter[0] = @$filters[6] if ($column =~ /sort1/ )     ;
270         $colfilter[0] = @$filters[7] if ($column =~ /sort2/ )     ;
271 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
272                                               
273 # 1st, loop rows.                             
274         my $linefield;                               
275         if (($line =~/closedate/) and ($podsp == 1)) {
276                 #Display by day
277                 $linefield .="dayname($line)";  
278         } elsif (($line=~/closedate/) and ($podsp == 2)) {
279                 #Display by Month
280                 $linefield .="monthname($line)";  
281         } elsif (($line=~/closedate/) and ($podsp == 3)) {
282                 #Display by Year
283                 $linefield .="Year($line)";
284         } elsif (($line =~/received/) and ($rodsp == 1)) {
285                 #Display by day
286                 $linefield .="dayname($line)";  
287         } elsif (($line=~/received/) and ($rodsp == 2)) {
288                 #Display by Month
289                 $linefield .="monthname($line)";  
290         } elsif (($line=~/received/) and ($rodsp == 3)) {
291                 #Display by Year
292                 $linefield .="Year($line)";
293         } else {
294                 $linefield .= $line;
295         }  
296         
297         my $strsth;
298         $strsth .= "select distinctrow $linefield from aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) and $line is not null ";
299         
300         if ( @linefilter ) {
301                 if ($linefilter[1]){
302                         if ($linefilter[0]){
303                                 $strsth .= " and $line between ? and ? " ;
304                         } else {
305                                 $strsth .= " and $line < ? " ;
306                         }
307                 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/))){
308                         $strsth .= " and $line > ? " ;
309                 } elsif ($linefilter[0]) {
310                         $linefilter[0] =~ s/\*/%/g;
311                         $strsth .= " and $line LIKE ? " ;
312                 }
313         }
314         $strsth .=" group by $linefield";
315         $strsth .=" order by $linefield";
316         warn "". $strsth;
317         
318         my $sth = $dbh->prepare( $strsth );
319         if (( @linefilter ) and ($linefilter[1])){
320                 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
321         } elsif ($linefilter[0]) {
322                 $sth->execute($linefilter[0]);
323         } else {
324                 $sth->execute;
325         }
326         
327         while ( my ($celvalue) = $sth->fetchrow) {
328                 my %cell;
329                 if ($celvalue) {
330                         $cell{rowtitle} = $celvalue;
331 #               } else {
332 #                       $cell{rowtitle} = "";
333                 }
334                 $cell{totalrow} = 0;
335                 push @loopline, \%cell;
336         }
337
338 # 2nd, loop cols.
339         my $colfield;
340         if (($column =~/closedate/) and ($podsp == 1)) {
341                 #Display by day
342                 $colfield .="dayname($column)";  
343         } elsif (($column=~/closedate/) and ($podsp == 2)) {
344                 #Display by Month
345                 $colfield .="monthname($column)";  
346         } elsif (($column=~/closedate/) and ($podsp == 3)) {
347                 #Display by Year
348                 $colfield .="Year($column)";
349         } elsif (($column =~/received/) and ($rodsp == 1)) {
350                 #Display by day
351                 $colfield .="dayname($column)";  
352         } elsif (($column=~/received/) and ($rodsp == 2)) {
353                 #Display by Month
354                 $colfield .="monthname($column)";  
355         } elsif (($column=~/received/) and ($rodsp == 3)) {
356                 #Display by Year
357                 $colfield .="Year($column)";
358         } else {
359                 $colfield .= $column;
360         }  
361         
362         my $strsth2;
363         $strsth2 .= "select distinctrow $colfield from aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) and $column is not null ";
364         
365         if ( @colfilter ) {
366                 if ($colfilter[1]){
367                         if ($colfilter[0]){
368                                 $strsth2 .= " and $column between ? and ? " ;
369                         } else {
370                                 $strsth2 .= " and $column < ? " ;
371                         }
372                 } elsif (($colfilter[0]) and (($column=~/closedate/) or ($column=~/received/))){
373                         $strsth2 .= " and $column > ? " ;
374                 } elsif ($colfilter[0]) {
375                         $colfilter[0] =~ s/\*/%/g;
376                         $strsth2 .= " and $column LIKE ? " ;
377                 }
378         }
379         $strsth2 .=" group by $colfield";
380         $strsth2 .=" order by $colfield";
381         warn "". $strsth2;
382         
383         my $sth2 = $dbh->prepare( $strsth2 );
384         if (( @colfilter ) and ($colfilter[1])){
385                 warn "from : ".$colfilter[0]." To  :".$colfilter[1];
386                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
387         } elsif ($colfilter[0]) {
388                 $sth2->execute($colfilter[0]);
389         } else {
390                 $sth2->execute;
391         }
392
393         while (my ($celvalue) = $sth2->fetchrow) {
394                 my %cell;
395                 if ($celvalue){
396 #               warn "coltitle :".$celvalue;
397                         $cell{coltitle} = $celvalue;
398                 }
399                 push @loopcol, \%cell;
400         }
401 #       warn "fin des titres colonnes";
402
403         my $i=0;
404         my @totalcol;
405         my $hilighted=-1;
406         
407         #Initialization of cell values.....
408         my %table;
409 #       warn "init table";
410         foreach my $row ( @loopline ) {
411                 foreach my $col ( @loopcol ) {
412 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
413                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
414                 }
415                 $table{$row->{rowtitle}}->{totalrow}=0;
416         }
417
418 # preparing calculation
419         my $strcalc ;
420         $strcalc .= "SELECT $linefield, $colfield, ";
421         $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
422         $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
423         $strcalc .= "FROM aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
424
425         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
426         $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
427         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
428         $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] ."'" if ( @$filters[1] );
429         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
430         $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] ."'" if ( @$filters[2] );
431         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
432         $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] ."'" if ( @$filters[3] );
433         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
434         $strcalc .= " AND aqbooksellers.name like '" . @$filters[4] ."'" if ( @$filters[4] );
435         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
436         $strcalc .= " AND aqbookfund.bookfundid like '" . @$filters[5] ."'" if ( @$filters[5] );
437         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
438         $strcalc .= " AND aqorders.sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
439         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
440         $strcalc .= " AND aqorders.sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
441         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
442         warn "". $strcalc;
443         my $dbcalc = $dbh->prepare($strcalc);
444         $dbcalc->execute;
445
446 #       warn "filling table";
447         my $emptycol; 
448         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
449 #               warn "filling table $row / $col / $value ";
450                 $emptycol = 1 if ($col eq undef);
451                 $col = "zzEMPTY" if ($col eq undef);
452                 $row = "zzEMPTY" if ($row eq undef);
453                 
454                 $table{$row}->{$col}+=$value;
455                 $table{$row}->{totalrow}+=$value;
456                 $grantotal += $value;
457         }
458
459         push @loopcol,{coltitle => "NULL"} if ($emptycol);
460         
461         foreach my $row ( sort keys %table ) {
462                 my @loopcell;
463                 #@loopcol ensures the order for columns is common with column titles
464                 # and the number matches the number of columns
465                 foreach my $col ( @loopcol ) {
466                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
467                         push @loopcell, {value => $value  } ;
468                 }
469                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
470                                                 'loopcell' => \@loopcell,
471                                                 'hilighted' => ($hilighted >0),
472                                                 'totalrow' => $table{$row}->{totalrow}
473                                         };
474                 $hilighted = -$hilighted;
475         }
476         
477 #       warn "footer processing";
478         foreach my $col ( @loopcol ) {
479                 my $total=0;
480                 foreach my $row ( @looprow ) {
481                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
482 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
483                 }
484 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
485                 push @loopfooter, {'totalcol' => $total};
486         }
487                         
488
489         # the header of the table
490         $globalline{loopfilter}=\@loopfilter;
491         # the core of the table
492         $globalline{looprow} = \@looprow;
493         $globalline{loopcol} = \@loopcol;
494 #       # the foot (totals by borrower type)
495         $globalline{loopfooter} = \@loopfooter;
496         $globalline{total}= $grantotal;
497         $globalline{line} = $line;
498         $globalline{column} = $column;
499         push @mainloop,\%globalline;
500         return \@mainloop;
501 }
502
503 1;