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 => '*' },
73 our $sep = $input->param("sep") // '';
74 $sep = "\t" if ($sep eq 'tabulation');
82 calculate( $line, $column, $podsp, $rodsp, $calc, \@filters );
83 if ( $output eq "screen" ) {
84 $template->param( mainloop => $results );
85 output_html_with_http_headers $input, $cookie, $template->output;
89 -type => 'application/vnd.sun.xml.calc',
91 -attachment => "$basename.csv",
92 -name => "$basename.csv"
94 my $cols = @$results[0]->{loopcol};
95 my $lines = @$results[0]->{looprow};
96 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
97 foreach my $col (@$cols) {
98 print $col->{coltitle} . $sep;
101 foreach my $line (@$lines) {
102 my $x = $line->{loopcell};
103 print $line->{rowtitle} . $sep;
104 foreach my $cell (@$x) {
105 print $cell->{value} . $sep;
107 print $line->{totalrow};
111 $cols = @$results[0]->{loopfooter};
112 foreach my $col (@$cols) {
113 print $sep. $col->{totalcol};
115 print $sep. @$results[0]->{total};
120 my $dbh = C4::Context->dbh;
122 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
124 my $booksellers = $req->fetchall_arrayref({});
126 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
131 while ( my ( $value, $desc ) = $req->fetchrow ) {
132 push @bselect, $value;
133 $bselect{$value} = $desc;
142 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
148 while ( my ($value) = $req->fetchrow ) {
151 push @s1select, $value;
152 $s1select{$value} = $value;
156 values => \@s1select,
157 labels => \%s1select,
162 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
170 while ( my ($value) = $req->fetchrow ) {
173 $hglghtsort2 = !($hassort1);
174 push @s2select, $value;
175 $s2select{$value} = $value;
179 values => \@s2select,
180 labels => \%s2select,
183 my $CGIsepChoice = GetDelimiterChoices;
185 my @branches = Koha::Libraries->search({}, { order_by => 'branchname' });
187 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode');
190 if($ccode_subfield_structure) {
191 $ccode_label = $ccode_subfield_structure->{liblibrarian};
192 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
195 my $itemtypes = Koha::ItemTypes->search_with_localization;
197 booksellers => $booksellers,
198 itemtypes => $itemtypes, # FIXME Should use the TT plugin instead
200 hassort1 => $hassort1,
201 hassort2 => $hassort2,
204 CGIsepChoice => $CGIsepChoice,
205 branches => \@branches,
206 ccode_label => $ccode_label,
207 ccode_avlist => $ccode_avlist,
211 output_html_with_http_headers $input, $cookie, $template->output;
214 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
227 my $dbh = C4::Context->dbh;
233 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
234 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
236 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
237 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
239 # format the dates filters, otherwise just fill as is
241 $cell{filter} = @$filters[$i];
243 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
244 if ( @$filters[$i] );
247 push @loopfilter, \%cell;
253 foreach ($line, $column) {
256 if ( $_ =~ /closedate/ ) {
257 $filter{$_}->[0] = @$filters[0];
258 $filter{$_}->[1] = @$filters[1];
261 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
262 } elsif ( $podsp == 2 ) {
263 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
264 } elsif ( $podsp == 3 ) {
265 $field{$a} = "Year($a)";
270 elsif ( $_ =~ /received/ ) {
271 $filter{$_}->[0] = @$filters[2];
272 $filter{$_}->[1] = @$filters[3];
275 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
276 } elsif ( $rodsp == 2 ) {
277 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
278 } elsif ( $rodsp == 3 ) {
279 $field{$a} = "Year($a)";
284 elsif ( $_ =~ /bookseller/ ) {
285 $filter{$_}->[0] = @$filters[4];
287 elsif ( $_ =~ /homebranch/ ) {
288 $filter{$_}->[0] = @$filters[5];
290 elsif ( $_ =~ /ccode/ ) {
291 $filter{$_}->[0] = @$filters[6];
293 elsif ( $_ =~ /itemtype/ ) {
294 $filter{$_}->[0] = @$filters[7];
296 elsif ( $_ =~ /budget/ ) {
297 $filter{$_}->[0] = @$filters[8];
299 elsif ( $_ =~ /sort1/ ) {
300 $filter{$_}->[0] = @$filters[9];
302 elsif ( $_ =~ /sort2/ ) {
303 $filter{$_}->[0] = @$filters[10];
307 my @linefilter = @{ $filter{$line} };
308 my $linefield = $field{$line};
309 my @colfilter = @{ $filter{$column} };
310 my $colfield = $field{$column};
314 SELECT DISTINCTROW $linefield
316 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
317 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
318 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
319 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
320 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
321 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
322 WHERE $line IS NOT NULL AND $line <> '' ";
325 if ( $linefilter[1] ) {
326 if ( $linefilter[0] ) {
327 $strsth .= " AND $line BETWEEN ? AND ? ";
330 $strsth .= " AND $line <= ? ";
335 and ( ( $line =~ /closedate/ )
336 or ( $line =~ /received/ ))
339 $strsth .= " AND $line >= ? ";
341 elsif ( $linefilter[0] ) {
342 $linefilter[0] =~ s/\*/%/g;
343 $strsth .= " AND $line LIKE ? ";
346 $strsth .= " GROUP BY $linefield";
347 $strsth .= " ORDER BY $line";
349 my $sth = $dbh->prepare($strsth);
350 if ( (@linefilter) and ( $linefilter[1] ) ) {
351 $sth->execute( $linefilter[0], $linefilter[1] );
353 elsif ( $linefilter[0] ) {
354 $sth->execute( $linefilter[0] );
359 while ( my ($celvalue) = $sth->fetchrow ) {
362 $cell{rowtitle} = $celvalue;
363 push @loopline, \%cell;
370 SELECT DISTINCTROW $colfield
372 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
373 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
374 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
375 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
376 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
377 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
378 WHERE $column IS NOT NULL AND $column <> ''
382 if ( $colfilter[1] ) {
383 if ( $colfilter[0] ) {
384 $strsth2 .= " AND $column BETWEEN ? AND ? ";
387 $strsth2 .= " AND $column <= ? ";
392 and ( ( $column =~ /closedate/ )
393 or ( $line =~ /received/ ))
396 $strsth2 .= " AND $column >= ? ";
398 elsif ( $colfilter[0] ) {
399 $colfilter[0] =~ s/\*/%/g;
400 $strsth2 .= " AND $column LIKE ? ";
404 $strsth2 .= " GROUP BY $colfield";
405 $strsth2 .= " ORDER BY $colfield";
407 my $sth2 = $dbh->prepare($strsth2);
409 if ( (@colfilter) and ($colfilter[1]) ) {
410 $sth2->execute( $colfilter[0], $colfilter[1] );
412 elsif ( $colfilter[0] ) {
413 $sth2->execute( $colfilter[0] );
418 while ( my $celvalue = $sth2->fetchrow ) {
421 $cell{coltitle} = $celvalue;
422 push @loopcol, \%cell;
429 #Initialization of cell values.....
432 foreach my $row (@loopline) {
433 foreach my $col (@loopcol) {
434 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
436 $table{ $row->{rowtitle} }->{totalrow} = 0;
439 # preparing calculation
441 $strcalc .= "SELECT $linefield, $colfield, ";
442 if ( $process == 1 ) {
443 $strcalc .= "COUNT(*) ";
444 } elsif ( $process == 2 ) {
445 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
446 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
447 $strcalc .= "SUM(aqorders.listprice) ";
453 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
454 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
455 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
456 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
457 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
458 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
459 WHERE aqorders.datecancellationprinted IS NULL ";
460 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
461 if ( $process == 4 );
462 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
463 if ( $process == 5 );
464 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
465 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
467 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
468 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
470 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
471 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
473 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
474 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
476 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
477 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
479 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
481 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
482 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
484 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
485 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
487 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
488 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
490 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
491 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
493 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
494 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
495 if ( @$filters[10] );
497 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
498 my $dbcalc = $dbh->prepare($strcalc);
502 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
503 $emptycol = 1 if ( !defined($col) );
504 $col = "zzEMPTY" if ( !defined($col) );
505 $row = "zzEMPTY" if ( !defined($row) );
507 $table{$row}->{$col} += $value;
508 $table{$row}->{totalrow} += $value;
509 $grantotal += $value;
512 push @loopcol, { coltitle => "NULL" } if ($emptycol);
514 foreach my $row ( sort keys %table ) {
516 #@loopcol ensures the order for columns is common with column titles
517 # and the number matches the number of columns
518 foreach my $col (@loopcol) {
519 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
520 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
521 push @loopcell, { value => $value };
524 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
525 loopcell => \@loopcell,
526 hilighted => ( $hilighted > 0 ),
527 totalrow => $table{$row}->{totalrow}
529 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
531 $hilighted = -$hilighted;
534 foreach my $col (@loopcol) {
536 foreach my $row (@looprow) {
538 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
541 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
545 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
547 push @loopfooter, { 'totalcol' => $total };
550 # the header of the table
551 $globalline{loopfilter} = \@loopfilter;
552 # the core of the table
553 $globalline{looprow} = \@looprow;
554 $globalline{loopcol} = \@loopcol;
556 # # the foot (totals by borrower type)
557 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
558 $globalline{loopfooter} = \@loopfooter;
559 $globalline{total} = $grantotal;
560 $globalline{line} = $line;
561 $globalline{column} = $column;
562 push @mainloop, \%globalline;