Bug 18901: Sysprefs translation: translate only *.pref files (not *.pref*)
[koha.git] / reports / issues_stats.pl
blobf7f5662f4bdbe7ceab2e2921f7c3ccb6e038105d
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 strict;
21 use warnings;
23 use CGI qw ( -utf8 );
24 use Date::Manip;
26 use C4::Auth;
27 use C4::Debug;
28 use C4::Context;
29 use C4::Koha;
30 use C4::Output;
31 use C4::Circulation;
32 use C4::Reports;
33 use C4::Members;
35 use Koha::AuthorisedValues;
36 use Koha::DateUtils;
37 use Koha::ItemTypes;
38 use C4::Members::AttributeTypes;
40 =head1 NAME
42 reports/issues_stats.pl
44 =head1 DESCRIPTION
46 Plugin that shows circulation stats
48 =cut
50 # my $debug = 1; # override for now.
51 my $input = CGI->new;
52 my $fullreportname = "reports/issues_stats.tt";
53 my $do_it = $input->param('do_it');
54 my $line = $input->param("Line");
55 my $column = $input->param("Column");
56 my @filters = $input->multi_param("Filter");
57 $filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
58 if ( $filters[0] );
59 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
60 if ( $filters[1] );
61 my $podsp = $input->param("DisplayBy");
62 my $type = $input->param("PeriodTypeSel");
63 my $daysel = $input->param("PeriodDaySel");
64 my $monthsel = $input->param("PeriodMonthSel");
65 my $calc = $input->param("Cellvalue");
66 my $output = $input->param("output");
67 my $basename = $input->param("basename");
69 my $attribute_filters;
70 my $vars = $input->Vars;
71 foreach(keys %$vars) {
72 if(/^Filter_borrower_attributes\.(.*)/) {
73 $attribute_filters->{$1} = $vars->{$_};
78 my ($template, $borrowernumber, $cookie) = get_template_and_user({
79 template_name => $fullreportname,
80 query => $input,
81 type => "intranet",
82 authnotrequired => 0,
83 flagsrequired => {reports => '*'},
84 debug => 0,
85 });
86 our $sep = $input->param("sep") // ';';
87 $sep = "\t" if ($sep eq 'tabulation');
88 $template->param(do_it => $do_it,
91 our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
93 our $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
94 our $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
96 our $Bsort1 = GetAuthorisedValues("Bsort1");
97 our $Bsort2 = GetAuthorisedValues("Bsort2");
98 my ($hassort1,$hassort2);
99 $hassort1=1 if $Bsort1;
100 $hassort2=1 if $Bsort2;
102 if ($do_it) {
103 # Displaying results
104 my $results = calculate( $line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters, $attribute_filters);
105 if ( $output eq "screen" ) {
107 # Printing results to screen
108 $template->param( mainloop => $results );
109 output_html_with_http_headers $input, $cookie, $template->output;
110 } else {
112 # Printing to a csv file
113 print $input->header(
114 -type => 'application/vnd.sun.xml.calc',
115 -encoding => 'utf-8',
116 -attachment => "$basename.csv",
117 -filename => "$basename.csv"
119 my $cols = @$results[0]->{loopcol};
120 my $lines = @$results[0]->{looprow};
122 # header top-right
123 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
125 # Other header
126 foreach my $col (@$cols) {
127 print $col->{coltitle} . $sep;
129 print "Total\n";
131 # Table
132 foreach my $line (@$lines) {
133 my $x = $line->{loopcell};
134 print $line->{rowtitle} . $sep;
135 print map { $_->{value} . $sep } @$x;
136 print $line->{totalrow}, "\n";
139 exit;
143 my $dbh = C4::Context->dbh;
144 my @values;
145 my %labels;
146 my %select;
148 # create itemtype arrayref for <select>.
149 our $itemtypes = Koha::ItemTypes->search_with_localization;
151 # location list
152 my @locations;
153 foreach (sort keys %$locations) {
154 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
157 my @ccodes;
158 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
159 push @ccodes, { code => $_, description => $ccodes->{$_} };
162 my $CGIextChoice = ( 'CSV' ); # FIXME translation
163 my $CGIsepChoice=GetDelimiterChoices;
165 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes(1);
166 my %attribute_types_by_class;
167 foreach my $attribute_type (@attribute_types) {
168 if ($attribute_type->{authorised_value_category}) {
169 my $authorised_values = C4::Koha::GetAuthorisedValues(
170 $attribute_type->{authorised_value_category});
172 foreach my $authorised_value (@$authorised_values) {
173 push @{ $attribute_type->{authorised_values} }, $authorised_value;
176 push @{ $attribute_types_by_class{$attribute_type->{class}} }, $attribute_type;
179 $template->param(
180 categoryloop => \@patron_categories,
181 itemtypes => $itemtypes,
182 locationloop => \@locations,
183 ccodeloop => \@ccodes,
184 hassort1 => $hassort1,
185 hassort2 => $hassort2,
186 Bsort1 => $Bsort1,
187 Bsort2 => $Bsort2,
188 CGIextChoice => $CGIextChoice,
189 CGIsepChoice => $CGIsepChoice,
190 attribute_types_by_class => \%attribute_types_by_class,
192 output_html_with_http_headers $input, $cookie, $template->output;
194 sub calculate {
195 my ( $line, $column, $dsp, $type, $daysel, $monthsel, $process, $filters, $attribute_filters ) = @_;
196 my @loopfooter;
197 my @loopcol;
198 my @loopline;
199 my @looprow;
200 my %globalline;
201 my $grantotal = 0;
203 # extract parameters
204 my $dbh = C4::Context->dbh;
206 my ($line_attribute_type, $column_attribute_type);
207 if($line =~ /^borrower_attributes\.(.*)/) {
208 $line_attribute_type = $1;
209 $line = "borrower_attributes.attribute";
211 if($column =~ /^borrower_attributes\.(.*)/) {
212 $column_attribute_type = $1;
213 $column = "borrower_attributes.attribute";
216 # Filters
217 # Checking filters
219 my @loopfilter;
220 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
221 my %cell;
222 ( @$filters[$i] ) or next;
223 if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
224 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
226 # format the dates filters, otherwise just fill as is
227 if ($i>=2) {
228 $cell{filter} = @$filters[$i];
229 } else {
230 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
231 if ( @$filters[$i] );
233 $cell{crit} = $i;
235 push @loopfilter, \%cell;
237 foreach (keys %$attribute_filters) {
238 next unless $attribute_filters->{$_};
239 push @loopfilter, { crit => "$_ =", filter => $attribute_filters->{$_} };
241 push @loopfilter, { crit => "Event", filter => $type };
242 push @loopfilter, { crit => "Display by", filter => $dsp } if ($dsp);
243 push @loopfilter, { crit => "Select Day", filter => $daysel } if ($daysel);
244 push @loopfilter, { crit => "Select Month", filter => $monthsel } if ($monthsel);
246 my @linefilter;
247 $debug and warn "filtres " . join "|", @$filters;
248 my ( $colsource, $linesource ) = ('', '');
249 $linefilter[1] = @$filters[1] if ( $line =~ /datetime/ );
250 $linefilter[0] =
251 ( $line =~ /datetime/ ) ? @$filters[0]
252 : ( $line =~ /category/ ) ? @$filters[2]
253 : ( $line =~ /itemtype/ ) ? @$filters[3]
254 : ( $line =~ /^branch/ ) ? @$filters[4]
255 : ( $line =~ /ccode/ ) ? @$filters[5]
256 : ( $line =~ /location/ ) ? @$filters[6]
257 : ( $line =~ /sort1/ ) ? @$filters[9]
258 : ( $line =~ /sort2/ ) ? @$filters[10]
259 : ( $line =~ /homebranch/) ? @$filters[11]
260 : ( $line =~ /holdingbranch/) ? @$filters[12]
261 : ( $line =~ /borrowers.branchcode/ ) ? @$filters[13]
262 : ( $line_attribute_type ) ? $attribute_filters->{$line_attribute_type}
263 : undef;
265 if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) {
266 $linesource = 'items';
269 my @colfilter;
270 $colfilter[1] = @$filters[1] if ($column =~ /datetime/);
271 $colfilter[0] =
272 ( $column =~ /datetime/ ) ? @$filters[0]
273 : ( $column =~ /category/ ) ? @$filters[2]
274 : ( $column =~ /itemtype/ ) ? @$filters[3]
275 : ( $column =~ /^branch/ ) ? @$filters[4]
276 : ( $column =~ /ccode/ ) ? @$filters[5]
277 : ( $column =~ /location/ ) ? @$filters[6]
278 : ( $column =~ /sort1/ ) ? @$filters[9]
279 : ( $column =~ /sort1/ ) ? @$filters[10]
280 : ( $column =~ /homebranch/) ? @$filters[11]
281 : ( $column =~ /holdingbranch/) ? @$filters[12]
282 : ( $column =~ /borrowers.branchcode/ ) ? @$filters[13]
283 : ( $column_attribute_type ) ? $attribute_filters->{$column_attribute_type}
284 : undef;
286 if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) {
287 $colsource = 'items';
290 # 1st, loop rows.
291 my $linefield;
292 if ( $line =~ /datetime/ ) {
294 # by Day, Month, Year or Hour (1,2,3,4 respectively)
295 $linefield =
296 ( $dsp == 1 ) ? " dayname($line)"
297 : ( $dsp == 2 ) ? "monthname($line)"
298 : ( $dsp == 3 ) ? " Year($line)"
299 : ( $dsp == 4 ) ? "extract(hour from $line)"
300 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
301 } else {
302 $linefield = $line;
304 my $lineorder =
305 ( $linefield =~ /dayname/ ) ? "weekday($line)"
306 : ( $linefield =~ /^month/ ) ? " month($line)"
307 : $linefield;
309 my $strsth;
310 if($line_attribute_type) {
311 $strsth = "SELECT attribute FROM borrower_attributes WHERE code = '$line_attribute_type' ";
312 } else {
313 $strsth = "SELECT distinctrow $linefield FROM statistics ";
315 # get stats on items if ccode or location, otherwise borrowers.
316 $strsth .=
317 ( $linesource eq 'items' )
318 ? " LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
319 : " LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
320 $strsth .= " WHERE $line is not null AND $line != '' ";
323 if ( $line =~ /datetime/ ) {
324 if ( $linefilter[1] and ( $linefilter[0] ) ) {
325 $strsth .= " AND $line between ? AND ? ";
326 } elsif ( $linefilter[1] ) {
327 $strsth .= " AND $line <= ? ";
328 } elsif ( $linefilter[0] ) {
329 $strsth .= " AND $line >= ? ";
331 $strsth .= " AND type ='" . $type . "' " if $type;
332 $strsth .= " AND dayname(datetime) ='" . $daysel . "' " if $daysel;
333 $strsth .= " AND monthname(datetime) ='" . $monthsel . "' " if $monthsel;
334 } elsif ( $linefilter[0] ) {
335 $linefilter[0] =~ s/\*/%/g;
336 $strsth .= " AND $line LIKE ? ";
338 $strsth .= " group by $linefield order by $lineorder ";
339 $debug and warn $strsth;
340 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth };
341 my $sth = $dbh->prepare($strsth);
342 if ( (@linefilter) and ($linefilter[0]) and ($linefilter[1]) ) {
343 $sth->execute( $linefilter[0], $linefilter[1] . " 23:59:59" );
344 } elsif ( $linefilter[1] ) {
345 $sth->execute( $linefilter[1] . " 23:59:59" );
346 } elsif ( $linefilter[0] ) {
347 $sth->execute( $linefilter[0] );
348 } else {
349 $sth->execute;
352 while ( my ($celvalue) = $sth->fetchrow ) {
353 my %cell = ( rowtitle => $celvalue, totalrow => 0 ); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display
354 $cell{rowtitle_display} =
355 ( $line =~ /ccode/ ) ? $ccodes->{$celvalue}
356 : ( $line =~ /location/ ) ? $locations->{$celvalue}
357 : ( $line =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description}
358 : $celvalue; # default fallback
359 if ( $line =~ /sort1/ ) {
360 foreach (@$Bsort1) {
361 ( $celvalue eq $_->{authorised_value} ) or next;
362 $cell{rowtitle_display} = $_->{lib} and last;
364 } elsif ( $line =~ /sort2/ ) {
365 foreach (@$Bsort2) {
366 ( $celvalue eq $_->{authorised_value} ) or next;
367 $cell{rowtitle_display} = $_->{lib} and last;
369 } elsif ($line =~ /category/) {
370 foreach my $patron_category ( @patron_categories ) {
371 ($celvalue eq $patron_category->categorycode) or next;
372 $cell{rowtitle_display} = $patron_category->description and last;
375 push @loopline, \%cell;
378 # 2nd, loop cols.
379 my $colfield;
380 my $colorder;
381 if ( $column =~ /datetime/ ) {
383 #Display by Day, Month or Year (1,2,3 respectively)
384 $colfield =
385 ( $dsp == 1 ) ? " dayname($column)"
386 : ( $dsp == 2 ) ? "monthname($column)"
387 : ( $dsp == 3 ) ? " Year($column)"
388 : ( $dsp == 4 ) ? "extract(hour from $column)"
389 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
390 } else {
391 $colfield = $column;
393 $colorder =
394 ( $colfield =~ /dayname/ ) ? "weekday($column)"
395 : ( $colfield =~ /^month/ ) ? " month($column)"
396 : $colfield;
397 my $strsth2;
398 if($column_attribute_type) {
399 $strsth2 = "SELECT attribute FROM borrower_attributes WHERE code = '$column_attribute_type' ";
400 } else {
401 $strsth2 = "SELECT distinctrow $colfield FROM statistics ";
403 # get stats on items if ccode or location, otherwise borrowers.
404 $strsth2 .=
405 ( $colsource eq 'items' )
406 ? "LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
407 : "LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
408 $strsth2 .= " WHERE $column IS NOT NULL AND $column != '' ";
411 if ( $column =~ /datetime/ ) {
412 if ( ( $colfilter[1] ) and ( $colfilter[0] ) ) {
413 $strsth2 .= " AND $column BETWEEN ? AND ? ";
414 } elsif ( $colfilter[1] ) {
415 $strsth2 .= " AND $column <= ? ";
416 } elsif ( $colfilter[0] ) {
417 $strsth2 .= " AND $column >= ? ";
419 $strsth2 .= " AND type ='". $type ."' " if $type;
420 $strsth2 .= " AND dayname(datetime) ='". $daysel ."' " if $daysel;
421 $strsth2 .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel;
422 } elsif ($colfilter[0]) {
423 $colfilter[0] =~ s/\*/%/g;
424 $strsth2 .= " AND $column LIKE ? " ;
427 $strsth2 .= " group by $colfield order by $colorder ";
428 $debug and warn $strsth2;
429 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth2 };
430 my $sth2 = $dbh->prepare($strsth2);
431 if ( (@colfilter) and ($colfilter[0]) and ($colfilter[1]) ) {
432 $sth2->execute( $colfilter[0], $colfilter[1] . " 23:59:59" );
433 } elsif ( $colfilter[1] ) {
434 $sth2->execute( $colfilter[1] . " 23:59:59" );
435 } elsif ( $colfilter[0] ) {
436 $sth2->execute( $colfilter[0] );
437 } else {
438 $sth2->execute;
441 while ( my ($celvalue) = $sth2->fetchrow ) {
442 my %cell = ( coltitle => $celvalue ); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display
443 $cell{coltitle_display} =
444 ( $column =~ /ccode/ ) ? $ccodes->{$celvalue}
445 : ( $column =~ /location/ ) ? $locations->{$celvalue}
446 : ( $column =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description}
447 : $celvalue; # default fallback
448 if ( $column =~ /sort1/ ) {
449 foreach (@$Bsort1) {
450 ( $celvalue eq $_->{authorised_value} ) or next;
451 $cell{coltitle_display} = $_->{lib} and last;
453 } elsif ( $column =~ /sort2/ ) {
454 foreach (@$Bsort2) {
455 ( $celvalue eq $_->{authorised_value} ) or next;
456 $cell{coltitle_display} = $_->{lib} and last;
458 } elsif ($column =~ /category/) {
459 foreach my $patron_category ( @patron_categories ) {
460 ($celvalue eq $patron_category->categorycode) or next;
461 $cell{coltitle_display} = $patron_category->description and last;
464 push @loopcol, \%cell;
467 #Initialization of cell values.....
468 my %table;
469 foreach my $row (@loopline) {
470 foreach my $col (@loopcol) {
471 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
472 table_set(\%table, $row->{rowtitle}, $col->{coltitle}, 0);
474 table_set(\%table, $row->{rowtitle}, 'totalrow', 0);
477 # preparing calculation
478 my $strcalc = "SELECT ";
479 if($line_attribute_type) {
480 $strcalc .= "TRIM(attribute_$line_attribute_type.attribute) AS line_attribute, ";
481 } else {
482 $strcalc .= "TRIM($linefield), ";
484 if($column_attribute_type) {
485 $strcalc .= "TRIM(attribute_$column_attribute_type.attribute) AS column_attribute, ";
486 } else {
487 $strcalc .= "TRIM($colfield), ";
489 $strcalc .=
490 ( $process == 1 ) ? " COUNT(*) "
491 : ( $process == 2 ) ? "(COUNT(DISTINCT borrowers.borrowernumber))"
492 : ( $process == 3 ) ? "(COUNT(DISTINCT statistics.itemnumber))"
493 : ( $process == 5 ) ? "(COUNT(DISTINCT items.biblionumber))"
494 : '';
495 if ( $process == 4 ) {
496 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
497 $rqbookcount->execute;
498 my ($bookcount) = $rqbookcount->fetchrow;
499 $strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount ";
501 $strcalc .= "
502 FROM statistics
503 LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber
505 foreach (keys %$attribute_filters) {
507 ($line_attribute_type and $line_attribute_type eq $_)
508 or $column_attribute_type and $column_attribute_type eq $_
509 or $attribute_filters->{$_}
511 $strcalc .= " LEFT JOIN borrower_attributes AS attribute_$_ ON (statistics.borrowernumber = attribute_$_.borrowernumber AND attribute_$_.code = '$_') ";
514 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber "
515 if ( $linefield =~ /^items\./
516 or $colfield =~ /^items\./
517 or $process == 5
518 or ( $colsource eq 'items' ) || @$filters[5] || @$filters[6] || @$filters[7] || @$filters[8] || @$filters[9] || @$filters[10] || @$filters[11] || @$filters[12] || @$filters[13] );
520 $strcalc .= "WHERE 1=1 ";
521 @$filters = map { defined($_) and s/\*/%/g; $_ } @$filters;
522 $strcalc .= " AND statistics.datetime >= '" . @$filters[0] . "'" if ( @$filters[0] );
523 $strcalc .= " AND statistics.datetime <= '" . @$filters[1] . " 23:59:59'" if ( @$filters[1] );
524 $strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] . "'" if ( @$filters[2] );
525 $strcalc .= " AND statistics.itemtype LIKE '" . @$filters[3] . "'" if ( @$filters[3] );
526 $strcalc .= " AND statistics.branch LIKE '" . @$filters[4] . "'" if ( @$filters[4] );
527 $strcalc .= " AND items.ccode LIKE '" . @$filters[5] . "'" if ( @$filters[5] );
528 $strcalc .= " AND items.location LIKE '" . @$filters[6] . "'" if ( @$filters[6] );
529 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] . "'" if ( @$filters[7] );
530 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] . "'" if ( @$filters[8] );
531 $strcalc .= " AND borrowers.sort1 LIKE '" . @$filters[9] . "'" if ( @$filters[9] );
532 $strcalc .= " AND borrowers.sort2 LIKE '" . @$filters[10] . "'" if ( @$filters[10] );
533 $strcalc .= " AND items.homebranch LIKE '" . @$filters[11] . "'" if ( @$filters[11] );
534 $strcalc .= " AND items.holdingbranch LIKE '" . @$filters[12] . "'" if ( @$filters[12] );
535 $strcalc .= " AND borrowers.branchcode LIKE '" . @$filters[13] . "'" if ( @$filters[13] );
536 $strcalc .= " AND dayname(datetime) LIKE '" . $daysel . "'" if ($daysel);
537 $strcalc .= " AND monthname(datetime) LIKE '" . $monthsel . "'" if ($monthsel);
538 $strcalc .= " AND statistics.type LIKE '" . $type . "'" if ($type);
539 foreach (keys %$attribute_filters) {
540 if($attribute_filters->{$_}) {
541 $strcalc .= " AND attribute_$_.attribute LIKE '" . $attribute_filters->{$_} . "'";
545 $strcalc .= " GROUP BY ";
546 if($line_attribute_type) {
547 $strcalc .= " line_attribute, ";
548 } else {
549 $strcalc .= " $linefield, ";
551 if($column_attribute_type) {
552 $strcalc .= " column_attribute ";
553 } else {
554 $strcalc .= " $colfield ";
557 $strcalc .= " ORDER BY ";
558 if($line_attribute_type) {
559 $strcalc .= " line_attribute, ";
560 } else {
561 $strcalc .= " $lineorder, ";
563 if($column_attribute_type) {
564 $strcalc .= " column_attribute ";
565 } else {
566 $strcalc .= " $colorder ";
569 ($debug) and warn $strcalc;
570 my $dbcalc = $dbh->prepare($strcalc);
571 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strcalc };
572 $dbcalc->execute;
573 my ( $emptycol, $emptyrow );
574 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
575 ($debug) and warn "filling table $row / $col / $value ";
576 unless ( defined $col ) {
577 $emptycol = 1;
579 unless ( defined $row ) {
580 $emptyrow = 1;
582 table_inc(\%table, $row, $col, $value);
583 table_inc(\%table, $row, 'totalrow', $value);
584 $grantotal += $value;
586 push @loopcol, { coltitle => "NULL", coltitle_display => 'NULL' } if ($emptycol);
587 push @loopline, { rowtitle => "NULL", rowtitle_display => 'NULL' } if ($emptyrow);
589 foreach my $row (@loopline) {
590 my @loopcell;
592 #@loopcol ensures the order for columns is common with column titles
593 # and the number matches the number of columns
594 foreach my $col (@loopcol) {
595 my $value = table_get(\%table, $row->{rowtitle}, $col->{coltitle});
596 push @loopcell, { value => $value };
598 push @looprow,
599 { 'rowtitle_display' => $row->{rowtitle_display},
600 'rowtitle' => $row->{rowtitle},
601 'loopcell' => \@loopcell,
602 'totalrow' => table_get(\%table, $row->{rowtitle}, 'totalrow'),
605 for my $col (@loopcol) {
606 my $total = 0;
607 foreach my $row (@looprow) {
608 $total += table_get(\%table, $row->{rowtitle}, $col->{coltitle}) || 0;
609 $debug and warn "value added " . table_get(\%table, $row->{rowtitle}, $col->{coltitle}) . "for line " . $row->{rowtitle};
611 push @loopfooter, { 'totalcol' => $total };
614 # the header of the table
615 $globalline{loopfilter} = \@loopfilter;
617 # the core of the table
618 $globalline{looprow} = \@looprow;
619 $globalline{loopcol} = \@loopcol;
621 # # the foot (totals by borrower type)
622 $globalline{loopfooter} = \@loopfooter;
623 $globalline{total} = $grantotal;
624 $globalline{line} = $line_attribute_type ? $line_attribute_type : $line;
625 $globalline{column} = $column_attribute_type ? $column_attribute_type : $column;
626 return [ ( \%globalline ) ];
629 sub null_to_zzempty {
630 my $string = shift;
632 if (!defined($string) or $string eq '' or uc($string) eq 'NULL') {
633 return 'zzEMPTY';
636 return $string;
639 sub table_set {
640 my ($table, $row, $col, $val) = @_;
642 $row = $row // '';
643 $col = $col // '';
644 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } = $val;
647 sub table_get {
648 my ($table, $row, $col) = @_;
650 $row = $row // '';
651 $col = $col // '';
652 return $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) };
655 sub table_inc {
656 my ($table, $row, $col, $inc) = @_;
658 $row = $row // '';
659 $col = $col // '';
660 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } += $inc;