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