New XML API
[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 HTML::Template;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
32 use Date::Manip;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40
41 =over2
42
43 =cut
44
45 my $input = new CGI;
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/issues_avg_stats.tmpl";
48 my $line = $input->param("Line");
49 my $column = $input->param("Column");
50 my @filters = $input->param("Filter");
51 my $podsp = $input->param("IssueDisplay");
52 my $rodsp = $input->param("ReturnDisplay");
53 my $calc = $input->param("Cellvalue");
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 my $del = $input->param("sep");
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 => {editcatalogue => 1},
65                                 debug => 1,
66                                 });
67 $template->param(do_it => $do_it);
68 if ($do_it) {
69 # Displaying results
70         my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
71         if ($output eq "screen"){
72 # Printing results to screen
73                 $template->param(mainloop => $results);
74                 output_html_with_http_headers $input, $cookie, $template->output;
75                 exit(1);
76         } else {
77 # Printing to a csv file
78                 print $input->header(-type => 'application/vnd.sun.xml.calc',
79                         -attachment=>"$basename.csv",
80                         -filename=>"$basename.csv" );
81                 my $cols = @$results[0]->{loopcol};
82                 my $lines = @$results[0]->{looprow};
83                 my $sep;
84                 $sep =C4::Context->preference("delimiter");
85 # header top-right
86                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 # Other header
88                 foreach my $col ( @$cols ) {
89                         print $col->{coltitle}.$sep;
90                 }
91                 print "Total\n";
92 # Table
93                 foreach my $line ( @$lines ) {
94                         my $x = $line->{loopcell};
95                         print $line->{rowtitle}.$sep;
96                         foreach my $cell (@$x) {
97                                 print $cell->{value}.$sep;
98                         }
99                         print $line->{totalrow};
100                         print "\n";
101                 }
102 # footer
103                 print "TOTAL";
104                 $cols = @$results[0]->{loopfooter};
105                 foreach my $col ( @$cols ) {
106                         print $sep.$col->{totalcol};
107                 }
108                 print $sep.@$results[0]->{total};
109                 exit(1);
110         }
111 # Displaying choices
112 } else {
113         my $dbh = C4::Context->dbh;
114         my @values;
115         my %labels;
116         my %select;
117         my $req;
118         $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
119         $req->execute;
120         my @select;
121         push @select,"";
122         $select{""}="";
123         while (my ($value, $desc) =$req->fetchrow) {
124                 push @select, $value;
125                 $select{$value}=$desc;
126         }
127         my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
128                                 -id => 'Filter',
129                                 -values   => \@select,
130                                 -labels   => \%select,
131                                 -size     => 1,
132                                 -multiple => 0 );
133         
134         $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
135         $req->execute;
136         undef @select;
137         undef %select;
138         push @select,"";
139         $select{""}="";
140         while (my ($value,$desc) =$req->fetchrow) {
141                 push @select, $value;
142                 $select{$value}=$desc;
143         }
144         my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
145                                 -id => 'Filter',
146                                 -values   => \@select,
147                                 -labels    => \%select,
148                                 -size     => 1,
149                                 -multiple => 0 );
150         
151         $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
152         $req->execute;
153         undef @select;
154         push @select,"";
155         my $hassort1;
156         while (my ($value) =$req->fetchrow) {
157                 $hassort1 =1 if ($value);
158                 push @select, $value;
159         }
160         my $branches=getbranches();
161         my @select_branch;
162         my %select_branches;
163         push @select_branch,"";
164         $select_branches{""} = "";
165         foreach my $branch (keys %$branches) {
166                 push @select_branch, $branch;
167                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
168         }
169         my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
170                                 -id => 'Filter',
171                                 -values   => \@select_branch,
172                                 -labels   => \%select_branches,
173                                 -size     => 1,
174                                 -multiple => 0 );
175         
176         my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
177                                 -id => 'Filter',
178                                 -values   => \@select,
179                                 -size     => 1,
180                                 -multiple => 0 );
181         
182         $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
183         $req->execute;
184         undef @select;
185         push @select,"";
186         my $hassort2;
187         my $hglghtsort2;
188         while (my ($value) =$req->fetchrow) {
189                 $hassort2 =1 if ($value);
190                 $hglghtsort2= !($hassort1);
191                 push @select, $value;
192         }
193         my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
194                                 -id => 'Filter',
195                                 -values   => \@select,
196                                 -size     => 1,
197                                 -multiple => 0 );
198         
199         my @mime = ( C4::Context->preference("MIME") );
200 #       foreach my $mime (@mime){
201 #               warn "".$mime;
202 #       }
203         
204         my $CGIextChoice=CGI::scrolling_list(
205                                 -name     => 'MIME',
206                                 -id       => 'MIME',
207                                 -values   => \@mime,
208                                 -size     => 1,
209                                 -multiple => 0 );
210         
211         my @dels = ( C4::Context->preference("delimiter") );
212         my $CGIsepChoice=CGI::scrolling_list(
213                                 -name     => 'sep',
214                                 -id       => 'sep',
215                                 -values   => \@dels,
216                                 -size     => 1,
217                                 -multiple => 0 );
218         
219         $template->param(
220                                         CGIBorCat => $CGIBorCat,
221                                         CGIItemType => $CGIItemTypes,
222                                         CGIBranch => $CGIBranch,
223                                         hassort1=> $hassort1,
224                                         hassort2=> $hassort2,
225                                         HlghtSort2 => $hglghtsort2,
226                                         CGISort1 => $CGISort1,
227                                         CGISort2 => $CGISort2,
228                                         CGIextChoice => $CGIextChoice,
229                                         CGIsepChoice => $CGIsepChoice
230                                         );
231 output_html_with_http_headers $input, $cookie, $template->output;
232 }
233
234
235
236
237 sub calculate {
238         my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
239         my @mainloop;
240         my @loopfooter;
241         my @loopcol;
242         my @loopline;
243         my @looprow;
244         my %globalline;
245         my $grantotal =0;
246 # extract parameters
247         my $dbh = C4::Context->dbh;
248
249 # Filters
250 # Checking filters
251 #
252         my @loopfilter;
253         for (my $i=0;$i<=6;$i++) {
254                 my %cell;
255                 if ( @$filters[$i] ) {
256                         if (($i==1) and (@$filters[$i-1])) {
257                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
258                         }
259                         $cell{filter} .= @$filters[$i];
260                         $cell{crit} .="Issue From" if ($i==0);
261                         $cell{crit} .="Issue To" if ($i==1);
262                         $cell{crit} .="Issue Month" if ($i==2);
263                         $cell{crit} .="Issue Day" if ($i==3);
264                         $cell{crit} .="Return From" if ($i==4);
265                         $cell{crit} .="Return To" if ($i==5);
266                         $cell{crit} .="Return Month" if ($i==6);
267                         $cell{crit} .="Return Day" if ($i==7);
268                         $cell{crit} .="Borrower Cat" if ($i==8);
269                         $cell{crit} .="Doc Type" if ($i==9);
270                         $cell{crit} .="Branch" if ($i==10);
271                         $cell{crit} .="Sort1" if ($i==11);
272                         $cell{crit} .="Sort2" if ($i==12);
273                         push @loopfilter, \%cell;
274                 }
275         }
276         push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
277         push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
278
279         
280         
281         my @linefilter;
282 #       warn "filtres ".@filters[0];
283 #       warn "filtres ".@filters[1];
284 #       warn "filtres ".@filters[2];
285 #       warn "filtres ".@filters[3];
286         $line = "issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
287         $line = "biblioitems.".$line if $line=~/itemtype/;
288         
289         $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
290         $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
291         $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
292         $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
293         $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
294         $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
295         $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
296         $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
297         $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
298         $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ )  ;
299         $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
300 #       $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
301         $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
302         $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
303 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
304
305         $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
306         $column = "biblioitems.".$column if $column=~/itemtype/;
307         my @colfilter ;
308         $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
309         $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
310         $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
311         $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
312         $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
313         $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
314         $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
315         $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
316         $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
317         $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ )  ;
318         $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
319 #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
320         $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
321         $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
322 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
323                                               
324 # 1st, loop rows.                             
325         my $linefield;
326         my $lineorder;                               
327         if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
328                 #Display by day
329                 $linefield .="dayname($line)";  
330                 $lineorder .="weekday($line)";  
331         } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
332                 #Display by Month
333                 $linefield .="monthname($line)";  
334                 $lineorder .="month($line)";  
335         } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
336                 #Display by Year
337                 $linefield .="Year($line)";
338                 $lineorder .= $line;  
339         } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
340                 $linefield .= "date_format('$line',\"%Y-%m-%d\")";
341                 $lineorder .= $line;  
342         } else {
343                 $linefield .= $line;
344                 $lineorder .= $line;  
345         }  
346         
347         my $strsth;
348         $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";
349         
350         if (($line=~/timestamp/) or ($line=~/returndate/)){
351                 if ($linefilter[1] and ($linefilter[0])){
352                         $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
353                 } elsif ($linefilter[1]) {
354                                 $strsth .= " and $line < '$linefilter[1]' " ;
355                 } elsif ($linefilter[0]) {
356                         $strsth .= " and $line > '$linefilter[0]' " ;
357                 }
358                 if ($linefilter[2]){
359                         $strsth .= " and dayname($line) = '$linefilter[2]' " ;
360                 }
361                 if ($linefilter[3]){
362                         $strsth .= " and monthname($line) = '$linefilter[3]' " ;
363                 }
364         } elsif ($linefilter[0]) {
365                 $linefilter[0] =~ s/\*/%/g;
366                 $strsth .= " and $line LIKE '$linefilter[0]' " ;
367         }
368         $strsth .=" group by $linefield";
369         $strsth .=" order by $lineorder";
370         warn "". $strsth;
371         
372         my $sth = $dbh->prepare( $strsth );
373         $sth->execute;
374
375         
376         while ( my ($celvalue) = $sth->fetchrow) {
377                 my %cell;
378                 if ($celvalue) {
379                         $cell{rowtitle} = $celvalue;
380                 } else {
381                         $cell{rowtitle} = "";
382                 }
383                 $cell{totalrow} = 0;
384                 push @loopline, \%cell;
385         }
386
387 # 2nd, loop cols.
388         my $colfield;
389         my $colorder;                               
390         if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
391                 #Display by day
392                 $colfield .="dayname($column)";  
393                 $colorder .="weekday($column)";
394         } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
395                 #Display by Month
396                 $colfield .="monthname($column)";  
397                 $colorder .="month($column)";  
398         } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
399                 #Display by Year
400                 $colfield .="Year($column)";
401                 $colorder .= $column;
402         } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
403                 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
404                 $colorder .= $column;
405         } else {
406                 $colfield .= $column;
407                 $colorder .= $column;
408         }  
409         
410         my $strsth2;
411         $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";
412         
413         if (($column=~/timestamp/) or ($column=~/returndate/)){
414                 if ($colfilter[1] and ($colfilter[0])){
415                         $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
416                 } elsif ($colfilter[1]) {
417                                 $strsth2 .= " and $column < '$colfilter[1]' " ;
418                 } elsif ($colfilter[0]) {
419                         $strsth2 .= " and $column > '$colfilter[0]' " ;
420                 }
421                 if ($colfilter[2]){
422                         $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
423                 }
424                 if ($colfilter[3]){
425                         $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
426                 }
427         } elsif ($colfilter[0]) {
428                 $colfilter[0] =~ s/\*/%/g;
429                 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
430         }
431         $strsth2 .=" group by $colfield";
432         $strsth2 .=" order by $colorder";
433         warn "". $strsth2;
434         
435         my $sth2 = $dbh->prepare( $strsth2 );
436         if (( @colfilter ) and ($colfilter[1])){
437                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
438         } elsif ($colfilter[0]) {
439                 $sth2->execute($colfilter[0]);
440         } else {
441                 $sth2->execute;
442         }
443         
444
445         while (my ($celvalue) = $sth2->fetchrow) {
446                 my %cell;
447                 my %ft;
448 #               warn "coltitle :".$celvalue;
449                 $cell{coltitle} = $celvalue;
450                 $ft{totalcol} = 0;
451                 push @loopcol, \%cell;
452         }
453 #       warn "fin des titres colonnes";
454
455         my $i=0;
456         my @totalcol;
457         my $hilighted=-1;
458         
459         #Initialization of cell values.....
460         my %table;
461         my %wgttable;
462         my %cnttable;
463         
464 #       warn "init table";
465         foreach my $row ( @loopline ) {
466                 foreach my $col ( @loopcol ) {
467 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
468                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
469                 }
470                 $table{$row->{rowtitle}}->{totalrow}=0;
471         }
472
473 # preparing calculation
474         my $strcalc ;
475         
476 # Processing average loanperiods
477         $strcalc .= "SELECT $linefield, $colfield, ";
478         $strcalc .= " DATE_SUB(date_due, INTERVAL CAST(issuingrules.issuelength AS SIGNED INTEGER) * (CAST(issues.renewals AS SIGNED INTEGER)+1) DAY) AS 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";
479
480         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
481         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
482         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
483         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
484         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
485         $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
486         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
487         $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
488         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
489         $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
490         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
491         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
492         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
493         $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
494         @$filters[11]=~ s/\*/%/g if (@$filters[11]);
495         $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
496         @$filters[12]=~ s/\*/%/g if (@$filters[12]);
497         $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
498         $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
499         $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
500         $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
501         $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
502         
503         $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
504         warn "SQL :". $strcalc;
505         
506         my $dbcalc = $dbh->prepare($strcalc);
507         $dbcalc->execute;
508 #       warn "filling table";
509         my $issues_count=0;
510         my $previous_row; 
511         my $previous_col;
512         my $loanlength; 
513         my $err;
514         my $emptycol;
515         my $weightrow;
516         
517         while (my  @data = $dbcalc->fetchrow) {
518                 my ($row, $col, $issuedate, $returndate, $weight)=@data;
519 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
520                 $emptycol=1 if ($col eq undef);
521                 $col = "zzEMPTY" if ($col eq undef);
522                 $row = "zzEMPTY" if ($row eq undef);
523 #               warn "506 row :".$row." column :".$col;
524                 my @result =split /:/,DateCalc($returndate,$issuedate) ;
525 #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
526 #  and seconds between the two
527                 $loanlength = $result[2]*7+$result[3];
528 #               warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
529 #               warn "513 row :".$row." column :".$col;
530                 $table{$row}->{$col}+=$weight*$loanlength;
531 #               $table{$row}->{totalrow}+=$weight*$loanlength;
532                 $cnttable{$row}->{$col}= 1;
533                 $wgttable{$row}->{$col}+=$weight;
534         }
535         
536         push @loopcol,{coltitle => "NULL"} if ($emptycol);
537         
538         foreach my $row ( sort keys %table ) {
539                 my @loopcell;
540         #@loopcol ensures the order for columns is common with column titles
541         # and the number matches the number of columns
542                 my $colcount=0;
543                 foreach my $col ( @loopcol ) {
544                         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}});
545
546                         $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
547                         $table{$row}->{totalrow}+=$value;
548                         #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
549                         $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
550                         push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
551                 }
552                 #warn "row : $row colcount:$colcount";
553                 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
554                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
555                                                 'loopcell' => \@loopcell,
556                                                 'hilighted' => ($hilighted >0),
557                                                 'totalrow' => ($total)?sprintf("%.2f",$total):0
558                                         };
559                 $hilighted = -$hilighted;
560         }
561 #       
562 # #     warn "footer processing";
563         foreach my $col ( @loopcol ) {
564                 my $total=0;
565                 my $nbrow=0;
566                 foreach my $row ( @looprow ) {
567                         $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}};
568                         $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
569 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
570                 }
571 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
572                 $total = $total/$nbrow if ($nbrow);
573                 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
574         
575         }
576                         
577
578         # the header of the table
579         $globalline{loopfilter}=\@loopfilter;
580         # the core of the table
581         $globalline{looprow} = \@looprow;
582         $globalline{loopcol} = \@loopcol;
583 #       # the foot (totals by borrower type)
584         $globalline{loopfooter} = \@loopfooter;
585         $globalline{total}= $grantotal;
586         $globalline{line} = $line;
587         $globalline{column} = $column;
588         push @mainloop,\%globalline;
589         return \@mainloop;
590 }
591
592 1;