bug 4170 updated overdue rules
[koha.git] / reports / issues_avg_stats.pl
blob678495f72b4cf0fa383760805ba333552c0563ad
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Reports;
31 use C4::Dates qw/format_date format_date_in_iso/;
32 use Date::Calc qw(Delta_Days);
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =over 2
42 =cut
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_avg_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 $filters[2]=format_date_in_iso($filters[2]);
53 $filters[3]=format_date_in_iso($filters[3]);
54 my $podsp = $input->param("IssueDisplay");
55 my $rodsp = $input->param("ReturnDisplay");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user({template_name => $fullreportname,
63 query => $input,
64 type => "intranet",
65 authnotrequired => 0,
66 flagsrequired => {reports => '*'},
67 debug => 1,
68 });
69 our $sep = $input->param("sep");
70 $sep = "\t" if ($sep eq 'tabulation');
71 $template->param(do_it => $do_it,
72 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
74 if ($do_it) {
75 # Displaying results
76 my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
77 if ($output eq "screen"){
78 # Printing results to screen
79 $template->param(mainloop => $results);
80 output_html_with_http_headers $input, $cookie, $template->output;
81 exit(1);
82 } else {
83 # Printing to a csv file
84 print $input->header(-type => 'application/vnd.sun.xml.calc',
85 -encoding => 'utf-8',
86 -attachment=>"$basename.csv",
87 -filename=>"$basename.csv" );
88 my $cols = @$results[0]->{loopcol};
89 my $lines = @$results[0]->{looprow};
90 # header top-right
91 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
92 # Other header
93 foreach my $col ( @$cols ) {
94 print $col->{coltitle}.$sep;
96 print "Total\n";
97 # Table
98 foreach my $line ( @$lines ) {
99 my $x = $line->{loopcell};
100 print $line->{rowtitle}.$sep;
101 foreach my $cell (@$x) {
102 print $cell->{value}.$sep;
104 print $line->{totalrow};
105 print "\n";
107 # footer
108 print "TOTAL";
109 $cols = @$results[0]->{loopfooter};
110 foreach my $col ( @$cols ) {
111 print $sep.$col->{totalcol};
113 print $sep.@$results[0]->{total};
114 exit(1);
116 # Displaying choices
117 } else {
118 my $dbh = C4::Context->dbh;
119 my @values;
120 my %labels;
121 my %select;
122 my $req;
123 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
124 $req->execute;
125 my @select;
126 push @select,"";
127 $select{""}="";
128 while (my ($value, $desc) =$req->fetchrow) {
129 push @select, $value;
130 $select{$value}=$desc;
132 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
133 -id => 'borcat',
134 -values => \@select,
135 -labels => \%select,
136 -size => 1,
137 -multiple => 0 );
139 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
140 $req->execute;
141 undef @select;
142 undef %select;
143 push @select,"";
144 $select{""}="";
145 while (my ($value,$desc) =$req->fetchrow) {
146 push @select, $value;
147 $select{$value}=$desc;
149 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
150 -id => 'itemtypes',
151 -values => \@select,
152 -labels => \%select,
153 -size => 1,
154 -multiple => 0 );
156 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
157 $req->execute;
158 undef @select;
159 push @select,"";
160 my $hassort1;
161 while (my ($value) =$req->fetchrow) {
162 $hassort1 =1 if ($value);
163 push @select, $value;
165 my $branches=GetBranches();
166 my @select_branch;
167 my %select_branches;
168 push @select_branch,"";
169 $select_branches{""} = "";
170 foreach my $branch (keys %$branches) {
171 push @select_branch, $branch;
172 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
174 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
175 -id => 'branch',
176 -values => \@select_branch,
177 -labels => \%select_branches,
178 -size => 1,
179 -multiple => 0 );
181 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
182 -id => 'sort1',
183 -values => \@select,
184 -size => 1,
185 -multiple => 0 );
187 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
188 $req->execute;
189 undef @select;
190 push @select,"";
191 my $hassort2;
192 my $hglghtsort2;
193 while (my ($value) =$req->fetchrow) {
194 $hassort2 =1 if ($value);
195 $hglghtsort2= !($hassort1);
196 push @select, $value;
198 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
199 -id => 'sort2',
200 -values => \@select,
201 -size => 1,
202 -multiple => 0 );
204 my @mime = ( C4::Context->preference("MIME") );
205 # foreach my $mime (@mime){
206 # warn "".$mime;
209 my $CGIextChoice=CGI::scrolling_list(
210 -name => 'MIME',
211 -id => 'MIME',
212 -values => \@mime,
213 -size => 1,
214 -multiple => 0 );
216 my $CGIsepChoice=GetDelimiterChoices;
218 $template->param(
219 CGIBorCat => $CGIBorCat,
220 CGIItemType => $CGIItemTypes,
221 CGIBranch => $CGIBranch,
222 hassort1=> $hassort1,
223 hassort2=> $hassort2,
224 HlghtSort2 => $hglghtsort2,
225 CGISort1 => $CGISort1,
226 CGISort2 => $CGISort2,
227 CGIextChoice => $CGIextChoice,
228 CGIsepChoice => $CGIsepChoice
230 output_html_with_http_headers $input, $cookie, $template->output;
236 sub calculate {
237 my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
238 my @mainloop;
239 my @loopfooter;
240 my @loopcol;
241 my @loopline;
242 my @looprow;
243 my %globalline;
244 my $grantotal =0;
245 # extract parameters
246 my $dbh = C4::Context->dbh;
248 # Filters
249 # Checking filters
251 my @loopfilter;
252 for (my $i=0;$i<=6;$i++) {
253 my %cell;
254 if ( @$filters[$i] ) {
255 if (($i==1) and (@$filters[$i-1])) {
256 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
258 # format the dates filters, otherwise just fill as is
259 if ($i>=4) {
260 $cell{filter} .= @$filters[$i];
261 } else {
262 $cell{filter} .= format_date(@$filters[$i]);
264 $cell{crit} .="Issue From" if ($i==0);
265 $cell{crit} .="Issue To" if ($i==1);
266 $cell{crit} .="Issue Month" if ($i==2);
267 $cell{crit} .="Issue Day" if ($i==3);
268 $cell{crit} .="Return From" if ($i==4);
269 $cell{crit} .="Return To" if ($i==5);
270 $cell{crit} .="Return Month" if ($i==6);
271 $cell{crit} .="Return Day" if ($i==7);
272 $cell{crit} .="Borrower Cat" if ($i==8);
273 $cell{crit} .="Doc Type" if ($i==9);
274 $cell{crit} .="Branch" if ($i==10);
275 $cell{crit} .="Sort1" if ($i==11);
276 $cell{crit} .="Sort2" if ($i==12);
277 push @loopfilter, \%cell;
280 push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
281 push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
285 my @linefilter;
286 # warn "filtres ".@filters[0];
287 # warn "filtres ".@filters[1];
288 # warn "filtres ".@filters[2];
289 # warn "filtres ".@filters[3];
290 $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
291 $line = "biblioitems.".$line if $line=~/itemtype/;
293 $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ ) ;
294 $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ ) ;
295 $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ ) ;
296 $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ ) ;
297 $linefilter[0] = @$filters[4] if ($line =~ /returndate/ ) ;
298 $linefilter[1] = @$filters[5] if ($line =~ /returndate/ ) ;
299 $linefilter[2] = @$filters[6] if ($line =~ /returndate/ ) ;
300 $linefilter[3] = @$filters[7] if ($line =~ /returndate/ ) ;
301 $linefilter[0] = @$filters[8] if ($line =~ /category/ ) ;
302 $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ ) ;
303 $linefilter[0] = @$filters[10] if ($line =~ /branch/ ) ;
304 # $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
305 $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
306 $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
307 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
309 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
310 $column = "biblioitems.".$column if $column=~/itemtype/;
311 my @colfilter ;
312 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
313 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
314 $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ ) ;
315 $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ ) ;
316 $colfilter[0] = @$filters[4] if ($column =~ /returndate/ ) ;
317 $colfilter[1] = @$filters[5] if ($column =~ /returndate/ ) ;
318 $colfilter[2] = @$filters[6] if ($column =~ /returndate/ ) ;
319 $colfilter[3] = @$filters[7] if ($column =~ /returndate/ ) ;
320 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
321 $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ ) ;
322 $colfilter[0] = @$filters[10] if ($column =~ /branch/ ) ;
323 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
324 $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
325 $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
326 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
328 # 1st, loop rows.
329 my $linefield;
330 my $lineorder;
331 if ((($line =~/timestamp/) and ($podsp == 1)) or (($line =~/returndate/) and ($rodsp == 1))) {
332 #Display by day
333 $linefield .="dayname($line)";
334 $lineorder .="weekday($line)";
335 } elsif ((($line =~/timestamp/) and ($podsp == 2)) or (($line =~/returndate/) and ($rodsp == 2))) {
336 #Display by Month
337 $linefield .="monthname($line)";
338 $lineorder .="month($line)";
339 } elsif ((($line =~/timestamp/) and ($podsp == 3)) or (($line =~/returndate/) and ($rodsp == 3))) {
340 #Display by Year
341 $linefield .="Year($line)";
342 $lineorder .= $line;
343 } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
344 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
345 $lineorder .= $line;
346 } else {
347 $linefield .= $line;
348 $lineorder .= $line;
351 my $strsth;
352 $strsth .= "select distinctrow $linefield
353 FROM `old_issues`
354 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
355 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
356 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
357 LEFT JOIN issuingrules ON
358 (issuingrules.branchcode=old_issues.branchcode
359 AND issuingrules.itemtype=biblioitems.itemtype
360 AND issuingrules.categorycode=borrowers.categorycode)
361 WHERE 1";
363 if (($line=~/timestamp/) or ($line=~/returndate/)){
364 if ($linefilter[1] and ($linefilter[0])){
365 $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
366 } elsif ($linefilter[1]) {
367 $strsth .= " AND $line < \'$linefilter[1]\' " ;
368 } elsif ($linefilter[0]) {
369 $strsth .= " AND $line > \'$linefilter[0]\' " ;
371 if ($linefilter[2]){
372 $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
374 if ($linefilter[3]){
375 $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
377 } elsif ($linefilter[0]) {
378 $linefilter[0] =~ s/\*/%/g;
379 $strsth .= " AND $line LIKE '$linefilter[0]' " ;
381 $strsth .=" GROUP BY $linefield";
382 $strsth .=" ORDER BY $lineorder";
384 my $sth = $dbh->prepare( $strsth );
385 $sth->execute;
388 while ( my ($celvalue) = $sth->fetchrow) {
389 my %cell;
390 if ($celvalue) {
391 $cell{rowtitle} = $celvalue;
392 } else {
393 $cell{rowtitle} = "";
395 $cell{totalrow} = 0;
396 push @loopline, \%cell;
399 # 2nd, loop cols.
400 my $colfield;
401 my $colorder;
402 if ((($column =~/timestamp/) and ($podsp == 1)) or (($column =~/returndate/) and ($rodsp == 1))) {
403 #Display by day
404 $colfield .="dayname($column)";
405 $colorder .="weekday($column)";
406 } elsif ((($column =~/timestamp/) and ($podsp == 2)) or (($column =~/returndate/) and ($rodsp == 2))) {
407 #Display by Month
408 $colfield .="monthname($column)";
409 $colorder .="month($column)";
410 } elsif ((($column =~/timestamp/) and ($podsp == 3)) or (($column =~/returndate/) and ($rodsp == 3))) {
411 #Display by Year
412 $colfield .="Year($column)";
413 $colorder .= $column;
414 } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
415 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
416 $colorder .= $column;
417 } else {
418 $colfield .= $column;
419 $colorder .= $column;
422 my $strsth2;
423 $strsth2 .= "SELECT distinctrow $colfield
424 FROM `old_issues`
425 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
426 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
427 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
428 LEFT JOIN issuingrules ON
429 (issuingrules.branchcode=old_issues.branchcode
430 AND issuingrules.itemtype=biblioitems.itemtype
431 AND issuingrules.categorycode=borrowers.categorycode)
432 WHERE 1";
434 if (($column=~/timestamp/) or ($column=~/returndate/)){
435 if ($colfilter[1] and ($colfilter[0])){
436 $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
437 } elsif ($colfilter[1]) {
438 $strsth2 .= " AND $column < '$colfilter[1]' " ;
439 } elsif ($colfilter[0]) {
440 $strsth2 .= " AND $column > '$colfilter[0]' " ;
442 if ($colfilter[2]){
443 $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
445 if ($colfilter[3]){
446 $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
448 } elsif ($colfilter[0]) {
449 $colfilter[0] =~ s/\*/%/g;
450 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
452 $strsth2 .=" GROUP BY $colfield";
453 $strsth2 .=" ORDER BY $colorder";
454 warn "". $strsth2;
456 my $sth2 = $dbh->prepare( $strsth2 );
457 if (( @colfilter ) and ($colfilter[1])){
458 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
459 } elsif ($colfilter[0]) {
460 $sth2->execute($colfilter[0]);
461 } else {
462 $sth2->execute;
466 while (my ($celvalue) = $sth2->fetchrow) {
467 my %cell;
468 my %ft;
469 # warn "coltitle :".$celvalue;
470 $cell{coltitle} = $celvalue;
471 $ft{totalcol} = 0;
472 push @loopcol, \%cell;
474 # warn "fin des titres colonnes";
476 my $i=0;
477 my @totalcol;
478 my $hilighted=-1;
480 #Initialization of cell values.....
481 my %table;
482 my %wgttable;
483 my %cnttable;
485 # warn "init table";
486 foreach my $row ( @loopline ) {
487 foreach my $col ( @loopcol ) {
488 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
489 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
491 $table{$row->{rowtitle}}->{totalrow}=0;
494 # preparing calculation
495 my $strcalc ;
497 # Processing average loanperiods
498 $strcalc .= "SELECT $linefield, $colfield, ";
499 $strcalc .= " issuedate, returndate, old_issues.timestamp, COUNT(*), date_due, old_issues.renewals, issuelength FROM `old_issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=branchcode AND issuingrules.itemtype=biblioitems.itemtype AND issuingrules.categorycode=categorycode) WHERE old_issues.itemnumber=items.itemnumber AND old_issues.borrowernumber=borrowers.borrowernumber";
501 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
502 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
503 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
504 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
505 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
506 $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
507 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
508 $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
509 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
510 $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
511 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
512 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
513 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
514 $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
515 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
516 $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
517 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
518 $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
519 $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
520 $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
521 $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
522 $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
524 $strcalc .= " group by $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
525 warn "SQL :". $strcalc;
527 my $dbcalc = $dbh->prepare($strcalc);
528 $dbcalc->execute;
529 # warn "filling table";
530 my $issues_count=0;
531 my $previous_row;
532 my $previous_col;
533 my $loanlength;
534 my $err;
535 my $emptycol;
536 my $weightrow;
538 while (my @data = $dbcalc->fetchrow) {
539 my ($row, $col, $issuedate, $returndate, $weight)=@data;
540 # warn "filling table $row / $col / $issuedate / $returndate /$weight";
541 $emptycol=1 if (!defined($col));
542 $col = "zzEMPTY" if (!defined($col));
543 $row = "zzEMPTY" if (!defined($row));
544 # fill returndate to avoid an error with date calc (needed for all non returned issues)
545 $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
546 # DateCalc returns => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes,
547 # and seconds between the two
548 $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
549 # warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
550 # warn "513 row :".$row." column :".$col;
551 $table{$row}->{$col}+=$weight*$loanlength;
552 # $table{$row}->{totalrow}+=$weight*$loanlength;
553 $cnttable{$row}->{$col}= 1;
554 $wgttable{$row}->{$col}+=$weight;
557 push @loopcol,{coltitle => "NULL"} if ($emptycol);
559 foreach my $row ( sort keys %table ) {
560 my @loopcell;
561 #@loopcol ensures the order for columns is common with column titles
562 # and the number matches the number of columns
563 my $colcount=0;
564 foreach my $col ( @loopcol ) {
565 my $value =$table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} / $wgttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} if ($table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}});
567 $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
568 $table{$row}->{totalrow}+=$value;
569 #warn "row : $row col:$col $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
570 $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
571 push @loopcell, {value => ($value)?sprintf("%.2f",$value):0 } ;
573 #warn "row : $row colcount:$colcount";
574 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
575 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
576 'loopcell' => \@loopcell,
577 'hilighted' => ($hilighted >0),
578 'totalrow' => ($total)?sprintf("%.2f",$total):0
580 $hilighted = -$hilighted;
583 # # warn "footer processing";
584 foreach my $col ( @loopcol ) {
585 my $total=0;
586 my $nbrow=0;
587 foreach my $row ( @looprow ) {
588 $total += $cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}}*$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
589 $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
590 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
592 # warn "summ for column ".$col->{coltitle}." = ".$total;
593 $total = $total/$nbrow if ($nbrow);
594 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
599 # the header of the table
600 $globalline{loopfilter}=\@loopfilter;
601 # the core of the table
602 $globalline{looprow} = \@looprow;
603 $globalline{loopcol} = \@loopcol;
604 # # the foot (totals by borrower type)
605 $globalline{loopfooter} = \@loopfooter;
606 $globalline{total}= $grantotal;
607 $globalline{line} = $line;
608 $globalline{column} = $column;
609 push @mainloop,\%globalline;
610 return \@mainloop;