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");
61 #warn "calcul : ".$calc;
62 my ($template, $borrowernumber, $cookie)
63 = get_template_and_user({template_name => $fullreportname,
67 flagsrequired => {reports => '*'},
70 our $sep = $input->param("sep");
71 $sep = "\t" if ($sep eq 'tabulation');
72 $template->param(do_it => $do_it,
73 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
77 calculate( $line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters );
78 if ( $output eq "screen" ) {
79 $template->param( mainloop => $results );
80 output_html_with_http_headers $input, $cookie, $template->output;
84 -type => 'application/vnd.sun.xml.calc',
86 -attachment => "$basename.csv",
87 -name => "$basename.csv"
89 my $cols = @$results[0]->{loopcol};
90 my $lines = @$results[0]->{looprow};
91 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
92 foreach my $col (@$cols) {
93 print $col->{coltitle} . $sep;
96 foreach my $line (@$lines) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle} . $sep;
99 foreach my $cell (@$x) {
100 print $cell->{value} . $sep;
102 print $line->{totalrow};
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col (@$cols) {
108 print $sep. $col->{totalcol};
110 print $sep. @$results[0]->{total};
115 my $dbh = C4::Context->dbh;
119 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
123 $select{''} = "All Suppliers";
124 while ( my ( $value, $desc ) = $req->fetchrow ) {
126 $select{$value}=$desc;
128 my $CGIBookSellers = CGI::scrolling_list(
137 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
142 $select{''} = "All Item Types";
143 while ( my ( $value, $desc ) = $req->fetchrow ) {
144 push @select, $value;
145 $select{$value} = $desc;
147 my $CGIItemTypes = CGI::scrolling_list(
156 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
161 $select{''} = "All budgets";
163 while ( my ( $value, $desc ) = $req->fetchrow ) {
164 push @select, $value;
165 $select{$value} = $desc;
167 my $CGIBudget = CGI::scrolling_list(
178 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
186 while ( my ($value) = $req->fetchrow ) {
189 push @select, $value;
190 $select{$value} = $value;
193 my $CGISort1 = CGI::scrolling_list(
204 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
214 while ( my ($value) = $req->fetchrow ) {
217 $hglghtsort2 = !($hassort1);
218 push @select, $value;
219 $select{$value} = $value;
222 my $CGISort2 = CGI::scrolling_list(
231 my $CGIextChoice = CGI::scrolling_list(
234 -values => ['CSV'], # FIXME translation
239 my $CGIsepChoice = GetDelimiterChoices;
242 CGIBookSeller => $CGIBookSellers,
243 CGIItemType => $CGIItemTypes,
244 CGIBudget => $CGIBudget,
245 hassort1 => $hassort1,
246 hassort2 => $hassort2,
247 HlghtSort2 => $hglghtsort2,
248 CGISort1 => $CGISort1,
249 CGISort2 => $CGISort2,
250 CGIextChoice => $CGIextChoice,
251 CGIsepChoice => $CGIsepChoice,
252 date_today => C4::Dates->new()->output()
256 output_html_with_http_headers $input, $cookie, $template->output;
259 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
269 my $dbh = C4::Context->dbh;
275 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
277 if ( @$filters[$i] ) {
278 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
279 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
281 # format the dates filters, otherwise just fill as is
283 $cell{filter} .= @$filters[$i];
285 $cell{filter} .= format_date(@$filters[$i]);
287 $cell{crit} .= "Placed On From" if ( $i == 0 );
288 $cell{crit} .= "Placed On To" if ( $i == 1 );
289 $cell{crit} .= "Received On From" if ( $i == 2 );
290 $cell{crit} .= "Received On To" if ( $i == 3 );
292 # $cell{crit} .= "Acquired On From" if ( $i == 4 );
293 # $cell{crit} .= "Acquired On To" if ( $i == 5 );
295 $cell{crit} .= "BookSeller" if ( $i == 4 );
296 $cell{crit} .= "Doc Type" if ( $i == 5 );
297 $cell{crit} .= "Budget" if ( $i == 6 );
298 $cell{crit} .= "Sort1" if ( $i == 7 );
299 $cell{crit} .= "Sort2" if ( $i == 8 );
300 push @loopfilter, \%cell;
306 # warn "filtres ".@filters[0];
307 # warn "filtres ".@filters[1];
308 # warn "filtres ".@filters[2];
309 # warn "filtres ".@filters[3];
311 $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
312 $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
313 $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
314 $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
316 # $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
317 # $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
319 $linefilter[0] = @$filters[4] if ( $line =~ /bookseller/ );
320 $linefilter[0] = @$filters[5] if ( $line =~ /itemtype/ );
321 $linefilter[0] = @$filters[6] if ( $line =~ /budget/ );
322 $linefilter[0] = @$filters[7] if ( $line =~ /sort1/ );
323 $linefilter[0] = @$filters[8] if ( $line =~ /sort2/ );
325 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
328 $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
329 $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
330 $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
331 $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
333 # $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
334 # $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
335 $colfilter[0] = @$filters[4] if ( $column =~ /bookseller/ );
336 $colfilter[0] = @$filters[5] if ( $column =~ /itemtype/ );
337 $colfilter[0] = @$filters[6] if ( $column =~ /budget/ );
338 $colfilter[0] = @$filters[7] if ( $column =~ /sort1/ );
339 $colfilter[0] = @$filters[8] if ( $column =~ /sort2/ );
341 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
343 # warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
347 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
350 $linefield .= "dayname($line)";
352 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
355 $linefield .= "monthname($line)";
357 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
360 $linefield .= "Year($line)";
363 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
366 $linefield .= "dayname($line)";
368 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
371 $linefield .= "monthname($line)";
373 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
376 $linefield .= "Year($line)";
379 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
382 # $linefield .= "dayname($line)";
384 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
387 # $linefield .= "monthname($line)";
389 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
392 # $linefield .= "Year($line)";
401 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket )
402 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
403 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
404 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
406 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
407 AND $line IS NOT NULL AND $line <> '' ";
409 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
412 if ( $linefilter[1] ) {
413 if ( $linefilter[0] ) {
414 $strsth .= " AND $line BETWEEN ? AND ? ";
417 $strsth .= " AND $line <= ? ";
422 and ( ( $line =~ /closedate/ )
423 or ( $line =~ /received/ ))
424 # or ( $line =~ /acquired/ ) )
427 $strsth .= " AND $line >= ? ";
429 elsif ( $linefilter[0] ) {
430 $linefilter[0] =~ s/\*/%/g;
431 $strsth .= " AND $line LIKE ? ";
434 $strsth .= " GROUP BY $linefield";
435 $strsth .= " ORDER BY $line";
437 #warn "377:strsth= $strsth";
439 my $sth = $dbh->prepare($strsth);
440 if ( (@linefilter) and ( $linefilter[1] ) ) {
441 $sth->execute( $linefilter[0], $linefilter[1] );
443 elsif ( $linefilter[0] ) {
444 $sth->execute( $linefilter[0] );
449 while ( my ($celvalue) = $sth->fetchrow ) {
452 $cell{rowtitle} = $celvalue;
453 push @loopline, \%cell;
457 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
461 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
464 $colfield .= "dayname($column)";
466 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
469 $colfield .= "monthname($column)";
471 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
474 $colfield .= "Year($column)";
477 elsif ( ( $column =~ /received/ ) and ( $rodsp == 1 ) ) {
480 $colfield .= "dayname($column)";
482 elsif ( ( $column =~ /received/ ) and ( $rodsp == 2 ) ) {
485 $colfield .= "monthname($column)";
487 elsif ( ( $column =~ /received/ ) and ( $rodsp == 3 ) ) {
490 $colfield .= "Year($column)";
493 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
496 # $colfield .= "dayname($column)";
498 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
501 # $colfield .= "monthname($column)";
503 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
506 # $colfield .= "Year($column)";
510 $colfield .= $column;
515 "SELECT distinctrow $colfield FROM (aqorders, aqbasket )
516 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
517 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
518 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
520 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
521 WHERE (aqorders.basketno=aqbasket.basketno) AND
522 $column IS NOT NULL AND $column <> '' ";
524 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
527 if ( $colfilter[1] ) {
528 if ( $colfilter[0] ) {
529 $strsth2 .= " AND $column BETWEEN ? AND ? ";
532 $strsth2 .= " AND $column <= ? ";
537 and ( ( $column =~ /closedate/ )
538 or ( $line =~ /received/ ))
539 # or ( $line =~ /acquired/ ) )
542 $strsth2 .= " AND $column >= ? ";
544 elsif ( $colfilter[0] ) {
545 $colfilter[0] =~ s/\*/%/g;
546 $strsth2 .= " AND $column LIKE ? ";
551 $strsth2 .= " GROUP BY $colfield";
552 $strsth2 .= " ORDER BY $column";
554 my $sth2 = $dbh->prepare($strsth2);
556 if ( (@colfilter) and ($colfilter[1]) ) {
557 $sth2->execute( $colfilter[0], $colfilter[1] );
559 elsif ( $colfilter[0] ) {
560 $sth2->execute( $colfilter[0] );
565 while ( my $celvalue = $sth2->fetchrow ) {
568 $cell{coltitle} = $celvalue;
569 push @loopcol, \%cell;
573 # warn "fin des titres colonnes";
579 #Initialization of cell values.....
582 # warn "init table...\n";
583 foreach my $row (@loopline) {
584 foreach my $col (@loopcol) {
585 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
587 $table{ $row->{rowtitle} }->{totalrow} = 0;
590 # preparing calculation
592 $strcalc .= "SELECT $linefield, $colfield, ";
593 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
594 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
595 if ( $process == 2 );
596 $strcalc .= "FROM (aqorders, aqbasket )
597 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
598 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
599 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
601 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
602 WHERE (aqorders.basketno=aqbasket.basketno) ";
604 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
606 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
607 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
609 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
610 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
612 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
613 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
615 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
616 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
618 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
619 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
620 # if ( @$filters[4] );
621 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
622 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
623 # if ( @$filters[5] );
624 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
625 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
627 @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
628 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
630 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
631 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[6] . "'"
633 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
634 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
636 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
637 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
640 $strcalc .= " AND aqorders.datecancellationprinted is NULL ";
642 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
644 # warn $strcalc . "\n";
646 my $dbcalc = $dbh->prepare($strcalc);
649 # warn "filling table";
651 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
652 next if ($row eq undef || $col eq undef);
654 # warn "filling table $row / $col / $value ";
656 $emptycol = 1 if ( !defined($col) );
657 $col = "zzEMPTY" if ( !defined($col) );
658 $row = "zzEMPTY" if ( !defined($row) );
660 $table{$row}->{$col} += $value;
661 $table{$row}->{totalrow} += $value;
662 $grantotal += $value;
665 push @loopcol, { coltitle => "NULL" } if ($emptycol);
667 foreach my $row ( sort keys %table ) {
669 #@loopcol ensures the order for columns is common with column titles
670 # and the number matches the number of columns
671 foreach my $col (@loopcol) {
672 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
673 push @loopcell, { value => $value };
677 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
678 'loopcell' => \@loopcell,
679 'hilighted' => ( $hilighted > 0 ),
680 'totalrow' => $table{$row}->{totalrow}
682 $hilighted = -$hilighted;
685 # warn "footer processing";
686 foreach my $col (@loopcol) {
688 foreach my $row (@looprow) {
690 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
693 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
697 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
700 # warn "summ for column ".$col->{coltitle}." = ".$total;
701 push @loopfooter, { 'totalcol' => $total };
704 # the header of the table
705 $globalline{loopfilter}=\@loopfilter;
706 # the core of the table
707 $globalline{looprow} = \@looprow;
708 $globalline{loopcol} = \@loopcol;
710 # # the foot (totals by borrower type)
711 $globalline{loopfooter} = \@loopfooter;
712 $globalline{total} = $grantotal;
713 $globalline{line} = $line;
714 $globalline{column} = $column;
715 push @mainloop, \%globalline;