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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
35 plugin that shows a stats on borrowers
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,
63 flagsrequired => {editcatalogue => 1},
66 $template->param(do_it => $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;
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};
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;
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;
92 print $line->{totalrow};
96 $cols = @$results[0]->{loopfooter};
97 foreach my $col ( @$cols ) {
98 print $sep.$col->{totalcol};
100 print $sep.@$results[0]->{total};
104 my $dbh = C4::Context->dbh;
109 $req = $dbh->prepare("select distinctrow id,name from aqbooksellers order by name");
114 while (my ($value, $desc) =$req->fetchrow) {
116 # $select{$value}=$desc;
118 my $CGIBookSellers=CGI::scrolling_list( -name => 'Filter',
121 # -labels => \%select,
125 $req = $dbh->prepare( "select distinctrow bookfundid,bookfundname from aqbookfund order by bookfundname");
131 while (my ($value,$desc) =$req->fetchrow) {
132 push @select, $value;
133 $select{$value}=$desc;
135 my $CGIBudget=CGI::scrolling_list( -name => 'Filter',
142 $req = $dbh->prepare("select distinctrow sort1 from aqorders where sort1 is not null order by sort1");
147 while (my ($value) =$req->fetchrow) {
148 $hassort1 =1 if ($value);
149 push @select, $value;
151 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
157 $req = $dbh->prepare("select distinctrow sort2 from aqorders where sort2 is not null order by sort2");
163 while (my ($value) =$req->fetchrow) {
164 $hassort2 =1 if ($value);
165 $hglghtsort2= !($hassort1);
166 push @select, $value;
168 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
174 my @mime = ( C4::Context->preference("MIME") );
175 foreach my $mime (@mime){
179 my $CGIextChoice=CGI::scrolling_list(
186 my @dels = ( C4::Context->preference("delimiter") );
187 my $CGIsepChoice=CGI::scrolling_list(
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
207 output_html_with_http_headers $input, $cookie, $template->output;
212 my ($line, $column, $podsp, $rodsp, ,$process, $filters) = @_;
221 my $dbh = C4::Context->dbh;
227 for (my $i=0;$i<=7;$i++) {
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]) ;
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;
247 # warn "filtres ".@filters[0];
248 # warn "filtres ".@filters[1];
249 # warn "filtres ".@filters[2];
250 # warn "filtres ".@filters[3];
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];
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];
276 if (($line =~/closedate/) and ($podsp == 1)) {
278 $linefield .="dayname($line)";
279 $lineorder .="weekday($line)";
280 } elsif (($line=~/closedate/) and ($podsp == 2)) {
282 $linefield .="monthname($line)";
283 $lineorder .="month($line)";
284 } elsif (($line=~/closedate/) and ($podsp == 3)) {
286 $linefield .="Year($line)";
287 $lineorder .="Year($line)";
288 } elsif (($line =~/received/) and ($rodsp == 1)) {
290 $linefield .="dayname($line)";
291 $lineorder .="weekday($line)";
292 } elsif (($line=~/received/) and ($rodsp == 2)) {
294 $linefield .="monthname($line)";
295 $lineorder .="month($line)";
296 } elsif (($line=~/received/) and ($rodsp == 3)) {
298 $linefield .="Year($line)";
299 $lineorder .="Year($line)";
306 $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 ";
311 $strsth .= " and $line between ? and ? " ;
313 $strsth .= " and $line < ? " ;
315 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/))){
316 $strsth .= " and $line > ? " ;
317 } elsif ($linefilter[0]) {
318 $linefilter[0] =~ s/\*/%/g;
319 $strsth .= " and $line LIKE ? " ;
322 $strsth .=" group by $linefield";
323 $strsth .=" order by $lineorder";
326 my $sth = $dbh->prepare( $strsth );
327 if (( @linefilter ) and ($linefilter[1])){
328 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
329 } elsif ($linefilter[0]) {
330 $sth->execute($linefilter[0]);
335 while ( my ($celvalue) = $sth->fetchrow) {
338 $cell{rowtitle} = $celvalue;
340 # $cell{rowtitle} = "";
343 push @loopline, \%cell;
349 if (($column =~/closedate/) and ($podsp == 1)) {
351 $colfield .="dayname($column)";
352 $colorder .="weekday($column)";
353 } elsif (($column=~/closedate/) and ($podsp == 2)) {
355 $colfield .="monthname($column)";
356 $colorder .="month($column)";
357 } elsif (($column=~/closedate/) and ($podsp == 3)) {
359 $colfield .="Year($column)";
360 $colorder .="Year($column)";
361 } elsif (($column =~/received/) and ($rodsp == 1)) {
363 $colfield .="dayname($column)";
364 $colorder .="weekday($column)";
365 } elsif (($column=~/received/) and ($rodsp == 2)) {
367 $colfield .="monthname($column)";
368 $colorder .="month($column)";
369 } elsif (($column=~/received/) and ($rodsp == 3)) {
371 $colfield .="Year($column)";
372 $colorder .="Year($column)";
374 $colfield .= $column;
375 $colorder .= $column;
379 $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 ";
384 $strsth2 .= " and $column between ? and ? " ;
386 $strsth2 .= " and $column < ? " ;
388 } elsif (($colfilter[0]) and (($column=~/closedate/) or ($column=~/received/))){
389 $strsth2 .= " and $column > ? " ;
390 } elsif ($colfilter[0]) {
391 $colfilter[0] =~ s/\*/%/g;
392 $strsth2 .= " and $column LIKE ? " ;
395 $strsth2 .=" group by $colfield";
396 $strsth2 .=" order by $colorder";
399 my $sth2 = $dbh->prepare( $strsth2 );
400 if (( @colfilter ) and ($colfilter[1])){
401 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
402 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
403 } elsif ($colfilter[0]) {
404 $sth2->execute($colfilter[0]);
409 while (my ($celvalue) = $sth2->fetchrow) {
412 # warn "coltitle :".$celvalue;
413 $cell{coltitle} = $celvalue;
415 push @loopcol, \%cell;
417 # warn "fin des titres colonnes";
423 #Initialization of cell values.....
426 foreach my $row ( @loopline ) {
427 foreach my $col ( @loopcol ) {
428 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
429 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
431 $table{$row->{rowtitle}}->{totalrow}=0;
434 # preparing calculation
436 $strcalc .= "SELECT $linefield, $colfield, ";
437 $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
438 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
439 $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) ";
441 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
442 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
443 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
444 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] ."'" if ( @$filters[1] );
445 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
446 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] ."'" if ( @$filters[2] );
447 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
448 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] ."'" if ( @$filters[3] );
449 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
450 $strcalc .= " AND aqbooksellers.name like '" . @$filters[4] ."'" if ( @$filters[4] );
451 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
452 $strcalc .= " AND aqbookfund.bookfundid like '" . @$filters[5] ."'" if ( @$filters[5] );
453 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
454 $strcalc .= " AND aqorders.sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
455 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
456 $strcalc .= " AND aqorders.sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
457 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
459 my $dbcalc = $dbh->prepare($strcalc);
462 # warn "filling table";
464 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
465 # warn "filling table $row / $col / $value ";
466 $emptycol = 1 if ($col eq undef);
467 $col = "zzEMPTY" if ($col eq undef);
468 $row = "zzEMPTY" if ($row eq undef);
470 $table{$row}->{$col}+=$value;
471 $table{$row}->{totalrow}+=$value;
472 $grantotal += $value;
475 push @loopcol,{coltitle => "NULL"} if ($emptycol);
477 foreach my $row ( sort keys %table ) {
479 #@loopcol ensures the order for columns is common with column titles
480 # and the number matches the number of columns
481 foreach my $col ( @loopcol ) {
482 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
483 push @loopcell, {value => $value } ;
485 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
486 'loopcell' => \@loopcell,
487 'hilighted' => ($hilighted >0),
488 'totalrow' => $table{$row}->{totalrow}
490 $hilighted = -$hilighted;
493 # warn "footer processing";
494 foreach my $col ( @loopcol ) {
496 foreach my $row ( @looprow ) {
497 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
498 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
500 # warn "summ for column ".$col->{coltitle}." = ".$total;
501 push @loopfooter, {'totalcol' => $total};
505 # the header of the table
506 $globalline{loopfilter}=\@loopfilter;
507 # the core of the table
508 $globalline{looprow} = \@looprow;
509 $globalline{loopcol} = \@loopcol;
510 # # the foot (totals by borrower type)
511 $globalline{loopfooter} = \@loopfooter;
512 $globalline{total}= $grantotal;
513 $globalline{line} = $line;
514 $globalline{column} = $column;
515 push @mainloop,\%globalline;