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