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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #use warnings; FIXME - Bug 2505
32 use C4::Dates qw/format_date format_date_in_iso/;
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]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[0]) : undef);
51 $filters[1]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[1]) : undef);
52 $filters[2]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[2]) : undef);
53 $filters[3]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[3]) : undef);
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");
62 #warn "calcul : ".$calc;
63 my ($template, $borrowernumber, $cookie)
64 = get_template_and_user({template_name => $fullreportname,
68 flagsrequired => {reports => '*'},
71 our $sep = $input->param("sep");
72 $sep = "\t" if ($sep eq 'tabulation');
73 $template->param(do_it => $do_it,
74 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
78 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
79 if ( $output eq "screen" ) {
80 $template->param( mainloop => $results );
81 output_html_with_http_headers $input, $cookie, $template->output;
85 -type => 'application/vnd.sun.xml.calc',
87 -attachment => "$basename.csv",
88 -name => "$basename.csv"
90 my $cols = @$results[0]->{loopcol};
91 my $lines = @$results[0]->{looprow};
92 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
93 foreach my $col (@$cols) {
94 print $col->{coltitle} . $sep;
97 foreach my $line (@$lines) {
98 my $x = $line->{loopcell};
99 print $line->{rowtitle} . $sep;
100 foreach my $cell (@$x) {
101 print $cell->{value} . $sep;
103 print $line->{totalrow};
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col (@$cols) {
109 print $sep. $col->{totalcol};
111 print $sep. @$results[0]->{total};
116 my $dbh = C4::Context->dbh;
120 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
124 $select{''} = "All Suppliers";
125 while ( my ( $value, $desc ) = $req->fetchrow ) {
127 $select{$value}=$desc;
129 my $CGIBookSellers = CGI::scrolling_list(
138 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
143 $select{''} = "All Item Types";
144 while ( my ( $value, $desc ) = $req->fetchrow ) {
145 push @select, $value;
146 $select{$value} = $desc;
148 my $CGIItemTypes = CGI::scrolling_list(
157 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
162 $select{''} = "All budgets";
164 while ( my ( $value, $desc ) = $req->fetchrow ) {
165 push @select, $value;
166 $select{$value} = $desc;
168 my $CGIBudget = CGI::scrolling_list(
179 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
187 while ( my ($value) = $req->fetchrow ) {
190 push @select, $value;
191 $select{$value} = $value;
194 my $CGISort1 = CGI::scrolling_list(
205 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
215 while ( my ($value) = $req->fetchrow ) {
218 $hglghtsort2 = !($hassort1);
219 push @select, $value;
220 $select{$value} = $value;
223 my $CGISort2 = CGI::scrolling_list(
232 my @mime = ( C4::Context->preference("MIME") );
233 foreach my $mime (@mime) {
237 my $CGIextChoice = CGI::scrolling_list(
245 my $CGIsepChoice = GetDelimiterChoices;
248 CGIBookSeller => $CGIBookSellers,
249 CGIItemType => $CGIItemTypes,
250 CGIBudget => $CGIBudget,
251 hassort1 => $hassort1,
252 hassort2 => $hassort2,
253 HlghtSort2 => $hglghtsort2,
254 CGISort1 => $CGISort1,
255 CGISort2 => $CGISort2,
256 CGIextChoice => $CGIextChoice,
257 CGIsepChoice => $CGIsepChoice,
258 date_today => C4::Dates->new()->output()
262 output_html_with_http_headers $input, $cookie, $template->output;
265 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
275 my $dbh = C4::Context->dbh;
281 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
283 if ( @$filters[$i] ) {
284 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
285 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
287 # format the dates filters, otherwise just fill as is
289 $cell{filter} .= @$filters[$i];
291 $cell{filter} .= format_date(@$filters[$i]);
293 $cell{crit} .= "Placed On From" if ( $i == 0 );
294 $cell{crit} .= "Placed On To" if ( $i == 1 );
295 $cell{crit} .= "Received On From" if ( $i == 2 );
296 $cell{crit} .= "Received On To" if ( $i == 3 );
298 # $cell{crit} .= "Acquired On From" if ( $i == 4 );
299 # $cell{crit} .= "Acquired On To" if ( $i == 5 );
301 $cell{crit} .= "BookSeller" if ( $i == 4 );
302 $cell{crit} .= "Doc Type" if ( $i == 5 );
303 $cell{crit} .= "Budget" if ( $i == 6 );
304 $cell{crit} .= "Sort1" if ( $i == 7 );
305 $cell{crit} .= "Sort2" if ( $i == 8 );
306 push @loopfilter, \%cell;
312 # warn "filtres ".@filters[0];
313 # warn "filtres ".@filters[1];
314 # warn "filtres ".@filters[2];
315 # warn "filtres ".@filters[3];
317 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
318 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
319 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
320 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
322 # $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
323 # $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
325 $linefilter[0] = @$filters[4] if ( $line =~ /bookseller/ );
326 $linefilter[0] = @$filters[5] if ( $line =~ /itemtype/ );
327 $linefilter[0] = @$filters[6] if ( $line =~ /budget/ );
328 $linefilter[0] = @$filters[7] if ( $line =~ /sort1/ );
329 $linefilter[0] = @$filters[8] if ( $line =~ /sort2/ );
331 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
334 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
335 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
336 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
337 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
339 # $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
340 # $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
341 $colfilter[0] = @$filters[4] if ( $column =~ /bookseller/ );
342 $colfilter[0] = @$filters[5] if ( $column =~ /itemtype/ );
343 $colfilter[0] = @$filters[6] if ( $column =~ /budget/ );
344 $colfilter[0] = @$filters[7] if ( $column =~ /sort1/ );
345 $colfilter[0] = @$filters[8] if ( $column =~ /sort2/ );
347 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
349 # warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
353 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
356 $linefield .= "dayname($line)";
358 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
361 $linefield .= "monthname($line)";
363 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
366 $linefield .= "Year($line)";
369 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
372 $linefield .= "dayname($line)";
374 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
377 $linefield .= "monthname($line)";
379 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
382 $linefield .= "Year($line)";
385 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
388 # $linefield .= "dayname($line)";
390 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
393 # $linefield .= "monthname($line)";
395 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
398 # $linefield .= "Year($line)";
407 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket )
408 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
409 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
410 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
412 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
413 AND $line IS NOT NULL AND $line <> '' ";
415 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
418 if ( $linefilter[1] ) {
419 if ( $linefilter[0] ) {
420 $strsth .= " AND $line BETWEEN ? AND ? ";
423 $strsth .= " AND $line <= ? ";
428 and ( ( $line =~ /closedate/ )
429 or ( $line =~ /received/ ))
430 # or ( $line =~ /acquired/ ) )
433 $strsth .= " AND $line >= ? ";
435 elsif ( $linefilter[0] ) {
436 $linefilter[0] =~ s/\*/%/g;
437 $strsth .= " AND $line LIKE ? ";
440 $strsth .= " GROUP BY $linefield";
441 $strsth .= " ORDER BY $linefield";
443 #warn "377:strsth= $strsth";
445 my $sth = $dbh->prepare($strsth);
446 if ( (@linefilter) and ( $linefilter[1] ) ) {
447 $sth->execute( $linefilter[0], $linefilter[1] );
449 elsif ( $linefilter[0] ) {
450 $sth->execute( $linefilter[0] );
455 while ( my ($celvalue) = $sth->fetchrow ) {
458 $cell{rowtitle} = $celvalue;
459 push @loopline, \%cell;
463 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
467 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
470 $colfield .= "dayname($column)";
472 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
475 $colfield .= "monthname($column)";
477 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
480 $colfield .= "Year($column)";
483 elsif ( ( $column =~ /received/ ) and ( $rodsp == 1 ) ) {
486 $colfield .= "dayname($column)";
488 elsif ( ( $column =~ /received/ ) and ( $rodsp == 2 ) ) {
491 $colfield .= "monthname($column)";
493 elsif ( ( $column =~ /received/ ) and ( $rodsp == 3 ) ) {
496 $colfield .= "Year($column)";
499 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
502 # $colfield .= "dayname($column)";
504 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
507 # $colfield .= "monthname($column)";
509 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
512 # $colfield .= "Year($column)";
516 $colfield .= $column;
521 "SELECT distinctrow $colfield FROM (aqorders, aqbasket )
522 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
523 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
524 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
526 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
527 WHERE (aqorders.basketno=aqbasket.basketno) AND
528 $column IS NOT NULL AND $column <> '' ";
530 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
533 if ( $colfilter[1] ) {
534 if ( $colfilter[0] ) {
535 $strsth2 .= " AND $column BETWEEN ? AND ? ";
538 $strsth2 .= " AND $column <= ? ";
543 and ( ( $column =~ /closedate/ )
544 or ( $line =~ /received/ ))
545 # or ( $line =~ /acquired/ ) )
548 $strsth2 .= " AND $column >= ? ";
550 elsif ( $colfilter[0] ) {
551 $colfilter[0] =~ s/\*/%/g;
552 $strsth2 .= " AND $column LIKE ? ";
557 $strsth2 .= " GROUP BY $colfield";
558 $strsth2 .= " ORDER BY $colfield";
560 my $sth2 = $dbh->prepare($strsth2);
562 if ( (@colfilter) and ($colfilter[1]) ) {
563 $sth2->execute( $colfilter[0], $colfilter[1] );
565 elsif ( $colfilter[0] ) {
566 $sth2->execute( $colfilter[0] );
571 while ( my $celvalue = $sth2->fetchrow ) {
574 $cell{coltitle} = $celvalue;
575 push @loopcol, \%cell;
579 # warn "fin des titres colonnes";
585 #Initialization of cell values.....
588 # warn "init table...\n";
589 foreach my $row (@loopline) {
590 foreach my $col (@loopcol) {
591 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
593 $table{ $row->{rowtitle} }->{totalrow} = 0;
596 # preparing calculation
598 $strcalc .= "SELECT $linefield, $colfield, ";
599 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
600 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
601 if ( $process == 2 );
602 $strcalc .= "FROM (aqorders, aqbasket )
603 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
604 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
605 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
607 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
608 WHERE (aqorders.basketno=aqbasket.basketno) ";
610 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
612 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
613 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
615 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
616 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
618 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
619 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
621 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
622 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
624 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
625 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
626 # if ( @$filters[4] );
627 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
628 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
629 # if ( @$filters[5] );
630 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
631 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
633 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
634 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
636 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
637 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[6] . "'"
639 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
640 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
642 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
643 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
646 $strcalc .= " AND aqorders.datecancellationprinted is NULL ";
648 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
650 # warn $strcalc . "\n";
652 my $dbcalc = $dbh->prepare($strcalc);
655 # warn "filling table";
657 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
658 next if ($row eq undef || $col eq undef);
660 # warn "filling table $row / $col / $value ";
662 $emptycol = 1 if ( !defined($col) );
663 $col = "zzEMPTY" if ( !defined($col) );
664 $row = "zzEMPTY" if ( !defined($row) );
666 $table{$row}->{$col} += $value;
667 $table{$row}->{totalrow} += $value;
668 $grantotal += $value;
671 push @loopcol, { coltitle => "NULL" } if ($emptycol);
673 foreach my $row ( sort keys %table ) {
675 #@loopcol ensures the order for columns is common with column titles
676 # and the number matches the number of columns
677 foreach my $col (@loopcol) {
678 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
679 push @loopcell, { value => $value };
683 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
684 'loopcell' => \@loopcell,
685 'hilighted' => ( $hilighted > 0 ),
686 'totalrow' => $table{$row}->{totalrow}
688 $hilighted = -$hilighted;
691 # warn "footer processing";
692 foreach my $col (@loopcol) {
694 foreach my $row (@looprow) {
696 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
699 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
703 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
706 # warn "summ for column ".$col->{coltitle}." = ".$total;
707 push @loopfooter, { 'totalcol' => $total };
710 # the header of the table
711 $globalline{loopfilter}=\@loopfilter;
712 # the core of the table
713 $globalline{looprow} = \@looprow;
714 $globalline{loopcol} = \@loopcol;
716 # # the foot (totals by borrower type)
717 $globalline{loopfooter} = \@loopfooter;
718 $globalline{total} = $grantotal;
719 $globalline{line} = $line;
720 $globalline{column} = $column;
721 push @mainloop, \%globalline;