Bug 21637: Fixed upercase letter in EasyAnalyticalRecords syspref
[koha.git] / reports / issues_stats.pl
blob3c1ba1facb18efe5c156aa8d8986c081317fe66a
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 C4::Members::AttributeTypes;
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 @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes(1);
170 my %attribute_types_by_class;
171 foreach my $attribute_type (@attribute_types) {
172 if ($attribute_type->{authorised_value_category}) {
173 my $authorised_values = C4::Koha::GetAuthorisedValues(
174 $attribute_type->{authorised_value_category});
176 foreach my $authorised_value (@$authorised_values) {
177 push @{ $attribute_type->{authorised_values} }, $authorised_value;
180 push @{ $attribute_types_by_class{$attribute_type->{class}} }, $attribute_type;
183 $template->param(
184 categoryloop => \@patron_categories,
185 itemtypes => $itemtypes,
186 locationloop => \@locations,
187 ccodeloop => \@ccodes,
188 hassort1 => $hassort1,
189 hassort2 => $hassort2,
190 Bsort1 => $Bsort1,
191 Bsort2 => $Bsort2,
192 CGIextChoice => $CGIextChoice,
193 CGIsepChoice => $CGIsepChoice,
194 attribute_types_by_class => \%attribute_types_by_class,
196 output_html_with_http_headers $input, $cookie, $template->output;
198 sub calculate {
199 my ( $line, $column, $dsp, $type, $daysel, $monthsel, $process, $filters, $attribute_filters ) = @_;
200 my @loopfooter;
201 my @loopcol;
202 my @loopline;
203 my @looprow;
204 my %globalline;
205 my $grantotal = 0;
207 # extract parameters
208 my $dbh = C4::Context->dbh;
210 my ($line_attribute_type, $column_attribute_type);
211 if($line =~ /^borrower_attributes\.(.*)/) {
212 $line_attribute_type = $1;
213 $line = "borrower_attributes.attribute";
215 if($column =~ /^borrower_attributes\.(.*)/) {
216 $column_attribute_type = $1;
217 $column = "borrower_attributes.attribute";
220 # Filters
221 # Checking filters
223 my @loopfilter;
224 for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
225 my %cell;
226 ( @$filters[$i] ) or next;
227 if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
228 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
230 # format the dates filters, otherwise just fill as is
231 if ($i>=2) {
232 $cell{filter} = @$filters[$i];
233 } else {
234 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
235 if ( @$filters[$i] );
237 $cell{crit} = $i;
239 push @loopfilter, \%cell;
241 foreach (keys %$attribute_filters) {
242 next unless $attribute_filters->{$_};
243 push @loopfilter, { crit => "$_ =", filter => $attribute_filters->{$_} };
245 push @loopfilter, { crit => "Event", filter => $type };
246 push @loopfilter, { crit => "Display by", filter => $dsp } if ($dsp);
247 push @loopfilter, { crit => "Select Day", filter => $daysel } if ($daysel);
248 push @loopfilter, { crit => "Select Month", filter => $monthsel } if ($monthsel);
250 my @linefilter;
251 $debug and warn "filtres " . join "|", @$filters;
252 my ( $colsource, $linesource ) = ('', '');
253 $linefilter[1] = @$filters[1] if ( $line =~ /datetime/ );
254 $linefilter[0] =
255 ( $line =~ /datetime/ ) ? @$filters[0]
256 : ( $line =~ /category/ ) ? @$filters[2]
257 : ( $line =~ /itemtype/ ) ? @$filters[3]
258 : ( $line =~ /^branch/ ) ? @$filters[4]
259 : ( $line =~ /ccode/ ) ? @$filters[5]
260 : ( $line =~ /location/ ) ? @$filters[6]
261 : ( $line =~ /sort1/ ) ? @$filters[9]
262 : ( $line =~ /sort2/ ) ? @$filters[10]
263 : ( $line =~ /homebranch/) ? @$filters[11]
264 : ( $line =~ /holdingbranch/) ? @$filters[12]
265 : ( $line =~ /borrowers.branchcode/ ) ? @$filters[13]
266 : ( $line_attribute_type ) ? $attribute_filters->{$line_attribute_type}
267 : undef;
269 if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) {
270 $linesource = 'items';
273 my @colfilter;
274 $colfilter[1] = @$filters[1] if ($column =~ /datetime/);
275 $colfilter[0] =
276 ( $column =~ /datetime/ ) ? @$filters[0]
277 : ( $column =~ /category/ ) ? @$filters[2]
278 : ( $column =~ /itemtype/ ) ? @$filters[3]
279 : ( $column =~ /^branch/ ) ? @$filters[4]
280 : ( $column =~ /ccode/ ) ? @$filters[5]
281 : ( $column =~ /location/ ) ? @$filters[6]
282 : ( $column =~ /sort1/ ) ? @$filters[9]
283 : ( $column =~ /sort1/ ) ? @$filters[10]
284 : ( $column =~ /homebranch/) ? @$filters[11]
285 : ( $column =~ /holdingbranch/) ? @$filters[12]
286 : ( $column =~ /borrowers.branchcode/ ) ? @$filters[13]
287 : ( $column_attribute_type ) ? $attribute_filters->{$column_attribute_type}
288 : undef;
290 if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) {
291 $colsource = 'items';
294 # 1st, loop rows.
295 my $linefield;
296 if ( $line =~ /datetime/ ) {
298 # by Day, Month, Year or Hour (1,2,3,4 respectively)
299 $linefield =
300 ( $dsp == 1 ) ? " dayname($line)"
301 : ( $dsp == 2 ) ? "monthname($line)"
302 : ( $dsp == 3 ) ? " Year($line)"
303 : ( $dsp == 4 ) ? "extract(hour from $line)"
304 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
305 } else {
306 $linefield = $line;
308 my $lineorder =
309 ( $linefield =~ /dayname/ ) ? "weekday($line)"
310 : ( $linefield =~ /^month/ ) ? " month($line)"
311 : $linefield;
313 my $strsth;
314 if($line_attribute_type) {
315 $strsth = "SELECT attribute FROM borrower_attributes WHERE code = '$line_attribute_type' ";
316 } else {
317 $strsth = "SELECT distinctrow $linefield FROM statistics ";
319 # get stats on items if ccode or location, otherwise borrowers.
320 $strsth .=
321 ( $linesource eq 'items' )
322 ? " LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
323 : " LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
324 $strsth .= " WHERE $line is not null AND $line != '' ";
327 if ( $line =~ /datetime/ ) {
328 if ( $linefilter[1] and ( $linefilter[0] ) ) {
329 $strsth .= " AND $line between ? AND ? ";
330 } elsif ( $linefilter[1] ) {
331 $strsth .= " AND $line <= ? ";
332 } elsif ( $linefilter[0] ) {
333 $strsth .= " AND $line >= ? ";
335 $strsth .= " AND type ='" . $type . "' " if $type;
336 $strsth .= " AND dayname(datetime) ='" . $daysel . "' " if $daysel;
337 $strsth .= " AND monthname(datetime) ='" . $monthsel . "' " if $monthsel;
338 } elsif ( $linefilter[0] ) {
339 $linefilter[0] =~ s/\*/%/g;
340 $strsth .= " AND $line LIKE ? ";
342 $strsth .= " group by $linefield order by $lineorder ";
343 $debug and warn $strsth;
344 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth };
345 my $sth = $dbh->prepare($strsth);
346 if ( (@linefilter) and ($linefilter[0]) and ($linefilter[1]) ) {
347 $sth->execute( $linefilter[0], $linefilter[1] . " 23:59:59" );
348 } elsif ( $linefilter[1] ) {
349 $sth->execute( $linefilter[1] . " 23:59:59" );
350 } elsif ( $linefilter[0] ) {
351 $sth->execute( $linefilter[0] );
352 } else {
353 $sth->execute;
356 my $itemtypes_map = { map { $_->{itemtype} => $_ } @{ $itemtypes } };
357 while ( my ($celvalue) = $sth->fetchrow ) {
358 my %cell = ( rowtitle => $celvalue, totalrow => 0 ); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display
359 $cell{rowtitle_display} =
360 ( $line =~ /ccode/ ) ? $ccodes->{$celvalue}
361 : ( $line =~ /location/ ) ? $locations->{$celvalue}
362 : ( $line =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
363 : $celvalue; # default fallback
364 if ( $line =~ /sort1/ ) {
365 foreach (@$Bsort1) {
366 ( $celvalue eq $_->{authorised_value} ) or next;
367 $cell{rowtitle_display} = $_->{lib} and last;
369 } elsif ( $line =~ /sort2/ ) {
370 foreach (@$Bsort2) {
371 ( $celvalue eq $_->{authorised_value} ) or next;
372 $cell{rowtitle_display} = $_->{lib} and last;
374 } elsif ($line =~ /category/) {
375 foreach my $patron_category ( @patron_categories ) {
376 ($celvalue eq $patron_category->categorycode) or next;
377 $cell{rowtitle_display} = $patron_category->description and last;
380 push @loopline, \%cell;
383 # 2nd, loop cols.
384 my $colfield;
385 my $colorder;
386 if ( $column =~ /datetime/ ) {
388 #Display by Day, Month or Year (1,2,3 respectively)
389 $colfield =
390 ( $dsp == 1 ) ? " dayname($column)"
391 : ( $dsp == 2 ) ? "monthname($column)"
392 : ( $dsp == 3 ) ? " Year($column)"
393 : ( $dsp == 4 ) ? "extract(hour from $column)"
394 : 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates
395 } else {
396 $colfield = $column;
398 $colorder =
399 ( $colfield =~ /dayname/ ) ? "weekday($column)"
400 : ( $colfield =~ /^month/ ) ? " month($column)"
401 : $colfield;
402 my $strsth2;
403 if($column_attribute_type) {
404 $strsth2 = "SELECT attribute FROM borrower_attributes WHERE code = '$column_attribute_type' ";
405 } else {
406 $strsth2 = "SELECT distinctrow $colfield FROM statistics ";
408 # get stats on items if ccode or location, otherwise borrowers.
409 $strsth2 .=
410 ( $colsource eq 'items' )
411 ? "LEFT JOIN items ON (statistics.itemnumber = items.itemnumber) "
412 : "LEFT JOIN borrowers ON (statistics.borrowernumber = borrowers.borrowernumber) ";
413 $strsth2 .= " WHERE $column IS NOT NULL AND $column != '' ";
416 if ( $column =~ /datetime/ ) {
417 if ( ( $colfilter[1] ) and ( $colfilter[0] ) ) {
418 $strsth2 .= " AND $column BETWEEN ? AND ? ";
419 } elsif ( $colfilter[1] ) {
420 $strsth2 .= " AND $column <= ? ";
421 } elsif ( $colfilter[0] ) {
422 $strsth2 .= " AND $column >= ? ";
424 $strsth2 .= " AND type ='". $type ."' " if $type;
425 $strsth2 .= " AND dayname(datetime) ='". $daysel ."' " if $daysel;
426 $strsth2 .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel;
427 } elsif ($colfilter[0]) {
428 $colfilter[0] =~ s/\*/%/g;
429 $strsth2 .= " AND $column LIKE ? " ;
432 $strsth2 .= " group by $colfield order by $colorder ";
433 $debug and warn $strsth2;
434 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth2 };
435 my $sth2 = $dbh->prepare($strsth2);
436 if ( (@colfilter) and ($colfilter[0]) and ($colfilter[1]) ) {
437 $sth2->execute( $colfilter[0], $colfilter[1] . " 23:59:59" );
438 } elsif ( $colfilter[1] ) {
439 $sth2->execute( $colfilter[1] . " 23:59:59" );
440 } elsif ( $colfilter[0] ) {
441 $sth2->execute( $colfilter[0] );
442 } else {
443 $sth2->execute;
446 while ( my ($celvalue) = $sth2->fetchrow ) {
447 my %cell = ( coltitle => $celvalue ); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display
448 $cell{coltitle_display} =
449 ( $column =~ /ccode/ ) ? $ccodes->{$celvalue}
450 : ( $column =~ /location/ ) ? $locations->{$celvalue}
451 : ( $column =~ /itemtype/ ) ? $itemtypes_map->{$celvalue}->{translated_description}
452 : $celvalue; # default fallback
453 if ( $column =~ /sort1/ ) {
454 foreach (@$Bsort1) {
455 ( $celvalue eq $_->{authorised_value} ) or next;
456 $cell{coltitle_display} = $_->{lib} and last;
458 } elsif ( $column =~ /sort2/ ) {
459 foreach (@$Bsort2) {
460 ( $celvalue eq $_->{authorised_value} ) or next;
461 $cell{coltitle_display} = $_->{lib} and last;
463 } elsif ($column =~ /category/) {
464 foreach my $patron_category ( @patron_categories ) {
465 ($celvalue eq $patron_category->categorycode) or next;
466 $cell{coltitle_display} = $patron_category->description and last;
469 push @loopcol, \%cell;
472 #Initialization of cell values.....
473 my %table;
474 foreach my $row (@loopline) {
475 foreach my $col (@loopcol) {
476 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) ";
477 table_set(\%table, $row->{rowtitle}, $col->{coltitle}, 0);
479 table_set(\%table, $row->{rowtitle}, 'totalrow', 0);
482 # preparing calculation
483 my $strcalc = "SELECT ";
484 if($line_attribute_type) {
485 $strcalc .= "TRIM(attribute_$line_attribute_type.attribute) AS line_attribute, ";
486 } else {
487 $strcalc .= "TRIM($linefield), ";
489 if($column_attribute_type) {
490 $strcalc .= "TRIM(attribute_$column_attribute_type.attribute) AS column_attribute, ";
491 } else {
492 $strcalc .= "TRIM($colfield), ";
494 $strcalc .=
495 ( $process == 1 ) ? " COUNT(*) "
496 : ( $process == 2 ) ? "(COUNT(DISTINCT borrowers.borrowernumber))"
497 : ( $process == 3 ) ? "(COUNT(DISTINCT statistics.itemnumber))"
498 : ( $process == 5 ) ? "(COUNT(DISTINCT items.biblionumber))"
499 : '';
500 if ( $process == 4 ) {
501 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
502 $rqbookcount->execute;
503 my ($bookcount) = $rqbookcount->fetchrow;
504 $strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount ";
506 $strcalc .= "
507 FROM statistics
508 LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber
510 foreach (keys %$attribute_filters) {
512 ($line_attribute_type and $line_attribute_type eq $_)
513 or $column_attribute_type and $column_attribute_type eq $_
514 or $attribute_filters->{$_}
516 $strcalc .= " LEFT JOIN borrower_attributes AS attribute_$_ ON (statistics.borrowernumber = attribute_$_.borrowernumber AND attribute_$_.code = '$_') ";
519 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber "
520 if ( $linefield =~ /^items\./
521 or $colfield =~ /^items\./
522 or $process == 5
523 or ( $colsource eq 'items' ) || @$filters[5] || @$filters[6] || @$filters[7] || @$filters[8] || @$filters[9] || @$filters[10] || @$filters[11] || @$filters[12] || @$filters[13] );
525 $strcalc .= "WHERE 1=1 ";
526 @$filters = map { defined($_) and s/\*/%/g; $_ } @$filters;
527 $strcalc .= " AND statistics.datetime >= '" . @$filters[0] . "'" if ( @$filters[0] );
528 $strcalc .= " AND statistics.datetime <= '" . @$filters[1] . " 23:59:59'" if ( @$filters[1] );
529 $strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] . "'" if ( @$filters[2] );
530 $strcalc .= " AND statistics.itemtype LIKE '" . @$filters[3] . "'" if ( @$filters[3] );
531 $strcalc .= " AND statistics.branch LIKE '" . @$filters[4] . "'" if ( @$filters[4] );
532 $strcalc .= " AND items.ccode LIKE '" . @$filters[5] . "'" if ( @$filters[5] );
533 $strcalc .= " AND items.location LIKE '" . @$filters[6] . "'" if ( @$filters[6] );
534 $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] . "'" if ( @$filters[7] );
535 $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] . "'" if ( @$filters[8] );
536 $strcalc .= " AND borrowers.sort1 LIKE '" . @$filters[9] . "'" if ( @$filters[9] );
537 $strcalc .= " AND borrowers.sort2 LIKE '" . @$filters[10] . "'" if ( @$filters[10] );
538 $strcalc .= " AND items.homebranch LIKE '" . @$filters[11] . "'" if ( @$filters[11] );
539 $strcalc .= " AND items.holdingbranch LIKE '" . @$filters[12] . "'" if ( @$filters[12] );
540 $strcalc .= " AND borrowers.branchcode LIKE '" . @$filters[13] . "'" if ( @$filters[13] );
541 $strcalc .= " AND dayname(datetime) LIKE '" . $daysel . "'" if ($daysel);
542 $strcalc .= " AND monthname(datetime) LIKE '" . $monthsel . "'" if ($monthsel);
543 $strcalc .= " AND statistics.type LIKE '" . $type . "'" if ($type);
544 foreach (keys %$attribute_filters) {
545 if($attribute_filters->{$_}) {
546 $strcalc .= " AND attribute_$_.attribute LIKE '" . $attribute_filters->{$_} . "'";
550 $strcalc .= " GROUP BY ";
551 if($line_attribute_type) {
552 $strcalc .= " line_attribute, ";
553 } else {
554 $strcalc .= " $linefield, ";
556 if($column_attribute_type) {
557 $strcalc .= " column_attribute ";
558 } else {
559 $strcalc .= " $colfield ";
562 $strcalc .= " ORDER BY ";
563 if($line_attribute_type) {
564 $strcalc .= " line_attribute, ";
565 } else {
566 $strcalc .= " $lineorder, ";
568 if($column_attribute_type) {
569 $strcalc .= " column_attribute ";
570 } else {
571 $strcalc .= " $colorder ";
574 ($debug) and warn $strcalc;
575 my $dbcalc = $dbh->prepare($strcalc);
576 push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strcalc };
577 $dbcalc->execute;
578 my ( $emptycol, $emptyrow );
579 while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
580 ($debug) and warn "filling table $row / $col / $value ";
581 unless ( defined $col ) {
582 $emptycol = 1;
584 unless ( defined $row ) {
585 $emptyrow = 1;
587 table_inc(\%table, $row, $col, $value);
588 table_inc(\%table, $row, 'totalrow', $value);
589 $grantotal += $value;
591 push @loopcol, { coltitle => "NULL", coltitle_display => 'NULL' } if ($emptycol);
592 push @loopline, { rowtitle => "NULL", rowtitle_display => 'NULL' } if ($emptyrow);
594 foreach my $row (@loopline) {
595 my @loopcell;
597 #@loopcol ensures the order for columns is common with column titles
598 # and the number matches the number of columns
599 foreach my $col (@loopcol) {
600 my $value = table_get(\%table, $row->{rowtitle}, $col->{coltitle});
601 push @loopcell, { value => $value };
603 push @looprow,
604 { 'rowtitle_display' => $row->{rowtitle_display},
605 'rowtitle' => $row->{rowtitle},
606 'loopcell' => \@loopcell,
607 'totalrow' => table_get(\%table, $row->{rowtitle}, 'totalrow'),
610 for my $col (@loopcol) {
611 my $total = 0;
612 foreach my $row (@looprow) {
613 $total += table_get(\%table, $row->{rowtitle}, $col->{coltitle}) || 0;
614 $debug and warn "value added " . table_get(\%table, $row->{rowtitle}, $col->{coltitle}) . "for line " . $row->{rowtitle};
616 push @loopfooter, { 'totalcol' => $total };
619 # the header of the table
620 $globalline{loopfilter} = \@loopfilter;
622 # the core of the table
623 $globalline{looprow} = \@looprow;
624 $globalline{loopcol} = \@loopcol;
626 # # the foot (totals by borrower type)
627 $globalline{loopfooter} = \@loopfooter;
628 $globalline{total} = $grantotal;
629 $globalline{line} = $line_attribute_type ? $line_attribute_type : $line;
630 $globalline{column} = $column_attribute_type ? $column_attribute_type : $column;
631 return [ ( \%globalline ) ];
634 sub null_to_zzempty {
635 my $string = shift;
637 if (!defined($string) or $string eq '' or uc($string) eq 'NULL') {
638 return 'zzEMPTY';
641 return $string;
644 sub table_set {
645 my ($table, $row, $col, $val) = @_;
647 $row = $row // '';
648 $col = $col // '';
649 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } = $val;
652 sub table_get {
653 my ($table, $row, $col) = @_;
655 $row = $row // '';
656 $col = $col // '';
657 return $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) };
660 sub table_inc {
661 my ($table, $row, $col, $inc) = @_;
663 $row = $row // '';
664 $col = $col // '';
665 $table->{ null_to_zzempty($row) }->{ null_to_zzempty($col) } += $inc;