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>.
29 use C4::Dates qw/format_date format_date_in_iso/;
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->param("Filter");
49 $filters[0] = format_date_in_iso( $filters[0] );
50 $filters[1] = format_date_in_iso( $filters[1] );
51 $filters[2] = format_date_in_iso( $filters[2] );
52 $filters[3] = format_date_in_iso( $filters[3] );
53 my $podsp = $input->param("PlacedOnDisplay");
54 my $rodsp = $input->param("ReceivedOnDisplay");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61 template_name => $fullreportname,
65 flagsrequired => { reports => '*' },
70 our $sep = $input->param("sep") // '';
71 $sep = "\t" if ($sep eq 'tabulation');
79 calculate( $line, $column, $podsp, $rodsp, $calc, \@filters );
80 if ( $output eq "screen" ) {
81 $template->param( mainloop => $results );
82 output_html_with_http_headers $input, $cookie, $template->output;
86 -type => 'application/vnd.sun.xml.calc',
88 -attachment => "$basename.csv",
89 -name => "$basename.csv"
91 my $cols = @$results[0]->{loopcol};
92 my $lines = @$results[0]->{looprow};
93 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
94 foreach my $col (@$cols) {
95 print $col->{coltitle} . $sep;
98 foreach my $line (@$lines) {
99 my $x = $line->{loopcell};
100 print $line->{rowtitle} . $sep;
101 foreach my $cell (@$x) {
102 print $cell->{value} . $sep;
104 print $line->{totalrow};
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col (@$cols) {
110 print $sep. $col->{totalcol};
112 print $sep. @$results[0]->{total};
117 my $dbh = C4::Context->dbh;
119 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
121 my $booksellers = $req->fetchall_arrayref({});
123 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
127 while ( my ( $value, $desc ) = $req->fetchrow ) {
128 push @iselect, $value;
129 $iselect{$value} = $desc;
136 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
141 while ( my ( $value, $desc ) = $req->fetchrow ) {
142 push @bselect, $value;
143 $bselect{$value} = $desc;
152 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
158 while ( my ($value) = $req->fetchrow ) {
161 push @s1select, $value;
162 $s1select{$value} = $value;
166 values => \@s1select,
167 labels => \%s1select,
172 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
180 while ( my ($value) = $req->fetchrow ) {
183 $hglghtsort2 = !($hassort1);
184 push @s2select, $value;
185 $s2select{$value} = $value;
189 values => \@s2select,
190 labels => \%s2select,
193 my $CGIsepChoice = GetDelimiterChoices;
195 my $branches = GetBranches;
197 foreach ( sort keys %$branches ) {
198 push @branches, $branches->{$_};
201 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode', '');
204 if($ccode_subfield_structure) {
205 $ccode_label = $ccode_subfield_structure->{liblibrarian};
206 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
210 booksellers => $booksellers,
211 ItemTypes => $ItemTypes,
213 hassort1 => $hassort1,
214 hassort2 => $hassort2,
217 CGIsepChoice => $CGIsepChoice,
218 branches => \@branches,
219 ccode_label => $ccode_label,
220 ccode_avlist => $ccode_avlist,
224 output_html_with_http_headers $input, $cookie, $template->output;
227 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
240 my $dbh = C4::Context->dbh;
246 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
247 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
249 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
250 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
252 # format the dates filters, otherwise just fill as is
254 $cell{filter} = @$filters[$i];
256 $cell{filter} = format_date(@$filters[$i]);
259 push @loopfilter, \%cell;
265 foreach ($line, $column) {
268 if ( $_ =~ /closedate/ ) {
269 $filter{$_}->[0] = @$filters[0];
270 $filter{$_}->[1] = @$filters[1];
273 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
274 } elsif ( $podsp == 2 ) {
275 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
276 } elsif ( $podsp == 3 ) {
277 $field{$a} = "Year($a)";
282 elsif ( $_ =~ /received/ ) {
283 $filter{$_}->[0] = @$filters[2];
284 $filter{$_}->[1] = @$filters[3];
287 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
288 } elsif ( $rodsp == 2 ) {
289 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
290 } elsif ( $rodsp == 3 ) {
291 $field{$a} = "Year($a)";
296 elsif ( $_ =~ /bookseller/ ) {
297 $filter{$_}->[0] = @$filters[4];
299 elsif ( $_ =~ /homebranch/ ) {
300 $filter{$_}->[0] = @$filters[5];
302 elsif ( $_ =~ /ccode/ ) {
303 $filter{$_}->[0] = @$filters[6];
305 elsif ( $_ =~ /itemtype/ ) {
306 $filter{$_}->[0] = @$filters[7];
308 elsif ( $_ =~ /budget/ ) {
309 $filter{$_}->[0] = @$filters[8];
311 elsif ( $_ =~ /sort1/ ) {
312 $filter{$_}->[0] = @$filters[9];
314 elsif ( $_ =~ /sort2/ ) {
315 $filter{$_}->[0] = @$filters[10];
319 my @linefilter = @{ $filter{$line} };
320 my $linefield = $field{$line};
321 my @colfilter = @{ $filter{$column} };
322 my $colfield = $field{$column};
326 SELECT DISTINCTROW $linefield
328 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
329 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
330 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
331 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
332 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
333 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
334 WHERE $line IS NOT NULL AND $line <> '' ";
337 if ( $linefilter[1] ) {
338 if ( $linefilter[0] ) {
339 $strsth .= " AND $line BETWEEN ? AND ? ";
342 $strsth .= " AND $line <= ? ";
347 and ( ( $line =~ /closedate/ )
348 or ( $line =~ /received/ ))
351 $strsth .= " AND $line >= ? ";
353 elsif ( $linefilter[0] ) {
354 $linefilter[0] =~ s/\*/%/g;
355 $strsth .= " AND $line LIKE ? ";
358 $strsth .= " GROUP BY $linefield";
359 $strsth .= " ORDER BY $line";
361 my $sth = $dbh->prepare($strsth);
362 if ( (@linefilter) and ( $linefilter[1] ) ) {
363 $sth->execute( $linefilter[0], $linefilter[1] );
365 elsif ( $linefilter[0] ) {
366 $sth->execute( $linefilter[0] );
371 while ( my ($celvalue) = $sth->fetchrow ) {
374 $cell{rowtitle} = $celvalue;
375 push @loopline, \%cell;
382 SELECT DISTINCTROW $colfield
384 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
385 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
386 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
387 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
388 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
389 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
390 WHERE $column IS NOT NULL AND $column <> ''
394 if ( $colfilter[1] ) {
395 if ( $colfilter[0] ) {
396 $strsth2 .= " AND $column BETWEEN ? AND ? ";
399 $strsth2 .= " AND $column <= ? ";
404 and ( ( $column =~ /closedate/ )
405 or ( $line =~ /received/ ))
408 $strsth2 .= " AND $column >= ? ";
410 elsif ( $colfilter[0] ) {
411 $colfilter[0] =~ s/\*/%/g;
412 $strsth2 .= " AND $column LIKE ? ";
416 $strsth2 .= " GROUP BY $colfield";
417 $strsth2 .= " ORDER BY $colfield";
419 my $sth2 = $dbh->prepare($strsth2);
421 if ( (@colfilter) and ($colfilter[1]) ) {
422 $sth2->execute( $colfilter[0], $colfilter[1] );
424 elsif ( $colfilter[0] ) {
425 $sth2->execute( $colfilter[0] );
430 while ( my $celvalue = $sth2->fetchrow ) {
433 $cell{coltitle} = $celvalue;
434 push @loopcol, \%cell;
442 #Initialization of cell values.....
445 foreach my $row (@loopline) {
446 foreach my $col (@loopcol) {
447 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
449 $table{ $row->{rowtitle} }->{totalrow} = 0;
452 # preparing calculation
454 $strcalc .= "SELECT $linefield, $colfield, ";
455 if ( $process == 1 ) {
456 $strcalc .= "COUNT(*) ";
457 } elsif ( $process == 2 ) {
458 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
459 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
460 $strcalc .= "SUM(aqorders.listprice) ";
466 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
467 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
468 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
469 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
470 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
471 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
472 WHERE aqorders.datecancellationprinted IS NULL ";
473 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
474 if ( $process == 4 );
475 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
476 if ( $process == 5 );
477 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
478 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
480 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
481 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
483 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
484 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
486 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
487 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
489 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
490 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
492 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
494 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
495 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
497 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
498 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
500 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
501 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
503 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
504 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
506 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
507 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
508 if ( @$filters[10] );
510 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
511 my $dbcalc = $dbh->prepare($strcalc);
515 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
516 $emptycol = 1 if ( !defined($col) );
517 $col = "zzEMPTY" if ( !defined($col) );
518 $row = "zzEMPTY" if ( !defined($row) );
520 $table{$row}->{$col} += $value;
521 $table{$row}->{totalrow} += $value;
522 $grantotal += $value;
525 push @loopcol, { coltitle => "NULL" } if ($emptycol);
527 foreach my $row ( sort keys %table ) {
529 #@loopcol ensures the order for columns is common with column titles
530 # and the number matches the number of columns
531 foreach my $col (@loopcol) {
532 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
533 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
534 push @loopcell, { value => $value };
537 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
538 loopcell => \@loopcell,
539 hilighted => ( $hilighted > 0 ),
540 totalrow => $table{$row}->{totalrow}
542 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
544 $hilighted = -$hilighted;
547 foreach my $col (@loopcol) {
549 foreach my $row (@looprow) {
551 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
554 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
558 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
560 push @loopfooter, { 'totalcol' => $total };
563 # the header of the table
564 $globalline{loopfilter} = \@loopfilter;
565 # the core of the table
566 $globalline{looprow} = \@looprow;
567 $globalline{loopcol} = \@loopcol;
569 # # the foot (totals by borrower type)
570 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
571 $globalline{loopfooter} = \@loopfooter;
572 $globalline{total} = $grantotal;
573 $globalline{line} = $line;
574 $globalline{column} = $column;
575 push @mainloop, \%globalline;