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