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
36 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 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 $filters[2]=format_date_in_iso($filters[2]);
53 $filters[3]=format_date_in_iso($filters[3]);
54 my $podsp = $input->param("PlacedOnDisplay");
55 my $rodsp = $input->param("ReceivedOnDisplay");
56 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
57 my $calc = $input->param("Cellvalue");
58 my $output = $input->param("output");
59 my $basename = $input->param("basename");
60 my $mime = $input->param("MIME");
61 my $del = $input->param("sep");
63 #warn "calcul : ".$calc;
64 my ($template, $borrowernumber, $cookie)
65 = get_template_and_user({template_name => $fullreportname,
69 flagsrequired => {reports => 1},
72 $template->param(do_it => $do_it,
73 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
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',
91 -attachment => "$basename.csv",
92 -name => "$basename.csv"
94 my $cols = @$results[0]->{loopcol};
95 my $lines = @$results[0]->{looprow};
97 $sep = C4::Context->preference("delimiter");
98 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
99 foreach my $col (@$cols) {
100 print $col->{coltitle} . $sep;
103 foreach my $line (@$lines) {
104 my $x = $line->{loopcell};
105 print $line->{rowtitle} . $sep;
106 foreach my $cell (@$x) {
107 print $cell->{value} . $sep;
109 print $line->{totalrow};
113 $cols = @$results[0]->{loopfooter};
114 foreach my $col (@$cols) {
115 print $sep. $col->{totalcol};
117 print $sep. @$results[0]->{total};
122 my $dbh = C4::Context->dbh;
129 "SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
135 while ( my ( $value, $desc ) = $req->fetchrow ) {
138 # $select{$value}=$desc;
140 my $CGIBookSellers = CGI::scrolling_list(
145 # -labels => \%select,
152 "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description"
159 while ( my ( $value, $desc ) = $req->fetchrow ) {
160 push @select, $value;
161 $select{$value} = $desc;
163 my $CGIItemTypes = CGI::scrolling_list(
174 "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname"
182 while ( my ( $value, $desc ) = $req->fetchrow ) {
183 push @select, $value;
184 $select{$value} = $desc;
186 my $CGIBudget = CGI::scrolling_list(
197 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
203 while ( my ($value) = $req->fetchrow ) {
204 $hassort1 = 1 if ($value);
205 push @select, $value;
207 my $CGISort1 = CGI::scrolling_list(
217 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
225 while ( my ($value) = $req->fetchrow ) {
226 $hassort2 = 1 if ($value);
227 $hglghtsort2 = !($hassort1);
228 push @select, $value;
230 my $CGISort2 = CGI::scrolling_list(
238 my @mime = ( C4::Context->preference("MIME") );
239 foreach my $mime (@mime) {
244 my $CGIextChoice = CGI::scrolling_list(
252 my @dels = ( C4::Context->preference("delimiter") );
253 my $CGIsepChoice = CGI::scrolling_list(
262 CGIBookSeller => $CGIBookSellers,
263 CGIItemType => $CGIItemTypes,
264 CGIBudget => $CGIBudget,
265 hassort1 => $hassort1,
266 hassort2 => $hassort2,
267 HlghtSort2 => $hglghtsort2,
268 CGISort1 => $CGISort1,
269 CGISort2 => $CGISort2,
270 CGIextChoice => $CGIextChoice,
271 CGIsepChoice => $CGIsepChoice
275 output_html_with_http_headers $input, $cookie, $template->output;
278 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
288 my $dbh = C4::Context->dbh;
294 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
296 if ( @$filters[$i] ) {
297 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
298 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
300 # format the dates filters, otherwise just fill as is
302 $cell{filter} .= @$filters[$i];
304 $cell{filter} .= format_date(@$filters[$i]);
306 $cell{crit} .= "Placed On From" if ( $i == 0 );
307 $cell{crit} .= "Placed On To" if ( $i == 1 );
308 $cell{crit} .= "Received On From" if ( $i == 2 );
309 $cell{crit} .= "Received On To" if ( $i == 3 );
311 $cell{crit} .= "Acquired On From" if ( $i == 4 );
312 $cell{crit} .= "Acquired On To" if ( $i == 5 );
314 $cell{crit} .= "BookSeller" if ( $i == 6 );
315 $cell{crit} .= "Doc Type" if ( $i == 7 );
316 $cell{crit} .= "Budget" if ( $i == 8 );
317 $cell{crit} .= "Sort1" if ( $i == 9 );
318 $cell{crit} .= "Sort2" if ( $i == 10 );
319 push @loopfilter, \%cell;
325 # warn "filtres ".@filters[0];
326 # warn "filtres ".@filters[1];
327 # warn "filtres ".@filters[2];
328 # warn "filtres ".@filters[3];
330 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
331 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
332 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
333 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
335 $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
336 $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
338 $linefilter[0] = @$filters[6] if ( $line =~ /bookseller/ );
339 $linefilter[0] = @$filters[7] if ( $line =~ /itemtype/ );
340 $linefilter[0] = @$filters[8] if ( $line =~ /bookfund/ );
341 $linefilter[0] = @$filters[9] if ( $line =~ /sort1/ );
342 $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
344 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
347 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
348 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
349 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
350 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
352 $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
353 $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
355 $colfilter[0] = @$filters[6] if ( $column =~ /bookseller/ );
356 $colfilter[0] = @$filters[7] if ( $column =~ /itemtype/ );
357 $colfilter[0] = @$filters[8] if ( $column =~ /bookfund/ );
358 $colfilter[0] = @$filters[9] if ( $column =~ /sort1/ );
359 $colfilter[0] = @$filters[10] if ( $column =~ /sort2/ );
361 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
363 #warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
367 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
370 $linefield .= "dayname($line)";
372 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
375 $linefield .= "monthname($line)";
377 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
380 $linefield .= "Year($line)";
383 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
386 $linefield .= "dayname($line)";
388 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
391 $linefield .= "monthname($line)";
393 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
396 $linefield .= "Year($line)";
399 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
402 $linefield .= "dayname($line)";
404 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
407 $linefield .= "monthname($line)";
409 elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
412 $linefield .= "Year($line)";
421 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
422 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
423 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
424 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
425 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
426 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
429 if ( $linefilter[1] ) {
430 if ( $linefilter[0] ) {
431 $strsth .= " AND $line BETWEEN ? AND ? ";
434 $strsth .= " AND $line < ? ";
439 and ( ( $line =~ /closedate/ )
440 or ( $line =~ /received/ )
441 or ( $line =~ /acquired/ ) )
444 $strsth .= " AND $line > ? ";
446 elsif ( $linefilter[0] ) {
447 $linefilter[0] =~ s/\*/%/g;
448 $strsth .= " AND $line LIKE ? ";
451 $strsth .= " GROUP BY $linefield";
452 $strsth .= " ORDER BY $linefield";
454 #warn "377:strsth= $strsth";
456 my $sth = $dbh->prepare($strsth);
457 if ( (@linefilter) and ( $linefilter[1] ) ) {
458 $sth->execute( "'" . $linefilter[0] . "'", "'" . $linefilter[1] . "'" );
460 elsif ( $linefilter[0] ) {
461 $sth->execute( $linefilter[0] );
467 while ( my ($celvalue) = $sth->fetchrow ) {
470 $cell{rowtitle} = $celvalue;
473 # $cell{rowtitle} = "";
476 push @loopline, \%cell;
479 #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
483 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
486 $colfield .= "dayname($column)";
488 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
491 $colfield .= "monthname($column)";
493 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
496 $colfield .= "Year($column)";
499 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
502 $colfield .= "dayname($column)";
504 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
507 $colfield .= "monthname($column)";
509 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
512 $colfield .= "Year($column)";
515 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
518 $colfield .= "dayname($column)";
520 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
523 $colfield .= "monthname($column)";
525 elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
528 $colfield .= "Year($column)";
532 $colfield .= $column;
537 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
538 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
539 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
540 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
541 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
542 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
543 AND $column IS NOT NULL";
546 if ( $colfilter[1] ) {
547 if ( $colfilter[0] ) {
548 $strsth2 .= " AND $column BETWEEN ? AND ? ";
551 $strsth2 .= " AND $column < ? ";
556 and ( ( $column =~ /closedate/ )
557 or ( $line =~ /received/ )
558 or ( $line =~ /acquired/ ) )
561 $strsth2 .= " AND $column > ? ";
563 elsif ( $colfilter[0] ) {
564 $colfilter[0] =~ s/\*/%/g;
565 $strsth2 .= " AND $column LIKE ? ";
568 $strsth2 .= " GROUP BY $colfield";
569 $strsth2 .= " ORDER BY $colfield";
571 # warn "MASON:. $strsth2";
573 my $sth2 = $dbh->prepare($strsth2);
574 if ( (@colfilter) and ( $colfilter[1] ) ) {
576 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
577 $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
579 elsif ( $colfilter[0] ) {
580 $sth2->execute( $colfilter[0] );
586 while ( my ($celvalue) = $sth2->fetchrow ) {
590 # warn "coltitle :".$celvalue;
591 $cell{coltitle} = $celvalue;
593 push @loopcol, \%cell;
596 # warn "fin des titres colonnes";
602 #Initialization of cell values.....
606 foreach my $row (@loopline) {
607 foreach my $col (@loopcol) {
609 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
610 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
612 $table{ $row->{rowtitle} }->{totalrow} = 0;
615 # preparing calculation
617 $strcalc .= "SELECT $linefield, $colfield, ";
618 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
619 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
620 if ( $process == 2 );
621 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
622 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
623 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
624 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
625 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
626 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
628 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
629 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
631 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
632 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
634 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
635 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
637 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
638 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
640 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
641 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
643 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
644 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
646 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
647 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
649 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
650 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
652 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
653 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
655 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
656 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
658 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
659 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
660 if ( @$filters[10] );
661 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
663 # warn "/n/n". $strcalc;
664 my $dbcalc = $dbh->prepare($strcalc);
667 # warn "filling table";
669 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
671 # warn "filling table $row / $col / $value ";
672 $emptycol = 1 if ( $col eq undef );
673 $col = "zzEMPTY" if ( $col eq undef );
674 $row = "zzEMPTY" if ( $row eq undef );
676 $table{$row}->{$col} += $value;
677 $table{$row}->{totalrow} += $value;
678 $grantotal += $value;
681 push @loopcol, { coltitle => "NULL" } if ($emptycol);
683 foreach my $row ( sort keys %table ) {
686 #@loopcol ensures the order for columns is common with column titles
687 # and the number matches the number of columns
688 foreach my $col (@loopcol) {
689 my $value = $table{$row}->{
690 ( $col->{coltitle} eq "NULL" )
694 push @loopcell, { value => $value };
698 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
699 'loopcell' => \@loopcell,
700 'hilighted' => ( $hilighted > 0 ),
701 'totalrow' => $table{$row}->{totalrow}
703 $hilighted = -$hilighted;
706 # warn "footer processing";
707 foreach my $col (@loopcol) {
709 foreach my $row (@looprow) {
711 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
714 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
718 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
721 # warn "summ for column ".$col->{coltitle}." = ".$total;
722 push @loopfooter, { 'totalcol' => $total };
725 # the header of the table
726 # $globalline{loopfilter}=\@loopfilter;
727 # the core of the table
728 $globalline{looprow} = \@looprow;
729 $globalline{loopcol} = \@loopcol;
731 # # the foot (totals by borrower type)
732 $globalline{loopfooter} = \@loopfooter;
733 $globalline{total} = $grantotal;
734 $globalline{line} = $line;
735 $globalline{column} = $column;
736 push @mainloop, \%globalline;