Bug 36072: opac-request-article should check syspref
[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 = C4::Context->csv_delimiter(scalar $input->param("sep"));
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 $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
117
118     my $itemtypes = Koha::ItemTypes->search_with_localization;
119
120     my $dbh = C4::Context->dbh;
121     my $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
122     $req->execute;
123     my @selects1;
124     my $hassort1;
125     while (my ($value) =$req->fetchrow) {
126         $hassort1 =1 if ($value);
127         push @selects1, $value;
128     }
129     my $Sort1 = {
130         values   => \@selects1,
131     };
132     
133     $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
134     $req->execute;
135     my @selects2;
136     my $hassort2;
137     my $hglghtsort2;
138     while (my ($value) =$req->fetchrow) {
139         $hassort2 =1 if ($value);
140         $hglghtsort2= !($hassort1);
141         push @selects2, $value;
142     }
143     my $Sort2 = {
144         values   => \@selects2,
145     };
146     
147     my $CGIsepChoice=GetDelimiterChoices;
148     
149     $template->param(
150                     patron_categories => $patron_categories,
151                     itemtypes    => $itemtypes,
152                     hassort1     => $hassort1,
153                     hassort2     => $hassort2,
154                     HlghtSort2   => $hglghtsort2,
155                     Sort1        => $Sort1,
156                     Sort2        => $Sort2,
157                     CGIsepChoice => $CGIsepChoice
158                     );
159 output_html_with_http_headers $input, $cookie, $template->output;
160 }
161
162
163
164
165 sub calculate {
166     my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
167     my @mainloop;
168     my @loopfooter;
169     my @loopcol;
170     my @loopline;
171     my @looprow;
172     my %globalline;
173     my $grantotal =0;
174     my $itype = C4::Context->preference('item-level_itypes') ? "items.itype" : "biblioitems.itemtype";
175 # extract parameters
176     my $dbh = C4::Context->dbh;
177
178 # Filters
179 # Checking filters
180 #
181     my @loopfilter;
182     for (my $i=0;$i<=6;$i++) {
183         my %cell;
184         if ( @$filters[$i] ) {
185             if (($i==1) and (@$filters[$i-1])) {
186                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
187             }
188             # format the dates filters, otherwise just fill as is
189             if ($i>=4) {
190                 $cell{filter} .= @$filters[$i];
191             } else {
192                 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
193                    if ( @$filters[$i] );
194             }
195             $cell{crit} .="Issue From" if ($i==0);
196             $cell{crit} .="Issue To" if ($i==1);
197             $cell{crit} .="Issue Month" if ($i==2);
198             $cell{crit} .="Issue Day" if ($i==3);
199             $cell{crit} .="Return From" if ($i==4);
200             $cell{crit} .="Return To" if ($i==5);
201             $cell{crit} .="Return Month" if ($i==6);
202             $cell{crit} .="Return Day" if ($i==7);
203             $cell{crit} .="Borrower Cat" if ($i==8);
204             $cell{crit} .="Doc Type" if ($i==9);
205             $cell{crit} .="Branch" if ($i==10);
206             $cell{crit} .="Sort1" if ($i==11);
207             $cell{crit} .="Sort2" if ($i==12);
208             push @loopfilter, \%cell;
209         }
210     }
211     push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
212     push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
213
214     
215     
216     my @linefilter;
217 #       warn "filtres ".@filters[0];
218 #       warn "filtres ".@filters[1];
219 #       warn "filtres ".@filters[2];
220 #       warn "filtres ".@filters[3];
221     $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
222     if ( $line=~/itemtype/ ) { $line = $itype; }
223     $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
224     $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
225     $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
226     $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
227     $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
228     $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
229     $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
230     $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
231     $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
232     $linefilter[0] = @$filters[9] if ($line eq $itype);
233     $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
234     $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
235     $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
236
237     $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
238     if ( $column=~/itemtype/ ) { $column = $itype; }
239     my @colfilter ;
240     $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
241     $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
242     $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
243     $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
244     $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
245     $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
246     $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
247     $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
248     $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
249     $colfilter[0] = @$filters[9] if ($column eq $itype);
250     $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
251     $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
252     $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
253                                             
254 # 1st, loop rows.                             
255     my $linefield;
256     my $lineorder;                               
257     if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
258         #Display by day
259         $linefield .="dayname($line)";  
260         $lineorder .="weekday($line)";  
261     } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
262         #Display by Month
263         $linefield .="monthname($line)";  
264         $lineorder .="month($line)";  
265     } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
266         #Display by Year
267         $linefield .="Year($line)";
268         $lineorder .= $line;  
269     } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
270         $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
271         $lineorder .= $line;  
272     } else {
273         $linefield .= $line;
274         $lineorder .= $line;  
275     }  
276     
277     my $strsth;
278     $strsth .= "select distinctrow $linefield 
279                 FROM `old_issues` 
280                 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
281                 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
282                 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
283                 WHERE 1";
284     
285     if (($line=~/timestamp/) or ($line=~/returndate/)){
286         if ($linefilter[1] and ($linefilter[0])){
287             $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
288         } elsif ($linefilter[1]) {
289                 $strsth .= " AND $line < \'$linefilter[1]\' " ;
290         } elsif ($linefilter[0]) {
291             $strsth .= " AND $line > \'$linefilter[0]\' " ;
292         }
293         if ($linefilter[2]){
294             $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
295         }
296         if ($linefilter[3]){
297             $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
298         }
299     } elsif ($linefilter[0]) {
300         $linefilter[0] =~ s/\*/%/g;
301         $strsth .= " AND $line LIKE '$linefilter[0]' " ;
302     }
303     $strsth .=" GROUP BY $linefield";
304     $strsth .=" ORDER BY $lineorder";
305    
306     my $sth = $dbh->prepare( $strsth );
307     $sth->execute;
308
309     while ( my ($celvalue) = $sth->fetchrow) {
310         my %cell;
311         if ($celvalue) {
312             $cell{rowtitle} = $celvalue;
313         } else {
314             $cell{rowtitle} = "";
315         }
316         $cell{totalrow} = 0;
317         push @loopline, \%cell;
318     }
319
320 # 2nd, loop cols.
321     my $colfield;
322     my $colorder;                               
323     if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
324         #Display by day
325         $colfield .="dayname($column)";  
326         $colorder .="weekday($column)";
327     } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
328         #Display by Month
329         $colfield .="monthname($column)";  
330         $colorder .="month($column)";  
331     } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
332         #Display by Year
333         $colfield .="Year($column)";
334         $colorder .= $column;
335     } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
336         $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
337         $colorder .= $column;
338     } else {
339         $colfield .= $column;
340         $colorder .= $column;
341     }  
342
343     my $strsth2;
344     $strsth2 .= "SELECT distinctrow $colfield 
345                   FROM `old_issues`
346                   LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
347                   LEFT JOIN items  ON items.itemnumber=old_issues.itemnumber  
348                   LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
349                   WHERE 1";
350     
351     if (($column=~/timestamp/) or ($column=~/returndate/)){
352         if ($colfilter[1] and ($colfilter[0])){
353             $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
354         } elsif ($colfilter[1]) {
355                 $strsth2 .= " AND $column < '$colfilter[1]' " ;
356         } elsif ($colfilter[0]) {
357             $strsth2 .= " AND $column > '$colfilter[0]' " ;
358         }
359         if ($colfilter[2]){
360             $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
361         }
362         if ($colfilter[3]){
363             $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
364         }
365     } elsif ($colfilter[0]) {
366         $colfilter[0] =~ s/\*/%/g;
367         $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
368     }
369     $strsth2 .=" GROUP BY $colfield";
370     $strsth2 .=" ORDER BY $colorder";
371     
372     my $sth2 = $dbh->prepare( $strsth2 );
373
374     $sth2->execute;
375
376     while (my ($celvalue) = $sth2->fetchrow) {
377         my %cell;
378         my %ft;
379 #               warn "coltitle :".$celvalue;
380         $cell{coltitle} = $celvalue;
381         $ft{totalcol} = 0;
382         push @loopcol, \%cell;
383     }
384 #       warn "fin des titres colonnes";
385
386     my $i=0;
387     my $hilighted=-1;
388     
389     #Initialization of cell values.....
390     my %table;
391     my %wgttable;
392     my %cnttable;
393     
394 #       warn "init table";
395     foreach my $row ( @loopline ) {
396         foreach my $col ( @loopcol ) {
397 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
398             $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
399         }
400         $table{$row->{rowtitle}}->{totalrow}=0;
401     }
402
403 # preparing calculation
404     my $strcalc ;
405     
406 # Processing average loanperiods
407     $strcalc .= "SELECT $linefield, $colfield, ";
408     $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";
409
410     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
411     $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
412     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
413     $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
414     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
415     $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
416     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
417     $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
418     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
419     $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
420     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
421     $strcalc .= " AND $itype like '" . @$filters[9] ."'" if ( @$filters[9] );
422     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
423     $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
424     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
425     $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
426     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
427     $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
428     $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
429     $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
430     $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
431     $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
432     
433     $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
434     
435     my $dbcalc = $dbh->prepare($strcalc);
436     $dbcalc->execute;
437 #       warn "filling table";
438     my $issues_count=0;
439     my $loanlength; 
440     my $emptycol;
441
442     while (my  @data = $dbcalc->fetchrow) {
443         my ($row, $col, $issuedate, $returndate, $weight)=@data;
444 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
445         $emptycol=1 if (!defined($col));
446         $col = "zzEMPTY" if (!defined($col));
447         $row = "zzEMPTY" if (!defined($row));
448     #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
449     #  and seconds between the two
450         $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
451     #           warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
452     #           warn "513 row :".$row." column :".$col;
453         $table{$row}->{$col}+=$weight*$loanlength;
454     #           $table{$row}->{totalrow}+=$weight*$loanlength;
455         $cnttable{$row}->{$col}= 1;
456         $wgttable{$row}->{$col}+=$weight;
457     }
458     
459     push @loopcol,{coltitle => "NULL"} if ($emptycol);
460     
461     foreach my $row ( sort keys %table ) {
462         my @loopcell;
463     #@loopcol ensures the order for columns is common with column titles
464     # and the number matches the number of columns
465         my $colcount=0;
466         foreach my $col ( @loopcol ) {
467             my $value;
468             if ($table{$row}->{
469                     (        ( $col->{coltitle} eq 'NULL' )
470                           or ( $col->{coltitle} eq q{} )
471                       ) ? 'zzEMPTY' : $col->{coltitle}
472                 }
473               ) {
474                 $value = $table{$row}->{
475                     (        ( $col->{coltitle} eq 'NULL' )
476                           or ( $col->{coltitle} eq q{} )
477                       ) ? 'zzEMPTY' : $col->{coltitle}
478                   } / $wgttable{$row}->{
479                     (        ( $col->{coltitle} eq 'NULL' )
480                           or ( $col->{coltitle} eq q{} )
481                       ) ? 'zzEMPTY' : $col->{coltitle}
482                   };
483             }
484             $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
485             $table{$row}->{totalrow}+=$value;
486             #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
487             $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
488             push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
489         }
490         #warn "row : $row colcount:$colcount";
491         my $total;
492         if ( $colcount > 0 ) {
493             $total = $table{$row}->{totalrow} / $colcount;
494         }
495         push @looprow,
496           { 'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
497             'loopcell' => \@loopcell,
498             'hilighted' => ( $hilighted > 0 ),
499             'totalrow'  => ($total) ? sprintf( "%.2f", $total ) : 0
500           };
501         $hilighted = -$hilighted;
502     }
503 #       
504 # #     warn "footer processing";
505     foreach my $col ( @loopcol ) {
506         my $total=0;
507         my $nbrow=0;
508         foreach my $row ( @looprow ) {
509             $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}};
510             $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
511 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
512         }
513 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
514         $total = $total/$nbrow if ($nbrow);
515         push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
516     
517     }
518             
519
520     # the header of the table
521     $globalline{loopfilter}=\@loopfilter;
522     # the core of the table
523     $globalline{looprow} = \@looprow;
524     $globalline{loopcol} = \@loopcol;
525 #       # the foot (totals by borrower type)
526     $globalline{loopfooter} = \@loopfooter;
527     $globalline{total}= $grantotal;
528     $globalline{line} = $line;
529     $globalline{column} = $column;
530     push @mainloop,\%globalline;
531     return \@mainloop;
532
533 }
534
535 1;