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