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