3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use C4
::Dates qw
/format_date format_date_in_iso/;
35 reports/acquisitions_stats.pl
39 Plugin that shows a stats on borrowers
44 my $do_it = $input->param('do_it');
45 my $fullreportname = "reports/acquisitions_stats.tt";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 $filters[0] = format_date_in_iso
( $filters[0] );
50 $filters[1] = format_date_in_iso
( $filters[1] );
51 $filters[2] = format_date_in_iso
( $filters[2] );
52 $filters[3] = format_date_in_iso
( $filters[3] );
53 my $podsp = $input->param("PlacedOnDisplay");
54 my $rodsp = $input->param("ReceivedOnDisplay");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
61 template_name
=> $fullreportname,
65 flagsrequired
=> { reports
=> '*' },
70 our $sep = $input->param("sep") // '';
71 $sep = "\t" if ($sep eq 'tabulation');
79 calculate
( $line, $column, $podsp, $rodsp, $calc, \
@filters );
80 if ( $output eq "screen" ) {
81 $template->param( mainloop
=> $results );
82 output_html_with_http_headers
$input, $cookie, $template->output;
86 -type
=> 'application/vnd.sun.xml.calc',
88 -attachment
=> "$basename.csv",
89 -name
=> "$basename.csv"
91 my $cols = @
$results[0]->{loopcol
};
92 my $lines = @
$results[0]->{looprow
};
93 print @
$results[0]->{line
} . "/" . @
$results[0]->{column
} . $sep;
94 foreach my $col (@
$cols) {
95 print $col->{coltitle
} . $sep;
98 foreach my $line (@
$lines) {
99 my $x = $line->{loopcell
};
100 print $line->{rowtitle
} . $sep;
101 foreach my $cell (@
$x) {
102 print $cell->{value
} . $sep;
104 print $line->{totalrow
};
108 $cols = @
$results[0]->{loopfooter
};
109 foreach my $col (@
$cols) {
110 print $sep. $col->{totalcol
};
112 print $sep. @
$results[0]->{total
};
117 my $dbh = C4
::Context
->dbh;
119 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
121 my $booksellers = $req->fetchall_arrayref({});
123 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
128 $select{''} = "All item types";
129 while ( my ( $value, $desc ) = $req->fetchrow ) {
130 push @select, $value;
131 $select{$value} = $desc;
133 my $CGIItemTypes = CGI
::scrolling_list
(
142 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
147 $select{''} = "All funds";
149 while ( my ( $value, $desc ) = $req->fetchrow ) {
150 push @select, $value;
151 $select{$value} = $desc;
153 my $CGIBudget = CGI
::scrolling_list
(
164 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
172 while ( my ($value) = $req->fetchrow ) {
175 push @select, $value;
176 $select{$value} = $value;
179 my $CGISort1 = CGI
::scrolling_list
(
190 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
200 while ( my ($value) = $req->fetchrow ) {
203 $hglghtsort2 = !($hassort1);
204 push @select, $value;
205 $select{$value} = $value;
208 my $CGISort2 = CGI
::scrolling_list
(
217 my $CGIextChoice = CGI
::scrolling_list
(
220 -values => ['CSV'], # FIXME translation
225 my $CGIsepChoice = GetDelimiterChoices
;
227 my $branches = GetBranches
;
229 foreach ( sort keys %$branches ) {
230 push @branches, $branches->{$_};
233 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField
('items.ccode', '');
236 if($ccode_subfield_structure) {
237 $ccode_label = $ccode_subfield_structure->{liblibrarian
};
238 $ccode_avlist = GetAuthorisedValues
($ccode_subfield_structure->{authorised_value
});
242 booksellers
=> $booksellers,
243 CGIItemType
=> $CGIItemTypes,
244 CGIBudget
=> $CGIBudget,
245 hassort1
=> $hassort1,
246 hassort2
=> $hassort2,
247 CGISort1
=> $CGISort1,
248 CGISort2
=> $CGISort2,
249 CGIextChoice
=> $CGIextChoice,
250 CGIsepChoice
=> $CGIsepChoice,
251 branches
=> \
@branches,
252 ccode_label
=> $ccode_label,
253 ccode_avlist
=> $ccode_avlist,
257 output_html_with_http_headers
$input, $cookie, $template->output;
260 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
273 my $dbh = C4
::Context
->dbh;
279 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
280 if( defined @
$filters[$i] and @
$filters[$i] ne '' ) {
282 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @
$filters[ $i - 1 ] ) ) {
283 $cell{err
} = 1 if ( @
$filters[$i] lt @
$filters[ $i - 1 ] );
285 # format the dates filters, otherwise just fill as is
287 $cell{filter
} = @
$filters[$i];
289 $cell{filter
} = format_date
(@
$filters[$i]);
292 push @loopfilter, \
%cell;
298 foreach ($line, $column) {
301 if ( $_ =~ /closedate/ ) {
302 $filter{$_}->[0] = @
$filters[0];
303 $filter{$_}->[1] = @
$filters[1];
306 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
307 } elsif ( $podsp == 2 ) {
308 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
309 } elsif ( $podsp == 3 ) {
310 $field{$a} = "Year($a)";
315 elsif ( $_ =~ /received/ ) {
316 $filter{$_}->[0] = @
$filters[2];
317 $filter{$_}->[1] = @
$filters[3];
320 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
321 } elsif ( $rodsp == 2 ) {
322 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
323 } elsif ( $rodsp == 3 ) {
324 $field{$a} = "Year($a)";
329 elsif ( $_ =~ /bookseller/ ) {
330 $filter{$_}->[0] = @
$filters[4];
332 elsif ( $_ =~ /homebranch/ ) {
333 $filter{$_}->[0] = @
$filters[5];
335 elsif ( $_ =~ /ccode/ ) {
336 $filter{$_}->[0] = @
$filters[6];
338 elsif ( $_ =~ /itemtype/ ) {
339 $filter{$_}->[0] = @
$filters[7];
341 elsif ( $_ =~ /budget/ ) {
342 $filter{$_}->[0] = @
$filters[8];
344 elsif ( $_ =~ /sort1/ ) {
345 $filter{$_}->[0] = @
$filters[9];
347 elsif ( $_ =~ /sort2/ ) {
348 $filter{$_}->[0] = @
$filters[10];
352 my @linefilter = @
{ $filter{$line} };
353 my $linefield = $field{$line};
354 my @colfilter = @
{ $filter{$column} };
355 my $colfield = $field{$column};
359 SELECT DISTINCTROW $linefield
361 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
362 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
363 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
364 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
365 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
366 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
367 WHERE $line IS NOT NULL AND $line <> '' ";
370 if ( $linefilter[1] ) {
371 if ( $linefilter[0] ) {
372 $strsth .= " AND $line BETWEEN ? AND ? ";
375 $strsth .= " AND $line <= ? ";
380 and ( ( $line =~ /closedate/ )
381 or ( $line =~ /received/ ))
384 $strsth .= " AND $line >= ? ";
386 elsif ( $linefilter[0] ) {
387 $linefilter[0] =~ s/\*/%/g;
388 $strsth .= " AND $line LIKE ? ";
391 $strsth .= " GROUP BY $linefield";
392 $strsth .= " ORDER BY $line";
394 my $sth = $dbh->prepare($strsth);
395 if ( (@linefilter) and ( $linefilter[1] ) ) {
396 $sth->execute( $linefilter[0], $linefilter[1] );
398 elsif ( $linefilter[0] ) {
399 $sth->execute( $linefilter[0] );
404 while ( my ($celvalue) = $sth->fetchrow ) {
407 $cell{rowtitle
} = $celvalue;
408 push @loopline, \
%cell;
415 SELECT DISTINCTROW $colfield
417 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
418 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
419 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
420 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
421 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
422 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
423 WHERE $column IS NOT NULL AND $column <> ''
427 if ( $colfilter[1] ) {
428 if ( $colfilter[0] ) {
429 $strsth2 .= " AND $column BETWEEN ? AND ? ";
432 $strsth2 .= " AND $column <= ? ";
437 and ( ( $column =~ /closedate/ )
438 or ( $line =~ /received/ ))
441 $strsth2 .= " AND $column >= ? ";
443 elsif ( $colfilter[0] ) {
444 $colfilter[0] =~ s/\*/%/g;
445 $strsth2 .= " AND $column LIKE ? ";
449 $strsth2 .= " GROUP BY $colfield";
450 $strsth2 .= " ORDER BY $colfield";
452 my $sth2 = $dbh->prepare($strsth2);
454 if ( (@colfilter) and ($colfilter[1]) ) {
455 $sth2->execute( $colfilter[0], $colfilter[1] );
457 elsif ( $colfilter[0] ) {
458 $sth2->execute( $colfilter[0] );
463 while ( my $celvalue = $sth2->fetchrow ) {
466 $cell{coltitle
} = $celvalue;
467 push @loopcol, \
%cell;
475 #Initialization of cell values.....
478 foreach my $row (@loopline) {
479 foreach my $col (@loopcol) {
480 $table{ $row->{rowtitle
} }->{ $col->{coltitle
} } = 0;
482 $table{ $row->{rowtitle
} }->{totalrow
} = 0;
485 # preparing calculation
487 $strcalc .= "SELECT $linefield, $colfield, ";
488 if ( $process == 1 ) {
489 $strcalc .= "COUNT(*) ";
490 } elsif ( $process == 2 ) {
491 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
492 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
493 $strcalc .= "SUM(aqorders.listprice) ";
499 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
500 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
501 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
502 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
503 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
504 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
505 WHERE aqorders.datecancellationprinted IS NULL ";
506 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
507 if ( $process == 4 );
508 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
509 if ( $process == 5 );
510 @
$filters[0] =~ s/\*/%/g if ( @
$filters[0] );
511 $strcalc .= " AND aqbasket.closedate >= '" . @
$filters[0] . "'"
513 @
$filters[1] =~ s/\*/%/g if ( @
$filters[1] );
514 $strcalc .= " AND aqbasket.closedate <= '" . @
$filters[1] . "'"
516 @
$filters[2] =~ s/\*/%/g if ( @
$filters[2] );
517 $strcalc .= " AND aqorders.datereceived >= '" . @
$filters[2] . "'"
519 @
$filters[3] =~ s/\*/%/g if ( @
$filters[3] );
520 $strcalc .= " AND aqorders.datereceived <= '" . @
$filters[3] . "'"
522 @
$filters[4] =~ s/\*/%/g if ( @
$filters[4] );
523 $strcalc .= " AND aqbooksellers.name LIKE '" . @
$filters[4] . "'"
525 $strcalc .= " AND items.homebranch = '" . @
$filters[5] . "'"
527 @
$filters[6] =~ s/\*/%/g if ( @
$filters[6] );
528 $strcalc .= " AND items.ccode = '" . @
$filters[6] . "'"
530 @
$filters[7] =~ s/\*/%/g if ( @
$filters[7] );
531 $strcalc .= " AND biblioitems.itemtype LIKE '" . @
$filters[7] . "'"
533 @
$filters[8] =~ s/\*/%/g if ( @
$filters[8] );
534 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @
$filters[8] . "'"
536 @
$filters[9] =~ s/\*/%/g if ( @
$filters[9] );
537 $strcalc .= " AND aqorders.sort1 LIKE '" . @
$filters[9] . "'"
539 @
$filters[10] =~ s/\*/%/g if ( @
$filters[10] );
540 $strcalc .= " AND aqorders.sort2 LIKE '" . @
$filters[10] . "'"
541 if ( @
$filters[10] );
543 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
544 my $dbcalc = $dbh->prepare($strcalc);
548 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
549 $emptycol = 1 if ( !defined($col) );
550 $col = "zzEMPTY" if ( !defined($col) );
551 $row = "zzEMPTY" if ( !defined($row) );
553 $table{$row}->{$col} += $value;
554 $table{$row}->{totalrow
} += $value;
555 $grantotal += $value;
558 push @loopcol, { coltitle
=> "NULL" } if ($emptycol);
560 foreach my $row ( sort keys %table ) {
562 #@loopcol ensures the order for columns is common with column titles
563 # and the number matches the number of columns
564 foreach my $col (@loopcol) {
565 my $value = $table{$row}->{ ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY" : $col->{coltitle
} };
566 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
567 push @loopcell, { value
=> $value };
570 rowtitle
=> ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
571 loopcell
=> \
@loopcell,
572 hilighted
=> ( $hilighted > 0 ),
573 totalrow
=> $table{$row}->{totalrow
}
575 $r->{totalrow
} = sprintf("%.2f", $r->{totalrow
}) if($r->{totalrow
} and grep /$process/, (3,4,5));
577 $hilighted = -$hilighted;
580 foreach my $col (@loopcol) {
582 foreach my $row (@looprow) {
584 ( $row->{rowtitle
} eq "NULL" ) ?
"zzEMPTY"
587 ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY"
591 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
593 push @loopfooter, { 'totalcol' => $total };
596 # the header of the table
597 $globalline{loopfilter
} = \
@loopfilter;
598 # the core of the table
599 $globalline{looprow
} = \
@looprow;
600 $globalline{loopcol
} = \
@loopcol;
602 # # the foot (totals by borrower type)
603 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
604 $globalline{loopfooter
} = \
@loopfooter;
605 $globalline{total
} = $grantotal;
606 $globalline{line
} = $line;
607 $globalline{column
} = $column;
608 push @mainloop, \
%globalline;