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 );
34 reports/acquisitions_stats.pl
38 Plugin that shows a stats on borrowers
43 my $do_it = $input->param('do_it');
44 my $fullreportname = "reports/acquisitions_stats.tt";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->multi_param("Filter");
48 my $podsp = $input->param("PlacedOnDisplay");
49 my $rodsp = $input->param("ReceivedOnDisplay");
50 my $calc = $input->param("Cellvalue");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
54 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
56 template_name => $fullreportname,
59 flagsrequired => { reports => '*' },
63 our $sep = $input->param("sep") // '';
64 $sep = "\t" if ($sep eq 'tabulation');
72 calculate( $line, $column, $podsp, $rodsp, $calc, \@filters );
73 if ( $output eq "screen" ) {
74 $template->param( mainloop => $results );
75 output_html_with_http_headers $input, $cookie, $template->output;
79 -type => 'application/vnd.sun.xml.calc',
81 -attachment => "$basename.csv",
82 -name => "$basename.csv"
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
87 foreach my $col (@$cols) {
88 print $col->{coltitle} . $sep;
91 foreach my $line (@$lines) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle} . $sep;
94 foreach my $cell (@$x) {
95 print $cell->{value} . $sep;
97 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col (@$cols) {
103 print $sep. $col->{totalcol};
105 print $sep. @$results[0]->{total};
110 my $dbh = C4::Context->dbh;
112 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
114 my $booksellers = $req->fetchall_arrayref({});
116 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
121 while ( my ( $value, $desc ) = $req->fetchrow ) {
122 push @bselect, $value;
123 $bselect{$value} = $desc;
132 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
138 while ( my ($value) = $req->fetchrow ) {
141 push @s1select, $value;
142 $s1select{$value} = $value;
146 values => \@s1select,
147 labels => \%s1select,
152 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
160 while ( my ($value) = $req->fetchrow ) {
163 $hglghtsort2 = !($hassort1);
164 push @s2select, $value;
165 $s2select{$value} = $value;
169 values => \@s2select,
170 labels => \%s2select,
173 my $CGIsepChoice = GetDelimiterChoices;
175 my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
177 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode');
180 if($ccode_subfield_structure) {
181 $ccode_label = $ccode_subfield_structure->{liblibrarian};
182 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
185 my $itemtypes = Koha::ItemTypes->search_with_localization;
187 booksellers => $booksellers,
188 itemtypes => $itemtypes, # FIXME Should use the TT plugin instead
190 hassort1 => $hassort1,
191 hassort2 => $hassort2,
194 CGIsepChoice => $CGIsepChoice,
195 branches => $libraries,
196 ccode_label => $ccode_label,
197 ccode_avlist => $ccode_avlist,
201 output_html_with_http_headers $input, $cookie, $template->output;
204 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
217 my $dbh = C4::Context->dbh;
223 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
224 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
226 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
227 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
229 $cell{filter} = @$filters[$i];
231 push @loopfilter, \%cell;
237 foreach ($line, $column) {
240 if ( $_ =~ /closedate/ ) {
241 $filter{$_}->[0] = @$filters[0];
242 $filter{$_}->[1] = @$filters[1];
245 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
246 } elsif ( $podsp == 2 ) {
247 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
248 } elsif ( $podsp == 3 ) {
249 $field{$a} = "Year($a)";
254 elsif ( $_ =~ /received/ ) {
255 $filter{$_}->[0] = @$filters[2];
256 $filter{$_}->[1] = @$filters[3];
259 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
260 } elsif ( $rodsp == 2 ) {
261 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
262 } elsif ( $rodsp == 3 ) {
263 $field{$a} = "Year($a)";
268 elsif ( $_ =~ /bookseller/ ) {
269 $filter{$_}->[0] = @$filters[4];
271 elsif ( $_ =~ /homebranch/ ) {
272 $filter{$_}->[0] = @$filters[5];
274 elsif ( $_ =~ /ccode/ ) {
275 $filter{$_}->[0] = @$filters[6];
277 elsif ( $_ =~ /itemtype/ ) {
278 $filter{$_}->[0] = @$filters[7];
280 elsif ( $_ =~ /budget/ ) {
281 $filter{$_}->[0] = @$filters[8];
283 elsif ( $_ =~ /sort1/ ) {
284 $filter{$_}->[0] = @$filters[9];
286 elsif ( $_ =~ /sort2/ ) {
287 $filter{$_}->[0] = @$filters[10];
291 my @linefilter = @{ $filter{$line} };
292 my $linefield = $field{$line};
293 my @colfilter = @{ $filter{$column} };
294 my $colfield = $field{$column};
298 SELECT DISTINCTROW $linefield
300 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
301 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
302 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
303 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
304 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
305 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
306 WHERE $line IS NOT NULL AND $line <> '' ";
309 if ( $linefilter[1] ) {
310 if ( $linefilter[0] ) {
311 $strsth .= " AND $line BETWEEN ? AND ? ";
314 $strsth .= " AND $line <= ? ";
319 and ( ( $line =~ /closedate/ )
320 or ( $line =~ /received/ ))
323 $strsth .= " AND $line >= ? ";
325 elsif ( $linefilter[0] ) {
326 $linefilter[0] =~ s/\*/%/g;
327 $strsth .= " AND $line LIKE ? ";
330 $strsth .= " GROUP BY $linefield";
331 $strsth .= " ORDER BY $line";
333 my $sth = $dbh->prepare($strsth);
334 if ( (@linefilter) and ( $linefilter[1] ) ) {
335 $sth->execute( $linefilter[0], $linefilter[1] );
337 elsif ( $linefilter[0] ) {
338 $sth->execute( $linefilter[0] );
343 while ( my ($celvalue) = $sth->fetchrow ) {
346 $cell{rowtitle} = $celvalue;
347 push @loopline, \%cell;
354 SELECT DISTINCTROW $colfield
356 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
357 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
358 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
359 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
360 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
361 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
362 WHERE $column IS NOT NULL AND $column <> ''
366 if ( $colfilter[1] ) {
367 if ( $colfilter[0] ) {
368 $strsth2 .= " AND $column BETWEEN ? AND ? ";
371 $strsth2 .= " AND $column <= ? ";
376 and ( ( $column =~ /closedate/ )
377 or ( $line =~ /received/ ))
380 $strsth2 .= " AND $column >= ? ";
382 elsif ( $colfilter[0] ) {
383 $colfilter[0] =~ s/\*/%/g;
384 $strsth2 .= " AND $column LIKE ? ";
388 $strsth2 .= " GROUP BY $colfield";
389 $strsth2 .= " ORDER BY $colfield";
391 my $sth2 = $dbh->prepare($strsth2);
393 if ( (@colfilter) and ($colfilter[1]) ) {
394 $sth2->execute( $colfilter[0], $colfilter[1] );
396 elsif ( $colfilter[0] ) {
397 $sth2->execute( $colfilter[0] );
402 while ( my $celvalue = $sth2->fetchrow ) {
405 $cell{coltitle} = $celvalue;
406 push @loopcol, \%cell;
413 #Initialization of cell values.....
416 foreach my $row (@loopline) {
417 foreach my $col (@loopcol) {
418 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
420 $table{ $row->{rowtitle} }->{totalrow} = 0;
423 # preparing calculation
425 $strcalc .= "SELECT $linefield, $colfield, ";
426 if ( $process == 1 ) {
427 $strcalc .= "COUNT(*) ";
428 } elsif ( $process == 2 ) {
429 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
430 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
431 $strcalc .= "SUM(aqorders.listprice) ";
437 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
438 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
439 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
440 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
441 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
442 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
443 WHERE aqorders.datecancellationprinted IS NULL ";
444 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
445 if ( $process == 4 );
446 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
447 if ( $process == 5 );
448 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
449 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
451 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
452 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
454 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
455 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
457 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
458 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
460 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
461 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
463 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
465 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
466 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
468 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
469 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
471 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
472 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
474 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
475 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
477 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
478 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
479 if ( @$filters[10] );
481 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
482 my $dbcalc = $dbh->prepare($strcalc);
486 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
487 $emptycol = 1 if ( !defined($col) );
488 $col = "zzEMPTY" if ( !defined($col) );
489 $row = "zzEMPTY" if ( !defined($row) );
491 $table{$row}->{$col} += $value;
492 $table{$row}->{totalrow} += $value;
493 $grantotal += $value;
496 push @loopcol, { coltitle => "NULL" } if ($emptycol);
498 foreach my $row ( sort keys %table ) {
500 #@loopcol ensures the order for columns is common with column titles
501 # and the number matches the number of columns
502 foreach my $col (@loopcol) {
503 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
504 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
505 push @loopcell, { value => $value };
508 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
509 loopcell => \@loopcell,
510 hilighted => ( $hilighted > 0 ),
511 totalrow => $table{$row}->{totalrow}
513 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
515 $hilighted = -$hilighted;
518 foreach my $col (@loopcol) {
520 foreach my $row (@looprow) {
522 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
525 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
529 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
531 push @loopfooter, { 'totalcol' => $total };
534 # the header of the table
535 $globalline{loopfilter} = \@loopfilter;
536 # the core of the table
537 $globalline{looprow} = \@looprow;
538 $globalline{loopcol} = \@loopcol;
540 # # the foot (totals by borrower type)
541 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
542 $globalline{loopfooter} = \@loopfooter;
543 $globalline{total} = $grantotal;
544 $globalline{line} = $line;
545 $globalline{column} = $column;
546 push @mainloop, \%globalline;