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
=> '*'},
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 @mime = ( C4
::Context
->preference("MIME") );
232 foreach my $mime (@mime) {
236 my $CGIextChoice = CGI
::scrolling_list
(
244 my $CGIsepChoice = GetDelimiterChoices
;
247 CGIBookSeller
=> $CGIBookSellers,
248 CGIItemType
=> $CGIItemTypes,
249 CGIBudget
=> $CGIBudget,
250 hassort1
=> $hassort1,
251 hassort2
=> $hassort2,
252 HlghtSort2
=> $hglghtsort2,
253 CGISort1
=> $CGISort1,
254 CGISort2
=> $CGISort2,
255 CGIextChoice
=> $CGIextChoice,
256 CGIsepChoice
=> $CGIsepChoice,
257 date_today
=> C4
::Dates
->new()->output()
261 output_html_with_http_headers
$input, $cookie, $template->output;
264 my ( $line, $column, $podsp, $rodsp, $aodsp, $process, $filters ) = @_;
274 my $dbh = C4
::Context
->dbh;
280 for ( my $i = 0 ; $i <= 8 ; $i++ ) {
282 if ( @
$filters[$i] ) {
283 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @
$filters[ $i - 1 ] ) ) {
284 $cell{err
} = 1 if ( @
$filters[$i] < @
$filters[ $i - 1 ] );
286 # format the dates filters, otherwise just fill as is
288 $cell{filter
} .= @
$filters[$i];
290 $cell{filter
} .= format_date
(@
$filters[$i]);
292 $cell{crit
} .= "Placed On From" if ( $i == 0 );
293 $cell{crit
} .= "Placed On To" if ( $i == 1 );
294 $cell{crit
} .= "Received On From" if ( $i == 2 );
295 $cell{crit
} .= "Received On To" if ( $i == 3 );
297 # $cell{crit} .= "Acquired On From" if ( $i == 4 );
298 # $cell{crit} .= "Acquired On To" if ( $i == 5 );
300 $cell{crit
} .= "BookSeller" if ( $i == 4 );
301 $cell{crit
} .= "Doc Type" if ( $i == 5 );
302 $cell{crit
} .= "Budget" if ( $i == 6 );
303 $cell{crit
} .= "Sort1" if ( $i == 7 );
304 $cell{crit
} .= "Sort2" if ( $i == 8 );
305 push @loopfilter, \
%cell;
311 # warn "filtres ".@filters[0];
312 # warn "filtres ".@filters[1];
313 # warn "filtres ".@filters[2];
314 # warn "filtres ".@filters[3];
316 $linefilter[0] = @
$filters[0] if ( $line =~ /closedate/ );
317 $linefilter[1] = @
$filters[1] if ( $line =~ /closedate/ );
318 $linefilter[0] = @
$filters[2] if ( $line =~ /received/ );
319 $linefilter[1] = @
$filters[3] if ( $line =~ /received/ );
321 # $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
322 # $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
324 $linefilter[0] = @
$filters[4] if ( $line =~ /bookseller/ );
325 $linefilter[0] = @
$filters[5] if ( $line =~ /itemtype/ );
326 $linefilter[0] = @
$filters[6] if ( $line =~ /budget/ );
327 $linefilter[0] = @
$filters[7] if ( $line =~ /sort1/ );
328 $linefilter[0] = @
$filters[8] if ( $line =~ /sort2/ );
330 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
333 $colfilter[0] = @
$filters[0] if ( $column =~ /closedate/ );
334 $colfilter[1] = @
$filters[1] if ( $column =~ /closedate/ );
335 $colfilter[0] = @
$filters[2] if ( $column =~ /received/ );
336 $colfilter[1] = @
$filters[3] if ( $column =~ /received/ );
338 # $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
339 # $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
340 $colfilter[0] = @
$filters[4] if ( $column =~ /bookseller/ );
341 $colfilter[0] = @
$filters[5] if ( $column =~ /itemtype/ );
342 $colfilter[0] = @
$filters[6] if ( $column =~ /budget/ );
343 $colfilter[0] = @
$filters[7] if ( $column =~ /sort1/ );
344 $colfilter[0] = @
$filters[8] if ( $column =~ /sort2/ );
346 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
348 # warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
352 if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
355 $linefield .= "dayname($line)";
357 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
360 $linefield .= "monthname($line)";
362 elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
365 $linefield .= "Year($line)";
368 elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
371 $linefield .= "dayname($line)";
373 elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
376 $linefield .= "monthname($line)";
378 elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
381 $linefield .= "Year($line)";
384 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
387 # $linefield .= "dayname($line)";
389 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
392 # $linefield .= "monthname($line)";
394 # elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
397 # $linefield .= "Year($line)";
406 "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket )
407 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
408 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
409 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
411 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
412 AND $line IS NOT NULL AND $line <> '' ";
414 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
417 if ( $linefilter[1] ) {
418 if ( $linefilter[0] ) {
419 $strsth .= " AND $line BETWEEN ? AND ? ";
422 $strsth .= " AND $line <= ? ";
427 and ( ( $line =~ /closedate/ )
428 or ( $line =~ /received/ ))
429 # or ( $line =~ /acquired/ ) )
432 $strsth .= " AND $line >= ? ";
434 elsif ( $linefilter[0] ) {
435 $linefilter[0] =~ s/\*/%/g;
436 $strsth .= " AND $line LIKE ? ";
439 $strsth .= " GROUP BY $linefield";
440 $strsth .= " ORDER BY $linefield";
442 #warn "377:strsth= $strsth";
444 my $sth = $dbh->prepare($strsth);
445 if ( (@linefilter) and ( $linefilter[1] ) ) {
446 $sth->execute( $linefilter[0], $linefilter[1] );
448 elsif ( $linefilter[0] ) {
449 $sth->execute( $linefilter[0] );
454 while ( my ($celvalue) = $sth->fetchrow ) {
457 $cell{rowtitle
} = $celvalue;
458 push @loopline, \
%cell;
462 # warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
466 if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
469 $colfield .= "dayname($column)";
471 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
474 $colfield .= "monthname($column)";
476 elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
479 $colfield .= "Year($column)";
482 elsif ( ( $column =~ /received/ ) and ( $rodsp == 1 ) ) {
485 $colfield .= "dayname($column)";
487 elsif ( ( $column =~ /received/ ) and ( $rodsp == 2 ) ) {
490 $colfield .= "monthname($column)";
492 elsif ( ( $column =~ /received/ ) and ( $rodsp == 3 ) ) {
495 $colfield .= "Year($column)";
498 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
501 # $colfield .= "dayname($column)";
503 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
506 # $colfield .= "monthname($column)";
508 # elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
511 # $colfield .= "Year($column)";
515 $colfield .= $column;
520 "SELECT distinctrow $colfield FROM (aqorders, aqbasket )
521 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
522 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
523 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
525 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
526 WHERE (aqorders.basketno=aqbasket.basketno) AND
527 $column IS NOT NULL AND $column <> '' ";
529 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
532 if ( $colfilter[1] ) {
533 if ( $colfilter[0] ) {
534 $strsth2 .= " AND $column BETWEEN ? AND ? ";
537 $strsth2 .= " AND $column <= ? ";
542 and ( ( $column =~ /closedate/ )
543 or ( $line =~ /received/ ))
544 # or ( $line =~ /acquired/ ) )
547 $strsth2 .= " AND $column >= ? ";
549 elsif ( $colfilter[0] ) {
550 $colfilter[0] =~ s/\*/%/g;
551 $strsth2 .= " AND $column LIKE ? ";
556 $strsth2 .= " GROUP BY $colfield";
557 $strsth2 .= " ORDER BY $colfield";
559 my $sth2 = $dbh->prepare($strsth2);
561 if ( (@colfilter) and ($colfilter[1]) ) {
562 $sth2->execute( $colfilter[0], $colfilter[1] );
564 elsif ( $colfilter[0] ) {
565 $sth2->execute( $colfilter[0] );
570 while ( my $celvalue = $sth2->fetchrow ) {
573 $cell{coltitle
} = $celvalue;
574 push @loopcol, \
%cell;
578 # warn "fin des titres colonnes";
584 #Initialization of cell values.....
587 # warn "init table...\n";
588 foreach my $row (@loopline) {
589 foreach my $col (@loopcol) {
590 $table{ $row->{rowtitle
} }->{ $col->{coltitle
} } = 0;
592 $table{ $row->{rowtitle
} }->{totalrow
} = 0;
595 # preparing calculation
597 $strcalc .= "SELECT $linefield, $colfield, ";
598 $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
599 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
600 if ( $process == 2 );
601 $strcalc .= "FROM (aqorders, aqbasket )
602 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
603 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
604 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
606 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
607 WHERE (aqorders.basketno=aqbasket.basketno) ";
609 # LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
611 @
$filters[0] =~ s/\*/%/g if ( @
$filters[0] );
612 $strcalc .= " AND aqbasket.closedate >= '" . @
$filters[0] . "'"
614 @
$filters[1] =~ s/\*/%/g if ( @
$filters[1] );
615 $strcalc .= " AND aqbasket.closedate <= '" . @
$filters[1] . "'"
617 @
$filters[2] =~ s/\*/%/g if ( @
$filters[2] );
618 $strcalc .= " AND aqorders.datereceived >= '" . @
$filters[2] . "'"
620 @
$filters[3] =~ s/\*/%/g if ( @
$filters[3] );
621 $strcalc .= " AND aqorders.datereceived <= '" . @
$filters[3] . "'"
623 # @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
624 # $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
625 # if ( @$filters[4] );
626 # @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
627 # $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
628 # if ( @$filters[5] );
629 @
$filters[4] =~ s/\*/%/g if ( @
$filters[4] );
630 $strcalc .= " AND aqbooksellers.name LIKE '" . @
$filters[4] . "'"
632 @
$filters[5] =~ s/\*/%/g if ( @
$filters[5] );
633 $strcalc .= " AND biblioitems.itemtype LIKE '" . @
$filters[5] . "'"
635 @
$filters[6] =~ s/\*/%/g if ( @
$filters[6] );
636 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @
$filters[6] . "'"
638 @
$filters[7] =~ s/\*/%/g if ( @
$filters[7] );
639 $strcalc .= " AND aqorders.sort1 LIKE '" . @
$filters[7] . "'"
641 @
$filters[8] =~ s/\*/%/g if ( @
$filters[8] );
642 $strcalc .= " AND aqorders.sort2 LIKE '" . @
$filters[8] . "'"
645 $strcalc .= " AND aqorders.datecancellationprinted is NULL ";
647 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
649 # warn $strcalc . "\n";
651 my $dbcalc = $dbh->prepare($strcalc);
654 # warn "filling table";
656 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
657 next if ($row eq undef || $col eq undef);
659 # warn "filling table $row / $col / $value ";
661 $emptycol = 1 if ( !defined($col) );
662 $col = "zzEMPTY" if ( !defined($col) );
663 $row = "zzEMPTY" if ( !defined($row) );
665 $table{$row}->{$col} += $value;
666 $table{$row}->{totalrow
} += $value;
667 $grantotal += $value;
670 push @loopcol, { coltitle
=> "NULL" } if ($emptycol);
672 foreach my $row ( sort keys %table ) {
674 #@loopcol ensures the order for columns is common with column titles
675 # and the number matches the number of columns
676 foreach my $col (@loopcol) {
677 my $value = $table{$row}->{ ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY" : $col->{coltitle
} };
678 push @loopcell, { value
=> $value };
682 'rowtitle' => ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
683 'loopcell' => \
@loopcell,
684 'hilighted' => ( $hilighted > 0 ),
685 'totalrow' => $table{$row}->{totalrow
}
687 $hilighted = -$hilighted;
690 # warn "footer processing";
691 foreach my $col (@loopcol) {
693 foreach my $row (@looprow) {
695 ( $row->{rowtitle
} eq "NULL" ) ?
"zzEMPTY"
698 ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY"
702 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
705 # warn "summ for column ".$col->{coltitle}." = ".$total;
706 push @loopfooter, { 'totalcol' => $total };
709 # the header of the table
710 $globalline{loopfilter
}=\
@loopfilter;
711 # the core of the table
712 $globalline{looprow
} = \
@looprow;
713 $globalline{loopcol
} = \
@loopcol;
715 # # the foot (totals by borrower type)
716 $globalline{loopfooter
} = \
@loopfooter;
717 $globalline{total
} = $grantotal;
718 $globalline{line
} = $line;
719 $globalline{column
} = $column;
720 push @mainloop, \
%globalline;