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