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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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->multi_param("Filter");
49 $filters[0] = eval { output_pref
( { dt
=> dt_from_string
( $filters[0]), dateonly
=> 1, dateformat
=> 'iso' } ); }
51 $filters[1] = eval { output_pref
( { dt
=> dt_from_string
( $filters[1]), dateonly
=> 1, dateformat
=> 'iso' } ); }
53 $filters[2] = eval { output_pref
( { dt
=> dt_from_string
( $filters[2]), dateonly
=> 1, dateformat
=> 'iso' } ); }
55 $filters[3] = eval { output_pref
( { dt
=> dt_from_string
( $filters[3]), dateonly
=> 1, dateformat
=> 'iso' } ); }
57 my $podsp = $input->param("PlacedOnDisplay");
58 my $rodsp = $input->param("ReceivedOnDisplay");
59 my $calc = $input->param("Cellvalue");
60 my $output = $input->param("output");
61 my $basename = $input->param("basename");
63 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
65 template_name
=> $fullreportname,
69 flagsrequired
=> { reports
=> '*' },
74 our $sep = $input->param("sep") // '';
75 $sep = "\t" if ($sep eq 'tabulation');
83 calculate
( $line, $column, $podsp, $rodsp, $calc, \
@filters );
84 if ( $output eq "screen" ) {
85 $template->param( mainloop
=> $results );
86 output_html_with_http_headers
$input, $cookie, $template->output;
90 -type
=> 'application/vnd.sun.xml.calc',
92 -attachment
=> "$basename.csv",
93 -name
=> "$basename.csv"
95 my $cols = @
$results[0]->{loopcol
};
96 my $lines = @
$results[0]->{looprow
};
97 print @
$results[0]->{line
} . "/" . @
$results[0]->{column
} . $sep;
98 foreach my $col (@
$cols) {
99 print $col->{coltitle
} . $sep;
102 foreach my $line (@
$lines) {
103 my $x = $line->{loopcell
};
104 print $line->{rowtitle
} . $sep;
105 foreach my $cell (@
$x) {
106 print $cell->{value
} . $sep;
108 print $line->{totalrow
};
112 $cols = @
$results[0]->{loopfooter
};
113 foreach my $col (@
$cols) {
114 print $sep. $col->{totalcol
};
116 print $sep. @
$results[0]->{total
};
121 my $dbh = C4
::Context
->dbh;
123 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
125 my $booksellers = $req->fetchall_arrayref({});
127 my $itemtypes = GetItemTypes
( style
=> 'array' );
129 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
134 while ( my ( $value, $desc ) = $req->fetchrow ) {
135 push @bselect, $value;
136 $bselect{$value} = $desc;
145 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
151 while ( my ($value) = $req->fetchrow ) {
154 push @s1select, $value;
155 $s1select{$value} = $value;
159 values => \
@s1select,
160 labels
=> \
%s1select,
165 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
173 while ( my ($value) = $req->fetchrow ) {
176 $hglghtsort2 = !($hassort1);
177 push @s2select, $value;
178 $s2select{$value} = $value;
182 values => \
@s2select,
183 labels
=> \
%s2select,
186 my $CGIsepChoice = GetDelimiterChoices
;
188 my $branches = GetBranches
;
190 foreach ( sort keys %$branches ) {
191 push @branches, $branches->{$_};
194 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField
('items.ccode', '');
197 if($ccode_subfield_structure) {
198 $ccode_label = $ccode_subfield_structure->{liblibrarian
};
199 $ccode_avlist = GetAuthorisedValues
($ccode_subfield_structure->{authorised_value
});
203 booksellers
=> $booksellers,
204 itemtypes
=> $itemtypes,
206 hassort1
=> $hassort1,
207 hassort2
=> $hassort2,
210 CGIsepChoice
=> $CGIsepChoice,
211 branches
=> \
@branches,
212 ccode_label
=> $ccode_label,
213 ccode_avlist
=> $ccode_avlist,
217 output_html_with_http_headers
$input, $cookie, $template->output;
220 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
233 my $dbh = C4
::Context
->dbh;
239 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
240 if( defined @
$filters[$i] and @
$filters[$i] ne '' ) {
242 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @
$filters[ $i - 1 ] ) ) {
243 $cell{err
} = 1 if ( @
$filters[$i] lt @
$filters[ $i - 1 ] );
245 # format the dates filters, otherwise just fill as is
247 $cell{filter
} = @
$filters[$i];
249 $cell{filter
} = eval { output_pref
( { dt
=> dt_from_string
( @
$filters[$i] ), dateonly
=> 1 }); }
250 if ( @
$filters[$i] );
253 push @loopfilter, \
%cell;
259 foreach ($line, $column) {
262 if ( $_ =~ /closedate/ ) {
263 $filter{$_}->[0] = @
$filters[0];
264 $filter{$_}->[1] = @
$filters[1];
267 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
268 } elsif ( $podsp == 2 ) {
269 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
270 } elsif ( $podsp == 3 ) {
271 $field{$a} = "Year($a)";
276 elsif ( $_ =~ /received/ ) {
277 $filter{$_}->[0] = @
$filters[2];
278 $filter{$_}->[1] = @
$filters[3];
281 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
282 } elsif ( $rodsp == 2 ) {
283 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
284 } elsif ( $rodsp == 3 ) {
285 $field{$a} = "Year($a)";
290 elsif ( $_ =~ /bookseller/ ) {
291 $filter{$_}->[0] = @
$filters[4];
293 elsif ( $_ =~ /homebranch/ ) {
294 $filter{$_}->[0] = @
$filters[5];
296 elsif ( $_ =~ /ccode/ ) {
297 $filter{$_}->[0] = @
$filters[6];
299 elsif ( $_ =~ /itemtype/ ) {
300 $filter{$_}->[0] = @
$filters[7];
302 elsif ( $_ =~ /budget/ ) {
303 $filter{$_}->[0] = @
$filters[8];
305 elsif ( $_ =~ /sort1/ ) {
306 $filter{$_}->[0] = @
$filters[9];
308 elsif ( $_ =~ /sort2/ ) {
309 $filter{$_}->[0] = @
$filters[10];
313 my @linefilter = @
{ $filter{$line} };
314 my $linefield = $field{$line};
315 my @colfilter = @
{ $filter{$column} };
316 my $colfield = $field{$column};
320 SELECT DISTINCTROW $linefield
322 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
323 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
324 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
325 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
326 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
327 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
328 WHERE $line IS NOT NULL AND $line <> '' ";
331 if ( $linefilter[1] ) {
332 if ( $linefilter[0] ) {
333 $strsth .= " AND $line BETWEEN ? AND ? ";
336 $strsth .= " AND $line <= ? ";
341 and ( ( $line =~ /closedate/ )
342 or ( $line =~ /received/ ))
345 $strsth .= " AND $line >= ? ";
347 elsif ( $linefilter[0] ) {
348 $linefilter[0] =~ s/\*/%/g;
349 $strsth .= " AND $line LIKE ? ";
352 $strsth .= " GROUP BY $linefield";
353 $strsth .= " ORDER BY $line";
355 my $sth = $dbh->prepare($strsth);
356 if ( (@linefilter) and ( $linefilter[1] ) ) {
357 $sth->execute( $linefilter[0], $linefilter[1] );
359 elsif ( $linefilter[0] ) {
360 $sth->execute( $linefilter[0] );
365 while ( my ($celvalue) = $sth->fetchrow ) {
368 $cell{rowtitle
} = $celvalue;
369 push @loopline, \
%cell;
376 SELECT DISTINCTROW $colfield
378 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
379 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
380 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
381 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
382 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
383 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
384 WHERE $column IS NOT NULL AND $column <> ''
388 if ( $colfilter[1] ) {
389 if ( $colfilter[0] ) {
390 $strsth2 .= " AND $column BETWEEN ? AND ? ";
393 $strsth2 .= " AND $column <= ? ";
398 and ( ( $column =~ /closedate/ )
399 or ( $line =~ /received/ ))
402 $strsth2 .= " AND $column >= ? ";
404 elsif ( $colfilter[0] ) {
405 $colfilter[0] =~ s/\*/%/g;
406 $strsth2 .= " AND $column LIKE ? ";
410 $strsth2 .= " GROUP BY $colfield";
411 $strsth2 .= " ORDER BY $colfield";
413 my $sth2 = $dbh->prepare($strsth2);
415 if ( (@colfilter) and ($colfilter[1]) ) {
416 $sth2->execute( $colfilter[0], $colfilter[1] );
418 elsif ( $colfilter[0] ) {
419 $sth2->execute( $colfilter[0] );
424 while ( my $celvalue = $sth2->fetchrow ) {
427 $cell{coltitle
} = $celvalue;
428 push @loopcol, \
%cell;
436 #Initialization of cell values.....
439 foreach my $row (@loopline) {
440 foreach my $col (@loopcol) {
441 $table{ $row->{rowtitle
} }->{ $col->{coltitle
} } = 0;
443 $table{ $row->{rowtitle
} }->{totalrow
} = 0;
446 # preparing calculation
448 $strcalc .= "SELECT $linefield, $colfield, ";
449 if ( $process == 1 ) {
450 $strcalc .= "COUNT(*) ";
451 } elsif ( $process == 2 ) {
452 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
453 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
454 $strcalc .= "SUM(aqorders.listprice) ";
460 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
461 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
462 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
463 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
464 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
465 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
466 WHERE aqorders.datecancellationprinted IS NULL ";
467 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
468 if ( $process == 4 );
469 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
470 if ( $process == 5 );
471 @
$filters[0] =~ s/\*/%/g if ( @
$filters[0] );
472 $strcalc .= " AND aqbasket.closedate >= '" . @
$filters[0] . "'"
474 @
$filters[1] =~ s/\*/%/g if ( @
$filters[1] );
475 $strcalc .= " AND aqbasket.closedate <= '" . @
$filters[1] . "'"
477 @
$filters[2] =~ s/\*/%/g if ( @
$filters[2] );
478 $strcalc .= " AND aqorders.datereceived >= '" . @
$filters[2] . "'"
480 @
$filters[3] =~ s/\*/%/g if ( @
$filters[3] );
481 $strcalc .= " AND aqorders.datereceived <= '" . @
$filters[3] . "'"
483 @
$filters[4] =~ s/\*/%/g if ( @
$filters[4] );
484 $strcalc .= " AND aqbooksellers.name LIKE '" . @
$filters[4] . "'"
486 $strcalc .= " AND items.homebranch = '" . @
$filters[5] . "'"
488 @
$filters[6] =~ s/\*/%/g if ( @
$filters[6] );
489 $strcalc .= " AND items.ccode = '" . @
$filters[6] . "'"
491 @
$filters[7] =~ s/\*/%/g if ( @
$filters[7] );
492 $strcalc .= " AND biblioitems.itemtype LIKE '" . @
$filters[7] . "'"
494 @
$filters[8] =~ s/\*/%/g if ( @
$filters[8] );
495 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @
$filters[8] . "'"
497 @
$filters[9] =~ s/\*/%/g if ( @
$filters[9] );
498 $strcalc .= " AND aqorders.sort1 LIKE '" . @
$filters[9] . "'"
500 @
$filters[10] =~ s/\*/%/g if ( @
$filters[10] );
501 $strcalc .= " AND aqorders.sort2 LIKE '" . @
$filters[10] . "'"
502 if ( @
$filters[10] );
504 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
505 my $dbcalc = $dbh->prepare($strcalc);
509 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
510 $emptycol = 1 if ( !defined($col) );
511 $col = "zzEMPTY" if ( !defined($col) );
512 $row = "zzEMPTY" if ( !defined($row) );
514 $table{$row}->{$col} += $value;
515 $table{$row}->{totalrow
} += $value;
516 $grantotal += $value;
519 push @loopcol, { coltitle
=> "NULL" } if ($emptycol);
521 foreach my $row ( sort keys %table ) {
523 #@loopcol ensures the order for columns is common with column titles
524 # and the number matches the number of columns
525 foreach my $col (@loopcol) {
526 my $value = $table{$row}->{ ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY" : $col->{coltitle
} };
527 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
528 push @loopcell, { value
=> $value };
531 rowtitle
=> ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
532 loopcell
=> \
@loopcell,
533 hilighted
=> ( $hilighted > 0 ),
534 totalrow
=> $table{$row}->{totalrow
}
536 $r->{totalrow
} = sprintf("%.2f", $r->{totalrow
}) if($r->{totalrow
} and grep /$process/, (3,4,5));
538 $hilighted = -$hilighted;
541 foreach my $col (@loopcol) {
543 foreach my $row (@looprow) {
545 ( $row->{rowtitle
} eq "NULL" ) ?
"zzEMPTY"
548 ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY"
552 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
554 push @loopfooter, { 'totalcol' => $total };
557 # the header of the table
558 $globalline{loopfilter
} = \
@loopfilter;
559 # the core of the table
560 $globalline{looprow
} = \
@looprow;
561 $globalline{loopcol
} = \
@loopcol;
563 # # the foot (totals by borrower type)
564 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
565 $globalline{loopfooter
} = \
@loopfooter;
566 $globalline{total
} = $grantotal;
567 $globalline{line
} = $line;
568 $globalline{column
} = $column;
569 push @mainloop, \
%globalline;