|
Lines 34-39
use C4::Members;
Link Here
|
| 34 |
|
34 |
|
| 35 |
use Koha::AuthorisedValues; |
35 |
use Koha::AuthorisedValues; |
| 36 |
use Koha::DateUtils; |
36 |
use Koha::DateUtils; |
|
|
37 |
use C4::Members::AttributeTypes; |
| 37 |
|
38 |
|
| 38 |
=head1 NAME |
39 |
=head1 NAME |
| 39 |
|
40 |
|
|
Lines 63-68
my $monthsel = $input->param("PeriodMonthSel");
Link Here
|
| 63 |
my $calc = $input->param("Cellvalue"); |
64 |
my $calc = $input->param("Cellvalue"); |
| 64 |
my $output = $input->param("output"); |
65 |
my $output = $input->param("output"); |
| 65 |
my $basename = $input->param("basename"); |
66 |
my $basename = $input->param("basename"); |
|
|
67 |
|
| 68 |
my $attribute_filters; |
| 69 |
my $vars = $input->Vars; |
| 70 |
foreach(keys %$vars) { |
| 71 |
if(/^Filter_borrower_attributes\.(.*)/) { |
| 72 |
$attribute_filters->{$1} = $vars->{$_}; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 66 |
my ($template, $borrowernumber, $cookie) = get_template_and_user({ |
77 |
my ($template, $borrowernumber, $cookie) = get_template_and_user({ |
| 67 |
template_name => $fullreportname, |
78 |
template_name => $fullreportname, |
| 68 |
query => $input, |
79 |
query => $input, |
|
Lines 71-77
my ($template, $borrowernumber, $cookie) = get_template_and_user({
Link Here
|
| 71 |
flagsrequired => {reports => '*'}, |
82 |
flagsrequired => {reports => '*'}, |
| 72 |
debug => 0, |
83 |
debug => 0, |
| 73 |
}); |
84 |
}); |
| 74 |
our $sep = $input->param("sep") // ''; |
85 |
our $sep = $input->param("sep") // ';'; |
| 75 |
$sep = "\t" if ($sep eq 'tabulation'); |
86 |
$sep = "\t" if ($sep eq 'tabulation'); |
| 76 |
$template->param(do_it => $do_it, |
87 |
$template->param(do_it => $do_it, |
| 77 |
); |
88 |
); |
|
Lines 88-132
my ($hassort1,$hassort2);
Link Here
|
| 88 |
$hassort1=1 if $Bsort1; |
99 |
$hassort1=1 if $Bsort1; |
| 89 |
$hassort2=1 if $Bsort2; |
100 |
$hassort2=1 if $Bsort2; |
| 90 |
|
101 |
|
| 91 |
|
|
|
| 92 |
if ($do_it) { |
102 |
if ($do_it) { |
| 93 |
# Displaying results |
103 |
# Displaying results |
| 94 |
my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters); |
104 |
my $results = calculate( $line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters, $attribute_filters); |
| 95 |
if ($output eq "screen"){ |
105 |
if ( $output eq "screen" ) { |
| 96 |
# Printing results to screen |
106 |
|
| 97 |
$template->param(mainloop => $results); |
107 |
# Printing results to screen |
| 98 |
output_html_with_http_headers $input, $cookie, $template->output; |
108 |
$template->param( mainloop => $results ); |
| 99 |
} else { |
109 |
output_html_with_http_headers $input, $cookie, $template->output; |
| 100 |
# Printing to a csv file |
110 |
} else { |
| 101 |
print $input->header(-type => 'application/vnd.sun.xml.calc', |
111 |
|
| 102 |
-encoding => 'utf-8', |
112 |
# Printing to a csv file |
| 103 |
-attachment=>"$basename.csv", |
113 |
print $input->header( |
| 104 |
-filename=>"$basename.csv" ); |
114 |
-type => 'application/vnd.sun.xml.calc', |
| 105 |
my $cols = @$results[0]->{loopcol}; |
115 |
-encoding => 'utf-8', |
| 106 |
my $lines = @$results[0]->{looprow}; |
116 |
-attachment => "$basename.csv", |
| 107 |
# header top-right |
117 |
-filename => "$basename.csv" |
| 108 |
print @$results[0]->{line} ."/". @$results[0]->{column} .$sep; |
118 |
); |
| 109 |
# Other header |
119 |
my $cols = @$results[0]->{loopcol}; |
| 110 |
foreach my $col ( @$cols ) { |
120 |
my $lines = @$results[0]->{looprow}; |
| 111 |
print $col->{coltitle}.$sep; |
121 |
|
| 112 |
} |
122 |
# header top-right |
| 113 |
print "Total\n"; |
123 |
print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep; |
| 114 |
# Table |
124 |
|
| 115 |
foreach my $line ( @$lines ) { |
125 |
# Other header |
| 116 |
my $x = $line->{loopcell}; |
126 |
foreach my $col (@$cols) { |
| 117 |
print $line->{rowtitle}.$sep; |
127 |
print $col->{coltitle} . $sep; |
| 118 |
print map {$_->{value}.$sep} @$x; |
128 |
} |
| 119 |
print $line->{totalrow}, "\n"; |
129 |
print "Total\n"; |
| 120 |
} |
130 |
|
| 121 |
# footer |
131 |
# Table |
| 122 |
print "TOTAL"; |
132 |
foreach my $line (@$lines) { |
| 123 |
$cols = @$results[0]->{loopfooter}; |
133 |
my $x = $line->{loopcell}; |
| 124 |
print map {$sep.$_->{totalcol}} @$cols; |
134 |
print $line->{rowtitle} . $sep; |
| 125 |
print $sep.@$results[0]->{total}; |
135 |
print map { $_->{value} . $sep } @$x; |
| 126 |
} |
136 |
print $line->{totalrow}, "\n"; |
| 127 |
exit; # exit either way after $do_it |
137 |
} |
|
|
138 |
} |
| 139 |
exit; |
| 128 |
} |
140 |
} |
| 129 |
|
141 |
|
|
|
142 |
|
| 130 |
my $dbh = C4::Context->dbh; |
143 |
my $dbh = C4::Context->dbh; |
| 131 |
my @values; |
144 |
my @values; |
| 132 |
my %labels; |
145 |
my %labels; |
|
Lines 151-239
foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
Link Here
|
| 151 |
|
164 |
|
| 152 |
my $CGIextChoice = ( 'CSV' ); # FIXME translation |
165 |
my $CGIextChoice = ( 'CSV' ); # FIXME translation |
| 153 |
my $CGIsepChoice=GetDelimiterChoices; |
166 |
my $CGIsepChoice=GetDelimiterChoices; |
| 154 |
|
167 |
|
|
|
168 |
my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes(1); |
| 169 |
my %attribute_types_by_class; |
| 170 |
foreach my $attribute_type (@attribute_types) { |
| 171 |
if ($attribute_type->{authorised_value_category}) { |
| 172 |
my $authorised_values = C4::Koha::GetAuthorisedValues( |
| 173 |
$attribute_type->{authorised_value_category}); |
| 174 |
|
| 175 |
foreach my $authorised_value (@$authorised_values) { |
| 176 |
push @{ $attribute_type->{authorised_values} }, $authorised_value; |
| 177 |
} |
| 178 |
} |
| 179 |
push @{ $attribute_types_by_class{$attribute_type->{class}} }, $attribute_type; |
| 180 |
} |
| 181 |
|
| 155 |
$template->param( |
182 |
$template->param( |
| 156 |
categoryloop => \@patron_categories, |
183 |
categoryloop => \@patron_categories, |
| 157 |
itemtypeloop => \@itemtypeloop, |
184 |
itemtypeloop => \@itemtypeloop, |
| 158 |
locationloop => \@locations, |
185 |
locationloop => \@locations, |
| 159 |
ccodeloop => \@ccodes, |
186 |
ccodeloop => \@ccodes, |
| 160 |
hassort1=> $hassort1, |
187 |
hassort1 => $hassort1, |
| 161 |
hassort2=> $hassort2, |
188 |
hassort2 => $hassort2, |
| 162 |
Bsort1 => $Bsort1, |
189 |
Bsort1 => $Bsort1, |
| 163 |
Bsort2 => $Bsort2, |
190 |
Bsort2 => $Bsort2, |
| 164 |
CGIextChoice => $CGIextChoice, |
191 |
CGIextChoice => $CGIextChoice, |
| 165 |
CGIsepChoice => $CGIsepChoice, |
192 |
CGIsepChoice => $CGIsepChoice, |
|
|
193 |
attribute_types_by_class => \%attribute_types_by_class, |
| 166 |
); |
194 |
); |
| 167 |
output_html_with_http_headers $input, $cookie, $template->output; |
195 |
output_html_with_http_headers $input, $cookie, $template->output; |
| 168 |
|
196 |
|
| 169 |
sub calculate { |
197 |
sub calculate { |
| 170 |
my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_; |
198 |
my ( $line, $column, $dsp, $type, $daysel, $monthsel, $process, $filters, $attribute_filters ) = @_; |
| 171 |
my @loopfooter; |
199 |
my @loopfooter; |
| 172 |
my @loopcol; |
200 |
my @loopcol; |
| 173 |
my @loopline; |
201 |
my @loopline; |
| 174 |
my @looprow; |
202 |
my @looprow; |
| 175 |
my %globalline; |
203 |
my %globalline; |
| 176 |
my $grantotal =0; |
204 |
my $grantotal = 0; |
| 177 |
# extract parameters |
205 |
|
| 178 |
my $dbh = C4::Context->dbh; |
206 |
# extract parameters |
| 179 |
|
207 |
my $dbh = C4::Context->dbh; |
| 180 |
# Filters |
208 |
|
| 181 |
# Checking filters |
209 |
my ($line_attribute_type, $column_attribute_type); |
| 182 |
# |
210 |
if($line =~ /^borrower_attributes\.(.*)/) { |
| 183 |
my @loopfilter; |
211 |
$line_attribute_type = $1; |
| 184 |
for (my $i=0;$i<=12;$i++) { |
212 |
$line = "borrower_attributes.attribute"; |
| 185 |
my %cell; |
213 |
} |
| 186 |
(@$filters[$i]) or next; |
214 |
if($column =~ /^borrower_attributes\.(.*)/) { |
| 187 |
if (($i==1) and (@$filters[$i-1])) { |
215 |
$column_attribute_type = $1; |
| 188 |
$cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ; |
216 |
$column = "borrower_attributes.attribute"; |
|
|
217 |
} |
| 218 |
|
| 219 |
# Filters |
| 220 |
# Checking filters |
| 221 |
# |
| 222 |
my @loopfilter; |
| 223 |
for ( my $i = 0 ; $i <= @$filters ; $i++ ) { |
| 224 |
my %cell; |
| 225 |
( @$filters[$i] ) or next; |
| 226 |
if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) { |
| 227 |
$cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] ); |
| 189 |
} |
228 |
} |
| 190 |
# format the dates filters, otherwise just fill as is |
229 |
# format the dates filters, otherwise just fill as is |
| 191 |
if ($i>=2) { |
230 |
if ($i>=2) { |
| 192 |
$cell{filter} = @$filters[$i]; |
231 |
$cell{filter} = @$filters[$i]; |
| 193 |
} else { |
232 |
} else { |
| 194 |
$cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); } |
233 |
$cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); } |
| 195 |
if ( @$filters[$i] ); |
234 |
if ( @$filters[$i] ); |
| 196 |
} |
235 |
} |
| 197 |
$cell{crit} = |
236 |
$cell{crit} = $i; |
| 198 |
( $i == 0 ) ? "Period From" |
237 |
|
| 199 |
: ( $i == 1 ) ? "Period To" |
238 |
push @loopfilter, \%cell; |
| 200 |
: ( $i == 2 ) ? "Patron Category =" |
239 |
} |
| 201 |
: ( $i == 3 ) ? "Item Type =" |
240 |
foreach (keys %$attribute_filters) { |
| 202 |
: ( $i == 4 ) ? "Library =" |
241 |
next unless $attribute_filters->{$_}; |
| 203 |
: ( $i == 5 ) ? "Collection =" |
242 |
push @loopfilter, { crit => "$_ =", filter => $attribute_filters->{$_} }; |
| 204 |
: ( $i == 6 ) ? "Location =" |
|
|
| 205 |
: ( $i == 7 ) ? "Item callnumber >=" |
| 206 |
: ( $i == 8 ) ? "Item callnumber <" |
| 207 |
: ( $i == 9 ) ? "sort1 =" |
| 208 |
: ( $i == 10 ) ? "sort2 =" |
| 209 |
: ( $i == 11 ) ? "Home library =" |
| 210 |
: ( $i == 12 )? "Holding library =" |
| 211 |
: "UNKNOWN FILTER ($i)"; |
| 212 |
|
| 213 |
# FIXME - no translation mechanism ! |
| 214 |
push @loopfilter, \%cell; |
| 215 |
} |
243 |
} |
| 216 |
push @loopfilter,{crit=>"Event", filter=>$type }; |
244 |
push @loopfilter, { crit => "Event", filter => $type }; |
| 217 |
push @loopfilter,{crit=>"Display by", filter=>$dsp } if ($dsp); |
245 |
push @loopfilter, { crit => "Display by", filter => $dsp } if ($dsp); |
| 218 |
push @loopfilter,{crit=>"Select Day", filter=>$daysel } if ($daysel); |
246 |
push @loopfilter, { crit => "Select Day", filter => $daysel } if ($daysel); |
| 219 |
push @loopfilter,{crit=>"Select Month",filter=>$monthsel} if ($monthsel); |
247 |
push @loopfilter, { crit => "Select Month", filter => $monthsel } if ($monthsel); |
| 220 |
|
248 |
|
| 221 |
my @linefilter; |
249 |
my @linefilter; |
| 222 |
$debug and warn "filtres ". join "|", @$filters; |
250 |
$debug and warn "filtres " . join "|", @$filters; |
| 223 |
my ($colsource, $linesource) = ('', ''); |
251 |
my ( $colsource, $linesource ) = ('', ''); |
| 224 |
$linefilter[1] = @$filters[1] if ($line =~ /datetime/); |
252 |
$linefilter[1] = @$filters[1] if ( $line =~ /datetime/ ); |
| 225 |
$linefilter[0] = |
253 |
$linefilter[0] = |
| 226 |
( $line =~ /datetime/ ) ? @$filters[0] |
254 |
( $line =~ /datetime/ ) ? @$filters[0] |
| 227 |
: ( $line =~ /category/ ) ? @$filters[2] |
255 |
: ( $line =~ /category/ ) ? @$filters[2] |
| 228 |
: ( $line =~ /itemtype/ ) ? @$filters[3] |
256 |
: ( $line =~ /itemtype/ ) ? @$filters[3] |
| 229 |
: ( $line =~ /^branch/ ) ? @$filters[4] |
257 |
: ( $line =~ /branch/ ) ? @$filters[4] |
| 230 |
: ( $line =~ /ccode/ ) ? @$filters[5] |
258 |
: ( $line =~ /ccode/ ) ? @$filters[5] |
| 231 |
: ( $line =~ /location/ ) ? @$filters[6] |
259 |
: ( $line =~ /location/ ) ? @$filters[6] |
| 232 |
: ( $line =~ /sort1/ ) ? @$filters[9] |
260 |
: ( $line =~ /sort1/ ) ? @$filters[9] |
| 233 |
: ( $line =~ /sort2/ ) ? @$filters[10] |
261 |
: ( $line =~ /sort2/ ) ? @$filters[10] |
| 234 |
: ( $line =~ /homebranch/) ? @$filters[11] |
262 |
: ( $line =~ /homebranch/) ? @$filters[11] |
| 235 |
: ( $line =~ /holdingbranch/) ? @$filters[12] |
263 |
: ( $line =~ /holdingbranch/) ? @$filters[12] |
| 236 |
: undef; |
264 |
: ( $line =~ /borrowers.branchcode/ ) ? @$filters[13] |
|
|
265 |
: ( $line_attribute_type ) ? $attribute_filters->{$line_attribute_type} |
| 266 |
: undef; |
| 237 |
|
267 |
|
| 238 |
if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) { |
268 |
if ( $line =~ /ccode/ or $line =~ /location/ or $line =~ /homebranch/ or $line =~ /holdingbranch/ ) { |
| 239 |
$linesource = 'items'; |
269 |
$linesource = 'items'; |
|
Lines 252-364
sub calculate {
Link Here
|
| 252 |
: ( $column =~ /sort1/ ) ? @$filters[10] |
282 |
: ( $column =~ /sort1/ ) ? @$filters[10] |
| 253 |
: ( $column =~ /homebranch/) ? @$filters[11] |
283 |
: ( $column =~ /homebranch/) ? @$filters[11] |
| 254 |
: ( $column =~ /holdingbranch/) ? @$filters[12] |
284 |
: ( $column =~ /holdingbranch/) ? @$filters[12] |
| 255 |
: undef; |
285 |
: ( $column =~ /borrowers.branchcode/ ) ? @$filters[13] |
|
|
286 |
: ( $column_attribute_type ) ? $attribute_filters->{$column_attribute_type} |
| 287 |
: undef; |
| 256 |
|
288 |
|
| 257 |
if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) { |
289 |
if ( $column =~ /ccode/ or $column =~ /location/ or $column =~ /homebranch/ or $column =~ /holdingbranch/ ) { |
| 258 |
$colsource = 'items'; |
290 |
$colsource = 'items'; |
| 259 |
} |
291 |
} |
| 260 |
# 1st, loop rows. |
292 |
|
| 261 |
my $linefield; |
293 |
# 1st, loop rows. |
| 262 |
if ($line =~ /datetime/) { |
294 |
my $linefield; |
| 263 |
# by Day, Month or Year (1,2,3 respectively) |
295 |
if ( $line =~ /datetime/ ) { |
| 264 |
$linefield = ($dsp == 1) ? " dayname($line)" : |
296 |
|
| 265 |
($dsp == 2) ? "monthname($line)" : |
297 |
# by Day, Month, Year or Hour (1,2,3,4 respectively) |
| 266 |
($dsp == 3) ? " Year($line)" : |
298 |
$linefield = |
| 267 |
'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::DateUtils |
299 |
( $dsp == 1 ) ? " dayname($line)" |
| 268 |
} else { |
300 |
: ( $dsp == 2 ) ? "monthname($line)" |
| 269 |
$linefield = $line; |
301 |
: ( $dsp == 3 ) ? " Year($line)" |
| 270 |
} |
302 |
: ( $dsp == 4 ) ? "extract(hour from $line)" |
| 271 |
my $lineorder = ($linefield =~ /dayname/) ? "weekday($line)" : |
303 |
: 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates |
| 272 |
($linefield =~ /^month/ ) ? " month($line)" : $linefield; |
304 |
} else { |
| 273 |
|
305 |
$linefield = $line; |
| 274 |
my $strsth = "SELECT distinctrow $linefield FROM statistics, "; |
306 |
} |
| 275 |
# get stats on items if ccode or location, otherwise borrowers. |
307 |
my $lineorder = |
| 276 |
$strsth .= ($linesource eq 'items' ) ? |
308 |
( $linefield =~ /dayname/ ) ? "weekday($line)" |
| 277 |
" items WHERE (statistics.itemnumber=items.itemnumber) " : |
309 |
: ( $linefield =~ /^month/ ) ? " month($line)" |
| 278 |
" borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) "; |
310 |
: $linefield; |
| 279 |
$strsth .= " AND $line is not null "; |
311 |
|
| 280 |
|
312 |
my $strsth; |
| 281 |
if ($line =~ /datetime/) { |
313 |
if($line_attribute_type) { |
| 282 |
if ($linefilter[1] and ($linefilter[0])) { |
314 |
$strsth = "SELECT attribute FROM borrower_attributes WHERE code = '$line_attribute_type' "; |
| 283 |
$strsth .= " AND $line between ? AND ? "; |
315 |
} else { |
| 284 |
} elsif ($linefilter[1]) { |
316 |
$strsth = "SELECT distinctrow $linefield FROM statistics, "; |
| 285 |
$strsth .= " AND $line < ? "; |
317 |
|
| 286 |
} elsif ($linefilter[0]) { |
318 |
# get stats on items if ccode or location, otherwise borrowers. |
| 287 |
$strsth .= " AND $line > ? "; |
319 |
$strsth .= |
| 288 |
} |
320 |
( $linesource eq 'items' ) |
| 289 |
$strsth .= " AND type ='".$type."' " if $type; |
321 |
? " items WHERE (statistics.itemnumber=items.itemnumber) " |
| 290 |
$strsth .= " AND dayname(datetime) ='". $daysel ."' " if $daysel; |
322 |
: " borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) "; |
| 291 |
$strsth .= " AND monthname(datetime) ='". $monthsel ."' " if $monthsel; |
323 |
} |
| 292 |
} elsif ($linefilter[0]) { |
324 |
$strsth .= " AND $line is not null "; |
| 293 |
$linefilter[0] =~ s/\*/%/g; |
325 |
|
| 294 |
$strsth .= " AND $line LIKE ? "; |
326 |
if ( $line =~ /datetime/ ) { |
| 295 |
} |
327 |
if ( $linefilter[1] and ( $linefilter[0] ) ) { |
| 296 |
$strsth .=" group by $linefield order by $lineorder "; |
328 |
$strsth .= " AND $line between ? AND ? "; |
| 297 |
$debug and warn $strsth; |
329 |
} elsif ( $linefilter[1] ) { |
| 298 |
push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strsth}; |
330 |
$strsth .= " AND $line < ? "; |
| 299 |
my $sth = $dbh->prepare( $strsth ); |
331 |
} elsif ( $linefilter[0] ) { |
| 300 |
if ((@linefilter) and ($linefilter[1])){ |
332 |
$strsth .= " AND $line > ? "; |
| 301 |
$sth->execute($linefilter[0],$linefilter[1]); |
333 |
} |
| 302 |
} elsif ($linefilter[0]) { |
334 |
$strsth .= " AND type ='" . $type . "' " if $type; |
| 303 |
$sth->execute($linefilter[0]); |
335 |
$strsth .= " AND dayname(datetime) ='" . $daysel . "' " if $daysel; |
| 304 |
} else { |
336 |
$strsth .= " AND monthname(datetime) ='" . $monthsel . "' " if $monthsel; |
| 305 |
$sth->execute; |
337 |
} elsif ( $linefilter[0] ) { |
| 306 |
} |
338 |
$linefilter[0] =~ s/\*/%/g; |
|
|
339 |
$strsth .= " AND $line LIKE ? "; |
| 340 |
} |
| 341 |
$strsth .= " group by $linefield order by $lineorder "; |
| 342 |
$debug and warn $strsth; |
| 343 |
push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth }; |
| 344 |
my $sth = $dbh->prepare($strsth); |
| 345 |
if ( (@linefilter) and ( $linefilter[1] ) ) { |
| 346 |
$sth->execute( $linefilter[0], $linefilter[1] ); |
| 347 |
} elsif ( $linefilter[0] ) { |
| 348 |
$sth->execute( $linefilter[0] ); |
| 349 |
} else { |
| 350 |
$sth->execute; |
| 351 |
} |
| 307 |
|
352 |
|
| 308 |
while (my ($celvalue) = $sth->fetchrow) { |
353 |
while ( my ($celvalue) = $sth->fetchrow ) { |
| 309 |
my %cell = (rowtitle => $celvalue, totalrow => 0); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display |
354 |
my %cell = ( rowtitle => $celvalue, totalrow => 0 ); # we leave 'rowtitle' as hash key (used when filling the table), and add coltitle_display |
| 310 |
$cell{rowtitle_display} = |
355 |
$cell{rowtitle_display} = |
| 311 |
($line =~ /ccode/ ) ? $ccodes->{$celvalue} : |
356 |
( $line =~ /ccode/ ) ? $ccodes->{$celvalue} |
| 312 |
($line =~ /location/) ? $locations->{$celvalue} : |
357 |
: ( $line =~ /location/ ) ? $locations->{$celvalue} |
| 313 |
($line =~ /itemtype/) ? $itemtypes->{$celvalue}->{description} : |
358 |
: ( $line =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description} |
| 314 |
$celvalue; # default fallback |
359 |
: $celvalue; # default fallback |
| 315 |
if ($line =~ /sort1/) { |
360 |
if ( $line =~ /sort1/ ) { |
| 316 |
foreach (@$Bsort1) { |
361 |
foreach (@$Bsort1) { |
| 317 |
($celvalue eq $_->{authorised_value}) or next; |
362 |
( $celvalue eq $_->{authorised_value} ) or next; |
| 318 |
$cell{rowtitle_display} = $_->{lib} and last; |
363 |
$cell{rowtitle_display} = $_->{lib} and last; |
| 319 |
} |
364 |
} |
| 320 |
} elsif ($line =~ /sort2/) { |
365 |
} elsif ( $line =~ /sort2/ ) { |
| 321 |
foreach (@$Bsort2) { |
366 |
foreach (@$Bsort2) { |
| 322 |
($celvalue eq $_->{authorised_value}) or next; |
367 |
( $celvalue eq $_->{authorised_value} ) or next; |
| 323 |
$cell{rowtitle_display} = $_->{lib} and last; |
368 |
$cell{rowtitle_display} = $_->{lib} and last; |
| 324 |
} |
369 |
} |
| 325 |
} elsif ($line =~ /category/) { |
370 |
} elsif ($line =~ /category/) { |
| 326 |
foreach my $patron_category ( @patron_categories ) { |
371 |
foreach my $patron_category ( @patron_categories ) { |
| 327 |
($celvalue eq $patron_category->categorycode) or next; |
372 |
($celvalue eq $patron_category->categorycode) or next; |
| 328 |
$cell{rowtitle_display} = $patron_category->description and last; |
373 |
$cell{rowtitle_display} = $patron_category->description and last; |
| 329 |
} |
374 |
} |
| 330 |
} |
375 |
} |
| 331 |
push @loopline, \%cell; |
376 |
push @loopline, \%cell; |
| 332 |
} |
377 |
} |
| 333 |
|
378 |
|
| 334 |
# 2nd, loop cols. |
379 |
# 2nd, loop cols. |
| 335 |
my $colfield; |
380 |
my $colfield; |
| 336 |
my $colorder; |
381 |
my $colorder; |
| 337 |
if ($column =~ /datetime/) { |
382 |
if ( $column =~ /datetime/ ) { |
| 338 |
#Display by Day, Month or Year (1,2,3 respectively) |
383 |
|
| 339 |
$colfield = ($dsp == 1) ? " dayname($column)" : |
384 |
#Display by Day, Month or Year (1,2,3 respectively) |
| 340 |
($dsp == 2) ? "monthname($column)" : |
385 |
$colfield = |
| 341 |
($dsp == 3) ? " Year($column)" : |
386 |
( $dsp == 1 ) ? " dayname($column)" |
| 342 |
'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::DateUtils |
387 |
: ( $dsp == 2 ) ? "monthname($column)" |
| 343 |
} else { |
388 |
: ( $dsp == 3 ) ? " Year($column)" |
| 344 |
$colfield = $column; |
389 |
: ( $dsp == 4 ) ? "extract(hour from $column)" |
| 345 |
} |
390 |
: 'date_format(`datetime`,"%Y-%m-%d")'; # Probably should be left alone or passed through Koha::Dates |
| 346 |
$colorder = ($colfield =~ /dayname/) ? "weekday($column)" : |
391 |
} else { |
| 347 |
($colfield =~ /^month/ ) ? " month($column)" : $colfield; |
392 |
$colfield = $column; |
| 348 |
my $strsth2 = "SELECT distinctrow $colfield FROM statistics, "; |
393 |
} |
| 349 |
# get stats on items if ccode or location, otherwise borrowers. |
394 |
$colorder = |
| 350 |
$strsth2 .= ($colsource eq 'items' ) ? |
395 |
( $colfield =~ /dayname/ ) ? "weekday($column)" |
| 351 |
"items WHERE (statistics.itemnumber=items.itemnumber) " : |
396 |
: ( $colfield =~ /^month/ ) ? " month($column)" |
| 352 |
"borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) "; |
397 |
: $colfield; |
| 353 |
$strsth2 .= " AND $column IS NOT NULL "; |
398 |
my $strsth2; |
| 354 |
|
399 |
if($column_attribute_type) { |
| 355 |
if ($column =~ /datetime/){ |
400 |
$strsth2 = "SELECT attribute FROM borrower_attributes WHERE code = '$column_attribute_type' "; |
| 356 |
if (($colfilter[1]) and ($colfilter[0])){ |
401 |
} else { |
| 357 |
$strsth2 .= " AND $column BETWEEN ? AND ? " ; |
402 |
$strsth2 = "SELECT distinctrow $colfield FROM statistics, "; |
| 358 |
} elsif ($colfilter[1]) { |
403 |
|
| 359 |
$strsth2 .= " AND $column < ? " ; |
404 |
# get stats on items if ccode or location, otherwise borrowers. |
| 360 |
} elsif ($colfilter[0]) { |
405 |
$strsth2 .= |
| 361 |
$strsth2 .= " AND $column > ? " ; |
406 |
( $colsource eq 'items' ) |
|
|
407 |
? "items WHERE (statistics.itemnumber=items.itemnumber) " |
| 408 |
: "borrowers WHERE (statistics.borrowernumber=borrowers.borrowernumber) "; |
| 409 |
} |
| 410 |
$strsth2 .= " AND $column IS NOT NULL "; |
| 411 |
|
| 412 |
if ( $column =~ /datetime/ ) { |
| 413 |
if ( ( $colfilter[1] ) and ( $colfilter[0] ) ) { |
| 414 |
$strsth2 .= " AND $column BETWEEN ? AND ? "; |
| 415 |
} elsif ( $colfilter[1] ) { |
| 416 |
$strsth2 .= " AND $column < ? "; |
| 417 |
} elsif ( $colfilter[0] ) { |
| 418 |
$strsth2 .= " AND $column > ? "; |
| 362 |
} |
419 |
} |
| 363 |
$strsth2 .= " AND type ='". $type ."' " if $type; |
420 |
$strsth2 .= " AND type ='". $type ."' " if $type; |
| 364 |
$strsth2 .= " AND dayname(datetime) ='". $daysel ."' " if $daysel; |
421 |
$strsth2 .= " AND dayname(datetime) ='". $daysel ."' " if $daysel; |
|
Lines 367-515
sub calculate {
Link Here
|
| 367 |
$colfilter[0] =~ s/\*/%/g; |
424 |
$colfilter[0] =~ s/\*/%/g; |
| 368 |
$strsth2 .= " AND $column LIKE ? " ; |
425 |
$strsth2 .= " AND $column LIKE ? " ; |
| 369 |
} |
426 |
} |
| 370 |
$strsth2 .=" GROUP BY $colfield ORDER BY $colorder "; |
|
|
| 371 |
|
| 372 |
my $sth2 = $dbh->prepare($strsth2); |
| 373 |
push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strsth2}; |
| 374 |
if ((@colfilter) and ($colfilter[1])){ |
| 375 |
$sth2->execute($colfilter[0], $colfilter[1]); |
| 376 |
} elsif ($colfilter[0]) { |
| 377 |
$sth2->execute($colfilter[0]); |
| 378 |
} else { |
| 379 |
$sth2->execute; |
| 380 |
} |
| 381 |
|
427 |
|
| 382 |
while (my ($celvalue) = $sth2->fetchrow) { |
428 |
$strsth2 .= " group by $colfield order by $colorder "; |
| 383 |
my %cell = (coltitle => $celvalue); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display |
429 |
$debug and warn $strsth2; |
| 384 |
$cell{coltitle_display} = |
430 |
push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strsth2 }; |
| 385 |
($column =~ /ccode/ ) ? $ccodes->{$celvalue} : |
431 |
my $sth2 = $dbh->prepare($strsth2); |
| 386 |
($column =~ /location/) ? $locations->{$celvalue} : |
432 |
if ( (@colfilter) and ( $colfilter[1] ) ) { |
| 387 |
($column =~ /itemtype/) ? $itemtypes->{$celvalue}->{description} : |
433 |
$sth2->execute( $colfilter[0], $colfilter[1] ); |
| 388 |
$celvalue; # default fallback |
434 |
} elsif ( $colfilter[0] ) { |
| 389 |
if ($column =~ /sort1/) { |
435 |
$sth2->execute( $colfilter[0] ); |
| 390 |
foreach (@$Bsort1) { |
436 |
} else { |
| 391 |
($celvalue eq $_->{authorised_value}) or next; |
437 |
$sth2->execute; |
| 392 |
$cell{coltitle_display} = $_->{lib} and last; |
438 |
} |
| 393 |
} |
439 |
|
| 394 |
} elsif ($column =~ /sort2/) { |
440 |
while ( my ($celvalue) = $sth2->fetchrow ) { |
| 395 |
foreach (@$Bsort2) { |
441 |
my %cell = ( coltitle => $celvalue ); # we leave 'coltitle' as hash key (used when filling the table), and add coltitle_display |
| 396 |
($celvalue eq $_->{authorised_value}) or next; |
442 |
$cell{coltitle_display} = |
| 397 |
$cell{coltitle_display} = $_->{lib} and last; |
443 |
( $column =~ /ccode/ ) ? $ccodes->{$celvalue} |
| 398 |
} |
444 |
: ( $column =~ /location/ ) ? $locations->{$celvalue} |
|
|
445 |
: ( $column =~ /itemtype/ ) ? $itemtypes->{$celvalue}->{description} |
| 446 |
: $celvalue; # default fallback |
| 447 |
if ( $column =~ /sort1/ ) { |
| 448 |
foreach (@$Bsort1) { |
| 449 |
( $celvalue eq $_->{authorised_value} ) or next; |
| 450 |
$cell{coltitle_display} = $_->{lib} and last; |
| 451 |
} |
| 452 |
} elsif ( $column =~ /sort2/ ) { |
| 453 |
foreach (@$Bsort2) { |
| 454 |
( $celvalue eq $_->{authorised_value} ) or next; |
| 455 |
$cell{coltitle_display} = $_->{lib} and last; |
| 456 |
} |
| 399 |
} elsif ($column =~ /category/) { |
457 |
} elsif ($column =~ /category/) { |
| 400 |
foreach my $patron_category ( @patron_categories ) { |
458 |
foreach my $patron_category ( @patron_categories ) { |
| 401 |
($celvalue eq $patron_category->categorycode) or next; |
459 |
($celvalue eq $patron_category->categorycode) or next; |
| 402 |
$cell{coltitle_display} = $patron_category->description and last; |
460 |
$cell{coltitle_display} = $patron_category->description and last; |
| 403 |
} |
461 |
} |
| 404 |
} |
462 |
} |
| 405 |
push @loopcol, \%cell; |
463 |
push @loopcol, \%cell; |
| 406 |
} |
464 |
} |
| 407 |
|
465 |
|
| 408 |
#Initialization of cell values..... |
466 |
#Initialization of cell values..... |
| 409 |
my %table; |
467 |
my %table; |
| 410 |
foreach my $row (@loopline) { |
468 |
foreach my $row (@loopline) { |
| 411 |
foreach my $col (@loopcol) { |
469 |
foreach my $col (@loopcol) { |
| 412 |
$debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) "; |
470 |
$debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} ) "; |
| 413 |
$table{$row->{rowtitle}}->{$col->{coltitle}} = 0; |
471 |
$table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0; |
| 414 |
} |
472 |
} |
| 415 |
$table{$row->{rowtitle}}->{totalrow} = 0; |
473 |
$table{ $row->{rowtitle} }->{totalrow} = 0; |
| 416 |
} |
474 |
} |
| 417 |
|
475 |
|
| 418 |
# preparing calculation |
476 |
# preparing calculation |
| 419 |
my $strcalc = "SELECT $linefield, $colfield, "; |
477 |
my $strcalc = "SELECT "; |
| 420 |
$strcalc .= ($process == 1) ? " COUNT(*) " : |
478 |
if($line_attribute_type) { |
| 421 |
($process == 2) ? "(COUNT(DISTINCT borrowers.borrowernumber))" : |
479 |
$strcalc .= "attribute_$line_attribute_type.attribute AS line_attribute, "; |
| 422 |
($process == 3) ? "(COUNT(DISTINCT statistics.itemnumber))" : ''; |
480 |
} else { |
| 423 |
if ($process == 4) { |
481 |
$strcalc .= "$linefield, "; |
| 424 |
my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items"); |
482 |
} |
| 425 |
$rqbookcount->execute; |
483 |
if($column_attribute_type) { |
| 426 |
my ($bookcount) = $rqbookcount->fetchrow; |
484 |
$strcalc .= "attribute_$column_attribute_type.attribute AS column_attribute, "; |
| 427 |
$strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount " ; |
485 |
} else { |
| 428 |
} |
486 |
$strcalc .= "$colfield, "; |
| 429 |
$strcalc .= " |
487 |
} |
|
|
488 |
$strcalc .= |
| 489 |
( $process == 1 ) ? " COUNT(*) " |
| 490 |
: ( $process == 2 ) ? "(COUNT(DISTINCT borrowers.borrowernumber))" |
| 491 |
: ( $process == 3 ) ? "(COUNT(DISTINCT statistics.itemnumber))" |
| 492 |
: ( $process == 5 ) ? "(COUNT(DISTINCT items.biblionumber))" |
| 493 |
: ''; |
| 494 |
if ( $process == 4 ) { |
| 495 |
my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items"); |
| 496 |
$rqbookcount->execute; |
| 497 |
my ($bookcount) = $rqbookcount->fetchrow; |
| 498 |
$strcalc .= "100*(COUNT(DISTINCT statistics.itemnumber))/ $bookcount "; |
| 499 |
} |
| 500 |
$strcalc .= " |
| 430 |
FROM statistics |
501 |
FROM statistics |
| 431 |
LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber |
502 |
LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber |
| 432 |
"; |
503 |
"; |
| 433 |
$strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " |
504 |
foreach (keys %$attribute_filters) { |
| 434 |
if ($linefield =~ /^items\./ or $colfield =~ /^items\./ or ($colsource eq 'items') |
505 |
if( |
| 435 |
||@$filters[5]||@$filters[6]||@$filters[7]||@$filters[8]); |
506 |
($line_attribute_type and $line_attribute_type eq $_) |
| 436 |
|
507 |
or $column_attribute_type and $column_attribute_type eq $_ |
| 437 |
$strcalc .= "WHERE 1=1 "; |
508 |
or $attribute_filters->{$_} |
| 438 |
@$filters = map {defined($_) and s/\*/%/g; $_} @$filters; |
509 |
) { |
| 439 |
$strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if (@$filters[0] ); |
510 |
$strcalc .= " LEFT JOIN borrower_attributes AS attribute_$_ ON (statistics.borrowernumber = attribute_$_.borrowernumber AND attribute_$_.code = '$_') "; |
| 440 |
$strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if (@$filters[1] ); |
511 |
} |
| 441 |
$strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] ."'" if (@$filters[2] ); |
512 |
} |
| 442 |
$strcalc .= " AND statistics.itemtype LIKE '" . @$filters[3] ."'" if (@$filters[3] ); |
513 |
$strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " |
| 443 |
$strcalc .= " AND statistics.branch LIKE '" . @$filters[4] ."'" if (@$filters[4] ); |
514 |
if ( $linefield =~ /^items\./ |
| 444 |
$strcalc .= " AND items.ccode LIKE '" . @$filters[5] ."'" if (@$filters[5] ); |
515 |
or $colfield =~ /^items\./ |
| 445 |
$strcalc .= " AND items.location LIKE '" . @$filters[6] ."'" if (@$filters[6] ); |
516 |
or $process == 5 |
| 446 |
$strcalc .= " AND items.itemcallnumber >='" . @$filters[7] ."'" if (@$filters[7] ); |
517 |
or ( $colsource eq 'items' ) || @$filters[5] || @$filters[6] || @$filters[7] || @$filters[8] ); |
| 447 |
$strcalc .= " AND items.itemcallnumber <'" . @$filters[8] ."'" if (@$filters[8] ); |
518 |
|
| 448 |
$strcalc .= " AND borrowers.sort1 LIKE '" . @$filters[9] ."'" if (@$filters[9] ); |
519 |
$strcalc .= "WHERE 1=1 "; |
| 449 |
$strcalc .= " AND borrowers.sort2 LIKE '" . @$filters[10]."'" if (@$filters[10]); |
520 |
@$filters = map { defined($_) and s/\*/%/g; $_ } @$filters; |
| 450 |
$strcalc .= " AND dayname(datetime) LIKE '" . $daysel ."'" if ($daysel ); |
521 |
$strcalc .= " AND statistics.datetime >= '" . @$filters[0] . "'" if ( @$filters[0] ); |
| 451 |
$strcalc .= " AND monthname(datetime) LIKE '" . $monthsel ."'" if ($monthsel); |
522 |
$strcalc .= " AND statistics.datetime <= '" . @$filters[1] . " 23:59:59'" if ( @$filters[1] ); |
| 452 |
$strcalc .= " AND statistics.type LIKE '" . $type ."'" if ($type ); |
523 |
$strcalc .= " AND borrowers.categorycode LIKE '" . @$filters[2] . "'" if ( @$filters[2] ); |
| 453 |
|
524 |
$strcalc .= " AND statistics.itemtype LIKE '" . @$filters[3] . "'" if ( @$filters[3] ); |
| 454 |
$strcalc .= " GROUP BY $linefield, $colfield order by $lineorder,$colorder"; |
525 |
$strcalc .= " AND statistics.branch LIKE '" . @$filters[4] . "'" if ( @$filters[4] ); |
| 455 |
($debug) and warn $strcalc; |
526 |
$strcalc .= " AND items.ccode LIKE '" . @$filters[5] . "'" if ( @$filters[5] ); |
| 456 |
my $dbcalc = $dbh->prepare($strcalc); |
527 |
$strcalc .= " AND items.location LIKE '" . @$filters[6] . "'" if ( @$filters[6] ); |
| 457 |
push @loopfilter, {crit=>'SQL =', sql=>1, filter=>$strcalc}; |
528 |
$strcalc .= " AND items.itemcallnumber >='" . @$filters[7] . "'" if ( @$filters[7] ); |
| 458 |
$dbcalc->execute; |
529 |
$strcalc .= " AND items.itemcallnumber <'" . @$filters[8] . "'" if ( @$filters[8] ); |
| 459 |
my ($emptycol,$emptyrow); |
530 |
$strcalc .= " AND borrowers.sort1 LIKE '" . @$filters[9] . "'" if ( @$filters[9] ); |
| 460 |
while (my ($row, $col, $value) = $dbcalc->fetchrow) { |
531 |
$strcalc .= " AND borrowers.sort2 LIKE '" . @$filters[10] . "'" if ( @$filters[10] ); |
| 461 |
($debug) and warn "filling table $row / $col / $value "; |
532 |
$strcalc .= " AND items.homebranch LIKE '" . @$filters[11] . "'" if ( @$filters[11] ); |
| 462 |
unless (defined $col) { |
533 |
$strcalc .= " AND items.holdingbranch LIKE '" . @$filters[12] . "'" if ( @$filters[12] ); |
| 463 |
$emptycol = 1; |
534 |
$strcalc .= " AND borrowers.branchcode LIKE '" . @$filters[13] . "'" if ( @$filters[13] ); |
| 464 |
$col = "zzEMPTY" ; |
535 |
$strcalc .= " AND dayname(datetime) LIKE '" . $daysel . "'" if ($daysel); |
| 465 |
} |
536 |
$strcalc .= " AND monthname(datetime) LIKE '" . $monthsel . "'" if ($monthsel); |
| 466 |
unless (defined $row) { |
537 |
$strcalc .= " AND statistics.type LIKE '" . $type . "'" if ($type); |
| 467 |
$emptyrow = 1; |
538 |
foreach (keys %$attribute_filters) { |
| 468 |
$row = "zzEMPTY"; |
539 |
if($attribute_filters->{$_}) { |
| 469 |
} |
540 |
$strcalc .= " AND attribute_$_.attribute LIKE '" . $attribute_filters->{$_} . "'"; |
| 470 |
$table{$row}->{$col} += $value; |
541 |
} |
| 471 |
$table{$row}->{totalrow} += $value; |
542 |
} |
| 472 |
$grantotal += $value; |
543 |
|
| 473 |
} |
544 |
$strcalc .= " GROUP BY "; |
| 474 |
push @loopcol, {coltitle => "NULL", coltitle_display => 'NULL'} if ($emptycol); |
545 |
if($line_attribute_type) { |
| 475 |
push @loopline,{rowtitle => "NULL", rowtitle_display => 'NULL'} if ($emptyrow); |
546 |
$strcalc .= " line_attribute, "; |
| 476 |
|
547 |
} else { |
| 477 |
foreach my $row (@loopline) { |
548 |
$strcalc .= " $linefield, "; |
| 478 |
my @loopcell; |
549 |
} |
| 479 |
#@loopcol ensures the order for columns is common with column titles |
550 |
if($column_attribute_type) { |
| 480 |
# and the number matches the number of columns |
551 |
$strcalc .= " column_attribute "; |
| 481 |
foreach my $col (@loopcol) { |
552 |
} else { |
| 482 |
my $value = $table{null_to_zzempty($row->{rowtitle})}->{null_to_zzempty($col->{coltitle})}; |
553 |
$strcalc .= " $colfield "; |
| 483 |
push @loopcell, {value => $value}; |
554 |
} |
| 484 |
} |
555 |
|
| 485 |
my $rowtitle = ($row->{rowtitle} eq "NULL") ? "zzEMPTY" : $row->{rowtitle}; |
556 |
$strcalc .= " ORDER BY "; |
| 486 |
push @looprow, { |
557 |
if($line_attribute_type) { |
| 487 |
'rowtitle_display' => $row->{rowtitle_display}, |
558 |
$strcalc .= " line_attribute, "; |
| 488 |
'rowtitle' => $rowtitle, |
559 |
} else { |
| 489 |
'loopcell' => \@loopcell, |
560 |
$strcalc .= " $lineorder, "; |
| 490 |
'totalrow' => $table{$rowtitle}->{totalrow} |
561 |
} |
| 491 |
}; |
562 |
if($column_attribute_type) { |
| 492 |
} |
563 |
$strcalc .= " column_attribute "; |
| 493 |
for my $col ( @loopcol ) { |
564 |
} else { |
| 494 |
my $total = 0; |
565 |
$strcalc .= " $colorder "; |
| 495 |
foreach my $row (@looprow) { |
566 |
} |
| 496 |
$total += $table{null_to_zzempty($row->{rowtitle})}->{null_to_zzempty($col->{coltitle})}; |
567 |
|
| 497 |
$debug and warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle}; |
568 |
($debug) and warn $strcalc; |
| 498 |
} |
569 |
my $dbcalc = $dbh->prepare($strcalc); |
| 499 |
push @loopfooter, {'totalcol' => $total}; |
570 |
push @loopfilter, { crit => 'SQL =', sql => 1, filter => $strcalc }; |
| 500 |
} |
571 |
$dbcalc->execute; |
|
|
572 |
my ( $emptycol, $emptyrow ); |
| 573 |
while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) { |
| 574 |
($debug) and warn "filling table $row / $col / $value "; |
| 575 |
unless ( defined $col ) { |
| 576 |
$emptycol = 1; |
| 577 |
$col = "zzEMPTY"; |
| 578 |
} |
| 579 |
unless ( defined $row ) { |
| 580 |
$emptyrow = 1; |
| 581 |
$row = "zzEMPTY"; |
| 582 |
} |
| 583 |
$table{$row}->{$col} += $value; |
| 584 |
$table{$row}->{totalrow} += $value; |
| 585 |
$grantotal += $value; |
| 586 |
} |
| 587 |
push @loopcol, { coltitle => "NULL", coltitle_display => 'NULL' } if ($emptycol); |
| 588 |
push @loopline, { rowtitle => "NULL", rowtitle_display => 'NULL' } if ($emptyrow); |
| 589 |
|
| 590 |
foreach my $row (@loopline) { |
| 591 |
my @loopcell; |
| 592 |
|
| 593 |
#@loopcol ensures the order for columns is common with column titles |
| 594 |
# and the number matches the number of columns |
| 595 |
foreach my $col (@loopcol) { |
| 596 |
my $value = $table{ null_to_zzempty( $row->{rowtitle} ) }->{ null_to_zzempty( $col->{coltitle} ) }; |
| 597 |
push @loopcell, { value => $value }; |
| 598 |
} |
| 599 |
my $rowtitle = ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY" : $row->{rowtitle}; |
| 600 |
push @looprow, |
| 601 |
{ 'rowtitle_display' => $row->{rowtitle_display}, |
| 602 |
'rowtitle' => $rowtitle, |
| 603 |
'loopcell' => \@loopcell, |
| 604 |
'totalrow' => $table{$rowtitle}->{totalrow} |
| 605 |
}; |
| 606 |
} |
| 607 |
for my $col (@loopcol) { |
| 608 |
my $total = 0; |
| 609 |
foreach my $row (@looprow) { |
| 610 |
$total += $table{ null_to_zzempty( $row->{rowtitle} ) }->{ null_to_zzempty( $col->{coltitle} ) }; |
| 611 |
$debug and warn "value added " . $table{ $row->{rowtitle} }->{ $col->{coltitle} } . "for line " . $row->{rowtitle}; |
| 612 |
} |
| 613 |
push @loopfooter, { 'totalcol' => $total }; |
| 614 |
} |
| 615 |
|
| 616 |
# the header of the table |
| 617 |
$globalline{loopfilter} = \@loopfilter; |
| 618 |
|
| 619 |
# the core of the table |
| 620 |
$globalline{looprow} = \@looprow; |
| 621 |
$globalline{loopcol} = \@loopcol; |
| 501 |
|
622 |
|
| 502 |
# the header of the table |
623 |
# # the foot (totals by borrower type) |
| 503 |
$globalline{loopfilter}=\@loopfilter; |
624 |
$globalline{loopfooter} = \@loopfooter; |
| 504 |
# the core of the table |
625 |
$globalline{total} = $grantotal; |
| 505 |
$globalline{looprow} = \@looprow; |
626 |
$globalline{line} = $line_attribute_type ? $line_attribute_type : $line; |
| 506 |
$globalline{loopcol} = \@loopcol; |
627 |
$globalline{column} = $column_attribute_type ? $column_attribute_type : $column; |
| 507 |
# # the foot (totals by borrower type) |
628 |
return [ ( \%globalline ) ]; |
| 508 |
$globalline{loopfooter} = \@loopfooter; |
|
|
| 509 |
$globalline{total} = $grantotal; |
| 510 |
$globalline{line} = $line; |
| 511 |
$globalline{column} = $column; |
| 512 |
return [(\%globalline)]; |
| 513 |
} |
629 |
} |
| 514 |
|
630 |
|
| 515 |
sub null_to_zzempty ($) { |
631 |
sub null_to_zzempty ($) { |
| 516 |
- |
|
|