Bug 7843: Followup - fix broken url
[koha.git] / reports / acquisitions_stats.pl
blob0f45989a5e11c8aeb825807bf628c8752f5a8551
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Reports;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Branch;
31 use C4::Biblio;
33 =head1 NAME
35 reports/acquisitions_stats.pl
37 =head1 DESCRIPTION
39 Plugin that shows a stats on borrowers
41 =cut
43 my $input = new CGI;
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,
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { reports => '*' },
66 debug => 1,
70 our $sep = $input->param("sep") // '';
71 $sep = "\t" if ($sep eq 'tabulation');
73 $template->param(
74 do_it => $do_it,
77 if ($do_it) {
78 my $results =
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;
84 else {
85 print $input->header(
86 -type => 'application/vnd.sun.xml.calc',
87 -encoding => 'utf-8',
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;
97 print "Total\n";
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};
105 print "\n";
107 print "TOTAL";
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col (@$cols) {
110 print $sep. $col->{totalcol};
112 print $sep. @$results[0]->{total};
114 exit;
116 else {
117 my $dbh = C4::Context->dbh;
118 my $req;
119 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
120 $req->execute;
121 my $booksellers = $req->fetchall_arrayref({});
123 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
124 $req->execute;
125 my @iselect;
126 my %iselect;
127 while ( my ( $value, $desc ) = $req->fetchrow ) {
128 push @iselect, $value;
129 $iselect{$value} = $desc;
131 my $ItemTypes = {
132 values => \@iselect,
133 labels => \%iselect,
136 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
137 $req->execute;
138 my @bselect;
139 my %bselect;
141 while ( my ( $value, $desc ) = $req->fetchrow ) {
142 push @bselect, $value;
143 $bselect{$value} = $desc;
145 my $Budgets = {
146 values => \@bselect,
147 labels => \%bselect,
150 $req =
151 $dbh->prepare(
152 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
154 $req->execute;
155 my @s1select;
156 my %s1select;
157 my $hassort1;
158 while ( my ($value) = $req->fetchrow ) {
159 if ($value) {
160 $hassort1 = 1;
161 push @s1select, $value;
162 $s1select{$value} = $value;
165 my $Sort1 = {
166 values => \@s1select,
167 labels => \%s1select,
170 $req =
171 $dbh->prepare(
172 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
174 $req->execute;
175 my @s2select;
176 my %s2select;
177 my $hassort2;
178 my $hglghtsort2;
180 while ( my ($value) = $req->fetchrow ) {
181 if ($value) {
182 $hassort2 = 1;
183 $hglghtsort2 = !($hassort1);
184 push @s2select, $value;
185 $s2select{$value} = $value;
188 my $Sort2 = {
189 values => \@s2select,
190 labels => \%s2select,
193 my $CGIsepChoice = GetDelimiterChoices;
195 my $branches = GetBranches;
196 my @branches;
197 foreach ( sort keys %$branches ) {
198 push @branches, $branches->{$_};
201 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode', '');
202 my $ccode_label;
203 my $ccode_avlist;
204 if($ccode_subfield_structure) {
205 $ccode_label = $ccode_subfield_structure->{liblibrarian};
206 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
209 $template->param(
210 booksellers => $booksellers,
211 ItemTypes => $ItemTypes,
212 Budgets => $Budgets,
213 hassort1 => $hassort1,
214 hassort2 => $hassort2,
215 Sort1 => $Sort1,
216 Sort2 => $Sort2,
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;
226 sub calculate {
227 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
228 my @mainloop;
229 my @loopfooter;
230 my @loopcol;
231 my @loopline;
232 my @looprow;
233 my %globalline;
234 my $grantotal = 0;
236 $podsp ||= 0;
237 $rodsp ||= 0;
239 # extract parameters
240 my $dbh = C4::Context->dbh;
242 # Filters
243 # Checking filters
245 my @loopfilter;
246 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
247 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
248 my %cell;
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
253 if ($i >= 4) {
254 $cell{filter} = @$filters[$i];
255 } else {
256 $cell{filter} = format_date(@$filters[$i]);
258 $cell{crit} = $i;
259 push @loopfilter, \%cell;
263 my %filter;
264 my %field;
265 foreach ($line, $column) {
266 $filter{$_} = [];
267 $field{$_} = $_;
268 if ( $_ =~ /closedate/ ) {
269 $filter{$_}->[0] = @$filters[0];
270 $filter{$_}->[1] = @$filters[1];
271 my $a = $_;
272 if ( $podsp == 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)";
278 } else {
279 $field{$a} = $a;
282 elsif ( $_ =~ /received/ ) {
283 $filter{$_}->[0] = @$filters[2];
284 $filter{$_}->[1] = @$filters[3];
285 my $a = $_;
286 if ( $rodsp == 1 ) {
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)";
292 } else {
293 field{$a} = $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};
324 # 1st, loop rows.
325 my $strsth = "
326 SELECT DISTINCTROW $linefield
327 FROM aqorders
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 <> '' ";
336 if (@linefilter) {
337 if ( $linefilter[1] ) {
338 if ( $linefilter[0] ) {
339 $strsth .= " AND $line BETWEEN ? AND ? ";
341 else {
342 $strsth .= " AND $line <= ? ";
345 elsif (
346 ( $linefilter[0] )
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] );
368 else {
369 $sth->execute;
371 while ( my ($celvalue) = $sth->fetchrow ) {
372 my %cell;
373 if ($celvalue) {
374 $cell{rowtitle} = $celvalue;
375 push @loopline, \%cell;
377 $cell{totalrow} = 0;
380 # 2nd, loop cols.
381 my $strsth2 = "
382 SELECT DISTINCTROW $colfield
383 FROM aqorders
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 <> ''
393 if (@colfilter) {
394 if ( $colfilter[1] ) {
395 if ( $colfilter[0] ) {
396 $strsth2 .= " AND $column BETWEEN ? AND ? ";
398 else {
399 $strsth2 .= " AND $column <= ? ";
402 elsif (
403 ( $colfilter[0] )
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] );
427 else {
428 $sth2->execute;
430 while ( my $celvalue = $sth2->fetchrow ) {
431 my %cell;
432 if ($celvalue) {
433 $cell{coltitle} = $celvalue;
434 push @loopcol, \%cell;
438 my $i = 0;
439 my @totalcol;
440 my $hilighted = -1;
442 #Initialization of cell values.....
443 my %table;
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
453 my $strcalc;
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) ";
461 } else {
462 $strcalc .= "NULL ";
464 $strcalc .= "
465 FROM aqorders
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] . "'"
479 if ( @$filters[0] );
480 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
481 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
482 if ( @$filters[1] );
483 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
484 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
485 if ( @$filters[2] );
486 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
487 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
488 if ( @$filters[3] );
489 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
490 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
491 if ( @$filters[4] );
492 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
493 if ( @$filters[5] );
494 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
495 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
496 if ( @$filters[6] );
497 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
498 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
499 if ( @$filters[7] );
500 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
501 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
502 if ( @$filters[8] );
503 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
504 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
505 if ( @$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);
512 $dbcalc->execute;
514 my $emptycol;
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 ) {
528 my @loopcell;
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 };
536 my $r = {
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));
543 push @looprow, $r;
544 $hilighted = -$hilighted;
547 foreach my $col (@loopcol) {
548 my $total = 0;
549 foreach my $row (@looprow) {
550 $total += $table{
551 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
552 : $row->{rowtitle}
553 }->{
554 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
555 : $col->{coltitle}
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;
576 return \@mainloop;