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 aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
419 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
421 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
424 if ( $linefilter[1] ) {
425 if ( $linefilter[0] ) {
426 $strsth .= " AND $line BETWEEN ? AND ? ";
429 $strsth .= " AND $line <= ? ";
434 and ( ( $line =~ /closedate/ )
435 or ( $line =~ /received/ ))
436 # or ( $line =~ /acquired/ ) )
439 $strsth .= " AND $line >= ? ";
441 elsif ( $linefilter[0] ) {
442 $linefilter[0] =~ s/\*/%/g;
443 $strsth .= " AND $line LIKE ? ";
446 $strsth .= " GROUP BY $linefield";
447 $strsth .= " ORDER BY $linefield";
449 #warn "377:strsth= $strsth";
451 my $sth = $dbh->prepare($strsth);
452 if ( (@linefilter) and ( $linefilter[1] ) ) {
453 $sth->execute( $linefilter[0], $linefilter[1] );
455 elsif ( $linefilter[0] ) {
456 $sth->execute( $linefilter[0] );
461 while ( my ($celvalue) = $sth->fetchrow ) {
464 $cell{rowtitle} = $celvalue;
465 push @loopline, \%cell;
469 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
473 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
476 $colfield .= "dayname($column)";
478 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
481 $colfield .= "monthname($column)";
483 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
486 $colfield .= "Year($column)";
489 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
492 $colfield .= "dayname($column)";
494 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
497 $colfield .= "monthname($column)";
499 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
502 $colfield .= "Year($column)";
505 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
508 # $colfield .= "dayname($column)";
510 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
513 # $colfield .= "monthname($column)";
515 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
518 # $colfield .= "Year($column)";
522 $colfield .= $column;
527 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
528 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
529 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
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";
534 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
537 if ( $colfilter[1] ) {
538 if ( $colfilter[0] ) {
539 $strsth2 .= " AND $column BETWEEN ? AND ? ";
542 $strsth2 .= " AND $column <= ? ";
547 and ( ( $column =~ /closedate/ )
548 or ( $line =~ /received/ ))
549 # or ( $line =~ /acquired/ ) )
552 $strsth2 .= " AND $column >= ? ";
554 elsif ( $colfilter[0] ) {
555 $colfilter[0] =~ s/\*/%/g;
556 $strsth2 .= " AND $column LIKE ? ";
559 $strsth2 .= " GROUP BY $colfield";
560 $strsth2 .= " ORDER BY $colfield";
562 # warn "MASON:. $strsth2";
564 my $sth2 = $dbh->prepare($strsth2);
565 if ( (@colfilter) and ($colfilter[1]) ) {
566 $sth2->execute( $colfilter[0], $colfilter[1] );
568 elsif ( $colfilter[0] ) {
569 $sth2->execute( $colfilter[0] );
574 while ( my $celvalue = $sth2->fetchrow ) {
577 $cell{coltitle} = $celvalue;
578 #warn "\$cell{coltitle} = $celvalue\n";
579 push @loopcol, \%cell;
584 # warn "fin des titres colonnes";
590 #Initialization of cell values.....
593 # warn "init table...\n";
594 foreach my $row (@loopline) {
595 foreach my $col (@loopcol) {
596 #warn " init table : $row->{rowtitle} / $col->{coltitle} \n";
597 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
599 $table{ $row->{rowtitle} }->{totalrow} = 0;
602 # preparing calculation
604 $strcalc .= "SELECT $linefield, $colfield, ";
605 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
606 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
607 if ( $process == 2 );
608 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
609 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
610 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
611 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
612 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
614 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
616 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
617 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
619 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
620 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
622 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
623 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
625 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
626 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
628 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
629 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
630 # if ( @$filters[4] );
631 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
632 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
633 # if ( @$filters[5] );
634 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
635 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
637 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
638 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
640 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
641 $strcalc .= " AND aqorderbreakdown.bookfundid LIKE '" . @$filters[6] . "'"
643 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
644 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
646 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
647 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
649 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
651 # warn $strcalc . "\n";
652 my $dbcalc = $dbh->prepare($strcalc);
655 # warn "filling table";
657 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
658 next if ($row eq undef || $col eq undef);
659 #warn "filling table $row / $col / $value ";
660 $emptycol = 1 if ( $col eq undef );
661 $col = "zzEMPTY" if ( $col eq undef );
662 $row = "zzEMPTY" if ( $row eq undef );
664 $table{$row}->{$col} += $value;
665 $table{$row}->{totalrow} += $value;
666 $grantotal += $value;
669 push @loopcol, { coltitle => "NULL" } if ($emptycol);
671 foreach my $row ( sort keys %table ) {
673 #@loopcol ensures the order for columns is common with column titles
674 # and the number matches the number of columns
675 foreach my $col (@loopcol) {
676 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
677 push @loopcell, { value => $value };
681 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
682 'loopcell' => \@loopcell,
683 'hilighted' => ( $hilighted > 0 ),
684 'totalrow' => $table{$row}->{totalrow}
686 $hilighted = -$hilighted;
689 # warn "footer processing";
690 foreach my $col (@loopcol) {
692 foreach my $row (@looprow) {
694 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
697 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
701 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
704 # warn "summ for column ".$col->{coltitle}." = ".$total;
705 push @loopfooter, { 'totalcol' => $total };
708 # the header of the table
709 $globalline{loopfilter}=\@loopfilter;
710 # the core of the table
711 $globalline{looprow} = \@looprow;
712 $globalline{loopcol} = \@loopcol;
714 # # the foot (totals by borrower type)
715 $globalline{loopfooter} = \@loopfooter;
716 $globalline{total} = $grantotal;
717 $globalline{line} = $line;
718 $globalline{column} = $column;
719 push @mainloop, \%globalline;