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;
123 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
126 $select{''} = "All Suppliers";
127 while ( my ( $value, $desc ) = $req->fetchrow ) {
129 $select{$value}=$desc;
131 my $CGIBookSellers = CGI::scrolling_list(
140 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
145 $select{''} = "All Item Types";
146 while ( my ( $value, $desc ) = $req->fetchrow ) {
147 push @select, $value;
148 $select{$value} = $desc;
150 my $CGIItemTypes = CGI::scrolling_list(
159 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
164 $select{''} = "All budgets";
166 while ( my ( $value, $desc ) = $req->fetchrow ) {
167 push @select, $value;
168 $select{$value} = $desc;
170 my $CGIBudget = CGI::scrolling_list(
181 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
189 while ( my ($value) = $req->fetchrow ) {
192 push @select, $value;
193 $select{$value} = $value;
196 my $CGISort1 = CGI::scrolling_list(
207 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
217 while ( my ($value) = $req->fetchrow ) {
220 $hglghtsort2 = !($hassort1);
221 push @select, $value;
222 $select{$value} = $value;
225 my $CGISort2 = CGI::scrolling_list(
234 my $CGIextChoice = CGI::scrolling_list(
237 -values => ['CSV'], # FIXME translation
242 my $CGIsepChoice = GetDelimiterChoices;
244 my $branches = GetBranches;
246 foreach ( sort keys %$branches ) {
247 push @branches, $branches->{$_};
250 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode', '');
253 if($ccode_subfield_structure) {
254 $ccode_label = $ccode_subfield_structure->{liblibrarian};
255 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
259 CGIBookSeller => $CGIBookSellers,
260 CGIItemType => $CGIItemTypes,
261 CGIBudget => $CGIBudget,
262 hassort1 => $hassort1,
263 hassort2 => $hassort2,
264 CGISort1 => $CGISort1,
265 CGISort2 => $CGISort2,
266 CGIextChoice => $CGIextChoice,
267 CGIsepChoice => $CGIsepChoice,
268 branches => \@branches,
269 ccode_label => $ccode_label,
270 ccode_avlist => $ccode_avlist,
274 output_html_with_http_headers $input, $cookie, $template->output;
277 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
290 my $dbh = C4::Context->dbh;
296 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
297 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
299 if ( ( ( $i == 1 ) or ( $i == 3 ) ) and ( @$filters[ $i - 1 ] ) ) {
300 $cell{err} = 1 if ( @$filters[$i] lt @$filters[ $i - 1 ] );
302 # format the dates filters, otherwise just fill as is
304 $cell{filter} = @$filters[$i];
306 $cell{filter} = format_date(@$filters[$i]);
309 when (0) { $cell{crit} = "Placed On From" }
310 when (1) { $cell{crit} = "Placed On To" }
311 when (2) { $cell{crit} = "Received On From" }
312 when (3) { $cell{crit} = "Received On To" }
313 when (4) { $cell{crit} = "Bookseller" }
314 when (5) { $cell{crit} = "Home branch" }
315 when (6) { $cell{crit} = "Collection" }
316 when (7) { $cell{crit} = "Doc Type" }
317 when (8) { $cell{crit} = "Budget" }
318 when (9) { $cell{crit} = "Sort1" }
319 when (10) { $cell{crit} = "Sort2" }
320 default { $cell{crit} = "" }
322 push @loopfilter, \%cell;
328 foreach ($line, $column) {
333 $filter{$_}->[0] = @$filters[0];
334 $filter{$_}->[1] = @$filters[1];
337 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
338 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
339 when (3) { $field{$a} = "Year($a)" }
340 default { $field{$a} = $a }
344 $filter{$_}->[0] = @$filters[2];
345 $filter{$_}->[1] = @$filters[3];
348 when (1) { $field{$a} = "concat(hex(weekday($a)+1),'-',dayname($a))" }
349 when (2) { $field{$a} = "concat(hex(month($a)),'-',monthname($a))" }
350 when (3) { $field{$a} = "Year($a)" }
351 default { $field{$a} = $a }
354 when (/bookseller/) {
355 $filter{$_}->[0] = @$filters[4];
357 when (/homebranch/) {
358 $filter{$_}->[0] = @$filters[5];
361 $filter{$_}->[0] = @$filters[6];
364 $filter{$_}->[0] = @$filters[7];
367 $filter{$_}->[0] = @$filters[8];
370 $filter{$_}->[0] = @$filters[9];
373 $filter{$_}->[0] = @$filters[10];
378 my @linefilter = @{ $filter{$line} };
379 my $linefield = $field{$line};
380 my @colfilter = @{ $filter{$column} };
381 my $colfield = $field{$column};
385 SELECT DISTINCTROW $linefield
387 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
388 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
389 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
390 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
391 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
392 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
393 WHERE $line IS NOT NULL AND $line <> '' ";
396 if ( $linefilter[1] ) {
397 if ( $linefilter[0] ) {
398 $strsth .= " AND $line BETWEEN ? AND ? ";
401 $strsth .= " AND $line <= ? ";
406 and ( ( $line =~ /closedate/ )
407 or ( $line =~ /received/ ))
410 $strsth .= " AND $line >= ? ";
412 elsif ( $linefilter[0] ) {
413 $linefilter[0] =~ s/\*/%/g;
414 $strsth .= " AND $line LIKE ? ";
417 $strsth .= " GROUP BY $linefield";
418 $strsth .= " ORDER BY $line";
420 my $sth = $dbh->prepare($strsth);
421 if ( (@linefilter) and ( $linefilter[1] ) ) {
422 $sth->execute( $linefilter[0], $linefilter[1] );
424 elsif ( $linefilter[0] ) {
425 $sth->execute( $linefilter[0] );
430 while ( my ($celvalue) = $sth->fetchrow ) {
433 $cell{rowtitle} = $celvalue;
434 push @loopline, \%cell;
441 SELECT DISTINCTROW $colfield
443 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
444 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
445 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
446 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
447 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
448 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
449 WHERE $column IS NOT NULL AND $column <> ''
453 if ( $colfilter[1] ) {
454 if ( $colfilter[0] ) {
455 $strsth2 .= " AND $column BETWEEN ? AND ? ";
458 $strsth2 .= " AND $column <= ? ";
463 and ( ( $column =~ /closedate/ )
464 or ( $line =~ /received/ ))
467 $strsth2 .= " AND $column >= ? ";
469 elsif ( $colfilter[0] ) {
470 $colfilter[0] =~ s/\*/%/g;
471 $strsth2 .= " AND $column LIKE ? ";
475 $strsth2 .= " GROUP BY $colfield";
476 $strsth2 .= " ORDER BY $colfield";
478 my $sth2 = $dbh->prepare($strsth2);
480 if ( (@colfilter) and ($colfilter[1]) ) {
481 $sth2->execute( $colfilter[0], $colfilter[1] );
483 elsif ( $colfilter[0] ) {
484 $sth2->execute( $colfilter[0] );
489 while ( my $celvalue = $sth2->fetchrow ) {
492 $cell{coltitle} = $celvalue;
493 push @loopcol, \%cell;
501 #Initialization of cell values.....
504 foreach my $row (@loopline) {
505 foreach my $col (@loopcol) {
506 $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
508 $table{ $row->{rowtitle} }->{totalrow} = 0;
511 # preparing calculation
513 $strcalc .= "SELECT $linefield, $colfield, ";
515 when (1) { $strcalc .= "COUNT(*) " }
516 when (2) { $strcalc .= "COUNT(DISTINCT(aqorders.biblionumber)) " }
517 when ([3,4,5]) { $strcalc .= "SUM(aqorders.listprice) " }
518 default { $strcalc .= "NULL " }
522 LEFT JOIN aqbasket ON (aqorders.basketno = aqbasket.basketno)
523 LEFT JOIN aqorders_items ON (aqorders.ordernumber = aqorders_items.ordernumber)
524 LEFT JOIN items ON (aqorders_items.itemnumber = items.itemnumber)
525 LEFT JOIN biblioitems ON (aqorders.biblionumber = biblioitems.biblionumber)
526 LEFT JOIN aqbudgets ON (aqorders.budget_id = aqbudgets.budget_id )
527 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid = aqbooksellers.id)
528 WHERE aqorders.datecancellationprinted IS NULL ";
529 $strcalc .= " AND (aqorders.datereceived IS NULL OR aqorders.datereceived = '') "
530 if ( $process == 4 );
531 $strcalc .= " AND aqorders.datereceived IS NOT NULL AND aqorders.datereceived <> '' "
532 if ( $process == 5 );
533 @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
534 $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
536 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
537 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
539 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
540 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
542 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
543 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
545 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
546 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
548 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
550 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
551 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
553 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
554 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
556 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
557 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
559 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
560 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
562 @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
563 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
564 if ( @$filters[10] );
566 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
567 my $dbcalc = $dbh->prepare($strcalc);
571 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
572 $emptycol = 1 if ( !defined($col) );
573 $col = "zzEMPTY" if ( !defined($col) );
574 $row = "zzEMPTY" if ( !defined($row) );
576 $table{$row}->{$col} += $value;
577 $table{$row}->{totalrow} += $value;
578 $grantotal += $value;
581 push @loopcol, { coltitle => "NULL" } if ($emptycol);
583 foreach my $row ( sort keys %table ) {
585 #@loopcol ensures the order for columns is common with column titles
586 # and the number matches the number of columns
587 foreach my $col (@loopcol) {
588 my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
589 $value = sprintf("%.2f", $value) if($value and grep /$process/, (3,4,5));
590 push @loopcell, { value => $value };
593 rowtitle => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
594 loopcell => \@loopcell,
595 hilighted => ( $hilighted > 0 ),
596 totalrow => $table{$row}->{totalrow}
598 $r->{totalrow} = sprintf("%.2f", $r->{totalrow}) if($r->{totalrow} and grep /$process/, (3,4,5));
600 $hilighted = -$hilighted;
603 foreach my $col (@loopcol) {
605 foreach my $row (@looprow) {
607 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
610 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
614 $total = sprintf("%.2f", $total) if($total and grep /$process/, (3,4,5));
616 push @loopfooter, { 'totalcol' => $total };
619 # the header of the table
620 $globalline{loopfilter} = \@loopfilter;
621 # the core of the table
622 $globalline{looprow} = \@looprow;
623 $globalline{loopcol} = \@loopcol;
625 # # the foot (totals by borrower type)
626 $grantotal = sprintf("%.2f", $grantotal) if ($grantotal and grep /$process/, (3,4,5));
627 $globalline{loopfooter} = \@loopfooter;
628 $globalline{total} = $grantotal;
629 $globalline{line} = $line;
630 $globalline{column} = $column;
631 push @mainloop, \%globalline;