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
32 use C4::Interface::CGI::Output;
33 use C4::Circulation::Circ2;
37 plugin that shows a stats on borrowers
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/acquisitions_stats.tmpl";
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my @filters = $input->param("Filter");
52 my $podsp = $input->param("PlacedOnDisplay");
53 my $rodsp = $input->param("ReceivedOnDisplay");
54 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 my $del = $input->param("sep");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user({template_name => $fullreportname,
66 flagsrequired => {editcatalogue => 1},
69 $template->param(do_it => $do_it);
71 warn "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
73 my $results = calculate($line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters);
74 if ($output eq "screen"){
75 $template->param(mainloop => $results);
76 output_html_with_http_headers $input, $cookie, $template->output;
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -name=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
109 my $dbh = C4::Context->dbh;
114 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
119 while (my ($value, $desc) =$req->fetchrow) {
121 # $select{$value}=$desc;
123 my $CGIBookSellers=CGI::scrolling_list( -name => 'Filter',
126 # -labels => \%select,
130 $req = $dbh->prepare( "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
136 while (my ($value,$desc) =$req->fetchrow) {
137 push @select, $value;
138 $select{$value}=$desc;
140 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
147 $req = $dbh->prepare( "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname");
153 while (my ($value,$desc) =$req->fetchrow) {
154 push @select, $value;
155 $select{$value}=$desc;
157 my $CGIBudget=CGI::scrolling_list( -name => 'Filter',
164 $req = $dbh->prepare("SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1");
169 while (my ($value) =$req->fetchrow) {
170 $hassort1 =1 if ($value);
171 push @select, $value;
173 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
179 $req = $dbh->prepare("SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2");
185 while (my ($value) =$req->fetchrow) {
186 $hassort2 =1 if ($value);
187 $hglghtsort2= !($hassort1);
188 push @select, $value;
190 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
196 my @mime = ( C4::Context->preference("MIME") );
197 foreach my $mime (@mime){
201 my $CGIextChoice=CGI::scrolling_list(
208 my @dels = ( C4::Context->preference("delimiter") );
209 my $CGIsepChoice=CGI::scrolling_list(
217 CGIBookSeller => $CGIBookSellers,
218 CGIItemType => $CGIItemTypes,
219 CGIBudget => $CGIBudget,
220 hassort1=> $hassort1,
221 hassort2=> $hassort2,
222 HlghtSort2 => $hglghtsort2,
223 CGISort1 => $CGISort1,
224 CGISort2 => $CGISort2,
225 CGIextChoice => $CGIextChoice,
226 CGIsepChoice => $CGIsepChoice
230 output_html_with_http_headers $input, $cookie, $template->output;
235 my ($line, $column, $podsp, $rodsp, $aodsp, $process, $filters) = @_;
244 my $dbh = C4::Context->dbh;
250 for (my $i=0;$i<=8;$i++) {
252 if ( @$filters[$i] ) {
253 if ((($i==1) or ($i==3)) and (@$filters[$i-1])) {
254 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
256 $cell{filter} .= @$filters[$i];
257 $cell{crit} .="Placed On From" if ($i==0);
258 $cell{crit} .="Placed On To" if ($i==1);
259 $cell{crit} .="Received On From" if ($i==2);
260 $cell{crit} .="Received On To" if ($i==3);
262 $cell{crit} .="Acquired On From" if ($i==4);
263 $cell{crit} .="Acquired On To" if ($i==5);
265 $cell{crit} .="BookSeller" if ($i==6);
266 $cell{crit} .="Doc Type" if ($i==7);
267 $cell{crit} .="Budget" if ($i==8);
268 $cell{crit} .="Sort1" if ($i==9);
269 $cell{crit} .="Sort2" if ($i==10);
270 push @loopfilter, \%cell;
275 # warn "filtres ".@filters[0];
276 # warn "filtres ".@filters[1];
277 # warn "filtres ".@filters[2];
278 # warn "filtres ".@filters[3];
280 $linefilter[0] = @$filters[0] if ($line =~ /closedate/ ) ;
281 $linefilter[1] = @$filters[1] if ($line =~ /closedate/ ) ;
282 $linefilter[0] = @$filters[2] if ($line =~ /received/ ) ;
283 $linefilter[1] = @$filters[3] if ($line =~ /received/ ) ;
285 $linefilter[0] = @$filters[4] if ($line =~ /acquired/ ) ;
286 $linefilter[1] = @$filters[5] if ($line =~ /acquired/ ) ;
288 $linefilter[0] = @$filters[6] if ($line =~ /bookseller/ ) ;
289 $linefilter[0] = @$filters[7] if ($line =~ /itemtype/ ) ;
290 $linefilter[0] = @$filters[8] if ($line =~ /bookfund/ ) ;
291 $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
292 $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
293 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
296 $colfilter[0] = @$filters[0] if ($column =~ /closedate/ ) ;
297 $colfilter[1] = @$filters[1] if ($column =~ /closedate/ ) ;
298 $colfilter[0] = @$filters[2] if ($column =~ /received/ ) ;
299 $colfilter[1] = @$filters[3] if ($column =~ /received/ ) ;
301 $colfilter[0] = @$filters[4] if ($column =~ /acquired/ ) ;
302 $colfilter[1] = @$filters[5] if ($column =~ /acquired/ ) ;
304 $colfilter[0] = @$filters[6] if ($column =~ /bookseller/ );
305 $colfilter[0] = @$filters[7] if ($column =~ /itemtype/ ) ;
306 $colfilter[0] = @$filters[8] if ($column =~ /bookfund/ ) ;
307 $colfilter[0] = @$filters[9] if ($column =~ /sort1/ ) ;
308 $colfilter[0] = @$filters[10] if ($column =~ /sort2/ ) ;
309 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
314 if (($line =~/closedate/) and ($podsp == 1)) {
316 $linefield .="dayname($line)";
317 $lineorder .="weekday($line)";
318 } elsif (($line=~/closedate/) and ($podsp == 2)) {
320 $linefield .="monthname($line)";
321 $lineorder .="month($line)";
322 } elsif (($line=~/closedate/) and ($podsp == 3)) {
324 $linefield .="Year($line)";
325 $lineorder .=$linefield;
326 } elsif (($line =~/received/) and ($rodsp == 1)) {
328 $linefield .="dayname($line)";
329 $lineorder .="weekday($line)";
330 } elsif (($line=~/received/) and ($rodsp == 2)) {
332 $linefield .="monthname($line)";
333 $lineorder .="month($line)";
334 } elsif (($line=~/received/) and ($rodsp == 3)) {
336 $linefield .="Year($line)";
337 $lineorder .=$linefield;
340 $lineorder .=$linefield;
344 $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 ";
349 $strsth .= " and $line between ? and ? " ;
351 $strsth .= " and $line < ? " ;
353 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/))){
354 $strsth .= " and $line > ? " ;
355 } elsif ($linefilter[0]) {
356 $linefilter[0] =~ s/\*/%/g;
357 $strsth .= " and $line LIKE ? " ;
360 $strsth .=" group by $linefield";
361 $strsth .=" order by $lineorder";
364 my $sth = $dbh->prepare( $strsth );
365 if (( @linefilter ) and ($linefilter[1])){
366 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
367 } elsif ($linefilter[0]) {
368 $sth->execute($linefilter[0]);
373 while ( my ($celvalue) = $sth->fetchrow) {
376 $cell{rowtitle} = $celvalue;
378 # $cell{rowtitle} = "";
381 push @loopline, \%cell;
384 warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
388 if (($line =~/closedate/) and ($podsp == 1)) {
390 $linefield .="dayname($line)";
391 } elsif (($line=~/closedate/) and ($podsp == 2)) {
393 $linefield .="monthname($line)";
394 } elsif (($line=~/closedate/) and ($podsp == 3)) {
396 $linefield .="Year($line)";
398 } elsif (($line =~/received/) and ($rodsp == 1)) {
400 $linefield .="dayname($line)";
401 } elsif (($line=~/received/) and ($rodsp == 2)) {
403 $linefield .="monthname($line)";
404 } elsif (($line=~/received/) and ($rodsp == 3)) {
406 $linefield .="Year($line)";
408 } elsif (($line =~/acquired/) and ($aodsp == 1)) {
410 $linefield .="dayname($line)";
411 } elsif (($line=~/acquired/) and ($aodsp == 2)) {
413 $linefield .="monthname($line)";
414 } elsif (($line=~/acquired/) and ($aodsp == 3)) {
416 $linefield .="Year($line)";
424 $strsth .= "SELECT DISTINCTROW $linefield FROM aqorders, aqbasket,aqorderbreakdown
425 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
426 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
427 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
428 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
429 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
434 $strsth .= " AND $line BETWEEN ? AND ? " ;
436 $strsth .= " AND $line < ? " ;
438 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/) or ($line=~/acquired/))){
439 $strsth .= " AND $line > ? " ;
440 } elsif ($linefilter[0]) {
441 $linefilter[0] =~ s/\*/%/g;
442 $strsth .= " AND $line LIKE ? " ;
445 $strsth .=" GROUP BY $linefield";
446 $strsth .=" ORDER BY $linefield";
447 warn "377:strsth= $strsth";
449 my $sth = $dbh->prepare( $strsth );
450 if (( @linefilter ) and ($linefilter[1])){
451 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
452 } elsif ($linefilter[0]) {
453 $sth->execute($linefilter[0]);
458 while ( my ($celvalue) = $sth->fetchrow) {
461 $cell{rowtitle} = $celvalue;
463 # $cell{rowtitle} = "";
466 push @loopline, \%cell;
469 warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
473 if (($column =~/closedate/) and ($podsp == 1)) {
475 $colfield .="dayname($column)";
476 $colorder .="weekday($column)";
477 } elsif (($column=~/closedate/) and ($podsp == 2)) {
479 $colfield .="monthname($column)";
480 $colorder .="month($column)";
481 } elsif (($column=~/closedate/) and ($podsp == 3)) {
483 $colfield .="Year($column)";
484 $colorder .= $colfield;
485 } elsif (($column =~/received/) and ($rodsp == 1)) {
487 $colfield .="dayname($column)";
488 $colorder .="weekday($column)";
489 } elsif (($column=~/received/) and ($rodsp == 2)) {
491 $colfield .="monthname($column)";
492 $colorder .="month($column)";
493 } elsif (($column=~/received/) and ($rodsp == 3)) {
495 $colfield .="Year($column)";
496 $colorder .= $colfield;
498 $colfield .= $column;
499 $colorder .= $colfield;
503 $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 ";
508 $strsth2 .= " and $column between ? and ? " ;
510 $strsth2 .= " and $column < ? " ;
512 } elsif (($colfilter[0]) and (($column=~/closedate/) or ($column=~/received/))){
513 $strsth2 .= " and $column > ? " ;
514 } elsif ($colfilter[0]) {
515 $colfilter[0] =~ s/\*/%/g;
516 $strsth2 .= " and $column LIKE ? " ;
519 $strsth2 .=" group by $colfield";
520 $strsth2 .=" order by $colorder";
523 my $sth2 = $dbh->prepare( $strsth2 );
524 if (( @colfilter ) and ($colfilter[1])){
525 warn "from : ".$colfilter[0]." To :".$colfilter[1];
526 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
527 } elsif ($colfilter[0]) {
528 $sth2->execute($colfilter[0]);
533 while (my ($celvalue) = $sth2->fetchrow) {
536 # warn "coltitle :".$celvalue;
537 $cell{coltitle} = $celvalue;
539 push @loopcol, \%cell;
541 # warn "fin des titres colonnes";
547 #Initialization of cell values.....
550 foreach my $row ( @loopline ) {
551 foreach my $col ( @loopcol ) {
552 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
553 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
555 $table{$row->{rowtitle}}->{totalrow}=0;
558 # preparing calculation
560 $strcalc .= "SELECT $linefield, $colfield, ";
561 $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
562 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
563 $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) ";
565 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
566 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
567 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
568 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] ."'" if ( @$filters[1] );
569 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
570 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] ."'" if ( @$filters[2] );
571 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
572 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] ."'" if ( @$filters[3] );
573 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
574 $strcalc .= " AND aqbooksellers.name like '" . @$filters[4] ."'" if ( @$filters[4] );
575 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
576 $strcalc .= " AND aqbookfund.bookfundid like '" . @$filters[5] ."'" if ( @$filters[5] );
577 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
578 $strcalc .= " AND aqorders.sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
579 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
580 $strcalc .= " AND aqorders.sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
581 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
583 my $dbcalc = $dbh->prepare($strcalc);
586 # warn "filling table";
588 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
589 # warn "filling table $row / $col / $value ";
590 $emptycol = 1 if ($col eq undef);
591 $col = "zzEMPTY" if ($col eq undef);
592 $row = "zzEMPTY" if ($row eq undef);
594 $table{$row}->{$col}+=$value;
595 $table{$row}->{totalrow}+=$value;
596 $grantotal += $value;
599 push @loopcol,{coltitle => "NULL"} if ($emptycol);
601 foreach my $row ( sort keys %table ) {
603 #@loopcol ensures the order for columns is common with column titles
604 # and the number matches the number of columns
605 foreach my $col ( @loopcol ) {
606 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
607 push @loopcell, {value => $value } ;
609 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
610 'loopcell' => \@loopcell,
611 'hilighted' => ($hilighted >0),
612 'totalrow' => $table{$row}->{totalrow}
614 $hilighted = -$hilighted;
617 # warn "footer processing";
618 foreach my $col ( @loopcol ) {
620 foreach my $row ( @looprow ) {
621 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
622 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
624 # warn "summ for column ".$col->{coltitle}." = ".$total;
625 push @loopfooter, {'totalcol' => $total};
629 # the header of the table
630 $globalline{loopfilter}=\@loopfilter;
631 # the core of the table
632 $globalline{looprow} = \@looprow;
633 $globalline{loopcol} = \@loopcol;
634 # # the foot (totals by borrower type)
635 $globalline{loopfooter} = \@loopfooter;
636 $globalline{total}= $grantotal;
637 $globalline{line} = $line;
638 $globalline{column} = $column;
639 push @mainloop,\%globalline;