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