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