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