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