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]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 $filters[2]=format_date_in_iso($filters[2]);
52 $filters[3]=format_date_in_iso($filters[3]);
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;
122 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
128 while ( my ( $value, $desc ) = $req->fetchrow ) {
131 # $select{$value}=$desc;
133 my $CGIBookSellers = CGI::scrolling_list(
138 # -labels => \%select,
145 "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description"
152 while ( my ( $value, $desc ) = $req->fetchrow ) {
153 push @select, $value;
154 $select{$value} = $desc;
156 my $CGIItemTypes = CGI::scrolling_list(
167 "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname"
175 while ( my ( $value, $desc ) = $req->fetchrow ) {
176 push @select, $value;
177 $select{$value} = $desc;
179 my $CGIBudget = CGI::scrolling_list(
190 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
196 while ( my ($value) = $req->fetchrow ) {
197 $hassort1 = 1 if ($value);
198 push @select, $value;
200 my $CGISort1 = CGI::scrolling_list(
210 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
218 while ( my ($value) = $req->fetchrow ) {
219 $hassort2 = 1 if ($value);
220 $hglghtsort2 = !($hassort1);
221 push @select, $value;
223 my $CGISort2 = CGI::scrolling_list(
231 my @mime = ( C4::Context->preference("MIME") );
232 foreach my $mime (@mime) {
236 my $CGIextChoice = CGI::scrolling_list(
244 my @dels = ( C4::Context->preference("delimiter") );
245 my $CGIsepChoice = CGI::scrolling_list(
254 CGIBookSeller => $CGIBookSellers,
255 CGIItemType => $CGIItemTypes,
256 CGIBudget => $CGIBudget,
257 hassort1 => $hassort1,
258 hassort2 => $hassort2,
259 HlghtSort2 => $hglghtsort2,
260 CGISort1 => $CGISort1,
261 CGISort2 => $CGISort2,
262 CGIextChoice => $CGIextChoice,
263 CGIsepChoice => $CGIsepChoice
267 output_html_with_http_headers $input, $cookie, $template->output;
270 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
280 my $dbh = C4::Context->dbh;
286 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
288 if ( @$filters[$i] ) {
289 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
290 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
292 # format the dates filters, otherwise just fill as is
294 $cell{filter} .= @$filters[$i];
296 $cell{filter} .= format_date(@$filters[$i]);
298 $cell{crit} .= "Placed On From" if ( $i == 0 );
299 $cell{crit} .= "Placed On To" if ( $i == 1 );
300 $cell{crit} .= "Received On From" if ( $i == 2 );
301 $cell{crit} .= "Received On To" if ( $i == 3 );
303 $cell{crit} .= "Acquired On From" if ( $i == 4 );
304 $cell{crit} .= "Acquired On To" if ( $i == 5 );
306 $cell{crit} .= "BookSeller" if ( $i == 6 );
307 $cell{crit} .= "Doc Type" if ( $i == 7 );
308 $cell{crit} .= "Budget" if ( $i == 8 );
309 $cell{crit} .= "Sort1" if ( $i == 9 );
310 $cell{crit} .= "Sort2" if ( $i == 10 );
311 push @loopfilter, \%cell;
317 # warn "filtres ".@filters[0];
318 # warn "filtres ".@filters[1];
319 # warn "filtres ".@filters[2];
320 # warn "filtres ".@filters[3];
322 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
323 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
324 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
325 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
327 $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
328 $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
330 $linefilter[0] = @$filters[6] if ( $line =~ /bookseller/ );
331 $linefilter[0] = @$filters[7] if ( $line =~ /itemtype/ );
332 $linefilter[0] = @$filters[8] if ( $line =~ /bookfund/ );
333 $linefilter[0] = @$filters[9] if ( $line =~ /sort1/ );
334 $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
336 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
339 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
340 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
341 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
342 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
344 $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
345 $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
347 $colfilter[0] = @$filters[6] if ( $column =~ /bookseller/ );
348 $colfilter[0] = @$filters[7] if ( $column =~ /itemtype/ );
349 $colfilter[0] = @$filters[8] if ( $column =~ /bookfund/ );
350 $colfilter[0] = @$filters[9] if ( $column =~ /sort1/ );
351 $colfilter[0] = @$filters[10] if ( $column =~ /sort2/ );
353 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
355 #warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
359 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
362 $linefield .= "dayname($line)";
364 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
367 $linefield .= "monthname($line)";
369 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
372 $linefield .= "Year($line)";
375 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
378 $linefield .= "dayname($line)";
380 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
383 $linefield .= "monthname($line)";
385 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
388 $linefield .= "Year($line)";
391 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
394 $linefield .= "dayname($line)";
396 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
399 $linefield .= "monthname($line)";
401 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
404 $linefield .= "Year($line)";
413 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
414 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
415 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
416 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
417 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
418 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
421 if ( $linefilter[1] ) {
422 if ( $linefilter[0] ) {
423 $strsth .= " AND $line BETWEEN ? AND ? ";
426 $strsth .= " AND $line < ? ";
431 and ( ( $line =~ /closedate/ )
432 or ( $line =~ /received/ )
433 or ( $line =~ /acquired/ ) )
436 $strsth .= " AND $line > ? ";
438 elsif ( $linefilter[0] ) {
439 $linefilter[0] =~ s/\*/%/g;
440 $strsth .= " AND $line LIKE ? ";
443 $strsth .= " GROUP BY $linefield";
444 $strsth .= " ORDER BY $linefield";
446 #warn "377:strsth= $strsth";
448 my $sth = $dbh->prepare($strsth);
449 if ( (@linefilter) and ( $linefilter[1] ) ) {
450 $sth->execute( "'" . $linefilter[0] . "'", "'" . $linefilter[1] . "'" );
452 elsif ( $linefilter[0] ) {
453 $sth->execute( $linefilter[0] );
459 while ( my ($celvalue) = $sth->fetchrow ) {
462 $cell{rowtitle} = $celvalue;
465 # $cell{rowtitle} = "";
468 push @loopline, \%cell;
471 #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
475 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
478 $colfield .= "dayname($column)";
480 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
483 $colfield .= "monthname($column)";
485 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
488 $colfield .= "Year($column)";
491 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
494 $colfield .= "dayname($column)";
496 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
499 $colfield .= "monthname($column)";
501 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
504 $colfield .= "Year($column)";
507 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
510 $colfield .= "dayname($column)";
512 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
515 $colfield .= "monthname($column)";
517 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
520 $colfield .= "Year($column)";
524 $colfield .= $column;
529 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
530 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
531 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
532 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
533 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
534 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
535 AND $column IS NOT NULL";
538 if ( $colfilter[1] ) {
539 if ( $colfilter[0] ) {
540 $strsth2 .= " AND $column BETWEEN ? AND ? ";
543 $strsth2 .= " AND $column < ? ";
548 and ( ( $column =~ /closedate/ )
549 or ( $line =~ /received/ )
550 or ( $line =~ /acquired/ ) )
553 $strsth2 .= " AND $column > ? ";
555 elsif ( $colfilter[0] ) {
556 $colfilter[0] =~ s/\*/%/g;
557 $strsth2 .= " AND $column LIKE ? ";
560 $strsth2 .= " GROUP BY $colfield";
561 $strsth2 .= " ORDER BY $colfield";
563 # warn "MASON:. $strsth2";
565 my $sth2 = $dbh->prepare($strsth2);
566 if ( (@colfilter) and ( $colfilter[1] ) ) {
568 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
569 $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
571 elsif ( $colfilter[0] ) {
572 $sth2->execute( $colfilter[0] );
578 while ( my ($celvalue) = $sth2->fetchrow ) {
582 # warn "coltitle :".$celvalue;
583 $cell{coltitle} = $celvalue;
585 push @loopcol, \%cell;
588 # warn "fin des titres colonnes";
594 #Initialization of cell values.....
598 foreach my $row (@loopline) {
599 foreach my $col (@loopcol) {
601 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
602 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
604 $table{ $row->{rowtitle} }->{totalrow} = 0;
607 # preparing calculation
609 $strcalc .= "SELECT $linefield, $colfield, ";
610 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
611 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
612 if ( $process == 2 );
613 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
614 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
615 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
616 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
617 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
618 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
620 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
621 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
623 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
624 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
626 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
627 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
629 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
630 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
632 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
633 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
635 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
636 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
638 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
639 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
641 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
642 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
644 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
645 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
647 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
648 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
650 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
651 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
652 if ( @$filters[10] );
653 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
655 # warn "/n/n". $strcalc;
656 my $dbcalc = $dbh->prepare($strcalc);
659 # warn "filling table";
661 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
663 # warn "filling table $row / $col / $value ";
664 $emptycol = 1 if ( $col eq undef );
665 $col = "zzEMPTY" if ( $col eq undef );
666 $row = "zzEMPTY" if ( $row eq undef );
668 $table{$row}->{$col} += $value;
669 $table{$row}->{totalrow} += $value;
670 $grantotal += $value;
673 push @loopcol, { coltitle => "NULL" } if ($emptycol);
675 foreach my $row ( sort keys %table ) {
678 #@loopcol ensures the order for columns is common with column titles
679 # and the number matches the number of columns
680 foreach my $col (@loopcol) {
681 my $value = $table{$row}->{
682 ( $col->{coltitle} eq "NULL" )
686 push @loopcell, { value => $value };
690 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
691 'loopcell' => \@loopcell,
692 'hilighted' => ( $hilighted > 0 ),
693 'totalrow' => $table{$row}->{totalrow}
695 $hilighted = -$hilighted;
698 # warn "footer processing";
699 foreach my $col (@loopcol) {
701 foreach my $row (@looprow) {
703 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
706 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
710 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
713 # warn "summ for column ".$col->{coltitle}." = ".$total;
714 push @loopfooter, { 'totalcol' => $total };
717 # the header of the table
718 # $globalline{loopfilter}=\@loopfilter;
719 # the core of the table
720 $globalline{looprow} = \@looprow;
721 $globalline{loopcol} = \@loopcol;
723 # # the foot (totals by borrower type)
724 $globalline{loopfooter} = \@loopfooter;
725 $globalline{total} = $grantotal;
726 $globalline{line} = $line;
727 $globalline{column} = $column;
728 push @mainloop, \%globalline;