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.tt";
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) {
304 if ( $_ =~ /closedate/ ) {
305 $filter{$_}->[0] = @$filters[0];
306 $filter{$_}->[1] = @$filters[1];
309 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
310 } elsif ( $podsp == 2 ) {
311 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
312 } elsif ( $podsp == 3 ) {
313 $field{$a} = "Year($a)";
318 elsif ( $_ =~ /received/ ) {
319 $filter{$_}->[0] = @$filters[2];
320 $filter{$_}->[1] = @$filters[3];
323 $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))";
324 } elsif ( $rodsp == 2 ) {
325 $field{$a} = "concat(hex(month($a)),'-',monthname($a))";
326 } elsif ( $rodsp == 3 ) {
327 $field{$a} = "Year($a)";
332 elsif ( $_ =~ /bookseller/ ) {
333 $filter{$_}->[0] = @$filters[4];
335 elsif ( $_ =~ /homebranch/ ) {
336 $filter{$_}->[0] = @$filters[5];
338 elsif ( $_ =~ /ccode/ ) {
339 $filter{$_}->[0] = @$filters[6];
341 elsif ( $_ =~ /itemtype/ ) {
342 $filter{$_}->[0] = @$filters[7];
344 elsif ( $_ =~ /budget/ ) {
345 $filter{$_}->[0] = @$filters[8];
347 elsif ( $_ =~ /sort1/ ) {
348 $filter{$_}->[0] = @$filters[9];
350 elsif ( $_ =~ /sort2/ ) {
351 $filter{$_}->[0] = @$filters[10];
355 my @linefilter = @{ $filter{$line} };
356 my $linefield = $field{$line};
357 my @colfilter = @{ $filter{$column} };
358 my $colfield = $field{$column};
362 SELECT DISTINCTROW $linefield
364 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
365 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
366 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
367 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
368 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
369 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
370 WHERE $line IS NOT NULL AND $line <> '' ";
373 if ( $linefilter[1] ) {
374 if ( $linefilter[0] ) {
375 $strsth .= " AND $line BETWEEN ? AND ? ";
378 $strsth .= " AND $line <= ? ";
383 and ( ( $line =~ /closedate/ )
384 or ( $line =~ /received/ ))
387 $strsth .= " AND $line >= ? ";
389 elsif ( $linefilter[0] ) {
390 $linefilter[0] =~ s/\*/%/g;
391 $strsth .= " AND $line LIKE ? ";
394 $strsth .= " GROUP BY $linefield";
395 $strsth .= " ORDER BY $line";
397 my $sth = $dbh->prepare($strsth);
398 if ( (@linefilter) and ( $linefilter[1] ) ) {
399 $sth->execute( $linefilter[0], $linefilter[1] );
401 elsif ( $linefilter[0] ) {
402 $sth->execute( $linefilter[0] );
407 while ( my ($celvalue) = $sth->fetchrow ) {
410 $cell{rowtitle} = $celvalue;
411 push @loopline, \%cell;
418 SELECT DISTINCTROW $colfield
420 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
421 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
422 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
423 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
424 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
425 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
426 WHERE $column IS NOT NULL AND $column <> ''
430 if ( $colfilter[1] ) {
431 if ( $colfilter[0] ) {
432 $strsth2 .= " AND $column BETWEEN ? AND ? ";
435 $strsth2 .= " AND $column <= ? ";
440 and ( ( $column =~ /closedate/ )
441 or ( $line =~ /received/ ))
444 $strsth2 .= " AND $column >= ? ";
446 elsif ( $colfilter[0] ) {
447 $colfilter[0] =~ s/\*/%/g;
448 $strsth2 .= " AND $column LIKE ? ";
452 $strsth2 .= " GROUP BY $colfield";
453 $strsth2 .= " ORDER BY $colfield";
455 my $sth2 = $dbh->prepare($strsth2);
457 if ( (@colfilter) and ($colfilter[1]) ) {
458 $sth2->execute( $colfilter[0], $colfilter[1] );
460 elsif ( $colfilter[0] ) {
461 $sth2->execute( $colfilter[0] );
466 while ( my $celvalue = $sth2->fetchrow ) {
469 $cell{coltitle} = $celvalue;
470 push @loopcol, \%cell;
478 #Initialization of cell values.....
481 foreach my $row (@loopline) {
482 foreach my $col (@loopcol) {
483 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
485 $table{ $row->{rowtitle} }->{totalrow} = 0;
488 # preparing calculation
490 $strcalc .= "SELECT $linefield, $colfield, ";
491 if ( $process == 1 ) {
492 $strcalc .= "COUNT(*) ";
493 } elsif ( $process == 2 ) {
494 $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) ";
495 } elsif ( $process == 3 || $process == 4 || $process == 5 ) {
496 $strcalc .= "SUM(aqorders.listprice) ";
502 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
503 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
504 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
505 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
506 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
507 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
508 WHERE aqorders.datecancellationprinted IS NULL ";
509 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
510 if ( $process == 4 );
511 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
512 if ( $process == 5 );
513 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
514 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
516 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
517 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
519 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
520 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
522 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
523 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
525 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
526 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
528 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
530 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
531 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
533 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
534 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
536 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
537 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
539 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
540 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
542 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
543 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
544 if ( @$filters[10] );
546 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
547 my $dbcalc = $dbh->prepare($strcalc);
551 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
552 $emptycol = 1 if ( !defined($col) );
553 $col = "zzEMPTY" if ( !defined($col) );
554 $row = "zzEMPTY" if ( !defined($row) );
556 $table{$row}->{$col} += $value;
557 $table{$row}->{totalrow} += $value;
558 $grantotal += $value;
561 push @loopcol, { coltitle => "NULL" } if ($emptycol);
563 foreach my $row ( sort keys %table ) {
565 #@loopcol ensures the order for columns is common with column titles
566 # and the number matches the number of columns
567 foreach my $col (@loopcol) {
568 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
569 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
570 push @loopcell, { value => $value };
573 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
574 loopcell => \@loopcell,
575 hilighted => ( $hilighted > 0 ),
576 totalrow => $table{$row}->{totalrow}
578 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
580 $hilighted = -$hilighted;
583 foreach my $col (@loopcol) {
585 foreach my $row (@looprow) {
587 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
590 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
594 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
596 push @loopfooter, { 'totalcol' => $total };
599 # the header of the table
600 $globalline{loopfilter} = \@loopfilter;
601 # the core of the table
602 $globalline{looprow} = \@looprow;
603 $globalline{loopcol} = \@loopcol;
605 # # the foot (totals by borrower type)
606 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
607 $globalline{loopfooter} = \@loopfooter;
608 $globalline{total} = $grantotal;
609 $globalline{line} = $line;
610 $globalline{column} = $column;
611 push @mainloop, \%globalline;