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
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 => get_date_format_string_for_DHTMLcalendar(),
77 "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
80 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
81 if ( $output eq "screen" ) {
82 $template->param( mainloop => $results );
83 output_html_with_http_headers $input, $cookie, $template->output;
88 -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 # format the dates filters, otherwise just fill as is
301 $cell{filter} .= @$filters[$i];
303 $cell{filter} .= format_date(@$filters[$i]);
305 $cell{crit} .= "Placed On From" if ( $i == 0 );
306 $cell{crit} .= "Placed On To" if ( $i == 1 );
307 $cell{crit} .= "Received On From" if ( $i == 2 );
308 $cell{crit} .= "Received On To" if ( $i == 3 );
310 $cell{crit} .= "Acquired On From" if ( $i == 4 );
311 $cell{crit} .= "Acquired On To" if ( $i == 5 );
313 $cell{crit} .= "BookSeller" if ( $i == 6 );
314 $cell{crit} .= "Doc Type" if ( $i == 7 );
315 $cell{crit} .= "Budget" if ( $i == 8 );
316 $cell{crit} .= "Sort1" if ( $i == 9 );
317 $cell{crit} .= "Sort2" if ( $i == 10 );
318 push @loopfilter, \%cell;
324 # warn "filtres ".@filters[0];
325 # warn "filtres ".@filters[1];
326 # warn "filtres ".@filters[2];
327 # warn "filtres ".@filters[3];
329 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
330 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
331 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
332 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
334 $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
335 $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
337 $linefilter[0] = @$filters[6] if ( $line =~ /bookseller/ );
338 $linefilter[0] = @$filters[7] if ( $line =~ /itemtype/ );
339 $linefilter[0] = @$filters[8] if ( $line =~ /bookfund/ );
340 $linefilter[0] = @$filters[9] if ( $line =~ /sort1/ );
341 $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
343 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
346 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
347 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
348 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
349 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
351 $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
352 $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
354 $colfilter[0] = @$filters[6] if ( $column =~ /bookseller/ );
355 $colfilter[0] = @$filters[7] if ( $column =~ /itemtype/ );
356 $colfilter[0] = @$filters[8] if ( $column =~ /bookfund/ );
357 $colfilter[0] = @$filters[9] if ( $column =~ /sort1/ );
358 $colfilter[0] = @$filters[10] if ( $column =~ /sort2/ );
360 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
362 #warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
366 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
369 $linefield .= "dayname($line)";
371 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
374 $linefield .= "monthname($line)";
376 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
379 $linefield .= "Year($line)";
382 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
385 $linefield .= "dayname($line)";
387 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
390 $linefield .= "monthname($line)";
392 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
395 $linefield .= "Year($line)";
398 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
401 $linefield .= "dayname($line)";
403 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
406 $linefield .= "monthname($line)";
408 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
411 $linefield .= "Year($line)";
420 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
421 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
422 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
423 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
424 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
425 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
428 if ( $linefilter[1] ) {
429 if ( $linefilter[0] ) {
430 $strsth .= " AND $line BETWEEN ? AND ? ";
433 $strsth .= " AND $line < ? ";
438 and ( ( $line =~ /closedate/ )
439 or ( $line =~ /received/ )
440 or ( $line =~ /acquired/ ) )
443 $strsth .= " AND $line > ? ";
445 elsif ( $linefilter[0] ) {
446 $linefilter[0] =~ s/\*/%/g;
447 $strsth .= " AND $line LIKE ? ";
450 $strsth .= " GROUP BY $linefield";
451 $strsth .= " ORDER BY $linefield";
453 #warn "377:strsth= $strsth";
455 my $sth = $dbh->prepare($strsth);
456 if ( (@linefilter) and ( $linefilter[1] ) ) {
457 $sth->execute( "'" . $linefilter[0] . "'", "'" . $linefilter[1] . "'" );
459 elsif ( $linefilter[0] ) {
460 $sth->execute( $linefilter[0] );
466 while ( my ($celvalue) = $sth->fetchrow ) {
469 $cell{rowtitle} = $celvalue;
472 # $cell{rowtitle} = "";
475 push @loopline, \%cell;
478 #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
482 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
485 $colfield .= "dayname($column)";
487 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
490 $colfield .= "monthname($column)";
492 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
495 $colfield .= "Year($column)";
498 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
501 $colfield .= "dayname($column)";
503 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
506 $colfield .= "monthname($column)";
508 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
511 $colfield .= "Year($column)";
514 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
517 $colfield .= "dayname($column)";
519 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
522 $colfield .= "monthname($column)";
524 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
527 $colfield .= "Year($column)";
531 $colfield .= $column;
536 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
537 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
538 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
539 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
540 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
541 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
542 AND $column IS NOT NULL";
545 if ( $colfilter[1] ) {
546 if ( $colfilter[0] ) {
547 $strsth2 .= " AND $column BETWEEN ? AND ? ";
550 $strsth2 .= " AND $column < ? ";
555 and ( ( $column =~ /closedate/ )
556 or ( $line =~ /received/ )
557 or ( $line =~ /acquired/ ) )
560 $strsth2 .= " AND $column > ? ";
562 elsif ( $colfilter[0] ) {
563 $colfilter[0] =~ s/\*/%/g;
564 $strsth2 .= " AND $column LIKE ? ";
567 $strsth2 .= " GROUP BY $colfield";
568 $strsth2 .= " ORDER BY $colfield";
570 # warn "MASON:. $strsth2";
572 my $sth2 = $dbh->prepare($strsth2);
573 if ( (@colfilter) and ( $colfilter[1] ) ) {
575 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
576 $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
578 elsif ( $colfilter[0] ) {
579 $sth2->execute( $colfilter[0] );
585 while ( my ($celvalue) = $sth2->fetchrow ) {
589 # warn "coltitle :".$celvalue;
590 $cell{coltitle} = $celvalue;
592 push @loopcol, \%cell;
595 # warn "fin des titres colonnes";
601 #Initialization of cell values.....
605 foreach my $row (@loopline) {
606 foreach my $col (@loopcol) {
608 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
609 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
611 $table{ $row->{rowtitle} }->{totalrow} = 0;
614 # preparing calculation
616 $strcalc .= "SELECT $linefield, $colfield, ";
617 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
618 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
619 if ( $process == 2 );
620 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
621 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
622 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
623 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
624 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
625 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
627 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
628 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
630 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
631 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
633 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
634 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
636 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
637 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
639 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
640 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
642 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
643 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
645 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
646 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
648 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
649 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
651 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
652 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
654 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
655 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
657 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
658 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
659 if ( @$filters[10] );
660 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
662 # warn "/n/n". $strcalc;
663 my $dbcalc = $dbh->prepare($strcalc);
666 # warn "filling table";
668 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
670 # warn "filling table $row / $col / $value ";
671 $emptycol = 1 if ( $col eq undef );
672 $col = "zzEMPTY" if ( $col eq undef );
673 $row = "zzEMPTY" if ( $row eq undef );
675 $table{$row}->{$col} += $value;
676 $table{$row}->{totalrow} += $value;
677 $grantotal += $value;
680 push @loopcol, { coltitle => "NULL" } if ($emptycol);
682 foreach my $row ( sort keys %table ) {
685 #@loopcol ensures the order for columns is common with column titles
686 # and the number matches the number of columns
687 foreach my $col (@loopcol) {
688 my $value = $table{$row}->{
689 ( $col->{coltitle} eq "NULL" )
693 push @loopcell, { value => $value };
697 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
698 'loopcell' => \@loopcell,
699 'hilighted' => ( $hilighted > 0 ),
700 'totalrow' => $table{$row}->{totalrow}
702 $hilighted = -$hilighted;
705 # warn "footer processing";
706 foreach my $col (@loopcol) {
708 foreach my $row (@looprow) {
710 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
713 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
717 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
720 # warn "summ for column ".$col->{coltitle}." = ".$total;
721 push @loopfooter, { 'totalcol' => $total };
724 # the header of the table
725 # $globalline{loopfilter}=\@loopfilter;
726 # the core of the table
727 $globalline{looprow} = \@looprow;
728 $globalline{loopcol} = \@loopcol;
730 # # the foot (totals by borrower type)
731 $globalline{loopfooter} = \@loopfooter;
732 $globalline{total} = $grantotal;
733 $globalline{line} = $line;
734 $globalline{column} = $column;
735 push @mainloop, \%globalline;