4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 use C4
::Dates qw
/format_date format_date_in_iso/;
38 plugin that shows a stats on borrowers
47 my $do_it = $input->param('do_it');
48 my $fullreportname = "reports/acquisitions_stats.tmpl";
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my @filters = $input->param("Filter");
52 $filters[0] = format_date_in_iso
( $filters[0] );
53 $filters[1] = format_date_in_iso
( $filters[1] );
54 $filters[2] = format_date_in_iso
( $filters[2] );
55 $filters[3] = format_date_in_iso
( $filters[3] );
56 my $podsp = $input->param("PlacedOnDisplay");
57 my $rodsp = $input->param("ReceivedOnDisplay");
58 my $calc = $input->param("Cellvalue");
59 my $output = $input->param("output");
60 my $basename = $input->param("basename");
62 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
64 template_name
=> $fullreportname,
68 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 itemtype,description FROM itemtypes ORDER BY description");
131 $select{''} = "All item types";
132 while ( my ( $value, $desc ) = $req->fetchrow ) {
133 push @select, $value;
134 $select{$value} = $desc;
136 my $CGIItemTypes = CGI
::scrolling_list
(
145 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
150 $select{''} = "All funds";
152 while ( my ( $value, $desc ) = $req->fetchrow ) {
153 push @select, $value;
154 $select{$value} = $desc;
156 my $CGIBudget = CGI
::scrolling_list
(
167 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
175 while ( my ($value) = $req->fetchrow ) {
178 push @select, $value;
179 $select{$value} = $value;
182 my $CGISort1 = CGI
::scrolling_list
(
193 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
203 while ( my ($value) = $req->fetchrow ) {
206 $hglghtsort2 = !($hassort1);
207 push @select, $value;
208 $select{$value} = $value;
211 my $CGISort2 = CGI
::scrolling_list
(
220 my $CGIextChoice = CGI
::scrolling_list
(
223 -values => ['CSV'], # FIXME translation
228 my $CGIsepChoice = GetDelimiterChoices
;
230 my $branches = GetBranches
;
232 foreach ( sort keys %$branches ) {
233 push @branches, $branches->{$_};
236 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField
('items.ccode', '');
239 if($ccode_subfield_structure) {
240 $ccode_label = $ccode_subfield_structure->{liblibrarian
};
241 $ccode_avlist = GetAuthorisedValues
($ccode_subfield_structure->{authorised_value
});
245 booksellers
=> $booksellers,
246 CGIItemType
=> $CGIItemTypes,
247 CGIBudget
=> $CGIBudget,
248 hassort1
=> $hassort1,
249 hassort2
=> $hassort2,
250 CGISort1
=> $CGISort1,
251 CGISort2
=> $CGISort2,
252 CGIextChoice
=> $CGIextChoice,
253 CGIsepChoice
=> $CGIsepChoice,
254 branches
=> \
@branches,
255 ccode_label
=> $ccode_label,
256 ccode_avlist
=> $ccode_avlist,
260 output_html_with_http_headers
$input, $cookie, $template->output;
263 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
276 my $dbh = C4
::Context
->dbh;
282 for ( my $i = 0 ; $i <= @
$filters ; $i++ ) {
283 if( defined @
$filters[$i] and @
$filters[$i] ne '' ) {
285 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @
$filters[ $i - 1 ] ) ) {
286 $cell{err
} = 1 if ( @
$filters[$i] lt @
$filters[ $i - 1 ] );
288 # format the dates filters, otherwise just fill as is
290 $cell{filter
} = @
$filters[$i];
292 $cell{filter
} = format_date
(@
$filters[$i]);
295 push @loopfilter, \
%cell;
301 foreach ($line, $column) {
306 $filter{$_}->[0] = @
$filters[0];
307 $filter{$_}->[1] = @
$filters[1];
310 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
311 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
312 when (3) { $field{$a} = "Year($a)" }
313 default { $field{$a} = $a }
317 $filter{$_}->[0] = @
$filters[2];
318 $filter{$_}->[1] = @
$filters[3];
321 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
322 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
323 when (3) { $field{$a} = "Year($a)" }
324 default { $field{$a} = $a }
327 when (/bookseller/) {
328 $filter{$_}->[0] = @
$filters[4];
330 when (/homebranch/) {
331 $filter{$_}->[0] = @
$filters[5];
334 $filter{$_}->[0] = @
$filters[6];
337 $filter{$_}->[0] = @
$filters[7];
340 $filter{$_}->[0] = @
$filters[8];
343 $filter{$_}->[0] = @
$filters[9];
346 $filter{$_}->[0] = @
$filters[10];
351 my @linefilter = @
{ $filter{$line} };
352 my $linefield = $field{$line};
353 my @colfilter = @
{ $filter{$column} };
354 my $colfield = $field{$column};
358 SELECT DISTINCTROW $linefield
360 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
361 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
362 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
363 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
364 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
365 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
366 WHERE $line IS NOT NULL AND $line <> '' ";
369 if ( $linefilter[1] ) {
370 if ( $linefilter[0] ) {
371 $strsth .= " AND $line BETWEEN ? AND ? ";
374 $strsth .= " AND $line <= ? ";
379 and ( ( $line =~ /closedate/ )
380 or ( $line =~ /received/ ))
383 $strsth .= " AND $line >= ? ";
385 elsif ( $linefilter[0] ) {
386 $linefilter[0] =~ s/\*/%/g;
387 $strsth .= " AND $line LIKE ? ";
390 $strsth .= " GROUP BY $linefield";
391 $strsth .= " ORDER BY $line";
393 my $sth = $dbh->prepare($strsth);
394 if ( (@linefilter) and ( $linefilter[1] ) ) {
395 $sth->execute( $linefilter[0], $linefilter[1] );
397 elsif ( $linefilter[0] ) {
398 $sth->execute( $linefilter[0] );
403 while ( my ($celvalue) = $sth->fetchrow ) {
406 $cell{rowtitle
} = $celvalue;
407 push @loopline, \
%cell;
414 SELECT DISTINCTROW $colfield
416 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
417 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
418 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
419 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
420 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
421 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
422 WHERE $column IS NOT NULL AND $column <> ''
426 if ( $colfilter[1] ) {
427 if ( $colfilter[0] ) {
428 $strsth2 .= " AND $column BETWEEN ? AND ? ";
431 $strsth2 .= " AND $column <= ? ";
436 and ( ( $column =~ /closedate/ )
437 or ( $line =~ /received/ ))
440 $strsth2 .= " AND $column >= ? ";
442 elsif ( $colfilter[0] ) {
443 $colfilter[0] =~ s/\*/%/g;
444 $strsth2 .= " AND $column LIKE ? ";
448 $strsth2 .= " GROUP BY $colfield";
449 $strsth2 .= " ORDER BY $colfield";
451 my $sth2 = $dbh->prepare($strsth2);
453 if ( (@colfilter) and ($colfilter[1]) ) {
454 $sth2->execute( $colfilter[0], $colfilter[1] );
456 elsif ( $colfilter[0] ) {
457 $sth2->execute( $colfilter[0] );
462 while ( my $celvalue = $sth2->fetchrow ) {
465 $cell{coltitle
} = $celvalue;
466 push @loopcol, \
%cell;
474 #Initialization of cell values.....
477 foreach my $row (@loopline) {
478 foreach my $col (@loopcol) {
479 $table{ $row->{rowtitle
} }->{ $col->{coltitle
} } = 0;
481 $table{ $row->{rowtitle
} }->{totalrow
} = 0;
484 # preparing calculation
486 $strcalc .= "SELECT $linefield, $colfield, ";
488 when (1) { $strcalc .= "COUNT(*) " }
489 when (2) { $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) " }
490 when ([3,4,5]) { $strcalc .= "SUM(aqorders.listprice) " }
491 default { $strcalc .= "NULL " }
495 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
496 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
497 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
498 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
499 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
500 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
501 WHERE aqorders.datecancellationprinted IS NULL ";
502 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
503 if ( $process == 4 );
504 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
505 if ( $process == 5 );
506 @
$filters[0] =~ s/\*/%/g if ( @
$filters[0] );
507 $strcalc .= " AND aqbasket.closedate >= '" . @
$filters[0] . "'"
509 @
$filters[1] =~ s/\*/%/g if ( @
$filters[1] );
510 $strcalc .= " AND aqbasket.closedate <= '" . @
$filters[1] . "'"
512 @
$filters[2] =~ s/\*/%/g if ( @
$filters[2] );
513 $strcalc .= " AND aqorders.datereceived >= '" . @
$filters[2] . "'"
515 @
$filters[3] =~ s/\*/%/g if ( @
$filters[3] );
516 $strcalc .= " AND aqorders.datereceived <= '" . @
$filters[3] . "'"
518 @
$filters[4] =~ s/\*/%/g if ( @
$filters[4] );
519 $strcalc .= " AND aqbooksellers.name LIKE '" . @
$filters[4] . "'"
521 $strcalc .= " AND items.homebranch = '" . @
$filters[5] . "'"
523 @
$filters[6] =~ s/\*/%/g if ( @
$filters[6] );
524 $strcalc .= " AND items.ccode = '" . @
$filters[6] . "'"
526 @
$filters[7] =~ s/\*/%/g if ( @
$filters[7] );
527 $strcalc .= " AND biblioitems.itemtype LIKE '" . @
$filters[7] . "'"
529 @
$filters[8] =~ s/\*/%/g if ( @
$filters[8] );
530 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @
$filters[8] . "'"
532 @
$filters[9] =~ s/\*/%/g if ( @
$filters[9] );
533 $strcalc .= " AND aqorders.sort1 LIKE '" . @
$filters[9] . "'"
535 @
$filters[10] =~ s/\*/%/g if ( @
$filters[10] );
536 $strcalc .= " AND aqorders.sort2 LIKE '" . @
$filters[10] . "'"
537 if ( @
$filters[10] );
539 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
540 my $dbcalc = $dbh->prepare($strcalc);
544 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
545 $emptycol = 1 if ( !defined($col) );
546 $col = "zzEMPTY" if ( !defined($col) );
547 $row = "zzEMPTY" if ( !defined($row) );
549 $table{$row}->{$col} += $value;
550 $table{$row}->{totalrow
} += $value;
551 $grantotal += $value;
554 push @loopcol, { coltitle
=> "NULL" } if ($emptycol);
556 foreach my $row ( sort keys %table ) {
558 #@loopcol ensures the order for columns is common with column titles
559 # and the number matches the number of columns
560 foreach my $col (@loopcol) {
561 my $value = $table{$row}->{ ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY" : $col->{coltitle
} };
562 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
563 push @loopcell, { value
=> $value };
566 rowtitle
=> ( $row eq "zzEMPTY" ) ?
"NULL" : $row,
567 loopcell
=> \
@loopcell,
568 hilighted
=> ( $hilighted > 0 ),
569 totalrow
=> $table{$row}->{totalrow
}
571 $r->{totalrow
} = sprintf("%.2f", $r->{totalrow
}) if($r->{totalrow
} and grep /$process/, (3,4,5));
573 $hilighted = -$hilighted;
576 foreach my $col (@loopcol) {
578 foreach my $row (@looprow) {
580 ( $row->{rowtitle
} eq "NULL" ) ?
"zzEMPTY"
583 ( $col->{coltitle
} eq "NULL" ) ?
"zzEMPTY"
587 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
589 push @loopfooter, { 'totalcol' => $total };
592 # the header of the table
593 $globalline{loopfilter
} = \
@loopfilter;
594 # the core of the table
595 $globalline{looprow
} = \
@looprow;
596 $globalline{loopcol
} = \
@loopcol;
598 # # the foot (totals by borrower type)
599 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
600 $globalline{loopfooter
} = \
@loopfooter;
601 $globalline{total
} = $grantotal;
602 $globalline{line
} = $line;
603 $globalline{column
} = $column;
604 push @mainloop, \
%globalline;