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
31 use C4
::Dates qw
/format_date format_date_in_iso/;
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]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso
($filters[0]) : undef);
50 $filters[1]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso
($filters[1]) : undef);
51 $filters[2]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso
($filters[2]) : undef);
52 $filters[3]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso
($filters[3]) : undef);
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");
61 #warn "calcul : ".$calc;
62 my ($template, $borrowernumber, $cookie)
63 = get_template_and_user
({template_name
=> $fullreportname,
67 flagsrequired
=> {reports
=> 1},
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(),
76 # warn "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
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 bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname");
162 $select{''} = "All Funds";
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 =~ /bookfund/ );
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 =~ /bookfund/ );
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,aqorderbreakdown)
408 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
409 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
410 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
411 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
413 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
416 if ( $linefilter[1] ) {
417 if ( $linefilter[0] ) {
418 $strsth .= " AND $line BETWEEN ? AND ? ";
421 $strsth .= " AND $line <= ? ";
426 and ( ( $line =~ /closedate/ )
427 or ( $line =~ /received/ ))
428 # or ( $line =~ /acquired/ ) )
431 $strsth .= " AND $line >= ? ";
433 elsif ( $linefilter[0] ) {
434 $linefilter[0] =~ s/\*/%/g;
435 $strsth .= " AND $line LIKE ? ";
438 $strsth .= " GROUP BY $linefield";
439 $strsth .= " ORDER BY $linefield";
441 #warn "377:strsth= $strsth";
443 my $sth = $dbh->prepare($strsth);
444 if ( (@linefilter) and ( $linefilter[1] ) ) {
445 $sth->execute( $linefilter[0], $linefilter[1] );
447 elsif ( $linefilter[0] ) {
448 $sth->execute( $linefilter[0] );
453 while ( my ($celvalue) = $sth->fetchrow ) {
456 $cell{rowtitle
} = $celvalue;
457 push @loopline, \
%cell;
461 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
465 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
468 $colfield .= "dayname($column)";
470 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
473 $colfield .= "monthname($column)";
475 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
478 $colfield .= "Year($column)";
481 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
484 $colfield .= "dayname($column)";
486 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
489 $colfield .= "monthname($column)";
491 elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
494 $colfield .= "Year($column)";
497 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
500 # $colfield .= "dayname($column)";
502 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
505 # $colfield .= "monthname($column)";
507 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
510 # $colfield .= "Year($column)";
514 $colfield .= $column;
519 "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
520 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
521 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
522 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
523 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
524 AND $column IS NOT NULL";
526 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
529 if ( $colfilter[1] ) {
530 if ( $colfilter[0] ) {
531 $strsth2 .= " AND $column BETWEEN ? AND ? ";
534 $strsth2 .= " AND $column <= ? ";
539 and ( ( $column =~ /closedate/ )
540 or ( $line =~ /received/ ))
541 # or ( $line =~ /acquired/ ) )
544 $strsth2 .= " AND $column >= ? ";
546 elsif ( $colfilter[0] ) {
547 $colfilter[0] =~ s/\*/%/g;
548 $strsth2 .= " AND $column LIKE ? ";
551 $strsth2 .= " GROUP BY $colfield";
552 $strsth2 .= " ORDER BY $colfield";
554 # warn "MASON:. $strsth2";
556 my $sth2 = $dbh->prepare($strsth2);
557 if ( (@colfilter) and ($colfilter[1]) ) {
558 $sth2->execute( $colfilter[0], $colfilter[1] );
560 elsif ( $colfilter[0] ) {
561 $sth2->execute( $colfilter[0] );
566 while ( my $celvalue = $sth2->fetchrow ) {
569 $cell{coltitle
} = $celvalue;
570 #warn "\$cell{coltitle} = $celvalue\n";
571 push @loopcol, \
%cell;
576 # warn "fin des titres colonnes";
582 #Initialization of cell values.....
585 # warn "init table...\n";
586 foreach my $row (@loopline) {
587 foreach my $col (@loopcol) {
588 #warn " init table : $row->{rowtitle} / $col->{coltitle} \n";
589 $table{ $row->{rowtitle
} }->{ $col->{coltitle
} } = 0;
591 $table{ $row->{rowtitle
} }->{totalrow
} = 0;
594 # preparing calculation
596 $strcalc .= "SELECT $linefield, $colfield, ";
597 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
598 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
599 if ( $process == 2 );
600 $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
601 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
602 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
603 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
604 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
606 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
608 @
$filters[0] =~ s/\*/%/g if ( @
$filters[0] );
609 $strcalc .= " AND aqbasket.closedate >= '" . @
$filters[0] . "'"
611 @
$filters[1] =~ s/\*/%/g if ( @
$filters[1] );
612 $strcalc .= " AND aqbasket.closedate <= '" . @
$filters[1] . "'"
614 @
$filters[2] =~ s/\*/%/g if ( @
$filters[2] );
615 $strcalc .= " AND aqorders.datereceived >= '" . @
$filters[2] . "'"
617 @
$filters[3] =~ s/\*/%/g if ( @
$filters[3] );
618 $strcalc .= " AND aqorders.datereceived <= '" . @
$filters[3] . "'"
620 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
621 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
622 # if ( @$filters[4] );
623 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
624 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
625 # if ( @$filters[5] );
626 @
$filters[4] =~ s/\*/%/g if ( @
$filters[4] );
627 $strcalc .= " AND aqbooksellers.name LIKE '" . @
$filters[4] . "'"
629 @
$filters[5] =~ s/\*/%/g if ( @
$filters[5] );
630 $strcalc .= " AND biblioitems.itemtype LIKE '" . @
$filters[5] . "'"
632 @
$filters[6] =~ s/\*/%/g if ( @
$filters[6] );
633 $strcalc .= " AND aqorderbreakdown.bookfundid LIKE '" . @
$filters[6] . "'"
635 @
$filters[7] =~ s/\*/%/g if ( @
$filters[7] );
636 $strcalc .= " AND aqorders.sort1 LIKE '" . @
$filters[7] . "'"
638 @
$filters[8] =~ s/\*/%/g if ( @
$filters[8] );
639 $strcalc .= " AND aqorders.sort2 LIKE '" . @
$filters[8] . "'"
641 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
643 # warn $strcalc . "\n";
644 my $dbcalc = $dbh->prepare($strcalc);
647 # warn "filling table";
649 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
650 next if ($row eq undef || $col eq undef);
651 #warn "filling table $row / $col / $value ";
652 $emptycol = 1 if ( !defined($col) );
653 $col = "zzEMPTY" if ( !defined($col) );
654 $row = "zzEMPTY" if ( !defined($row) );
656 $table{$row}->{$col} += $value;
657 $table{$row}->{totalrow
} += $value;
658 $grantotal += $value;
661 push @loopcol, { coltitle
=> "NULL" } if ($emptycol);
663 foreach my $row ( sort keys %table ) {
665 #@loopcol ensures the order for columns is common with column titles
666 # and the number matches the number of columns
667 foreach my $col (@loopcol) {
668 my $value = $table{$row}->{ ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY" : $col->{coltitle
} };
669 push @loopcell, { value
=> $value };
673 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
674 'loopcell' => \
@loopcell,
675 'hilighted' => ( $hilighted > 0 ),
676 'totalrow' => $table{$row}->{totalrow
}
678 $hilighted = -$hilighted;
681 # warn "footer processing";
682 foreach my $col (@loopcol) {
684 foreach my $row (@looprow) {
686 ( $row->{rowtitle
} eq "NULL" ) ?
"zzEMPTY"
689 ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY"
693 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
696 # warn "summ for column ".$col->{coltitle}." = ".$total;
697 push @loopfooter, { 'totalcol' => $total };
700 # the header of the table
701 $globalline{loopfilter
}=\
@loopfilter;
702 # the core of the table
703 $globalline{looprow
} = \
@looprow;
704 $globalline{loopcol
} = \
@loopcol;
706 # # the foot (totals by borrower type)
707 $globalline{loopfooter
} = \
@loopfooter;
708 $globalline{total
} = $grantotal;
709 $globalline{line
} = $line;
710 $globalline{column
} = $column;
711 push @mainloop, \
%globalline;