removing koha.mysql. the DB structure is now in $KOHA/installer/kohastructure.sql
[koha.git] / reports / issues_avg_stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
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 Date::Calc qw(Delta_Days);
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38
39 =over2
40
41 =cut
42
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/issues_avg_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $podsp = $input->param("IssueDisplay");
50 my $rodsp = $input->param("ReturnDisplay");
51 my $calc = $input->param("Cellvalue");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
56 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58         = get_template_and_user({template_name => $fullreportname,
59                                 query => $input,
60                                 type => "intranet",
61                                 authnotrequired => 0,
62                                 flagsrequired => {reports => 1},
63                                 debug => 1,
64                                 });
65 $template->param(do_it => $do_it,
66                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
67                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
68                 IntranetNav => C4::Context->preference("IntranetNav"),
69                 );
70 if ($do_it) {
71 # Displaying results
72         my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
73         if ($output eq "screen"){
74 # Printing results to screen
75                 $template->param(mainloop => $results);
76                 output_html_with_http_headers $input, $cookie, $template->output;
77                 exit(1);
78         } else {
79 # Printing to a csv file
80                 print $input->header(-type => 'application/vnd.sun.xml.calc',
81                                      -encoding    => 'utf-8',
82                         -attachment=>"$basename.csv",
83                         -filename=>"$basename.csv" );
84                 my $cols = @$results[0]->{loopcol};
85                 my $lines = @$results[0]->{looprow};
86                 my $sep;
87                 $sep =C4::Context->preference("delimiter");
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 @dels = ( C4::Context->preference("delimiter") );
215         my $CGIsepChoice=CGI::scrolling_list(
216                                 -name     => 'sep',
217                                 -id       => 'sep',
218                                 -values   => \@dels,
219                                 -size     => 1,
220                                 -multiple => 0 );
221         
222         $template->param(
223                                         CGIBorCat => $CGIBorCat,
224                                         CGIItemType => $CGIItemTypes,
225                                         CGIBranch => $CGIBranch,
226                                         hassort1=> $hassort1,
227                                         hassort2=> $hassort2,
228                                         HlghtSort2 => $hglghtsort2,
229                                         CGISort1 => $CGISort1,
230                                         CGISort2 => $CGISort2,
231                                         CGIextChoice => $CGIextChoice,
232                                         CGIsepChoice => $CGIsepChoice
233                                         );
234 output_html_with_http_headers $input, $cookie, $template->output;
235 }
236
237
238
239
240 sub calculate {
241         my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
242         my @mainloop;
243         my @loopfooter;
244         my @loopcol;
245         my @loopline;
246         my @looprow;
247         my %globalline;
248         my $grantotal =0;
249 # extract parameters
250         my $dbh = C4::Context->dbh;
251
252 # Filters
253 # Checking filters
254 #
255         my @loopfilter;
256         for (my $i=0;$i<=6;$i++) {
257                 my %cell;
258                 if ( @$filters[$i] ) {
259                         if (($i==1) and (@$filters[$i-1])) {
260                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
261                         }
262                         $cell{filter} .= @$filters[$i];
263                         $cell{crit} .="Issue From" if ($i==0);
264                         $cell{crit} .="Issue To" if ($i==1);
265                         $cell{crit} .="Issue Month" if ($i==2);
266                         $cell{crit} .="Issue Day" if ($i==3);
267                         $cell{crit} .="Return From" if ($i==4);
268                         $cell{crit} .="Return To" if ($i==5);
269                         $cell{crit} .="Return Month" if ($i==6);
270                         $cell{crit} .="Return Day" if ($i==7);
271                         $cell{crit} .="Borrower Cat" if ($i==8);
272                         $cell{crit} .="Doc Type" if ($i==9);
273                         $cell{crit} .="Branch" if ($i==10);
274                         $cell{crit} .="Sort1" if ($i==11);
275                         $cell{crit} .="Sort2" if ($i==12);
276                         push @loopfilter, \%cell;
277                 }
278         }
279         push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
280         push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
281
282         
283         
284         my @linefilter;
285 #       warn "filtres ".@filters[0];
286 #       warn "filtres ".@filters[1];
287 #       warn "filtres ".@filters[2];
288 #       warn "filtres ".@filters[3];
289         $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
290         $line = "biblioitems.".$line if $line=~/itemtype/;
291         
292         $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
293         $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
294         $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
295         $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
296         $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
297         $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
298         $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
299         $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
300         $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
301         $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ )  ;
302         $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
303 #       $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
304         $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
305         $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
306 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
307
308         $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
309         $column = "biblioitems.".$column if $column=~/itemtype/;
310         my @colfilter ;
311         $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
312         $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
313         $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
314         $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
315         $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
316         $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
317         $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
318         $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
319         $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
320         $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ )  ;
321         $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
322 #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
323         $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
324         $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
325 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
326                                               
327 # 1st, loop rows.                             
328         my $linefield;
329         my $lineorder;                               
330         if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
331                 #Display by day
332                 $linefield .="dayname($line)";  
333                 $lineorder .="weekday($line)";  
334         } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
335                 #Display by Month
336                 $linefield .="monthname($line)";  
337                 $lineorder .="month($line)";  
338         } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
339                 #Display by Year
340                 $linefield .="Year($line)";
341                 $lineorder .= $line;  
342         } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
343                 $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
344                 $lineorder .= $line;  
345         } else {
346                 $linefield .= $line;
347                 $lineorder .= $line;  
348         }  
349         
350         my $strsth;
351         $strsth .= "select distinctrow $linefield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND  issuingrules.itemtype=biblioitems.itemtype AND  issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
352         
353         if (($line=~/timestamp/) or ($line=~/returndate/)){
354                 if ($linefilter[1] and ($linefilter[0])){
355                         $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
356                 } elsif ($linefilter[1]) {
357                                 $strsth .= " and $line < \'$linefilter[1]\' " ;
358                 } elsif ($linefilter[0]) {
359                         $strsth .= " and $line > \'$linefilter[0]\' " ;
360                 }
361                 if ($linefilter[2]){
362                         $strsth .= " and dayname($line) = '$linefilter[2]' " ;
363                 }
364                 if ($linefilter[3]){
365                         $strsth .= " and monthname($line) = '$linefilter[3]' " ;
366                 }
367         } elsif ($linefilter[0]) {
368                 $linefilter[0] =~ s/\*/%/g;
369                 $strsth .= " and $line LIKE '$linefilter[0]' " ;
370         }
371         $strsth .=" group by $linefield";
372         $strsth .=" order by $lineorder";
373         warn "". $strsth;
374         
375         my $sth = $dbh->prepare( $strsth );
376         $sth->execute;
377
378         
379         while ( my ($celvalue) = $sth->fetchrow) {
380                 my %cell;
381                 if ($celvalue) {
382                         $cell{rowtitle} = $celvalue;
383                 } else {
384                         $cell{rowtitle} = "";
385                 }
386                 $cell{totalrow} = 0;
387                 push @loopline, \%cell;
388         }
389
390 # 2nd, loop cols.
391         my $colfield;
392         my $colorder;                               
393         if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
394                 #Display by day
395                 $colfield .="dayname($column)";  
396                 $colorder .="weekday($column)";
397         } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
398                 #Display by Month
399                 $colfield .="monthname($column)";  
400                 $colorder .="month($column)";  
401         } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
402                 #Display by Year
403                 $colfield .="Year($column)";
404                 $colorder .= $column;
405         } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
406                 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
407                 $colorder .= $column;
408         } else {
409                 $colfield .= $column;
410                 $colorder .= $column;
411         }  
412         
413         my $strsth2;
414         $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND  issuingrules.itemtype=biblioitems.itemtype AND  issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
415         
416         if (($column=~/timestamp/) or ($column=~/returndate/)){
417                 if ($colfilter[1] and ($colfilter[0])){
418                         $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
419                 } elsif ($colfilter[1]) {
420                                 $strsth2 .= " and $column < '$colfilter[1]' " ;
421                 } elsif ($colfilter[0]) {
422                         $strsth2 .= " and $column > '$colfilter[0]' " ;
423                 }
424                 if ($colfilter[2]){
425                         $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
426                 }
427                 if ($colfilter[3]){
428                         $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
429                 }
430         } elsif ($colfilter[0]) {
431                 $colfilter[0] =~ s/\*/%/g;
432                 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
433         }
434         $strsth2 .=" group by $colfield";
435         $strsth2 .=" order by $colorder";
436         warn "". $strsth2;
437         
438         my $sth2 = $dbh->prepare( $strsth2 );
439         if (( @colfilter ) and ($colfilter[1])){
440                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
441         } elsif ($colfilter[0]) {
442                 $sth2->execute($colfilter[0]);
443         } else {
444                 $sth2->execute;
445         }
446         
447
448         while (my ($celvalue) = $sth2->fetchrow) {
449                 my %cell;
450                 my %ft;
451 #               warn "coltitle :".$celvalue;
452                 $cell{coltitle} = $celvalue;
453                 $ft{totalcol} = 0;
454                 push @loopcol, \%cell;
455         }
456 #       warn "fin des titres colonnes";
457
458         my $i=0;
459         my @totalcol;
460         my $hilighted=-1;
461         
462         #Initialization of cell values.....
463         my %table;
464         my %wgttable;
465         my %cnttable;
466         
467 #       warn "init table";
468         foreach my $row ( @loopline ) {
469                 foreach my $col ( @loopcol ) {
470 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
471                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
472                 }
473                 $table{$row->{rowtitle}}->{totalrow}=0;
474         }
475
476 # preparing calculation
477         my $strcalc ;
478         
479 # Processing average loanperiods
480         $strcalc .= "SELECT $linefield, $colfield, ";
481         $strcalc .= " issuedate, returndate, COUNT(*), date_due, issues.renewals, issuelength FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN issuingrules ON (issuingrules.branchcode=issues.branchcode AND  issuingrules.itemtype=biblioitems.itemtype AND  issuingrules.categorycode=borrowers.categorycode) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
482
483         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
484         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
485         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
486         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
487         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
488         $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
489         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
490         $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
491         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
492         $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
493         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
494         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
495         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
496         $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
497         @$filters[11]=~ s/\*/%/g if (@$filters[11]);
498         $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
499         @$filters[12]=~ s/\*/%/g if (@$filters[12]);
500         $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
501         $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
502         $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
503         $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
504         $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
505         
506         $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
507         warn "SQL :". $strcalc;
508         
509         my $dbcalc = $dbh->prepare($strcalc);
510         $dbcalc->execute;
511 #       warn "filling table";
512         my $issues_count=0;
513         my $previous_row; 
514         my $previous_col;
515         my $loanlength; 
516         my $err;
517         my $emptycol;
518         my $weightrow;
519         
520         while (my  @data = $dbcalc->fetchrow) {
521                 my ($row, $col, $issuedate, $returndate, $weight)=@data;
522 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
523                 $emptycol=1 if ($col eq undef);
524                 $col = "zzEMPTY" if ($col eq undef);
525                 $row = "zzEMPTY" if ($row eq undef);
526 #               warn "506 row :".$row." column :".$col;
527                 my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
528 #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
529 #  and seconds between the two
530                 $loanlength = $result;
531 #               warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
532 #               warn "513 row :".$row." column :".$col;
533                 $table{$row}->{$col}+=$weight*$loanlength;
534 #               $table{$row}->{totalrow}+=$weight*$loanlength;
535                 $cnttable{$row}->{$col}= 1;
536                 $wgttable{$row}->{$col}+=$weight;
537         }
538         
539         push @loopcol,{coltitle => "NULL"} if ($emptycol);
540         
541         foreach my $row ( sort keys %table ) {
542                 my @loopcell;
543         #@loopcol ensures the order for columns is common with column titles
544         # and the number matches the number of columns
545                 my $colcount=0;
546                 foreach my $col ( @loopcol ) {
547                         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}});
548
549                         $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
550                         $table{$row}->{totalrow}+=$value;
551                         #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
552                         $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
553                         push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
554                 }
555                 #warn "row : $row colcount:$colcount";
556                 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
557                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
558                                                 'loopcell' => \@loopcell,
559                                                 'hilighted' => ($hilighted >0),
560                                                 'totalrow' => ($total)?sprintf("%.2f",$total):0
561                                         };
562                 $hilighted = -$hilighted;
563         }
564 #       
565 # #     warn "footer processing";
566         foreach my $col ( @loopcol ) {
567                 my $total=0;
568                 my $nbrow=0;
569                 foreach my $row ( @looprow ) {
570                         $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}};
571                         $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
572 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
573                 }
574 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
575                 $total = $total/$nbrow if ($nbrow);
576                 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
577         
578         }
579                         
580
581         # the header of the table
582         $globalline{loopfilter}=\@loopfilter;
583         # the core of the table
584         $globalline{looprow} = \@looprow;
585         $globalline{loopcol} = \@loopcol;
586 #       # the foot (totals by borrower type)
587         $globalline{loopfooter} = \@loopfooter;
588         $globalline{total}= $grantotal;
589         $globalline{line} = $line;
590         $globalline{column} = $column;
591         push @mainloop,\%globalline;
592         return \@mainloop;
593 }
594
595 1;