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>.
22 use C4::Auth qw( get_template_and_user );
25 use C4::Reports qw( GetDelimiterChoices );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Koha qw( GetAuthorisedValues );
28 use C4::Biblio qw( GetMarcSubfieldStructureFromKohaField );
30 use Koha::DateUtils qw( dt_from_string output_pref );
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,
68 flagsrequired => { reports => '*' },
72 our $sep = C4::Context->csv_delimiter(scalar $input->param("sep"));
80 calculate( $line, $column, $podsp, $rodsp, $calc, \@filters );
81 if ( $output eq "screen" ) {
82 $template->param( mainloop => $results );
83 output_html_with_http_headers $input, $cookie, $template->output;
87 -type => 'application/vnd.sun.xml.calc',
89 -attachment => "$basename.csv",
90 -name => "$basename.csv"
92 my $cols = @$results[0]->{loopcol};
93 my $lines = @$results[0]->{looprow};
94 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
95 foreach my $col (@$cols) {
96 print $col->{coltitle} . $sep;
99 foreach my $line (@$lines) {
100 my $x = $line->{loopcell};
101 print $line->{rowtitle} . $sep;
102 foreach my $cell (@$x) {
103 print $cell->{value} . $sep;
105 print $line->{totalrow};
109 $cols = @$results[0]->{loopfooter};
110 foreach my $col (@$cols) {
111 print $sep. $col->{totalcol};
113 print $sep. @$results[0]->{total};
118 my $dbh = C4::Context->dbh;
120 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
122 my $booksellers = $req->fetchall_arrayref({});
124 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
129 while ( my ( $value, $desc ) = $req->fetchrow ) {
130 push @bselect, $value;
131 $bselect{$value} = $desc;
140 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
146 while ( my ($value) = $req->fetchrow ) {
149 push @s1select, $value;
150 $s1select{$value} = $value;
154 values => \@s1select,
155 labels => \%s1select,
160 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
168 while ( my ($value) = $req->fetchrow ) {
171 $hglghtsort2 = !($hassort1);
172 push @s2select, $value;
173 $s2select{$value} = $value;
177 values => \@s2select,
178 labels => \%s2select,
181 my $CGIsepChoice = GetDelimiterChoices;
183 my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
185 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode');
188 if($ccode_subfield_structure) {
189 $ccode_label = $ccode_subfield_structure->{liblibrarian};
190 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
193 my $itemtypes = Koha::ItemTypes->search_with_localization;
195 booksellers => $booksellers,
196 itemtypes => $itemtypes, # FIXME Should use the TT plugin instead
198 hassort1 => $hassort1,
199 hassort2 => $hassort2,
202 CGIsepChoice => $CGIsepChoice,
203 branches => $libraries,
204 ccode_label => $ccode_label,
205 ccode_avlist => $ccode_avlist,
209 output_html_with_http_headers $input, $cookie, $template->output;
212 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
225 my $dbh = C4::Context->dbh;
231 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
232 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
234 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
235 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
237 # format the dates filters, otherwise just fill as is
239 $cell{filter} = @$filters[$i];
241 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
242 if ( @$filters[$i] );
245 push @loopfilter, \%cell;
251 foreach ($line, $column) {
254 if ( $_ =~ /closedate/ ) {
255 $filter{$_}->[0] = @$filters[0];
256 $filter{$_}->[1] = @$filters[1];
259 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
260 } elsif ( $podsp == 2 ) {
261 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
262 } elsif ( $podsp == 3 ) {
263 $field{$a} = "Year($a)";
268 elsif ( $_ =~ /received/ ) {
269 $filter{$_}->[0] = @$filters[2];
270 $filter{$_}->[1] = @$filters[3];
273 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
274 } elsif ( $rodsp == 2 ) {
275 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
276 } elsif ( $rodsp == 3 ) {
277 $field{$a} = "Year($a)";
282 elsif ( $_ =~ /bookseller/ ) {
283 $filter{$_}->[0] = @$filters[4];
285 elsif ( $_ =~ /homebranch/ ) {
286 $filter{$_}->[0] = @$filters[5];
288 elsif ( $_ =~ /ccode/ ) {
289 $filter{$_}->[0] = @$filters[6];
291 elsif ( $_ =~ /itemtype/ ) {
292 $filter{$_}->[0] = @$filters[7];
294 elsif ( $_ =~ /budget/ ) {
295 $filter{$_}->[0] = @$filters[8];
297 elsif ( $_ =~ /sort1/ ) {
298 $filter{$_}->[0] = @$filters[9];
300 elsif ( $_ =~ /sort2/ ) {
301 $filter{$_}->[0] = @$filters[10];
305 my @linefilter = @{ $filter{$line} };
306 my $linefield = $field{$line};
307 my @colfilter = @{ $filter{$column} };
308 my $colfield = $field{$column};
312 SELECT DISTINCTROW $linefield
314 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
315 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
316 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
317 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
318 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
319 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
320 WHERE $line IS NOT NULL AND $line <> '' ";
323 if ( $linefilter[1] ) {
324 if ( $linefilter[0] ) {
325 $strsth .= " AND $line BETWEEN ? AND ? ";
328 $strsth .= " AND $line <= ? ";
333 and ( ( $line =~ /closedate/ )
334 or ( $line =~ /received/ ))
337 $strsth .= " AND $line >= ? ";
339 elsif ( $linefilter[0] ) {
340 $linefilter[0] =~ s/\*/%/g;
341 $strsth .= " AND $line LIKE ? ";
344 $strsth .= " GROUP BY $linefield";
345 $strsth .= " ORDER BY $line";
347 my $sth = $dbh->prepare($strsth);
348 if ( (@linefilter) and ( $linefilter[1] ) ) {
349 $sth->execute( $linefilter[0], $linefilter[1] );
351 elsif ( $linefilter[0] ) {
352 $sth->execute( $linefilter[0] );
357 while ( my ($celvalue) = $sth->fetchrow ) {
360 $cell{rowtitle} = $celvalue;
361 push @loopline, \%cell;
368 SELECT DISTINCTROW $colfield
370 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
371 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
372 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
373 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
374 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
375 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
376 WHERE $column IS NOT NULL AND $column <> ''
380 if ( $colfilter[1] ) {
381 if ( $colfilter[0] ) {
382 $strsth2 .= " AND $column BETWEEN ? AND ? ";
385 $strsth2 .= " AND $column <= ? ";
390 and ( ( $column =~ /closedate/ )
391 or ( $line =~ /received/ ))
394 $strsth2 .= " AND $column >= ? ";
396 elsif ( $colfilter[0] ) {
397 $colfilter[0] =~ s/\*/%/g;
398 $strsth2 .= " AND $column LIKE ? ";
402 $strsth2 .= " GROUP BY $colfield";
403 $strsth2 .= " ORDER BY $colfield";
405 my $sth2 = $dbh->prepare($strsth2);
407 if ( (@colfilter) and ($colfilter[1]) ) {
408 $sth2->execute( $colfilter[0], $colfilter[1] );
410 elsif ( $colfilter[0] ) {
411 $sth2->execute( $colfilter[0] );
416 while ( my $celvalue = $sth2->fetchrow ) {
419 $cell{coltitle} = $celvalue;
420 push @loopcol, \%cell;
427 #Initialization of cell values.....
430 foreach my $row (@loopline) {
431 foreach my $col (@loopcol) {
432 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
434 $table{ $row->{rowtitle} }->{totalrow} = 0;
437 # preparing calculation
439 $strcalc .= "SELECT $linefield, $colfield, ";
440 if ( $process == 1 ) {
441 $strcalc .= "COUNT(*) ";
442 } elsif ( $process == 2 ) {
443 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
444 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
445 $strcalc .= "SUM(aqorders.listprice) ";
451 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
452 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
453 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
454 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
455 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
456 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
457 WHERE aqorders.datecancellationprinted IS NULL ";
458 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
459 if ( $process == 4 );
460 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
461 if ( $process == 5 );
462 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
463 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
465 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
466 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
468 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
469 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
471 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
472 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
474 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
475 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
477 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
479 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
480 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
482 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
483 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
485 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
486 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
488 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
489 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
491 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
492 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
493 if ( @$filters[10] );
495 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
496 my $dbcalc = $dbh->prepare($strcalc);
500 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
501 $emptycol = 1 if ( !defined($col) );
502 $col = "zzEMPTY" if ( !defined($col) );
503 $row = "zzEMPTY" if ( !defined($row) );
505 $table{$row}->{$col} += $value;
506 $table{$row}->{totalrow} += $value;
507 $grantotal += $value;
510 push @loopcol, { coltitle => "NULL" } if ($emptycol);
512 foreach my $row ( sort keys %table ) {
514 #@loopcol ensures the order for columns is common with column titles
515 # and the number matches the number of columns
516 foreach my $col (@loopcol) {
517 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
518 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
519 push @loopcell, { value => $value };
522 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
523 loopcell => \@loopcell,
524 hilighted => ( $hilighted > 0 ),
525 totalrow => $table{$row}->{totalrow}
527 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
529 $hilighted = -$hilighted;
532 foreach my $col (@loopcol) {
534 foreach my $row (@looprow) {
536 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
539 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
543 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
545 push @loopfooter, { 'totalcol' => $total };
548 # the header of the table
549 $globalline{loopfilter} = \@loopfilter;
550 # the core of the table
551 $globalline{looprow} = \@looprow;
552 $globalline{loopcol} = \@loopcol;
554 # # the foot (totals by borrower type)
555 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
556 $globalline{loopfooter} = \@loopfooter;
557 $globalline{total} = $grantotal;
558 $globalline{line} = $line;
559 $globalline{column} = $column;
560 push @mainloop, \%globalline;