Bug 7368: Typo in kohastructure.sql documentation line
[koha.git] / reports / acquisitions_stats.pl
blobcdc5b9a561288a50f507028ddd86fb92011846ba
1 #!/usr/bin/perl
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
11 # version.
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.
21 # test comment
23 use Modern::Perl;
25 use C4::Auth;
26 use CGI;
27 use C4::Context;
28 use C4::Reports;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Circulation;
32 use C4::Dates qw/format_date format_date_in_iso/;
33 use C4::Branch;
34 use C4::Biblio;
36 =head1 NAME
38 plugin that shows a stats on borrowers
40 =head1 DESCRIPTION
42 =over 2
44 =cut
46 my $input = new CGI;
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,
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { reports => '*' },
69 debug => 1,
73 our $sep = $input->param("sep") // '';
74 $sep = "\t" if ($sep eq 'tabulation');
76 $template->param(
77 do_it => $do_it,
80 if ($do_it) {
81 my $results =
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;
87 else {
88 print $input->header(
89 -type => 'application/vnd.sun.xml.calc',
90 -encoding => 'utf-8',
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;
100 print "Total\n";
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};
108 print "\n";
110 print "TOTAL";
111 $cols = @$results[0]->{loopfooter};
112 foreach my $col (@$cols) {
113 print $sep. $col->{totalcol};
115 print $sep. @$results[0]->{total};
117 exit;
119 else {
120 my $dbh = C4::Context->dbh;
121 my $req;
122 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
123 $req->execute;
124 my $booksellers = $req->fetchall_arrayref({});
126 $req = $dbh->prepare("SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
127 $req->execute;
128 my @select;
129 my %select;
130 push @select, "";
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(
137 -name => 'Filter',
138 -id => 'itemtypes',
139 -values => \@select,
140 -labels => \%select,
141 -size => 1,
142 -multiple => 0
145 $req = $dbh->prepare("SELECT DISTINCTROW budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
146 $req->execute;
147 undef @select;
148 undef %select;
149 push @select, "";
150 $select{''} = "All budgets";
152 while ( my ( $value, $desc ) = $req->fetchrow ) {
153 push @select, $value;
154 $select{$value} = $desc;
156 my $CGIBudget = CGI::scrolling_list(
157 -name => 'Filter',
158 -id => 'budget',
159 -values => \@select,
160 -labels => \%select,
161 -size => 1,
162 -multiple => 0
165 $req =
166 $dbh->prepare(
167 "SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1"
169 $req->execute;
170 undef @select;
171 undef %select;
172 push @select, "";
173 $select{''} = "All";
174 my $hassort1;
175 while ( my ($value) = $req->fetchrow ) {
176 if ($value) {
177 $hassort1 = 1;
178 push @select, $value;
179 $select{$value} = $value;
182 my $CGISort1 = CGI::scrolling_list(
183 -name => 'Filter',
184 -id => 'sort1',
185 -values => \@select,
186 -labels => \%select,
187 -size => 1,
188 -multiple => 0
191 $req =
192 $dbh->prepare(
193 "SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2"
195 $req->execute;
196 undef @select;
197 undef %select;
198 push @select, "";
199 $select{''} = "All";
200 my $hassort2;
201 my $hglghtsort2;
203 while ( my ($value) = $req->fetchrow ) {
204 if ($value) {
205 $hassort2 = 1;
206 $hglghtsort2 = !($hassort1);
207 push @select, $value;
208 $select{$value} = $value;
211 my $CGISort2 = CGI::scrolling_list(
212 -name => 'Filter',
213 -id => 'sort2',
214 -values => \@select,
215 -labels => \%select,
216 -size => 1,
217 -multiple => 0
220 my $CGIextChoice = CGI::scrolling_list(
221 -name => 'MIME',
222 -id => 'MIME',
223 -values => ['CSV'], # FIXME translation
224 -size => 1,
225 -multiple => 0
228 my $CGIsepChoice = GetDelimiterChoices;
230 my $branches = GetBranches;
231 my @branches;
232 foreach ( sort keys %$branches ) {
233 push @branches, $branches->{$_};
236 my $ccode_subfield_structure = GetMarcSubfieldStructureFromKohaField('items.ccode', '');
237 my $ccode_label;
238 my $ccode_avlist;
239 if($ccode_subfield_structure) {
240 $ccode_label = $ccode_subfield_structure->{liblibrarian};
241 $ccode_avlist = GetAuthorisedValues($ccode_subfield_structure->{authorised_value});
244 $template->param(
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;
262 sub calculate {
263 my ( $line, $column, $podsp, $rodsp, $process, $filters ) = @_;
264 my @mainloop;
265 my @loopfooter;
266 my @loopcol;
267 my @loopline;
268 my @looprow;
269 my %globalline;
270 my $grantotal = 0;
272 $podsp ||= 0;
273 $rodsp ||= 0;
275 # extract parameters
276 my $dbh = C4::Context->dbh;
278 # Filters
279 # Checking filters
281 my @loopfilter;
282 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
283 if( defined @$filters[$i] and @$filters[$i] ne '' ) {
284 my %cell;
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
289 if ($i >= 4) {
290 $cell{filter} = @$filters[$i];
291 } else {
292 $cell{filter} = format_date(@$filters[$i]);
294 $cell{crit} = $i;
295 push @loopfilter, \%cell;
299 my %filter;
300 my %field;
301 foreach ($line, $column) {
302 $filter{$_} = [];
303 $field{$_} = $_;
304 given ($_) {
305 when (/closedate/) {
306 $filter{$_}->[0] = @$filters[0];
307 $filter{$_}->[1] = @$filters[1];
308 my $a = $_;
309 given ($podsp) {
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 }
316 when (/received/) {
317 $filter{$_}->[0] = @$filters[2];
318 $filter{$_}->[1] = @$filters[3];
319 my $a = $_;
320 given ($rodsp) {
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];
333 when (/ccode/) {
334 $filter{$_}->[0] = @$filters[6];
336 when (/itemtype/) {
337 $filter{$_}->[0] = @$filters[7];
339 when (/budget/) {
340 $filter{$_}->[0] = @$filters[8];
342 when (/sort1/) {
343 $filter{$_}->[0] = @$filters[9];
345 when (/sort2/) {
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};
356 # 1st, loop rows.
357 my $strsth = "
358 SELECT DISTINCTROW $linefield
359 FROM aqorders
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 <> '' ";
368 if (@linefilter) {
369 if ( $linefilter[1] ) {
370 if ( $linefilter[0] ) {
371 $strsth .= " AND $line BETWEEN ? AND ? ";
373 else {
374 $strsth .= " AND $line <= ? ";
377 elsif (
378 ( $linefilter[0] )
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] );
400 else {
401 $sth->execute;
403 while ( my ($celvalue) = $sth->fetchrow ) {
404 my %cell;
405 if ($celvalue) {
406 $cell{rowtitle} = $celvalue;
407 push @loopline, \%cell;
409 $cell{totalrow} = 0;
412 # 2nd, loop cols.
413 my $strsth2 = "
414 SELECT DISTINCTROW $colfield
415 FROM aqorders
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 <> ''
425 if (@colfilter) {
426 if ( $colfilter[1] ) {
427 if ( $colfilter[0] ) {
428 $strsth2 .= " AND $column BETWEEN ? AND ? ";
430 else {
431 $strsth2 .= " AND $column <= ? ";
434 elsif (
435 ( $colfilter[0] )
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] );
459 else {
460 $sth2->execute;
462 while ( my $celvalue = $sth2->fetchrow ) {
463 my %cell;
464 if ($celvalue) {
465 $cell{coltitle} = $celvalue;
466 push @loopcol, \%cell;
470 my $i = 0;
471 my @totalcol;
472 my $hilighted = -1;
474 #Initialization of cell values.....
475 my %table;
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
485 my $strcalc;
486 $strcalc .= "SELECT $linefield, $colfield, ";
487 given ($process) {
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 " }
493 $strcalc .= "
494 FROM aqorders
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] . "'"
508 if ( @$filters[0] );
509 @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
510 $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
511 if ( @$filters[1] );
512 @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
513 $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
514 if ( @$filters[2] );
515 @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
516 $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
517 if ( @$filters[3] );
518 @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
519 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
520 if ( @$filters[4] );
521 $strcalc .= " AND items.homebranch = '" . @$filters[5] . "'"
522 if ( @$filters[5] );
523 @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
524 $strcalc .= " AND items.ccode = '" . @$filters[6] . "'"
525 if ( @$filters[6] );
526 @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
527 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
528 if ( @$filters[7] );
529 @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
530 $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[8] . "'"
531 if ( @$filters[8] );
532 @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
533 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
534 if ( @$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);
541 $dbcalc->execute;
543 my $emptycol;
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 ) {
557 my @loopcell;
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 };
565 my $r = {
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));
572 push @looprow, $r;
573 $hilighted = -$hilighted;
576 foreach my $col (@loopcol) {
577 my $total = 0;
578 foreach my $row (@looprow) {
579 $total += $table{
580 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
581 : $row->{rowtitle}
582 }->{
583 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
584 : $col->{coltitle}
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;
605 return \@mainloop;