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