fix for bug 2543: report output by tabulation a bit too literal
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use Date::Calc qw(Delta_Days);
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38 =over 2
39
40 =cut
41
42 my $input = new CGI;
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_avg_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso($filters[0]);
49 $filters[1]=format_date_in_iso($filters[1]);
50 $filters[2]=format_date_in_iso($filters[2]);
51 $filters[3]=format_date_in_iso($filters[3]);
52 my $podsp = $input->param("IssueDisplay");
53 my $rodsp = $input->param("ReturnDisplay");
54 my $calc = $input->param("Cellvalue");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 #warn "calcul : ".$calc;
59 my ($template, $borrowernumber, $cookie)
60     = get_template_and_user({template_name => $fullreportname,
61                 query => $input,
62                 type => "intranet",
63                 authnotrequired => 0,
64                 flagsrequired => {reports => 1},
65                 debug => 1,
66                     });
67 our $sep     = $input->param("sep");
68 $sep = "\t" if ($sep eq 'tabulation');
69 $template->param(do_it => $do_it,
70         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
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(1);
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(1);
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     my $branches=GetBranches();
164     my @select_branch;
165     my %select_branches;
166     push @select_branch,"";
167     $select_branches{""} = "";
168     foreach my $branch (keys %$branches) {
169         push @select_branch, $branch;
170         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
171     }
172     my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
173                 -id => 'branch',
174                 -values   => \@select_branch,
175                 -labels   => \%select_branches,
176                 -size     => 1,
177                 -multiple => 0 );
178     
179     my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
180                 -id => 'sort1',
181                 -values   => \@select,
182                 -size     => 1,
183                 -multiple => 0 );
184     
185     $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
186     $req->execute;
187     undef @select;
188     push @select,"";
189     my $hassort2;
190     my $hglghtsort2;
191     while (my ($value) =$req->fetchrow) {
192         $hassort2 =1 if ($value);
193         $hglghtsort2= !($hassort1);
194         push @select, $value;
195     }
196     my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
197                 -id => 'sort2',
198                 -values   => \@select,
199                 -size     => 1,
200                 -multiple => 0 );
201     
202     my @mime = ( C4::Context->preference("MIME") );
203 #       foreach my $mime (@mime){
204 #               warn "".$mime;
205 #       }
206     
207     my $CGIextChoice=CGI::scrolling_list(
208                 -name     => 'MIME',
209                 -id       => 'MIME',
210                 -values   => \@mime,
211                 -size     => 1,
212                 -multiple => 0 );
213     
214     my $CGIsepChoice=GetDelimiterChoices;
215     
216     $template->param(
217                     CGIBorCat => $CGIBorCat,
218                     CGIItemType => $CGIItemTypes,
219                     CGIBranch => $CGIBranch,
220                     hassort1=> $hassort1,
221                     hassort2=> $hassort2,
222                     HlghtSort2 => $hglghtsort2,
223                     CGISort1 => $CGISort1,
224                     CGISort2 => $CGISort2,
225                     CGIextChoice => $CGIextChoice,
226                     CGIsepChoice => $CGIsepChoice
227                     );
228 output_html_with_http_headers $input, $cookie, $template->output;
229 }
230
231
232
233
234 sub calculate {
235     my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
236     my @mainloop;
237     my @loopfooter;
238     my @loopcol;
239     my @loopline;
240     my @looprow;
241     my %globalline;
242     my $grantotal =0;
243 # extract parameters
244     my $dbh = C4::Context->dbh;
245
246 # Filters
247 # Checking filters
248 #
249     my @loopfilter;
250     for (my $i=0;$i<=6;$i++) {
251         my %cell;
252         if ( @$filters[$i] ) {
253             if (($i==1) and (@$filters[$i-1])) {
254                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
255             }
256             # format the dates filters, otherwise just fill as is
257             if ($i>=4) {
258                 $cell{filter} .= @$filters[$i];
259             } else {
260                 $cell{filter} .= format_date(@$filters[$i]);
261             }
262             $cell{crit} .="Issue From" if ($i==0);
263             $cell{crit} .="Issue To" if ($i==1);
264             $cell{crit} .="Issue Month" if ($i==2);
265             $cell{crit} .="Issue Day" if ($i==3);
266             $cell{crit} .="Return From" if ($i==4);
267             $cell{crit} .="Return To" if ($i==5);
268             $cell{crit} .="Return Month" if ($i==6);
269             $cell{crit} .="Return Day" if ($i==7);
270             $cell{crit} .="Borrower Cat" if ($i==8);
271             $cell{crit} .="Doc Type" if ($i==9);
272             $cell{crit} .="Branch" if ($i==10);
273             $cell{crit} .="Sort1" if ($i==11);
274             $cell{crit} .="Sort2" if ($i==12);
275             push @loopfilter, \%cell;
276         }
277     }
278     push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
279     push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
280
281     
282     
283     my @linefilter;
284 #       warn "filtres ".@filters[0];
285 #       warn "filtres ".@filters[1];
286 #       warn "filtres ".@filters[2];
287 #       warn "filtres ".@filters[3];
288     $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
289     $line = "biblioitems.".$line if $line=~/itemtype/;
290     
291     $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
292     $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
293     $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
294     $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
295     $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
296     $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
297     $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
298     $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
299     $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
300     $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ )  ;
301     $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
302 #       $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
303     $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
304     $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
305 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
306
307     $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
308     $column = "biblioitems.".$column if $column=~/itemtype/;
309     my @colfilter ;
310     $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
311     $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
312     $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
313     $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
314     $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
315     $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
316     $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
317     $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
318     $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
319     $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ )  ;
320     $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
321 #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
322     $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
323     $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
324 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
325                                             
326 # 1st, loop rows.                             
327     my $linefield;
328     my $lineorder;                               
329     if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
330         #Display by day
331         $linefield .="dayname($line)";  
332         $lineorder .="weekday($line)";  
333     } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
334         #Display by Month
335         $linefield .="monthname($line)";  
336         $lineorder .="month($line)";  
337     } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
338         #Display by Year
339         $linefield .="Year($line)";
340         $lineorder .= $line;  
341     } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
342         $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
343         $lineorder .= $line;  
344     } else {
345         $linefield .= $line;
346         $lineorder .= $line;  
347     }  
348     
349     my $strsth;
350     $strsth .= "select distinctrow $linefield 
351                 FROM `old_issues` 
352                 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
353                 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
354                 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
355                 LEFT JOIN issuingrules ON 
356                     (issuingrules.branchcode=old_issues.branchcode
357                     AND  issuingrules.itemtype=biblioitems.itemtype 
358                     AND  issuingrules.categorycode=borrowers.categorycode) 
359                 WHERE 1";
360     
361     if (($line=~/timestamp/) or ($line=~/returndate/)){
362         if ($linefilter[1] and ($linefilter[0])){
363             $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
364         } elsif ($linefilter[1]) {
365                 $strsth .= " AND $line < \'$linefilter[1]\' " ;
366         } elsif ($linefilter[0]) {
367             $strsth .= " AND $line > \'$linefilter[0]\' " ;
368         }
369         if ($linefilter[2]){
370             $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
371         }
372         if ($linefilter[3]){
373             $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
374         }
375     } elsif ($linefilter[0]) {
376         $linefilter[0] =~ s/\*/%/g;
377         $strsth .= " AND $line LIKE '$linefilter[0]' " ;
378     }
379     $strsth .=" GROUP BY $linefield";
380     $strsth .=" ORDER BY $lineorder";
381    
382     my $sth = $dbh->prepare( $strsth );
383     $sth->execute;
384
385     
386     while ( my ($celvalue) = $sth->fetchrow) {
387         my %cell;
388         if ($celvalue) {
389             $cell{rowtitle} = $celvalue;
390         } else {
391             $cell{rowtitle} = "";
392         }
393         $cell{totalrow} = 0;
394         push @loopline, \%cell;
395     }
396
397 # 2nd, loop cols.
398     my $colfield;
399     my $colorder;                               
400     if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
401         #Display by day
402         $colfield .="dayname($column)";  
403         $colorder .="weekday($column)";
404     } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
405         #Display by Month
406         $colfield .="monthname($column)";  
407         $colorder .="month($column)";  
408     } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
409         #Display by Year
410         $colfield .="Year($column)";
411         $colorder .= $column;
412     } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
413         $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
414         $colorder .= $column;
415     } else {
416         $colfield .= $column;
417         $colorder .= $column;
418     }  
419     
420     my $strsth2;
421     $strsth2 .= "SELECT distinctrow $colfield 
422                   FROM `old_issues`
423                   LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
424                   LEFT JOIN items  ON items.itemnumber=old_issues.itemnumber  
425                   LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
426                   LEFT JOIN issuingrules ON 
427                     (issuingrules.branchcode=old_issues.branchcode 
428                     AND  issuingrules.itemtype=biblioitems.itemtype 
429                     AND  issuingrules.categorycode=borrowers.categorycode) 
430                   WHERE 1";
431     
432     if (($column=~/timestamp/) or ($column=~/returndate/)){
433         if ($colfilter[1] and ($colfilter[0])){
434             $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
435         } elsif ($colfilter[1]) {
436                 $strsth2 .= " AND $column < '$colfilter[1]' " ;
437         } elsif ($colfilter[0]) {
438             $strsth2 .= " AND $column > '$colfilter[0]' " ;
439         }
440         if ($colfilter[2]){
441             $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
442         }
443         if ($colfilter[3]){
444             $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
445         }
446     } elsif ($colfilter[0]) {
447         $colfilter[0] =~ s/\*/%/g;
448         $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
449     }
450     $strsth2 .=" GROUP BY $colfield";
451     $strsth2 .=" ORDER BY $colorder";
452     warn "". $strsth2;
453     
454     my $sth2 = $dbh->prepare( $strsth2 );
455     if (( @colfilter ) and ($colfilter[1])){
456         $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
457     } elsif ($colfilter[0]) {
458         $sth2->execute($colfilter[0]);
459     } else {
460         $sth2->execute;
461     }
462     
463
464     while (my ($celvalue) = $sth2->fetchrow) {
465         my %cell;
466         my %ft;
467 #               warn "coltitle :".$celvalue;
468         $cell{coltitle} = $celvalue;
469         $ft{totalcol} = 0;
470         push @loopcol, \%cell;
471     }
472 #       warn "fin des titres colonnes";
473
474     my $i=0;
475     my @totalcol;
476     my $hilighted=-1;
477     
478     #Initialization of cell values.....
479     my %table;
480     my %wgttable;
481     my %cnttable;
482     
483 #       warn "init table";
484     foreach my $row ( @loopline ) {
485         foreach my $col ( @loopcol ) {
486 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
487             $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
488         }
489         $table{$row->{rowtitle}}->{totalrow}=0;
490     }
491
492 # preparing calculation
493     my $strcalc ;
494     
495 # Processing average loanperiods
496     $strcalc .= "SELECT $linefield, $colfield, ";
497     $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";
498
499     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
500     $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
501     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
502     $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
503     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
504     $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
505     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
506     $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
507     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
508     $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
509     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
510     $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
511     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
512     $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
513     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
514     $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
515     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
516     $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
517     $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
518     $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
519     $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
520     $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
521     
522     $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
523     warn "SQL :". $strcalc;
524     
525     my $dbcalc = $dbh->prepare($strcalc);
526     $dbcalc->execute;
527 #       warn "filling table";
528     my $issues_count=0;
529     my $previous_row; 
530     my $previous_col;
531     my $loanlength; 
532     my $err;
533     my $emptycol;
534     my $weightrow;
535     
536     while (my  @data = $dbcalc->fetchrow) {
537         my ($row, $col, $issuedate, $returndate, $weight)=@data;
538 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
539         $emptycol=1 if (!defined($col));
540         $col = "zzEMPTY" if (!defined($col));
541         $row = "zzEMPTY" if (!defined($row));
542         # fill returndate to avoid an error with date calc (needed for all non returned issues)
543         $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
544     #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
545     #  and seconds between the two
546         $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
547     #           warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
548     #           warn "513 row :".$row." column :".$col;
549         $table{$row}->{$col}+=$weight*$loanlength;
550     #           $table{$row}->{totalrow}+=$weight*$loanlength;
551         $cnttable{$row}->{$col}= 1;
552         $wgttable{$row}->{$col}+=$weight;
553     }
554     
555     push @loopcol,{coltitle => "NULL"} if ($emptycol);
556     
557     foreach my $row ( sort keys %table ) {
558         my @loopcell;
559     #@loopcol ensures the order for columns is common with column titles
560     # and the number matches the number of columns
561         my $colcount=0;
562         foreach my $col ( @loopcol ) {
563             my $value =$table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} / $wgttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} if ($table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}});
564
565             $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
566             $table{$row}->{totalrow}+=$value;
567             #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
568             $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
569             push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
570         }
571         #warn "row : $row colcount:$colcount";
572         my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
573         push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
574                         'loopcell' => \@loopcell,
575                         'hilighted' => ($hilighted >0),
576                         'totalrow' => ($total)?sprintf("%.2f",$total):0
577                     };
578         $hilighted = -$hilighted;
579     }
580 #       
581 # #     warn "footer processing";
582     foreach my $col ( @loopcol ) {
583         my $total=0;
584         my $nbrow=0;
585         foreach my $row ( @looprow ) {
586             $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}};
587             $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
588 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
589         }
590 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
591         $total = $total/$nbrow if ($nbrow);
592         push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
593     
594     }
595             
596
597     # the header of the table
598     $globalline{loopfilter}=\@loopfilter;
599     # the core of the table
600     $globalline{looprow} = \@looprow;
601     $globalline{loopcol} = \@loopcol;
602 #       # the foot (totals by borrower type)
603     $globalline{loopfooter} = \@loopfooter;
604     $globalline{total}= $grantotal;
605     $globalline{line} = $line;
606     $globalline{column} = $column;
607     push @mainloop,\%globalline;
608     return \@mainloop;
609 }
610
611 1;