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>.
36 reports/acquisitions_stats.pl
40 Plugin that shows a stats on borrowers
45 my $do_it = $input->param('do_it');
46 my $fullreportname = "reports/acquisitions_stats.tt";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->multi_param("Filter");
50 $filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
52 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
54 $filters[2] = eval { output_pref( { dt => dt_from_string( $filters[2]), dateonly => 1, dateformat => 'iso' } ); }
56 $filters[3] = eval { output_pref( { dt => dt_from_string( $filters[3]), dateonly => 1, dateformat => 'iso' } ); }
58 my $podsp = $input->param("PlacedOnDisplay");
59 my $rodsp = $input->param("ReceivedOnDisplay");
60 my $calc = $input->param("Cellvalue");
61 my $output = $input->param("output");
62 my $basename = $input->param("basename");
64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66 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 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
132 while ( my ( $value, $desc ) = $req->fetchrow ) {
133 push @bselect, $value;
134 $bselect{$value} = $desc;
143 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
149 while ( my ($value) = $req->fetchrow ) {
152 push @s1select, $value;
153 $s1select{$value} = $value;
157 values => \@s1select,
158 labels => \%s1select,
163 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
171 while ( my ($value) = $req->fetchrow ) {
174 $hglghtsort2 = !($hassort1);
175 push @s2select, $value;
176 $s2select{$value} = $value;
180 values => \@s2select,
181 labels => \%s2select,
184 my $CGIsepChoice = GetDelimiterChoices;
186 my @branches = Koha::Libraries->search({}, { order_by => 'branchname' });
188 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode');
191 if($ccode_subfield_structure) {
192 $ccode_label = $ccode_subfield_structure->{liblibrarian};
193 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
196 my $itemtypes = Koha::ItemTypes->search_with_localization;
198 booksellers => $booksellers,
199 itemtypes => $itemtypes, # FIXME Should use the TT plugin instead
201 hassort1 => $hassort1,
202 hassort2 => $hassort2,
205 CGIsepChoice => $CGIsepChoice,
206 branches => \@branches,
207 ccode_label => $ccode_label,
208 ccode_avlist => $ccode_avlist,
212 output_html_with_http_headers $input, $cookie, $template->output;
215 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
228 my $dbh = C4::Context->dbh;
234 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
235 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
237 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
238 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
240 # format the dates filters, otherwise just fill as is
242 $cell{filter} = @$filters[$i];
244 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
245 if ( @$filters[$i] );
248 push @loopfilter, \%cell;
254 foreach ($line, $column) {
257 if ( $_ =~ /closedate/ ) {
258 $filter{$_}->[0] = @$filters[0];
259 $filter{$_}->[1] = @$filters[1];
262 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
263 } elsif ( $podsp == 2 ) {
264 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
265 } elsif ( $podsp == 3 ) {
266 $field{$a} = "Year($a)";
271 elsif ( $_ =~ /received/ ) {
272 $filter{$_}->[0] = @$filters[2];
273 $filter{$_}->[1] = @$filters[3];
276 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
277 } elsif ( $rodsp == 2 ) {
278 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
279 } elsif ( $rodsp == 3 ) {
280 $field{$a} = "Year($a)";
285 elsif ( $_ =~ /bookseller/ ) {
286 $filter{$_}->[0] = @$filters[4];
288 elsif ( $_ =~ /homebranch/ ) {
289 $filter{$_}->[0] = @$filters[5];
291 elsif ( $_ =~ /ccode/ ) {
292 $filter{$_}->[0] = @$filters[6];
294 elsif ( $_ =~ /itemtype/ ) {
295 $filter{$_}->[0] = @$filters[7];
297 elsif ( $_ =~ /budget/ ) {
298 $filter{$_}->[0] = @$filters[8];
300 elsif ( $_ =~ /sort1/ ) {
301 $filter{$_}->[0] = @$filters[9];
303 elsif ( $_ =~ /sort2/ ) {
304 $filter{$_}->[0] = @$filters[10];
308 my @linefilter = @{ $filter{$line} };
309 my $linefield = $field{$line};
310 my @colfilter = @{ $filter{$column} };
311 my $colfield = $field{$column};
315 SELECT DISTINCTROW $linefield
317 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
318 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
319 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
320 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
321 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
322 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
323 WHERE $line IS NOT NULL AND $line <> '' ";
326 if ( $linefilter[1] ) {
327 if ( $linefilter[0] ) {
328 $strsth .= " AND $line BETWEEN ? AND ? ";
331 $strsth .= " AND $line <= ? ";
336 and ( ( $line =~ /closedate/ )
337 or ( $line =~ /received/ ))
340 $strsth .= " AND $line >= ? ";
342 elsif ( $linefilter[0] ) {
343 $linefilter[0] =~ s/\*/%/g;
344 $strsth .= " AND $line LIKE ? ";
347 $strsth .= " GROUP BY $linefield";
348 $strsth .= " ORDER BY $line";
350 my $sth = $dbh->prepare($strsth);
351 if ( (@linefilter) and ( $linefilter[1] ) ) {
352 $sth->execute( $linefilter[0], $linefilter[1] );
354 elsif ( $linefilter[0] ) {
355 $sth->execute( $linefilter[0] );
360 while ( my ($celvalue) = $sth->fetchrow ) {
363 $cell{rowtitle} = $celvalue;
364 push @loopline, \%cell;
371 SELECT DISTINCTROW $colfield
373 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
374 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
375 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
376 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
377 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
378 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
379 WHERE $column IS NOT NULL AND $column <> ''
383 if ( $colfilter[1] ) {
384 if ( $colfilter[0] ) {
385 $strsth2 .= " AND $column BETWEEN ? AND ? ";
388 $strsth2 .= " AND $column <= ? ";
393 and ( ( $column =~ /closedate/ )
394 or ( $line =~ /received/ ))
397 $strsth2 .= " AND $column >= ? ";
399 elsif ( $colfilter[0] ) {
400 $colfilter[0] =~ s/\*/%/g;
401 $strsth2 .= " AND $column LIKE ? ";
405 $strsth2 .= " GROUP BY $colfield";
406 $strsth2 .= " ORDER BY $colfield";
408 my $sth2 = $dbh->prepare($strsth2);
410 if ( (@colfilter) and ($colfilter[1]) ) {
411 $sth2->execute( $colfilter[0], $colfilter[1] );
413 elsif ( $colfilter[0] ) {
414 $sth2->execute( $colfilter[0] );
419 while ( my $celvalue = $sth2->fetchrow ) {
422 $cell{coltitle} = $celvalue;
423 push @loopcol, \%cell;
431 #Initialization of cell values.....
434 foreach my $row (@loopline) {
435 foreach my $col (@loopcol) {
436 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
438 $table{ $row->{rowtitle} }->{totalrow} = 0;
441 # preparing calculation
443 $strcalc .= "SELECT $linefield, $colfield, ";
444 if ( $process == 1 ) {
445 $strcalc .= "COUNT(*) ";
446 } elsif ( $process == 2 ) {
447 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
448 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
449 $strcalc .= "SUM(aqorders.listprice) ";
455 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
456 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
457 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
458 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
459 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
460 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
461 WHERE aqorders.datecancellationprinted IS NULL ";
462 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
463 if ( $process == 4 );
464 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
465 if ( $process == 5 );
466 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
467 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
469 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
470 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
472 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
473 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
475 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
476 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
478 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
479 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
481 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
483 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
484 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
486 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
487 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
489 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
490 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
492 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
493 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
495 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
496 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
497 if ( @$filters[10] );
499 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
500 my $dbcalc = $dbh->prepare($strcalc);
504 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
505 $emptycol = 1 if ( !defined($col) );
506 $col = "zzEMPTY" if ( !defined($col) );
507 $row = "zzEMPTY" if ( !defined($row) );
509 $table{$row}->{$col} += $value;
510 $table{$row}->{totalrow} += $value;
511 $grantotal += $value;
514 push @loopcol, { coltitle => "NULL" } if ($emptycol);
516 foreach my $row ( sort keys %table ) {
518 #@loopcol ensures the order for columns is common with column titles
519 # and the number matches the number of columns
520 foreach my $col (@loopcol) {
521 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
522 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
523 push @loopcell, { value => $value };
526 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
527 loopcell => \@loopcell,
528 hilighted => ( $hilighted > 0 ),
529 totalrow => $table{$row}->{totalrow}
531 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
533 $hilighted = -$hilighted;
536 foreach my $col (@loopcol) {
538 foreach my $row (@looprow) {
540 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
543 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
547 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
549 push @loopfooter, { 'totalcol' => $total };
552 # the header of the table
553 $globalline{loopfilter} = \@loopfilter;
554 # the core of the table
555 $globalline{looprow} = \@looprow;
556 $globalline{loopcol} = \@loopcol;
558 # # the foot (totals by borrower type)
559 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
560 $globalline{loopfooter} = \@loopfooter;
561 $globalline{total} = $grantotal;
562 $globalline{line} = $line;
563 $globalline{column} = $column;
564 push @mainloop, \%globalline;