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');
77 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
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 itemtype,description FROM itemtypes ORDER BY description");
130 $select{''} = "All Item Types";
131 while ( my ( $value, $desc ) = $req->fetchrow ) {
132 push @select, $value;
133 $select{$value} = $desc;
135 my $CGIItemTypes = CGI::scrolling_list(
144 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
149 $select{''} = "All budgets";
151 while ( my ( $value, $desc ) = $req->fetchrow ) {
152 push @select, $value;
153 $select{$value} = $desc;
155 my $CGIBudget = CGI::scrolling_list(
166 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
174 while ( my ($value) = $req->fetchrow ) {
177 push @select, $value;
178 $select{$value} = $value;
181 my $CGISort1 = CGI::scrolling_list(
192 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
202 while ( my ($value) = $req->fetchrow ) {
205 $hglghtsort2 = !($hassort1);
206 push @select, $value;
207 $select{$value} = $value;
210 my $CGISort2 = CGI::scrolling_list(
219 my $CGIextChoice = CGI::scrolling_list(
222 -values => ['CSV'], # FIXME translation
227 my $CGIsepChoice = GetDelimiterChoices;
229 my $branches = GetBranches;
231 foreach ( sort keys %$branches ) {
232 push @branches, $branches->{$_};
235 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode', '');
238 if($ccode_subfield_structure) {
239 $ccode_label = $ccode_subfield_structure->{liblibrarian};
240 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
244 booksellers => $booksellers,
245 CGIItemType => $CGIItemTypes,
246 CGIBudget => $CGIBudget,
247 hassort1 => $hassort1,
248 hassort2 => $hassort2,
249 CGISort1 => $CGISort1,
250 CGISort2 => $CGISort2,
251 CGIextChoice => $CGIextChoice,
252 CGIsepChoice => $CGIsepChoice,
253 branches => \@branches,
254 ccode_label => $ccode_label,
255 ccode_avlist => $ccode_avlist,
259 output_html_with_http_headers $input, $cookie, $template->output;
262 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
275 my $dbh = C4::Context->dbh;
281 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
282 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
284 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
285 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
287 # format the dates filters, otherwise just fill as is
289 $cell{filter} = @$filters[$i];
291 $cell{filter} = format_date(@$filters[$i]);
294 push @loopfilter, \%cell;
300 foreach ($line, $column) {
305 $filter{$_}->[0] = @$filters[0];
306 $filter{$_}->[1] = @$filters[1];
309 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
310 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
311 when (3) { $field{$a} = "Year($a)" }
312 default { $field{$a} = $a }
316 $filter{$_}->[0] = @$filters[2];
317 $filter{$_}->[1] = @$filters[3];
320 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
321 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
322 when (3) { $field{$a} = "Year($a)" }
323 default { $field{$a} = $a }
326 when (/bookseller/) {
327 $filter{$_}->[0] = @$filters[4];
329 when (/homebranch/) {
330 $filter{$_}->[0] = @$filters[5];
333 $filter{$_}->[0] = @$filters[6];
336 $filter{$_}->[0] = @$filters[7];
339 $filter{$_}->[0] = @$filters[8];
342 $filter{$_}->[0] = @$filters[9];
345 $filter{$_}->[0] = @$filters[10];
350 my @linefilter = @{ $filter{$line} };
351 my $linefield = $field{$line};
352 my @colfilter = @{ $filter{$column} };
353 my $colfield = $field{$column};
357 SELECT DISTINCTROW $linefield
359 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
360 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
361 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
362 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
363 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
364 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
365 WHERE $line IS NOT NULL AND $line <> '' ";
368 if ( $linefilter[1] ) {
369 if ( $linefilter[0] ) {
370 $strsth .= " AND $line BETWEEN ? AND ? ";
373 $strsth .= " AND $line <= ? ";
378 and ( ( $line =~ /closedate/ )
379 or ( $line =~ /received/ ))
382 $strsth .= " AND $line >= ? ";
384 elsif ( $linefilter[0] ) {
385 $linefilter[0] =~ s/\*/%/g;
386 $strsth .= " AND $line LIKE ? ";
389 $strsth .= " GROUP BY $linefield";
390 $strsth .= " ORDER BY $line";
392 my $sth = $dbh->prepare($strsth);
393 if ( (@linefilter) and ( $linefilter[1] ) ) {
394 $sth->execute( $linefilter[0], $linefilter[1] );
396 elsif ( $linefilter[0] ) {
397 $sth->execute( $linefilter[0] );
402 while ( my ($celvalue) = $sth->fetchrow ) {
405 $cell{rowtitle} = $celvalue;
406 push @loopline, \%cell;
413 SELECT DISTINCTROW $colfield
415 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
416 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
417 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
418 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
419 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
420 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
421 WHERE $column IS NOT NULL AND $column <> ''
425 if ( $colfilter[1] ) {
426 if ( $colfilter[0] ) {
427 $strsth2 .= " AND $column BETWEEN ? AND ? ";
430 $strsth2 .= " AND $column <= ? ";
435 and ( ( $column =~ /closedate/ )
436 or ( $line =~ /received/ ))
439 $strsth2 .= " AND $column >= ? ";
441 elsif ( $colfilter[0] ) {
442 $colfilter[0] =~ s/\*/%/g;
443 $strsth2 .= " AND $column LIKE ? ";
447 $strsth2 .= " GROUP BY $colfield";
448 $strsth2 .= " ORDER BY $colfield";
450 my $sth2 = $dbh->prepare($strsth2);
452 if ( (@colfilter) and ($colfilter[1]) ) {
453 $sth2->execute( $colfilter[0], $colfilter[1] );
455 elsif ( $colfilter[0] ) {
456 $sth2->execute( $colfilter[0] );
461 while ( my $celvalue = $sth2->fetchrow ) {
464 $cell{coltitle} = $celvalue;
465 push @loopcol, \%cell;
473 #Initialization of cell values.....
476 foreach my $row (@loopline) {
477 foreach my $col (@loopcol) {
478 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
480 $table{ $row->{rowtitle} }->{totalrow} = 0;
483 # preparing calculation
485 $strcalc .= "SELECT $linefield, $colfield, ";
487 when (1) { $strcalc .= "COUNT(*) " }
488 when (2) { $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) " }
489 when ([3,4,5]) { $strcalc .= "SUM(aqorders.listprice) " }
490 default { $strcalc .= "NULL " }
494 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
495 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
496 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
497 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
498 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
499 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
500 WHERE aqorders.datecancellationprinted IS NULL ";
501 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
502 if ( $process == 4 );
503 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
504 if ( $process == 5 );
505 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
506 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
508 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
509 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
511 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
512 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
514 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
515 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
517 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
518 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
520 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
522 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
523 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
525 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
526 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
528 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
529 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
531 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
532 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
534 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
535 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
536 if ( @$filters[10] );
538 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
539 my $dbcalc = $dbh->prepare($strcalc);
543 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
544 $emptycol = 1 if ( !defined($col) );
545 $col = "zzEMPTY" if ( !defined($col) );
546 $row = "zzEMPTY" if ( !defined($row) );
548 $table{$row}->{$col} += $value;
549 $table{$row}->{totalrow} += $value;
550 $grantotal += $value;
553 push @loopcol, { coltitle => "NULL" } if ($emptycol);
555 foreach my $row ( sort keys %table ) {
557 #@loopcol ensures the order for columns is common with column titles
558 # and the number matches the number of columns
559 foreach my $col (@loopcol) {
560 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
561 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
562 push @loopcell, { value => $value };
565 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
566 loopcell => \@loopcell,
567 hilighted => ( $hilighted > 0 ),
568 totalrow => $table{$row}->{totalrow}
570 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
572 $hilighted = -$hilighted;
575 foreach my $col (@loopcol) {
577 foreach my $row (@looprow) {
579 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
582 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
586 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
588 push @loopfooter, { 'totalcol' => $total };
591 # the header of the table
592 $globalline{loopfilter} = \@loopfilter;
593 # the core of the table
594 $globalline{looprow} = \@looprow;
595 $globalline{loopcol} = \@loopcol;
597 # # the foot (totals by borrower type)
598 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
599 $globalline{loopfooter} = \@loopfooter;
600 $globalline{total} = $grantotal;
601 $globalline{line} = $line;
602 $globalline{column} = $column;
603 push @mainloop, \%globalline;