4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
31 use C4::Dates qw/format_date format_date_in_iso/;
35 plugin that shows a stats on borrowers
44 my $do_it = $input->param('do_it');
45 my $fullreportname = "reports/acquisitions_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 $filters[0]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[0]) : undef);
50 $filters[1]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[1]) : undef);
51 $filters[2]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[2]) : undef);
52 $filters[3]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[3]) : undef);
53 my $podsp = $input->param("PlacedOnDisplay");
54 my $rodsp = $input->param("ReceivedOnDisplay");
55 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 my $del = $input->param("sep");
62 #warn "calcul : ".$calc;
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => $fullreportname,
68 flagsrequired => {reports => 1},
71 $template->param(do_it => $do_it,
72 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
75 # warn "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
77 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
78 if ( $output eq "screen" ) {
79 $template->param( mainloop => $results );
80 output_html_with_http_headers $input, $cookie, $template->output;
84 -type => 'application/vnd.sun.xml.calc',
86 -attachment => "$basename.csv",
87 -name => "$basename.csv"
89 my $cols = @$results[0]->{loopcol};
90 my $lines = @$results[0]->{looprow};
92 $sep = C4::Context->preference("delimiter");
93 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
94 foreach my $col (@$cols) {
95 print $col->{coltitle} . $sep;
98 foreach my $line (@$lines) {
99 my $x = $line->{loopcell};
100 print $line->{rowtitle} . $sep;
101 foreach my $cell (@$x) {
102 print $cell->{value} . $sep;
104 print $line->{totalrow};
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col (@$cols) {
110 print $sep. $col->{totalcol};
112 print $sep. @$results[0]->{total};
117 my $dbh = C4::Context->dbh;
121 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
125 $select{''} = "All Suppliers";
126 while ( my ( $value, $desc ) = $req->fetchrow ) {
128 $select{$value}=$desc;
130 my $CGIBookSellers = CGI::scrolling_list(
139 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
144 $select{''} = "All Item Types";
145 while ( my ( $value, $desc ) = $req->fetchrow ) {
146 push @select, $value;
147 $select{$value} = $desc;
149 my $CGIItemTypes = CGI::scrolling_list(
158 $req = $dbh->prepare("SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname");
163 $select{''} = "All Funds";
165 while ( my ( $value, $desc ) = $req->fetchrow ) {
166 push @select, $value;
167 $select{$value} = $desc;
169 my $CGIBudget = CGI::scrolling_list(
180 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
188 while ( my ($value) = $req->fetchrow ) {
191 push @select, $value;
192 $select{$value} = $value;
195 my $CGISort1 = CGI::scrolling_list(
206 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
216 while ( my ($value) = $req->fetchrow ) {
219 $hglghtsort2 = !($hassort1);
220 push @select, $value;
221 $select{$value} = $value;
224 my $CGISort2 = CGI::scrolling_list(
233 my @mime = ( C4::Context->preference("MIME") );
234 foreach my $mime (@mime) {
238 my $CGIextChoice = CGI::scrolling_list(
246 my @dels = ( C4::Context->preference("delimiter") );
247 my $CGIsepChoice = CGI::scrolling_list(
256 CGIBookSeller => $CGIBookSellers,
257 CGIItemType => $CGIItemTypes,
258 CGIBudget => $CGIBudget,
259 hassort1 => $hassort1,
260 hassort2 => $hassort2,
261 HlghtSort2 => $hglghtsort2,
262 CGISort1 => $CGISort1,
263 CGISort2 => $CGISort2,
264 CGIextChoice => $CGIextChoice,
265 CGIsepChoice => $CGIsepChoice,
266 date_today => C4::Dates->new()->output()
270 output_html_with_http_headers $input, $cookie, $template->output;
273 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
283 my $dbh = C4::Context->dbh;
289 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
291 if ( @$filters[$i] ) {
292 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
293 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
295 # format the dates filters, otherwise just fill as is
297 $cell{filter} .= @$filters[$i];
299 $cell{filter} .= format_date(@$filters[$i]);
301 $cell{crit} .= "Placed On From" if ( $i == 0 );
302 $cell{crit} .= "Placed On To" if ( $i == 1 );
303 $cell{crit} .= "Received On From" if ( $i == 2 );
304 $cell{crit} .= "Received On To" if ( $i == 3 );
306 # $cell{crit} .= "Acquired On From" if ( $i == 4 );
307 # $cell{crit} .= "Acquired On To" if ( $i == 5 );
309 $cell{crit} .= "BookSeller" if ( $i == 4 );
310 $cell{crit} .= "Doc Type" if ( $i == 5 );
311 $cell{crit} .= "Budget" if ( $i == 6 );
312 $cell{crit} .= "Sort1" if ( $i == 7 );
313 $cell{crit} .= "Sort2" if ( $i == 8 );
314 push @loopfilter, \%cell;
320 # warn "filtres ".@filters[0];
321 # warn "filtres ".@filters[1];
322 # warn "filtres ".@filters[2];
323 # warn "filtres ".@filters[3];
325 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
326 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
327 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
328 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
330 # $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
331 # $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
333 $linefilter[0] = @$filters[4] if ( $line =~ /bookseller/ );
334 $linefilter[0] = @$filters[5] if ( $line =~ /itemtype/ );
335 $linefilter[0] = @$filters[6] if ( $line =~ /bookfund/ );
336 $linefilter[0] = @$filters[7] if ( $line =~ /sort1/ );
337 $linefilter[0] = @$filters[8] if ( $line =~ /sort2/ );
339 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
342 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
343 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
344 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
345 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
347 # $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
348 # $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
349 $colfilter[0] = @$filters[4] if ( $column =~ /bookseller/ );
350 $colfilter[0] = @$filters[5] if ( $column =~ /itemtype/ );
351 $colfilter[0] = @$filters[6] if ( $column =~ /bookfund/ );
352 $colfilter[0] = @$filters[7] if ( $column =~ /sort1/ );
353 $colfilter[0] = @$filters[8] if ( $column =~ /sort2/ );
355 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
357 # warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
361 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
364 $linefield .= "dayname($line)";
366 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
369 $linefield .= "monthname($line)";
371 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
374 $linefield .= "Year($line)";
377 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
380 $linefield .= "dayname($line)";
382 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
385 $linefield .= "monthname($line)";
387 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
390 $linefield .= "Year($line)";
393 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
396 # $linefield .= "dayname($line)";
398 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
401 # $linefield .= "monthname($line)";
403 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
406 # $linefield .= "Year($line)";
415 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
416 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
417 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
418 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
419 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
420 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
423 if ( $linefilter[1] ) {
424 if ( $linefilter[0] ) {
425 $strsth .= " AND $line BETWEEN ? AND ? ";
428 $strsth .= " AND $line <= ? ";
433 and ( ( $line =~ /closedate/ )
434 or ( $line =~ /received/ ))
435 # or ( $line =~ /acquired/ ) )
438 $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";
448 #warn "377:strsth= $strsth";
450 my $sth = $dbh->prepare($strsth);
451 if ( (@linefilter) and ( $linefilter[1] ) ) {
452 $sth->execute( $linefilter[0], $linefilter[1] );
454 elsif ( $linefilter[0] ) {
455 $sth->execute( $linefilter[0] );
460 while ( my ($celvalue) = $sth->fetchrow ) {
463 $cell{rowtitle} = $celvalue;
464 push @loopline, \%cell;
468 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
472 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
475 $colfield .= "dayname($column)";
477 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
480 $colfield .= "monthname($column)";
482 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
485 $colfield .= "Year($column)";
488 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
491 $colfield .= "dayname($column)";
493 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
496 $colfield .= "monthname($column)";
498 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
501 $colfield .= "Year($column)";
504 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
507 # $colfield .= "dayname($column)";
509 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
512 # $colfield .= "monthname($column)";
514 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
517 # $colfield .= "Year($column)";
521 $colfield .= $column;
526 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
527 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
528 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
529 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
530 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
531 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
532 AND $column IS NOT NULL";
535 if ( $colfilter[1] ) {
536 if ( $colfilter[0] ) {
537 $strsth2 .= " AND $column BETWEEN ? AND ? ";
540 $strsth2 .= " AND $column <= ? ";
545 and ( ( $column =~ /closedate/ )
546 or ( $line =~ /received/ ))
547 # or ( $line =~ /acquired/ ) )
550 $strsth2 .= " AND $column >= ? ";
552 elsif ( $colfilter[0] ) {
553 $colfilter[0] =~ s/\*/%/g;
554 $strsth2 .= " AND $column LIKE ? ";
557 $strsth2 .= " GROUP BY $colfield";
558 $strsth2 .= " ORDER BY $colfield";
560 # warn "MASON:. $strsth2";
562 my $sth2 = $dbh->prepare($strsth2);
563 if ( (@colfilter) and ($colfilter[1]) ) {
564 $sth2->execute( $colfilter[0], $colfilter[1] );
566 elsif ( $colfilter[0] ) {
567 $sth2->execute( $colfilter[0] );
572 while ( my $celvalue = $sth2->fetchrow ) {
575 $cell{coltitle} = $celvalue;
576 #warn "\$cell{coltitle} = $celvalue\n";
577 push @loopcol, \%cell;
582 # warn "fin des titres colonnes";
588 #Initialization of cell values.....
591 # warn "init table...\n";
592 foreach my $row (@loopline) {
593 foreach my $col (@loopcol) {
594 #warn " init table : $row->{rowtitle} / $col->{coltitle} \n";
595 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
597 $table{ $row->{rowtitle} }->{totalrow} = 0;
600 # preparing calculation
602 $strcalc .= "SELECT $linefield, $colfield, ";
603 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
604 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
605 if ( $process == 2 );
606 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
607 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
608 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
609 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
610 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
611 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
613 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
614 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
616 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
617 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
619 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
620 $strcalc .= " AND aqorderdelivery.deliverydate >= '" . @$filters[2] . "'"
622 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
623 $strcalc .= " AND aqorderdelivery.deliverydate <= '" . @$filters[3] . "'"
625 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
626 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
627 # if ( @$filters[4] );
628 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
629 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
630 # if ( @$filters[5] );
631 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
632 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
634 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
635 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
637 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
638 $strcalc .= " AND aqorderbreakdown.bookfundid LIKE '" . @$filters[6] . "'"
640 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
641 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
643 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
644 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
646 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
648 # warn $strcalc . "\n";
649 my $dbcalc = $dbh->prepare($strcalc);
652 # warn "filling table";
654 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
655 #warn "filling table $row / $col / $value ";
656 $emptycol = 1 if ( $col eq undef );
657 $col = "zzEMPTY" if ( $col eq undef );
658 $row = "zzEMPTY" if ( $row eq undef );
660 $table{$row}->{$col} += $value;
661 $table{$row}->{totalrow} += $value;
662 $grantotal += $value;
665 push @loopcol, { coltitle => "NULL" } if ($emptycol);
667 foreach my $row ( sort keys %table ) {
669 #@loopcol ensures the order for columns is common with column titles
670 # and the number matches the number of columns
671 foreach my $col (@loopcol) {
672 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
673 push @loopcell, { value => $value };
677 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
678 'loopcell' => \@loopcell,
679 'hilighted' => ( $hilighted > 0 ),
680 'totalrow' => $table{$row}->{totalrow}
682 $hilighted = -$hilighted;
685 # warn "footer processing";
686 foreach my $col (@loopcol) {
688 foreach my $row (@looprow) {
690 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
693 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
697 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
700 # warn "summ for column ".$col->{coltitle}." = ".$total;
701 push @loopfooter, { 'totalcol' => $total };
704 # the header of the table
705 $globalline{loopfilter}=\@loopfilter;
706 # the core of the table
707 $globalline{looprow} = \@looprow;
708 $globalline{loopcol} = \@loopcol;
710 # # the foot (totals by borrower type)
711 $globalline{loopfooter} = \@loopfooter;
712 $globalline{total} = $grantotal;
713 $globalline{line} = $line;
714 $globalline{column} = $column;
715 push @mainloop, \%globalline;