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