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