moving itemslost to reports from tools
[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 =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 my $podsp = $input->param("IssueDisplay");
49 my $rodsp = $input->param("ReturnDisplay");
50 my $calc = $input->param("Cellvalue");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57         = get_template_and_user({template_name => $fullreportname,
58                                 query => $input,
59                                 type => "intranet",
60                                 authnotrequired => 0,
61                                 flagsrequired => {reports => 1},
62                                 debug => 1,
63                                 });
64 $template->param(do_it => $do_it,
65                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
66                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
67                 IntranetNav => C4::Context->preference("IntranetNav"),
68                 );
69 if ($do_it) {
70 # Displaying results
71         my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
72         if ($output eq "screen"){
73 # Printing results to screen
74                 $template->param(mainloop => $results);
75                 output_html_with_http_headers $input, $cookie, $template->output;
76                 exit(1);
77         } else {
78 # Printing to a csv file
79                 print $input->header(-type => 'application/vnd.sun.xml.calc',
80                                      -encoding    => 'utf-8',
81                         -attachment=>"$basename.csv",
82                         -filename=>"$basename.csv" );
83                 my $cols = @$results[0]->{loopcol};
84                 my $lines = @$results[0]->{looprow};
85                 my $sep;
86                 $sep =C4::Context->preference("delimiter");
87 # header top-right
88                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
89 # Other header
90                 foreach my $col ( @$cols ) {
91                         print $col->{coltitle}.$sep;
92                 }
93                 print "Total\n";
94 # Table
95                 foreach my $line ( @$lines ) {
96                         my $x = $line->{loopcell};
97                         print $line->{rowtitle}.$sep;
98                         foreach my $cell (@$x) {
99                                 print $cell->{value}.$sep;
100                         }
101                         print $line->{totalrow};
102                         print "\n";
103                 }
104 # footer
105                 print "TOTAL";
106                 $cols = @$results[0]->{loopfooter};
107                 foreach my $col ( @$cols ) {
108                         print $sep.$col->{totalcol};
109                 }
110                 print $sep.@$results[0]->{total};
111                 exit(1);
112         }
113 # Displaying choices
114 } else {
115         my $dbh = C4::Context->dbh;
116         my @values;
117         my %labels;
118         my %select;
119         my $req;
120         $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
121         $req->execute;
122         my @select;
123         push @select,"";
124         $select{""}="";
125         while (my ($value, $desc) =$req->fetchrow) {
126                 push @select, $value;
127                 $select{$value}=$desc;
128         }
129         my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
130                                 -id => 'borcat',
131                                 -values   => \@select,
132                                 -labels   => \%select,
133                                 -size     => 1,
134                                 -multiple => 0 );
135         
136         $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
137         $req->execute;
138         undef @select;
139         undef %select;
140         push @select,"";
141         $select{""}="";
142         while (my ($value,$desc) =$req->fetchrow) {
143                 push @select, $value;
144                 $select{$value}=$desc;
145         }
146         my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
147                                 -id => 'itemtypes',
148                                 -values   => \@select,
149                                 -labels    => \%select,
150                                 -size     => 1,
151                                 -multiple => 0 );
152         
153         $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
154         $req->execute;
155         undef @select;
156         push @select,"";
157         my $hassort1;
158         while (my ($value) =$req->fetchrow) {
159                 $hassort1 =1 if ($value);
160                 push @select, $value;
161         }
162         my $branches=GetBranches();
163         my @select_branch;
164         my %select_branches;
165         push @select_branch,"";
166         $select_branches{""} = "";
167         foreach my $branch (keys %$branches) {
168                 push @select_branch, $branch;
169                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
170         }
171         my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
172                                 -id => 'branch',
173                                 -values   => \@select_branch,
174                                 -labels   => \%select_branches,
175                                 -size     => 1,
176                                 -multiple => 0 );
177         
178         my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
179                                 -id => 'sort1',
180                                 -values   => \@select,
181                                 -size     => 1,
182                                 -multiple => 0 );
183         
184         $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
185         $req->execute;
186         undef @select;
187         push @select,"";
188         my $hassort2;
189         my $hglghtsort2;
190         while (my ($value) =$req->fetchrow) {
191                 $hassort2 =1 if ($value);
192                 $hglghtsort2= !($hassort1);
193                 push @select, $value;
194         }
195         my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
196                                 -id => 'sort2',
197                                 -values   => \@select,
198                                 -size     => 1,
199                                 -multiple => 0 );
200         
201         my @mime = ( C4::Context->preference("MIME") );
202 #       foreach my $mime (@mime){
203 #               warn "".$mime;
204 #       }
205         
206         my $CGIextChoice=CGI::scrolling_list(
207                                 -name     => 'MIME',
208                                 -id       => 'MIME',
209                                 -values   => \@mime,
210                                 -size     => 1,
211                                 -multiple => 0 );
212         
213         my @dels = ( C4::Context->preference("delimiter") );
214         my $CGIsepChoice=CGI::scrolling_list(
215                                 -name     => 'sep',
216                                 -id       => 'sep',
217                                 -values   => \@dels,
218                                 -size     => 1,
219                                 -multiple => 0 );
220         
221         $template->param(
222                                         CGIBorCat => $CGIBorCat,
223                                         CGIItemType => $CGIItemTypes,
224                                         CGIBranch => $CGIBranch,
225                                         hassort1=> $hassort1,
226                                         hassort2=> $hassort2,
227                                         HlghtSort2 => $hglghtsort2,
228                                         CGISort1 => $CGISort1,
229                                         CGISort2 => $CGISort2,
230                                         CGIextChoice => $CGIextChoice,
231                                         CGIsepChoice => $CGIsepChoice
232                                         );
233 output_html_with_http_headers $input, $cookie, $template->output;
234 }
235
236
237
238
239 sub calculate {
240         my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
241         my @mainloop;
242         my @loopfooter;
243         my @loopcol;
244         my @loopline;
245         my @looprow;
246         my %globalline;
247         my $grantotal =0;
248 # extract parameters
249         my $dbh = C4::Context->dbh;
250
251 # Filters
252 # Checking filters
253 #
254         my @loopfilter;
255         for (my $i=0;$i<=6;$i++) {
256                 my %cell;
257                 if ( @$filters[$i] ) {
258                         if (($i==1) and (@$filters[$i-1])) {
259                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
260                         }
261                         $cell{filter} .= @$filters[$i];
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 = "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 = "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 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";
351         
352         if (($line=~/timestamp/) or ($line=~/returndate/)){
353                 if ($linefilter[1] and ($linefilter[0])){
354                         $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
355                 } elsif ($linefilter[1]) {
356                                 $strsth .= " and $line < \'$linefilter[1]\' " ;
357                 } elsif ($linefilter[0]) {
358                         $strsth .= " and $line > \'$linefilter[0]\' " ;
359                 }
360                 if ($linefilter[2]){
361                         $strsth .= " and dayname($line) = '$linefilter[2]' " ;
362                 }
363                 if ($linefilter[3]){
364                         $strsth .= " and monthname($line) = '$linefilter[3]' " ;
365                 }
366         } elsif ($linefilter[0]) {
367                 $linefilter[0] =~ s/\*/%/g;
368                 $strsth .= " and $line LIKE '$linefilter[0]' " ;
369         }
370         $strsth .=" group by $linefield";
371         $strsth .=" order by $lineorder";
372         warn "". $strsth;
373         
374         my $sth = $dbh->prepare( $strsth );
375         $sth->execute;
376
377         
378         while ( my ($celvalue) = $sth->fetchrow) {
379                 my %cell;
380                 if ($celvalue) {
381                         $cell{rowtitle} = $celvalue;
382                 } else {
383                         $cell{rowtitle} = "";
384                 }
385                 $cell{totalrow} = 0;
386                 push @loopline, \%cell;
387         }
388
389 # 2nd, loop cols.
390         my $colfield;
391         my $colorder;                               
392         if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
393                 #Display by day
394                 $colfield .="dayname($column)";  
395                 $colorder .="weekday($column)";
396         } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
397                 #Display by Month
398                 $colfield .="monthname($column)";  
399                 $colorder .="month($column)";  
400         } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
401                 #Display by Year
402                 $colfield .="Year($column)";
403                 $colorder .= $column;
404         } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
405                 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
406                 $colorder .= $column;
407         } else {
408                 $colfield .= $column;
409                 $colorder .= $column;
410         }  
411         
412         my $strsth2;
413         $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";
414         
415         if (($column=~/timestamp/) or ($column=~/returndate/)){
416                 if ($colfilter[1] and ($colfilter[0])){
417                         $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
418                 } elsif ($colfilter[1]) {
419                                 $strsth2 .= " and $column < '$colfilter[1]' " ;
420                 } elsif ($colfilter[0]) {
421                         $strsth2 .= " and $column > '$colfilter[0]' " ;
422                 }
423                 if ($colfilter[2]){
424                         $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
425                 }
426                 if ($colfilter[3]){
427                         $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
428                 }
429         } elsif ($colfilter[0]) {
430                 $colfilter[0] =~ s/\*/%/g;
431                 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
432         }
433         $strsth2 .=" group by $colfield";
434         $strsth2 .=" order by $colorder";
435         warn "". $strsth2;
436         
437         my $sth2 = $dbh->prepare( $strsth2 );
438         if (( @colfilter ) and ($colfilter[1])){
439                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
440         } elsif ($colfilter[0]) {
441                 $sth2->execute($colfilter[0]);
442         } else {
443                 $sth2->execute;
444         }
445         
446
447         while (my ($celvalue) = $sth2->fetchrow) {
448                 my %cell;
449                 my %ft;
450 #               warn "coltitle :".$celvalue;
451                 $cell{coltitle} = $celvalue;
452                 $ft{totalcol} = 0;
453                 push @loopcol, \%cell;
454         }
455 #       warn "fin des titres colonnes";
456
457         my $i=0;
458         my @totalcol;
459         my $hilighted=-1;
460         
461         #Initialization of cell values.....
462         my %table;
463         my %wgttable;
464         my %cnttable;
465         
466 #       warn "init table";
467         foreach my $row ( @loopline ) {
468                 foreach my $col ( @loopcol ) {
469 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
470                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
471                 }
472                 $table{$row->{rowtitle}}->{totalrow}=0;
473         }
474
475 # preparing calculation
476         my $strcalc ;
477         
478 # Processing average loanperiods
479         $strcalc .= "SELECT $linefield, $colfield, ";
480         $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";
481
482         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
483         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
484         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
485         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
486         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
487         $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
488         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
489         $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
490         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
491         $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
492         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
493         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
494         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
495         $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
496         @$filters[11]=~ s/\*/%/g if (@$filters[11]);
497         $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
498         @$filters[12]=~ s/\*/%/g if (@$filters[12]);
499         $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
500         $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
501         $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
502         $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
503         $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
504         
505         $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
506         warn "SQL :". $strcalc;
507         
508         my $dbcalc = $dbh->prepare($strcalc);
509         $dbcalc->execute;
510 #       warn "filling table";
511         my $issues_count=0;
512         my $previous_row; 
513         my $previous_col;
514         my $loanlength; 
515         my $err;
516         my $emptycol;
517         my $weightrow;
518         
519         while (my  @data = $dbcalc->fetchrow) {
520                 my ($row, $col, $issuedate, $returndate, $weight)=@data;
521 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
522                 $emptycol=1 if ($col eq undef);
523                 $col = "zzEMPTY" if ($col eq undef);
524                 $row = "zzEMPTY" if ($row eq undef);
525 #               warn "506 row :".$row." column :".$col;
526                 my $result =Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
527 #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
528 #  and seconds between the two
529                 $loanlength = $result;
530 #               warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
531 #               warn "513 row :".$row." column :".$col;
532                 $table{$row}->{$col}+=$weight*$loanlength;
533 #               $table{$row}->{totalrow}+=$weight*$loanlength;
534                 $cnttable{$row}->{$col}= 1;
535                 $wgttable{$row}->{$col}+=$weight;
536         }
537         
538         push @loopcol,{coltitle => "NULL"} if ($emptycol);
539         
540         foreach my $row ( sort keys %table ) {
541                 my @loopcell;
542         #@loopcol ensures the order for columns is common with column titles
543         # and the number matches the number of columns
544                 my $colcount=0;
545                 foreach my $col ( @loopcol ) {
546                         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}});
547
548                         $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
549                         $table{$row}->{totalrow}+=$value;
550                         #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
551                         $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
552                         push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
553                 }
554                 #warn "row : $row colcount:$colcount";
555                 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
556                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
557                                                 'loopcell' => \@loopcell,
558                                                 'hilighted' => ($hilighted >0),
559                                                 'totalrow' => ($total)?sprintf("%.2f",$total):0
560                                         };
561                 $hilighted = -$hilighted;
562         }
563 #       
564 # #     warn "footer processing";
565         foreach my $col ( @loopcol ) {
566                 my $total=0;
567                 my $nbrow=0;
568                 foreach my $row ( @looprow ) {
569                         $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}};
570                         $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
571 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
572                 }
573 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
574                 $total = $total/$nbrow if ($nbrow);
575                 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
576         
577         }
578                         
579
580         # the header of the table
581         $globalline{loopfilter}=\@loopfilter;
582         # the core of the table
583         $globalline{looprow} = \@looprow;
584         $globalline{loopcol} = \@loopcol;
585 #       # the foot (totals by borrower type)
586         $globalline{loopfooter} = \@loopfooter;
587         $globalline{total}= $grantotal;
588         $globalline{line} = $line;
589         $globalline{column} = $column;
590         push @mainloop,\%globalline;
591         return \@mainloop;
592 }
593
594 1;