Bug 24808: enable translation on results.js
[koha.git] / reports / issues_stats.pl
blob16a8f77bfc1a96c2c1df9703c68108f5905c44cc
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 CGI qw ( -utf8 );
23 use Date::Manip;
25 use C4::Auth;
26 use C4::Debug;
27 use C4::Context;
28 use C4::Koha;
29 use C4::Output;
30 use C4::Circulation;
31 use C4::Reports;
32 use C4::Members;
34 use Koha::AuthorisedValues;
35 use Koha::DateUtils;
36 use Koha::ItemTypes;
37 use Koha::Patron::Attribute::Types;
39 =head1 NAME
41 reports/issues_stats.pl
43 =head1 DESCRIPTION
45 Plugin that shows circulation stats
47 =cut
49 # my $debug = 1; # override for now.
50 my $input = CGI->new;
51 my $fullreportname = "reports/issues_stats.tt";
52 my $do_it = $input->param('do_it');
53 my $line = $input->param("Line");
54 my $column = $input->param("Column");
55 my @filters = $input->multi_param("Filter");
56 $filters[0] = eval { output_pref( { dt => dt_from_string( $filters[0]), dateonly => 1, dateformat => 'iso' } ); }
57 if ( $filters[0] );
58 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
59 if ( $filters[1] );
60 my $podsp = $input->param("DisplayBy");
61 my $type = $input->param("PeriodTypeSel");
62 my $daysel = $input->param("PeriodDaySel");
63 my $monthsel = $input->param("PeriodMonthSel");
64 my $calc = $input->param("Cellvalue");
65 my $output = $input->param("output");
66 my $basename = $input->param("basename");
68 my $attribute_filters;
69 my $vars = $input->Vars;
70 foreach(keys %$vars) {
71 if(/^Filter_borrower_attributes\.(.*)/) {
72 $attribute_filters->{$1} = $vars->{$_};
77 my ($template, $borrowernumber, $cookie) = get_template_and_user({
78 template_name => $fullreportname,
79 query => $input,
80 type => "intranet",
81 authnotrequired => 0,
82 flagsrequired => {reports => '*'},
83 debug => 0,
84 });
85 our $sep = $input->param("sep") // ';';
86 $sep = "\t" if ($sep eq 'tabulation');
87 $template->param(do_it => $do_it,
90 our $itemtypes = Koha::ItemTypes->search_with_localization->unblessed;
92 our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
94 our $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) };
95 our $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) };
97 our $Bsort1 = GetAuthorisedValues("Bsort1");
98 our $Bsort2 = GetAuthorisedValues("Bsort2");
99 my ($hassort1,$hassort2);
100 $hassort1=1 if $Bsort1;
101 $hassort2=1 if $Bsort2;
103 if ($do_it) {
104 # Displaying results
105 my $results = calculate( $line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters, $attribute_filters);
106 if ( $output eq "screen" ) {
108 # Printing results to screen
109 $template->param( mainloop => $results );
110 output_html_with_http_headers $input, $cookie, $template->output;
111 } else {
113 # Printing to a csv file
114 print $input->header(
115 -type => 'application/vnd.sun.xml.calc',
116 -encoding => 'utf-8',
117 -attachment => "$basename.csv",
118 -filename => "$basename.csv"
120 my $cols = @$results[0]->{loopcol};
121 my $lines = @$results[0]->{looprow};
123 # header top-right
124 print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
126 # Other header
127 foreach my $col (@$cols) {
128 print $col->{coltitle} . $sep;
130 print "Total\n";
132 # Table
133 foreach my $line (@$lines) {
134 my $x = $line->{loopcell};
135 print $line->{rowtitle} . $sep;
136 print map { $_->{value} . $sep } @$x;
137 print $line->{totalrow}, "\n";
140 # footer
141 print "TOTAL";
142 $cols = @$results[0]->{loopfooter};
143 print map {$sep.$_->{totalcol}} @$cols;
144 print $sep.@$results[0]->{total};
146 exit;
150 my $dbh = C4::Context->dbh;
151 my @values;
152 my %labels;
153 my %select;
155 # location list
156 my @locations;
157 foreach (sort keys %$locations) {
158 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
161 my @ccodes;
162 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
163 push @ccodes, { code => $_, description => $ccodes->{$_} };
166 my $CGIextChoice = ( 'CSV' ); # FIXME translation
167 my $CGIsepChoice=GetDelimiterChoices;
169 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
170 my $attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
171 my %attribute_types_by_class;
172 while ( my ( $attribute_type ) = $attribute_types->next ) {
173 $attribute_type = $attribute_type->unblessed;
174 if ($attribute_type->{authorised_value_category}) {
175 my $authorised_values = C4::Koha::GetAuthorisedValues(
176 $attribute_type->{authorised_value_category});
178 foreach my $authorised_value (@$authorised_values) {
179 push @{ $attribute_type->{authorised_values} }, $authorised_value;
182 push @{ $attribute_types_by_class{$attribute_type->{class}} }, $attribute_type;
185 $template->param(
186 categoryloop => \@patron_categories,
187 itemtypes => $itemtypes,
188 locationloop => \@locations,
189 ccodeloop => \@ccodes,
190 hassort1 => $hassort1,
191 hassort2 => $hassort2,
192 Bsort1 => $Bsort1,
193 Bsort2 => $Bsort2,
194 CGIextChoice => $CGIextChoice,
195 CGIsepChoice => $CGIsepChoice,
196 attribute_types_by_class => \%attribute_types_by_class,
198 output_html_with_http_headers $input, $cookie, $template->output;
200 sub calculate {
201 my ( $line, $column, $dsp, $type, $daysel, $monthsel, $process, $filters, $attribute_filters ) = @_;
202 my @loopfooter;
203 my @loopcol;
204 my @loopline;
205 my @looprow;
206 my %globalline;
207 my $grantotal = 0;
209 # extract parameters
210 my $dbh = C4::Context->dbh;
212 my ($line_attribute_type, $column_attribute_type);
213 if($line =~ /^borrower_attributes\.(.*)/) {
214 $line_attribute_type = $1;
215 $line = "borrower_attributes.attribute";
217 if($column =~ /^borrower_attributes\.(.*)/) {
218 $column_attribute_type = $1;
219 $column = "borrower_attributes.attribute";
222 # Filters
223 # Checking filters
225 my @loopfilter;
226 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
227 my %cell;
228 ( @$filters[$i] ) or next;
229 if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
230 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
232 # format the dates filters, otherwise just fill as is
233 if ($i>=2) {
234 $cell{filter} = @$filters[$i];
235 } else {
236 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
237 if ( @$filters[$i] );
239 $cell{crit} = $i;
241 push @loopfilter, \%cell;
243 foreach (keys %$attribute_filters) {
244 next unless $attribute_filters->{$_};
245 push @loopfilter, { crit => "$_ =", filter => $attribute_filters->{$_} };
247 push @loopfilter, { crit => "Event", filter => $type };
248 push @loopfilter, { crit => "Display by", filter => $dsp } if ($dsp);
249 push @loopfilter, { crit => "Select Day", filter => $daysel } if ($daysel);
250 push @loopfilter, { crit => "Select Month", filter => $monthsel } if ($monthsel);
252 my @linefilter;
253 $debug and warn "filtres " . join "|", @$filters;
254 my ( $colsource, $linesource ) = ('', '');
255 $linefilter[1] = @$filters[1] if ( $line =~ /datetime/ );
256 $linefilter[0] =
257 ( $line =~ /datetime/ ) ? @$filters[0]
258 : ( $line =~ /category/ ) ? @$filters[2]
259 : ( $line =~ /itemtype/ ) ? @$filters[3]
260 : ( $line =~ /^branch/ ) ? @$filters[4]
261 : ( $line =~ /ccode/ ) ? @$filters[5]
262 : ( $line =~ /location/ ) ? @$filters[6]
263 : ( $line =~ /sort1/ ) ? @$filters[9]
264 : ( $line =~ /sort2/ ) ? @$filters[10]
265 : ( $line =~ /homebranch/) ? @$filters[11]
266 : ( $line =~ /holdingbranch/) ? @$filters[12]
267 : ( $line =~ /borrowers.branchcode/ ) ? @$filters[13]
268 : ( $line_attribute_type ) ? $attribute_filters->{$line_attribute_type}
269 : undef;
271 if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) {
272 $linesource = 'items';
275 my @colfilter;
276 $colfilter[1] = @$filters[1] if ($column =~ /datetime/);
277 $colfilter[0] =
278 ( $column =~ /datetime/ ) ? @$filters[0]
279 : ( $column =~ /category/ ) ? @$filters[2]
280 : ( $column =~ /itemtype/ ) ? @$filters[3]
281 : ( $column =~ /^branch/ ) ? @$filters[4]
282 : ( $column =~ /ccode/ ) ? @$filters[5]
283 : ( $column =~ /location/ ) ? @$filters[6]
284 : ( $column =~ /sort1/ ) ? @$filters[9]
285 : ( $column =~ /sort1/ ) ? @$filters[10]
286 : ( $column =~ /homebranch/) ? @$filters[11]
287 : ( $column =~ /holdingbranch/) ? @$filters[12]
288 : ( $column =~ /borrowers.branchcode/ ) ? @$filters[13]
289 : ( $column_attribute_type ) ? $attribute_filters->{$column_attribute_type}
290 : undef;
292 if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) {
293 $colsource = 'items';
296 # 1st, loop rows.
297 my $linefield;
298 if ( $line =~ /datetime/ ) {
300 # by Day, Month, Year or Hour (1,2,3,4 respectively)
301 $linefield =
302 ( $dsp == 1 ) ? " dayname($line)"
303 : ( $dsp == 2 ) ? "monthname($line)"
304 : ( $dsp == 3 ) ? " Year($line)"
305 : ( $dsp == 4 ) ? "extract(hour from $line)"
306 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
307 } else {
308 $linefield = $line;
310 my $lineorder =
311 ( $linefield =~ /dayname/ ) ? "weekday($line)"
312 : ( $linefield =~ /^month/ ) ? " month($line)"
313 : $linefield;
315 my $strsth;
316 if($line_attribute_type) {
317 $strsth = "SELECT attribute FROM borrower_attributes WHERE code = '$line_attribute_type' ";
318 } else {
319 $strsth = "SELECT distinctrow $linefield FROM statistics ";
321 # get stats on items if ccode or location, otherwise borrowers.
322 $strsth .=
323 ( $linesource eq 'items' )
324 ? " LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
325 : " LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
326 $strsth .= " WHERE $line is not null AND $line != '' ";
329 if ( $line =~ /datetime/ ) {
330 if ( $linefilter[1] and ( $linefilter[0] ) ) {
331 $strsth .= " AND $line between ? AND ? ";
332 } elsif ( $linefilter[1] ) {
333 $strsth .= " AND $line <= ? ";
334 } elsif ( $linefilter[0] ) {
335 $strsth .= " AND $line >= ? ";
337 $strsth .= " AND type ='" . $type . "' " if $type;
338 $strsth .= " AND dayname(datetime) ='" . $daysel . "' " if $daysel;
339 $strsth .= " AND monthname(datetime) ='" . $monthsel . "' " if $monthsel;
340 } elsif ( $linefilter[0] ) {
341 $linefilter[0] =~ s/\*/%/g;
342 $strsth .= " AND $line LIKE ? ";
344 $strsth .= " group by $linefield order by $lineorder ";
345 $debug and warn $strsth;
346 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth };
347 my $sth = $dbh->prepare($strsth);
348 if ( (@linefilter) and ($linefilter[0]) and ($linefilter[1]) ) {
349 $sth->execute( $linefilter[0], $linefilter[1] . " 23:59:59" );
350 } elsif ( $linefilter[1] ) {
351 $sth->execute( $linefilter[1] . " 23:59:59" );
352 } elsif ( $linefilter[0] ) {
353 $sth->execute( $linefilter[0] );
354 } else {
355 $sth->execute;
358 my $itemtypes_map = { map { $_->{itemtype} => $_ } @{ $itemtypes } };
359 while ( my ($celvalue) = $sth->fetchrow ) {
360 my %cell = ( rowtitle => $celvalue, totalrow => 0 ); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display
361 $cell{rowtitle_display} =
362 ( $line =~ /ccode/ ) ? $ccodes->{$celvalue}
363 : ( $line =~ /location/ ) ? $locations->{$celvalue}
364 : ( $line =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
365 : $celvalue; # default fallback
366 if ( $line =~ /sort1/ ) {
367 foreach (@$Bsort1) {
368 ( $celvalue eq $_->{authorised_value} ) or next;
369 $cell{rowtitle_display} = $_->{lib} and last;
371 } elsif ( $line =~ /sort2/ ) {
372 foreach (@$Bsort2) {
373 ( $celvalue eq $_->{authorised_value} ) or next;
374 $cell{rowtitle_display} = $_->{lib} and last;
376 } elsif ($line =~ /category/) {
377 foreach my $patron_category ( @patron_categories ) {
378 ($celvalue eq $patron_category->categorycode) or next;
379 $cell{rowtitle_display} = $patron_category->description and last;
382 push @loopline, \%cell;
385 # 2nd, loop cols.
386 my $colfield;
387 my $colorder;
388 if ( $column =~ /datetime/ ) {
390 #Display by Day, Month or Year (1,2,3 respectively)
391 $colfield =
392 ( $dsp == 1 ) ? " dayname($column)"
393 : ( $dsp == 2 ) ? "monthname($column)"
394 : ( $dsp == 3 ) ? " Year($column)"
395 : ( $dsp == 4 ) ? "extract(hour from $column)"
396 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
397 } else {
398 $colfield = $column;
400 $colorder =
401 ( $colfield =~ /dayname/ ) ? "weekday($column)"
402 : ( $colfield =~ /^month/ ) ? " month($column)"
403 : $colfield;
404 my $strsth2;
405 if($column_attribute_type) {
406 $strsth2 = "SELECT attribute FROM borrower_attributes WHERE code = '$column_attribute_type' ";
407 } else {
408 $strsth2 = "SELECT distinctrow $colfield FROM statistics ";
410 # get stats on items if ccode or location, otherwise borrowers.
411 $strsth2 .=
412 ( $colsource eq 'items' )
413 ? "LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
414 : "LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
415 $strsth2 .= " WHERE $column IS NOT NULL AND $column != '' ";
418 if ( $column =~ /datetime/ ) {
419 if ( ( $colfilter[1] ) and ( $colfilter[0] ) ) {
420 $strsth2 .= " AND $column BETWEEN ? AND ? ";
421 } elsif ( $colfilter[1] ) {
422 $strsth2 .= " AND $column <= ? ";
423 } elsif ( $colfilter[0] ) {
424 $strsth2 .= " AND $column >= ? ";
426 $strsth2 .= " AND type ='". $type ."' " if $type;
427 $strsth2 .= " AND dayname(datetime) ='". $daysel ."' " if $daysel;
428 $strsth2 .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel;
429 } elsif ($colfilter[0]) {
430 $colfilter[0] =~ s/\*/%/g;
431 $strsth2 .= " AND $column LIKE ? " ;
434 $strsth2 .= " group by $colfield order by $colorder ";
435 $debug and warn $strsth2;
436 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth2 };
437 my $sth2 = $dbh->prepare($strsth2);
438 if ( (@colfilter) and ($colfilter[0]) and ($colfilter[1]) ) {
439 $sth2->execute( $colfilter[0], $colfilter[1] . " 23:59:59" );
440 } elsif ( $colfilter[1] ) {
441 $sth2->execute( $colfilter[1] . " 23:59:59" );
442 } elsif ( $colfilter[0] ) {
443 $sth2->execute( $colfilter[0] );
444 } else {
445 $sth2->execute;
448 while ( my ($celvalue) = $sth2->fetchrow ) {
449 my %cell = ( coltitle => $celvalue ); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display
450 $cell{coltitle_display} =
451 ( $column =~ /ccode/ ) ? $ccodes->{$celvalue}
452 : ( $column =~ /location/ ) ? $locations->{$celvalue}
453 : ( $column =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
454 : $celvalue; # default fallback
455 if ( $column =~ /sort1/ ) {
456 foreach (@$Bsort1) {
457 ( $celvalue eq $_->{authorised_value} ) or next;
458 $cell{coltitle_display} = $_->{lib} and last;
460 } elsif ( $column =~ /sort2/ ) {
461 foreach (@$Bsort2) {
462 ( $celvalue eq $_->{authorised_value} ) or next;
463 $cell{coltitle_display} = $_->{lib} and last;
465 } elsif ($column =~ /category/) {
466 foreach my $patron_category ( @patron_categories ) {
467 ($celvalue eq $patron_category->categorycode) or next;
468 $cell{coltitle_display} = $patron_category->description and last;
471 push @loopcol, \%cell;
474 #Initialization of cell values.....
475 my %table;
476 foreach my $row (@loopline) {
477 foreach my $col (@loopcol) {
478 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
479 table_set(\%table, $row->{rowtitle}, $col->{coltitle}, 0);
481 table_set(\%table, $row->{rowtitle}, 'totalrow', 0);
484 # preparing calculation
485 my $strcalc = "SELECT ";
486 if($line_attribute_type) {
487 $strcalc .= "TRIM(attribute_$line_attribute_type.attribute) AS line_attribute, ";
488 } else {
489 $strcalc .= "TRIM($linefield), ";
491 if($column_attribute_type) {
492 $strcalc .= "TRIM(attribute_$column_attribute_type.attribute) AS column_attribute, ";
493 } else {
494 $strcalc .= "TRIM($colfield), ";
496 $strcalc .=
497 ( $process == 1 ) ? " COUNT(*) "
498 : ( $process == 2 ) ? "(COUNT(DISTINCT borrowers.borrowernumber))"
499 : ( $process == 3 ) ? "(COUNT(DISTINCT statistics.itemnumber))"
500 : ( $process == 5 ) ? "(COUNT(DISTINCT items.biblionumber))"
501 : '';
502 if ( $process == 4 ) {
503 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
504 $rqbookcount->execute;
505 my ($bookcount) = $rqbookcount->fetchrow;
506 $strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount ";
508 $strcalc .= "
509 FROM statistics
510 LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber
512 foreach (keys %$attribute_filters) {
514 ($line_attribute_type and $line_attribute_type eq $_)
515 or $column_attribute_type and $column_attribute_type eq $_
516 or $attribute_filters->{$_}
518 $strcalc .= " LEFT JOIN borrower_attributes AS attribute_$_ ON (statistics.borrowernumber = attribute_$_.borrowernumber AND attribute_$_.code = '$_') ";
521 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber "
522 if ( $linefield =~ /^items\./
523 or $colfield =~ /^items\./
524 or $process == 5
525 or ( $colsource eq 'items' ) || @$filters[5] || @$filters[6] || @$filters[7] || @$filters[8] || @$filters[9] || @$filters[10] || @$filters[11] || @$filters[12] || @$filters[13] );
527 $strcalc .= "WHERE 1=1 ";
528 @$filters = map { defined($_) and s/\*/%/g; $_ } @$filters;
529 $strcalc .= " AND statistics.datetime >= '" . @$filters[0] . "'" if ( @$filters[0] );
530 $strcalc .= " AND statistics.datetime <= '" . @$filters[1] . " 23:59:59'" if ( @$filters[1] );
531 $strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] . "'" if ( @$filters[2] );
532 $strcalc .= " AND statistics.itemtype LIKE '" . @$filters[3] . "'" if ( @$filters[3] );
533 $strcalc .= " AND statistics.branch LIKE '" . @$filters[4] . "'" if ( @$filters[4] );
534 $strcalc .= " AND items.ccode LIKE '" . @$filters[5] . "'" if ( @$filters[5] );
535 $strcalc .= " AND items.location LIKE '" . @$filters[6] . "'" if ( @$filters[6] );
536 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] . "'" if ( @$filters[7] );
537 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] . "'" if ( @$filters[8] );
538 $strcalc .= " AND borrowers.sort1 LIKE '" . @$filters[9] . "'" if ( @$filters[9] );
539 $strcalc .= " AND borrowers.sort2 LIKE '" . @$filters[10] . "'" if ( @$filters[10] );
540 $strcalc .= " AND items.homebranch LIKE '" . @$filters[11] . "'" if ( @$filters[11] );
541 $strcalc .= " AND items.holdingbranch LIKE '" . @$filters[12] . "'" if ( @$filters[12] );
542 $strcalc .= " AND borrowers.branchcode LIKE '" . @$filters[13] . "'" if ( @$filters[13] );
543 $strcalc .= " AND dayname(datetime) LIKE '" . $daysel . "'" if ($daysel);
544 $strcalc .= " AND monthname(datetime) LIKE '" . $monthsel . "'" if ($monthsel);
545 $strcalc .= " AND statistics.type LIKE '" . $type . "'" if ($type);
546 foreach (keys %$attribute_filters) {
547 if($attribute_filters->{$_}) {
548 $strcalc .= " AND attribute_$_.attribute LIKE '" . $attribute_filters->{$_} . "'";
552 $strcalc .= " GROUP BY ";
553 if($line_attribute_type) {
554 $strcalc .= " line_attribute, ";
555 } else {
556 $strcalc .= " $linefield, ";
558 if($column_attribute_type) {
559 $strcalc .= " column_attribute ";
560 } else {
561 $strcalc .= " $colfield ";
564 $strcalc .= " ORDER BY ";
565 if($line_attribute_type) {
566 $strcalc .= " line_attribute, ";
567 } else {
568 $strcalc .= " $lineorder, ";
570 if($column_attribute_type) {
571 $strcalc .= " column_attribute ";
572 } else {
573 $strcalc .= " $colorder ";
576 ($debug) and warn $strcalc;
577 my $dbcalc = $dbh->prepare($strcalc);
578 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strcalc };
579 $dbcalc->execute;
580 my ( $emptycol, $emptyrow );
581 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
582 ($debug) and warn "filling table $row / $col / $value ";
583 unless ( defined $col ) {
584 $emptycol = 1;
586 unless ( defined $row ) {
587 $emptyrow = 1;
589 table_inc(\%table, $row, $col, $value);
590 table_inc(\%table, $row, 'totalrow', $value);
591 $grantotal += $value;
593 push @loopcol, { coltitle => "NULL", coltitle_display => 'NULL' } if ($emptycol);
594 push @loopline, { rowtitle => "NULL", rowtitle_display => 'NULL' } if ($emptyrow);
596 foreach my $row (@loopline) {
597 my @loopcell;
599 #@loopcol ensures the order for columns is common with column titles
600 # and the number matches the number of columns
601 foreach my $col (@loopcol) {
602 my $value = table_get(\%table, $row->{rowtitle}, $col->{coltitle});
603 push @loopcell, { value => $value };
605 push @looprow,
606 { 'rowtitle_display' => $row->{rowtitle_display},
607 'rowtitle' => $row->{rowtitle},
608 'loopcell' => \@loopcell,
609 'totalrow' => table_get(\%table, $row->{rowtitle}, 'totalrow'),
612 for my $col (@loopcol) {
613 my $total = 0;
614 foreach my $row (@looprow) {
615 $total += table_get(\%table, $row->{rowtitle}, $col->{coltitle}) || 0;
616 $debug and warn "value added " . table_get(\%table, $row->{rowtitle}, $col->{coltitle}) . "for line " . $row->{rowtitle};
618 push @loopfooter, { 'totalcol' => $total };
621 # the header of the table
622 $globalline{loopfilter} = \@loopfilter;
624 # the core of the table
625 $globalline{looprow} = \@looprow;
626 $globalline{loopcol} = \@loopcol;
628 # # the foot (totals by borrower type)
629 $globalline{loopfooter} = \@loopfooter;
630 $globalline{total} = $grantotal;
631 $globalline{line} = $line_attribute_type ? $line_attribute_type : $line;
632 $globalline{column} = $column_attribute_type ? $column_attribute_type : $column;
633 return [ ( \%globalline ) ];
636 sub null_to_zzempty {
637 my $string = shift;
639 if (!defined($string) or $string eq '' or uc($string) eq 'NULL') {
640 return 'zzEMPTY';
643 return $string;
646 sub table_set {
647 my ($table, $row, $col, $val) = @_;
649 $row = $row // '';
650 $col = $col // '';
651 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } = $val;
654 sub table_get {
655 my ($table, $row, $col) = @_;
657 $row = $row // '';
658 $col = $col // '';
659 return $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) };
662 sub table_inc {
663 my ($table, $row, $col, $inc) = @_;
665 $row = $row // '';
666 $col = $col // '';
667 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } += $inc;