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
35 plugin that shows a stats on borrowers
45 my $do_it = $input->param('do_it');
46 my $fullreportname = "reports/acquisitions_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $podsp = $input->param("PlacedOnDisplay");
51 my $rodsp = $input->param("ReceivedOnDisplay");
52 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
53 my $calc = $input->param("Cellvalue");
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 my $del = $input->param("sep");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user({template_name => $fullreportname,
65 flagsrequired => {reports => 1},
68 $template->param(do_it => $do_it,
69 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
70 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
71 IntranetNav => C4::Context->preference("IntranetNav"),
76 "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
79 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
80 if ( $output eq "screen" ) {
81 $template->param( mainloop => $results );
82 output_html_with_http_headers $input, $cookie, $template->output;
87 -type => 'application/vnd.sun.xml.calc',
89 -attachment => "$basename.csv",
90 -name => "$basename.csv"
92 my $cols = @$results[0]->{loopcol};
93 my $lines = @$results[0]->{looprow};
95 $sep = C4::Context->preference("delimiter");
96 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
97 foreach my $col (@$cols) {
98 print $col->{coltitle} . $sep;
101 foreach my $line (@$lines) {
102 my $x = $line->{loopcell};
103 print $line->{rowtitle} . $sep;
104 foreach my $cell (@$x) {
105 print $cell->{value} . $sep;
107 print $line->{totalrow};
111 $cols = @$results[0]->{loopfooter};
112 foreach my $col (@$cols) {
113 print $sep. $col->{totalcol};
115 print $sep. @$results[0]->{total};
120 my $dbh = C4::Context->dbh;
127 "SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
133 while ( my ( $value, $desc ) = $req->fetchrow ) {
136 # $select{$value}=$desc;
138 my $CGIBookSellers = CGI::scrolling_list(
143 # -labels => \%select,
150 "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description"
157 while ( my ( $value, $desc ) = $req->fetchrow ) {
158 push @select, $value;
159 $select{$value} = $desc;
161 my $CGIItemTypes = CGI::scrolling_list(
172 "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname"
180 while ( my ( $value, $desc ) = $req->fetchrow ) {
181 push @select, $value;
182 $select{$value} = $desc;
184 my $CGIBudget = CGI::scrolling_list(
195 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
201 while ( my ($value) = $req->fetchrow ) {
202 $hassort1 = 1 if ($value);
203 push @select, $value;
205 my $CGISort1 = CGI::scrolling_list(
215 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
223 while ( my ($value) = $req->fetchrow ) {
224 $hassort2 = 1 if ($value);
225 $hglghtsort2 = !($hassort1);
226 push @select, $value;
228 my $CGISort2 = CGI::scrolling_list(
236 my @mime = ( C4::Context->preference("MIME") );
237 foreach my $mime (@mime) {
242 my $CGIextChoice = CGI::scrolling_list(
250 my @dels = ( C4::Context->preference("delimiter") );
251 my $CGIsepChoice = CGI::scrolling_list(
260 CGIBookSeller => $CGIBookSellers,
261 CGIItemType => $CGIItemTypes,
262 CGIBudget => $CGIBudget,
263 hassort1 => $hassort1,
264 hassort2 => $hassort2,
265 HlghtSort2 => $hglghtsort2,
266 CGISort1 => $CGISort1,
267 CGISort2 => $CGISort2,
268 CGIextChoice => $CGIextChoice,
269 CGIsepChoice => $CGIsepChoice
273 output_html_with_http_headers $input, $cookie, $template->output;
276 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
286 my $dbh = C4::Context->dbh;
292 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
294 if ( @$filters[$i] ) {
295 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
296 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
298 $cell{filter} .= @$filters[$i];
299 $cell{crit} .= "Placed On From" if ( $i == 0 );
300 $cell{crit} .= "Placed On To" if ( $i == 1 );
301 $cell{crit} .= "Received On From" if ( $i == 2 );
302 $cell{crit} .= "Received On To" if ( $i == 3 );
304 $cell{crit} .= "Acquired On From" if ( $i == 4 );
305 $cell{crit} .= "Acquired On To" if ( $i == 5 );
307 $cell{crit} .= "BookSeller" if ( $i == 6 );
308 $cell{crit} .= "Doc Type" if ( $i == 7 );
309 $cell{crit} .= "Budget" if ( $i == 8 );
310 $cell{crit} .= "Sort1" if ( $i == 9 );
311 $cell{crit} .= "Sort2" if ( $i == 10 );
312 push @loopfilter, \%cell;
318 # warn "filtres ".@filters[0];
319 # warn "filtres ".@filters[1];
320 # warn "filtres ".@filters[2];
321 # warn "filtres ".@filters[3];
323 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
324 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
325 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
326 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
328 $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
329 $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
331 $linefilter[0] = @$filters[6] if ( $line =~ /bookseller/ );
332 $linefilter[0] = @$filters[7] if ( $line =~ /itemtype/ );
333 $linefilter[0] = @$filters[8] if ( $line =~ /bookfund/ );
334 $linefilter[0] = @$filters[9] if ( $line =~ /sort1/ );
335 $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
337 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
340 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
341 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
342 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
343 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
345 $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
346 $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
348 $colfilter[0] = @$filters[6] if ( $column =~ /bookseller/ );
349 $colfilter[0] = @$filters[7] if ( $column =~ /itemtype/ );
350 $colfilter[0] = @$filters[8] if ( $column =~ /bookfund/ );
351 $colfilter[0] = @$filters[9] if ( $column =~ /sort1/ );
352 $colfilter[0] = @$filters[10] if ( $column =~ /sort2/ );
354 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
356 #warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
360 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
363 $linefield .= "dayname($line)";
365 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
368 $linefield .= "monthname($line)";
370 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
373 $linefield .= "Year($line)";
376 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
379 $linefield .= "dayname($line)";
381 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
384 $linefield .= "monthname($line)";
386 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
389 $linefield .= "Year($line)";
392 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
395 $linefield .= "dayname($line)";
397 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
400 $linefield .= "monthname($line)";
402 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
405 $linefield .= "Year($line)";
414 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
415 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
416 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
417 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
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 ";
422 if ( $linefilter[1] ) {
423 if ( $linefilter[0] ) {
424 $strsth .= " AND $line BETWEEN ? AND ? ";
427 $strsth .= " AND $line < ? ";
432 and ( ( $line =~ /closedate/ )
433 or ( $line =~ /received/ )
434 or ( $line =~ /acquired/ ) )
437 $strsth .= " AND $line > ? ";
439 elsif ( $linefilter[0] ) {
440 $linefilter[0] =~ s/\*/%/g;
441 $strsth .= " AND $line LIKE ? ";
444 $strsth .= " GROUP BY $linefield";
445 $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] . "'" );
453 elsif ( $linefilter[0] ) {
454 $sth->execute( $linefilter[0] );
460 while ( my ($celvalue) = $sth->fetchrow ) {
463 $cell{rowtitle} = $celvalue;
466 # $cell{rowtitle} = "";
469 push @loopline, \%cell;
472 #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
476 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
479 $colfield .= "dayname($column)";
481 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
484 $colfield .= "monthname($column)";
486 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
489 $colfield .= "Year($column)";
492 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
495 $colfield .= "dayname($column)";
497 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
500 $colfield .= "monthname($column)";
502 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
505 $colfield .= "Year($column)";
508 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
511 $colfield .= "dayname($column)";
513 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
516 $colfield .= "monthname($column)";
518 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
521 $colfield .= "Year($column)";
525 $colfield .= $column;
530 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
531 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
532 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
533 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
534 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
535 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
536 AND $column IS NOT NULL";
539 if ( $colfilter[1] ) {
540 if ( $colfilter[0] ) {
541 $strsth2 .= " AND $column BETWEEN ? AND ? ";
544 $strsth2 .= " AND $column < ? ";
549 and ( ( $column =~ /closedate/ )
550 or ( $line =~ /received/ )
551 or ( $line =~ /acquired/ ) )
554 $strsth2 .= " AND $column > ? ";
556 elsif ( $colfilter[0] ) {
557 $colfilter[0] =~ s/\*/%/g;
558 $strsth2 .= " AND $column LIKE ? ";
561 $strsth2 .= " GROUP BY $colfield";
562 $strsth2 .= " ORDER BY $colfield";
564 # warn "MASON:. $strsth2";
566 my $sth2 = $dbh->prepare($strsth2);
567 if ( (@colfilter) and ( $colfilter[1] ) ) {
569 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
570 $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
572 elsif ( $colfilter[0] ) {
573 $sth2->execute( $colfilter[0] );
579 while ( my ($celvalue) = $sth2->fetchrow ) {
583 # warn "coltitle :".$celvalue;
584 $cell{coltitle} = $celvalue;
586 push @loopcol, \%cell;
589 # warn "fin des titres colonnes";
595 #Initialization of cell values.....
599 foreach my $row (@loopline) {
600 foreach my $col (@loopcol) {
602 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
603 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
605 $table{ $row->{rowtitle} }->{totalrow} = 0;
608 # preparing calculation
610 $strcalc .= "SELECT $linefield, $colfield, ";
611 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
612 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
613 if ( $process == 2 );
614 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
615 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
616 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
617 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
618 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
619 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
621 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
622 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
624 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
625 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
627 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
628 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
630 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
631 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
633 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
634 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
636 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
637 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
639 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
640 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
642 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
643 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
645 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
646 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
648 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
649 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
651 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
652 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
653 if ( @$filters[10] );
654 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
656 # warn "/n/n". $strcalc;
657 my $dbcalc = $dbh->prepare($strcalc);
660 # warn "filling table";
662 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
664 # warn "filling table $row / $col / $value ";
665 $emptycol = 1 if ( $col eq undef );
666 $col = "zzEMPTY" if ( $col eq undef );
667 $row = "zzEMPTY" if ( $row eq undef );
669 $table{$row}->{$col} += $value;
670 $table{$row}->{totalrow} += $value;
671 $grantotal += $value;
674 push @loopcol, { coltitle => "NULL" } if ($emptycol);
676 foreach my $row ( sort keys %table ) {
679 #@loopcol ensures the order for columns is common with column titles
680 # and the number matches the number of columns
681 foreach my $col (@loopcol) {
682 my $value = $table{$row}->{
683 ( $col->{coltitle} eq "NULL" )
687 push @loopcell, { value => $value };
691 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
692 'loopcell' => \@loopcell,
693 'hilighted' => ( $hilighted > 0 ),
694 'totalrow' => $table{$row}->{totalrow}
696 $hilighted = -$hilighted;
699 # warn "footer processing";
700 foreach my $col (@loopcol) {
702 foreach my $row (@looprow) {
704 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
707 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
711 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
714 # warn "summ for column ".$col->{coltitle}." = ".$total;
715 push @loopfooter, { 'totalcol' => $total };
718 # the header of the table
719 # $globalline{loopfilter}=\@loopfilter;
720 # the core of the table
721 $globalline{looprow} = \@looprow;
722 $globalline{loopcol} = \@loopcol;
724 # # the foot (totals by borrower type)
725 $globalline{loopfooter} = \@loopfooter;
726 $globalline{total} = $grantotal;
727 $globalline{line} = $line;
728 $globalline{column} = $column;
729 push @mainloop, \%globalline;