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");
61 #warn "calcul : ".$calc;
62 my ($template, $borrowernumber, $cookie)
63 = get_template_and_user({template_name => $fullreportname,
67 flagsrequired => {editcatalogue => 1},
70 $template->param(do_it => $do_it,
71 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
72 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
73 IntranetNav => C4::Context->preference("IntranetNav"),
78 "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
81 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
82 if ( $output eq "screen" ) {
83 $template->param( mainloop => $results );
84 output_html_with_http_headers $input, $cookie, $template->output;
89 -type => 'application/vnd.sun.xml.calc',
90 -attachment => "$basename.csv",
91 -name => "$basename.csv"
93 my $cols = @$results[0]->{loopcol};
94 my $lines = @$results[0]->{looprow};
96 $sep = C4::Context->preference("delimiter");
97 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
98 foreach my $col (@$cols) {
99 print $col->{coltitle} . $sep;
102 foreach my $line (@$lines) {
103 my $x = $line->{loopcell};
104 print $line->{rowtitle} . $sep;
105 foreach my $cell (@$x) {
106 print $cell->{value} . $sep;
108 print $line->{totalrow};
112 $cols = @$results[0]->{loopfooter};
113 foreach my $col (@$cols) {
114 print $sep. $col->{totalcol};
116 print $sep. @$results[0]->{total};
121 my $dbh = C4::Context->dbh;
128 "SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
134 while ( my ( $value, $desc ) = $req->fetchrow ) {
137 # $select{$value}=$desc;
139 my $CGIBookSellers = CGI::scrolling_list(
144 # -labels => \%select,
151 "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description"
158 while ( my ( $value, $desc ) = $req->fetchrow ) {
159 push @select, $value;
160 $select{$value} = $desc;
162 my $CGIItemTypes = CGI::scrolling_list(
173 "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname"
181 while ( my ( $value, $desc ) = $req->fetchrow ) {
182 push @select, $value;
183 $select{$value} = $desc;
185 my $CGIBudget = CGI::scrolling_list(
196 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
202 while ( my ($value) = $req->fetchrow ) {
203 $hassort1 = 1 if ($value);
204 push @select, $value;
206 my $CGISort1 = CGI::scrolling_list(
216 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
224 while ( my ($value) = $req->fetchrow ) {
225 $hassort2 = 1 if ($value);
226 $hglghtsort2 = !($hassort1);
227 push @select, $value;
229 my $CGISort2 = CGI::scrolling_list(
237 my @mime = ( C4::Context->preference("MIME") );
238 foreach my $mime (@mime) {
243 my $CGIextChoice = CGI::scrolling_list(
251 my @dels = ( C4::Context->preference("delimiter") );
252 my $CGIsepChoice = CGI::scrolling_list(
261 CGIBookSeller => $CGIBookSellers,
262 CGIItemType => $CGIItemTypes,
263 CGIBudget => $CGIBudget,
264 hassort1 => $hassort1,
265 hassort2 => $hassort2,
266 HlghtSort2 => $hglghtsort2,
267 CGISort1 => $CGISort1,
268 CGISort2 => $CGISort2,
269 CGIextChoice => $CGIextChoice,
270 CGIsepChoice => $CGIsepChoice
274 output_html_with_http_headers $input, $cookie, $template->output;
277 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
287 my $dbh = C4::Context->dbh;
293 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
295 if ( @$filters[$i] ) {
296 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
297 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
299 $cell{filter} .= @$filters[$i];
300 $cell{crit} .= "Placed On From" if ( $i == 0 );
301 $cell{crit} .= "Placed On To" if ( $i == 1 );
302 $cell{crit} .= "Received On From" if ( $i == 2 );
303 $cell{crit} .= "Received On To" if ( $i == 3 );
305 $cell{crit} .= "Acquired On From" if ( $i == 4 );
306 $cell{crit} .= "Acquired On To" if ( $i == 5 );
308 $cell{crit} .= "BookSeller" if ( $i == 6 );
309 $cell{crit} .= "Doc Type" if ( $i == 7 );
310 $cell{crit} .= "Budget" if ( $i == 8 );
311 $cell{crit} .= "Sort1" if ( $i == 9 );
312 $cell{crit} .= "Sort2" if ( $i == 10 );
313 push @loopfilter, \%cell;
319 # warn "filtres ".@filters[0];
320 # warn "filtres ".@filters[1];
321 # warn "filtres ".@filters[2];
322 # warn "filtres ".@filters[3];
324 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
325 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
326 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
327 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
329 $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
330 $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
332 $linefilter[0] = @$filters[6] if ( $line =~ /bookseller/ );
333 $linefilter[0] = @$filters[7] if ( $line =~ /itemtype/ );
334 $linefilter[0] = @$filters[8] if ( $line =~ /bookfund/ );
335 $linefilter[0] = @$filters[9] if ( $line =~ /sort1/ );
336 $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
338 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
341 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
342 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
343 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
344 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
346 $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
347 $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
349 $colfilter[0] = @$filters[6] if ( $column =~ /bookseller/ );
350 $colfilter[0] = @$filters[7] if ( $column =~ /itemtype/ );
351 $colfilter[0] = @$filters[8] if ( $column =~ /bookfund/ );
352 $colfilter[0] = @$filters[9] if ( $column =~ /sort1/ );
353 $colfilter[0] = @$filters[10] 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.biblioitemnumber= items.biblioitemnumber)
417 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
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] );
461 while ( my ($celvalue) = $sth->fetchrow ) {
464 $cell{rowtitle} = $celvalue;
467 # $cell{rowtitle} = "";
470 push @loopline, \%cell;
473 #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
477 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
480 $colfield .= "dayname($column)";
482 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
485 $colfield .= "monthname($column)";
487 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
490 $colfield .= "Year($column)";
493 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
496 $colfield .= "dayname($column)";
498 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
501 $colfield .= "monthname($column)";
503 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
506 $colfield .= "Year($column)";
509 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
512 $colfield .= "dayname($column)";
514 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
517 $colfield .= "monthname($column)";
519 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
522 $colfield .= "Year($column)";
526 $colfield .= $column;
531 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
532 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
533 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
534 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
535 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
536 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
537 AND $column IS NOT NULL";
540 if ( $colfilter[1] ) {
541 if ( $colfilter[0] ) {
542 $strsth2 .= " AND $column BETWEEN ? AND ? ";
545 $strsth2 .= " AND $column < ? ";
550 and ( ( $column =~ /closedate/ )
551 or ( $line =~ /received/ )
552 or ( $line =~ /acquired/ ) )
555 $strsth2 .= " AND $column > ? ";
557 elsif ( $colfilter[0] ) {
558 $colfilter[0] =~ s/\*/%/g;
559 $strsth2 .= " AND $column LIKE ? ";
562 $strsth2 .= " GROUP BY $colfield";
563 $strsth2 .= " ORDER BY $colfield";
565 # warn "MASON:. $strsth2";
567 my $sth2 = $dbh->prepare($strsth2);
568 if ( (@colfilter) and ( $colfilter[1] ) ) {
570 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
571 $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
573 elsif ( $colfilter[0] ) {
574 $sth2->execute( $colfilter[0] );
580 while ( my ($celvalue) = $sth2->fetchrow ) {
584 # warn "coltitle :".$celvalue;
585 $cell{coltitle} = $celvalue;
587 push @loopcol, \%cell;
590 # warn "fin des titres colonnes";
596 #Initialization of cell values.....
600 foreach my $row (@loopline) {
601 foreach my $col (@loopcol) {
603 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
604 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
606 $table{ $row->{rowtitle} }->{totalrow} = 0;
609 # preparing calculation
611 $strcalc .= "SELECT $linefield, $colfield, ";
612 $strcalc .= "COUNT( aqorders.ordernumber ) " if ( $process == 1 );
613 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
614 if ( $process == 2 );
615 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
616 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
617 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
618 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
619 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
620 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
622 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
623 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
625 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
626 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
628 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
629 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
631 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
632 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
634 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
635 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
637 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
638 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
640 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
641 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
643 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
644 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
646 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
647 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
649 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
650 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
652 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
653 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
654 if ( @$filters[10] );
655 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
657 # warn "/n/n". $strcalc;
658 my $dbcalc = $dbh->prepare($strcalc);
661 # warn "filling table";
663 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
665 # warn "filling table $row / $col / $value ";
666 $emptycol = 1 if ( $col eq undef );
667 $col = "zzEMPTY" if ( $col eq undef );
668 $row = "zzEMPTY" if ( $row eq undef );
670 $table{$row}->{$col} += $value;
671 $table{$row}->{totalrow} += $value;
672 $grantotal += $value;
675 push @loopcol, { coltitle => "NULL" } if ($emptycol);
677 foreach my $row ( sort keys %table ) {
680 #@loopcol ensures the order for columns is common with column titles
681 # and the number matches the number of columns
682 foreach my $col (@loopcol) {
683 my $value = $table{$row}->{
684 ( $col->{coltitle} eq "NULL" )
688 push @loopcell, { value => $value };
692 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
693 'loopcell' => \@loopcell,
694 'hilighted' => ( $hilighted > 0 ),
695 'totalrow' => $table{$row}->{totalrow}
697 $hilighted = -$hilighted;
700 # warn "footer processing";
701 foreach my $col (@loopcol) {
703 foreach my $row (@looprow) {
705 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
708 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
712 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
715 # warn "summ for column ".$col->{coltitle}." = ".$total;
716 push @loopfooter, { 'totalcol' => $total };
719 # the header of the table
720 # $globalline{loopfilter}=\@loopfilter;
721 # the core of the table
722 $globalline{looprow} = \@looprow;
723 $globalline{loopcol} = \@loopcol;
725 # # the foot (totals by borrower type)
726 $globalline{loopfooter} = \@loopfooter;
727 $globalline{total} = $grantotal;
728 $globalline{line} = $line;
729 $globalline{column} = $column;
730 push @mainloop, \%globalline;