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 = $input->param("sep") // '';
73 $sep = "\t" if ($sep eq 'tabulation');
81 calculate( $line, $column, $podsp, $rodsp, $calc, \@filters );
82 if ( $output eq "screen" ) {
83 $template->param( mainloop => $results );
84 output_html_with_http_headers $input, $cookie, $template->output;
88 -type => 'application/vnd.sun.xml.calc',
90 -attachment => "$basename.csv",
91 -name => "$basename.csv"
93 my $cols = @$results[0]->{loopcol};
94 my $lines = @$results[0]->{looprow};
95 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
96 foreach my $col (@$cols) {
97 print $col->{coltitle} . $sep;
100 foreach my $line (@$lines) {
101 my $x = $line->{loopcell};
102 print $line->{rowtitle} . $sep;
103 foreach my $cell (@$x) {
104 print $cell->{value} . $sep;
106 print $line->{totalrow};
110 $cols = @$results[0]->{loopfooter};
111 foreach my $col (@$cols) {
112 print $sep. $col->{totalcol};
114 print $sep. @$results[0]->{total};
119 my $dbh = C4::Context->dbh;
121 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
123 my $booksellers = $req->fetchall_arrayref({});
125 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
130 while ( my ( $value, $desc ) = $req->fetchrow ) {
131 push @bselect, $value;
132 $bselect{$value} = $desc;
141 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
147 while ( my ($value) = $req->fetchrow ) {
150 push @s1select, $value;
151 $s1select{$value} = $value;
155 values => \@s1select,
156 labels => \%s1select,
161 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
169 while ( my ($value) = $req->fetchrow ) {
172 $hglghtsort2 = !($hassort1);
173 push @s2select, $value;
174 $s2select{$value} = $value;
178 values => \@s2select,
179 labels => \%s2select,
182 my $CGIsepChoice = GetDelimiterChoices;
184 my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
186 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode');
189 if($ccode_subfield_structure) {
190 $ccode_label = $ccode_subfield_structure->{liblibrarian};
191 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
194 my $itemtypes = Koha::ItemTypes->search_with_localization;
196 booksellers => $booksellers,
197 itemtypes => $itemtypes, # FIXME Should use the TT plugin instead
199 hassort1 => $hassort1,
200 hassort2 => $hassort2,
203 CGIsepChoice => $CGIsepChoice,
204 branches => $libraries,
205 ccode_label => $ccode_label,
206 ccode_avlist => $ccode_avlist,
210 output_html_with_http_headers $input, $cookie, $template->output;
213 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
226 my $dbh = C4::Context->dbh;
232 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
233 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
235 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
236 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
238 # format the dates filters, otherwise just fill as is
240 $cell{filter} = @$filters[$i];
242 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
243 if ( @$filters[$i] );
246 push @loopfilter, \%cell;
252 foreach ($line, $column) {
255 if ( $_ =~ /closedate/ ) {
256 $filter{$_}->[0] = @$filters[0];
257 $filter{$_}->[1] = @$filters[1];
260 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
261 } elsif ( $podsp == 2 ) {
262 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
263 } elsif ( $podsp == 3 ) {
264 $field{$a} = "Year($a)";
269 elsif ( $_ =~ /received/ ) {
270 $filter{$_}->[0] = @$filters[2];
271 $filter{$_}->[1] = @$filters[3];
274 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
275 } elsif ( $rodsp == 2 ) {
276 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
277 } elsif ( $rodsp == 3 ) {
278 $field{$a} = "Year($a)";
283 elsif ( $_ =~ /bookseller/ ) {
284 $filter{$_}->[0] = @$filters[4];
286 elsif ( $_ =~ /homebranch/ ) {
287 $filter{$_}->[0] = @$filters[5];
289 elsif ( $_ =~ /ccode/ ) {
290 $filter{$_}->[0] = @$filters[6];
292 elsif ( $_ =~ /itemtype/ ) {
293 $filter{$_}->[0] = @$filters[7];
295 elsif ( $_ =~ /budget/ ) {
296 $filter{$_}->[0] = @$filters[8];
298 elsif ( $_ =~ /sort1/ ) {
299 $filter{$_}->[0] = @$filters[9];
301 elsif ( $_ =~ /sort2/ ) {
302 $filter{$_}->[0] = @$filters[10];
306 my @linefilter = @{ $filter{$line} };
307 my $linefield = $field{$line};
308 my @colfilter = @{ $filter{$column} };
309 my $colfield = $field{$column};
313 SELECT DISTINCTROW $linefield
315 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
316 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
317 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
318 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
319 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
320 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
321 WHERE $line IS NOT NULL AND $line <> '' ";
324 if ( $linefilter[1] ) {
325 if ( $linefilter[0] ) {
326 $strsth .= " AND $line BETWEEN ? AND ? ";
329 $strsth .= " AND $line <= ? ";
334 and ( ( $line =~ /closedate/ )
335 or ( $line =~ /received/ ))
338 $strsth .= " AND $line >= ? ";
340 elsif ( $linefilter[0] ) {
341 $linefilter[0] =~ s/\*/%/g;
342 $strsth .= " AND $line LIKE ? ";
345 $strsth .= " GROUP BY $linefield";
346 $strsth .= " ORDER BY $line";
348 my $sth = $dbh->prepare($strsth);
349 if ( (@linefilter) and ( $linefilter[1] ) ) {
350 $sth->execute( $linefilter[0], $linefilter[1] );
352 elsif ( $linefilter[0] ) {
353 $sth->execute( $linefilter[0] );
358 while ( my ($celvalue) = $sth->fetchrow ) {
361 $cell{rowtitle} = $celvalue;
362 push @loopline, \%cell;
369 SELECT DISTINCTROW $colfield
371 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
372 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
373 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
374 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
375 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
376 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
377 WHERE $column IS NOT NULL AND $column <> ''
381 if ( $colfilter[1] ) {
382 if ( $colfilter[0] ) {
383 $strsth2 .= " AND $column BETWEEN ? AND ? ";
386 $strsth2 .= " AND $column <= ? ";
391 and ( ( $column =~ /closedate/ )
392 or ( $line =~ /received/ ))
395 $strsth2 .= " AND $column >= ? ";
397 elsif ( $colfilter[0] ) {
398 $colfilter[0] =~ s/\*/%/g;
399 $strsth2 .= " AND $column LIKE ? ";
403 $strsth2 .= " GROUP BY $colfield";
404 $strsth2 .= " ORDER BY $colfield";
406 my $sth2 = $dbh->prepare($strsth2);
408 if ( (@colfilter) and ($colfilter[1]) ) {
409 $sth2->execute( $colfilter[0], $colfilter[1] );
411 elsif ( $colfilter[0] ) {
412 $sth2->execute( $colfilter[0] );
417 while ( my $celvalue = $sth2->fetchrow ) {
420 $cell{coltitle} = $celvalue;
421 push @loopcol, \%cell;
428 #Initialization of cell values.....
431 foreach my $row (@loopline) {
432 foreach my $col (@loopcol) {
433 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
435 $table{ $row->{rowtitle} }->{totalrow} = 0;
438 # preparing calculation
440 $strcalc .= "SELECT $linefield, $colfield, ";
441 if ( $process == 1 ) {
442 $strcalc .= "COUNT(*) ";
443 } elsif ( $process == 2 ) {
444 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
445 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
446 $strcalc .= "SUM(aqorders.listprice) ";
452 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
453 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
454 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
455 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
456 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
457 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
458 WHERE aqorders.datecancellationprinted IS NULL ";
459 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
460 if ( $process == 4 );
461 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
462 if ( $process == 5 );
463 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
464 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
466 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
467 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
469 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
470 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
472 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
473 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
475 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
476 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
478 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
480 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
481 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
483 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
484 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
486 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
487 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
489 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
490 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
492 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
493 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
494 if ( @$filters[10] );
496 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
497 my $dbcalc = $dbh->prepare($strcalc);
501 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
502 $emptycol = 1 if ( !defined($col) );
503 $col = "zzEMPTY" if ( !defined($col) );
504 $row = "zzEMPTY" if ( !defined($row) );
506 $table{$row}->{$col} += $value;
507 $table{$row}->{totalrow} += $value;
508 $grantotal += $value;
511 push @loopcol, { coltitle => "NULL" } if ($emptycol);
513 foreach my $row ( sort keys %table ) {
515 #@loopcol ensures the order for columns is common with column titles
516 # and the number matches the number of columns
517 foreach my $col (@loopcol) {
518 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
519 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
520 push @loopcell, { value => $value };
523 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
524 loopcell => \@loopcell,
525 hilighted => ( $hilighted > 0 ),
526 totalrow => $table{$row}->{totalrow}
528 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
530 $hilighted = -$hilighted;
533 foreach my $col (@loopcol) {
535 foreach my $row (@looprow) {
537 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
540 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
544 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
546 push @loopfooter, { 'totalcol' => $total };
549 # the header of the table
550 $globalline{loopfilter} = \@loopfilter;
551 # the core of the table
552 $globalline{looprow} = \@looprow;
553 $globalline{loopcol} = \@loopcol;
555 # # the foot (totals by borrower type)
556 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
557 $globalline{loopfooter} = \@loopfooter;
558 $globalline{total} = $grantotal;
559 $globalline{line} = $line;
560 $globalline{column} = $column;
561 push @mainloop, \%globalline;