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