Merge commit 'kc/master'
[koha.git] / reports / issues_avg_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Reports;
31 use C4::Dates qw/format_date format_date_in_iso/;
32 use Date::Calc qw(Delta_Days);
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40 =over 2
41
42 =cut
43
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_avg_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 $filters[2]=format_date_in_iso($filters[2]);
53 $filters[3]=format_date_in_iso($filters[3]);
54 my $podsp = $input->param("IssueDisplay");
55 my $rodsp = $input->param("ReturnDisplay");
56 my $calc = $input->param("Cellvalue");
57 my $output = $input->param("output");
58 my $basename = $input->param("basename");
59 my $mime = $input->param("MIME");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62     = get_template_and_user({template_name => $fullreportname,
63                 query => $input,
64                 type => "intranet",
65                 authnotrequired => 0,
66                 flagsrequired => {reports => '*'},
67                 debug => 1,
68                     });
69 our $sep     = $input->param("sep");
70 $sep = "\t" if ($sep eq 'tabulation');
71 $template->param(do_it => $do_it,
72         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
73     );
74 if ($do_it) {
75 # Displaying results
76     my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
77     if ($output eq "screen"){
78 # Printing results to screen
79         $template->param(mainloop => $results);
80         output_html_with_http_headers $input, $cookie, $template->output;
81         exit(1);
82     } else {
83 # Printing to a csv file
84         print $input->header(-type => 'application/vnd.sun.xml.calc',
85                                     -encoding    => 'utf-8',
86             -attachment=>"$basename.csv",
87             -filename=>"$basename.csv" );
88         my $cols = @$results[0]->{loopcol};
89         my $lines = @$results[0]->{looprow};
90 # header top-right
91         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
92 # Other header
93         foreach my $col ( @$cols ) {
94             print $col->{coltitle}.$sep;
95         }
96         print "Total\n";
97 # Table
98         foreach my $line ( @$lines ) {
99             my $x = $line->{loopcell};
100             print $line->{rowtitle}.$sep;
101             foreach my $cell (@$x) {
102                 print $cell->{value}.$sep;
103             }
104             print $line->{totalrow};
105             print "\n";
106         }
107 # footer
108         print "TOTAL";
109         $cols = @$results[0]->{loopfooter};
110         foreach my $col ( @$cols ) {
111             print $sep.$col->{totalcol};
112         }
113         print $sep.@$results[0]->{total};
114         exit(1);
115     }
116 # Displaying choices
117 } else {
118     my $dbh = C4::Context->dbh;
119     my @values;
120     my %labels;
121     my %select;
122     my $req;
123     $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
124     $req->execute;
125     my @select;
126     push @select,"";
127     $select{""}="";
128     while (my ($value, $desc) =$req->fetchrow) {
129         push @select, $value;
130         $select{$value}=$desc;
131     }
132     my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
133                 -id => 'borcat',
134                 -values   => \@select,
135                 -labels   => \%select,
136                 -size     => 1,
137                 -multiple => 0 );
138     
139     $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
140     $req->execute;
141     undef @select;
142     undef %select;
143     push @select,"";
144     $select{""}="";
145     while (my ($value,$desc) =$req->fetchrow) {
146         push @select, $value;
147         $select{$value}=$desc;
148     }
149     my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
150                 -id => 'itemtypes',
151                 -values   => \@select,
152                 -labels    => \%select,
153                 -size     => 1,
154                 -multiple => 0 );
155     
156     $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
157     $req->execute;
158     undef @select;
159     push @select,"";
160     my $hassort1;
161     while (my ($value) =$req->fetchrow) {
162         $hassort1 =1 if ($value);
163         push @select, $value;
164     }
165     my $branches=GetBranches();
166     my @select_branch;
167     my %select_branches;
168     push @select_branch,"";
169     $select_branches{""} = "";
170     foreach my $branch (keys %$branches) {
171         push @select_branch, $branch;
172         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
173     }
174     my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
175                 -id => 'branch',
176                 -values   => \@select_branch,
177                 -labels   => \%select_branches,
178                 -size     => 1,
179                 -multiple => 0 );
180     
181     my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
182                 -id => 'sort1',
183                 -values   => \@select,
184                 -size     => 1,
185                 -multiple => 0 );
186     
187     $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
188     $req->execute;
189     undef @select;
190     push @select,"";
191     my $hassort2;
192     my $hglghtsort2;
193     while (my ($value) =$req->fetchrow) {
194         $hassort2 =1 if ($value);
195         $hglghtsort2= !($hassort1);
196         push @select, $value;
197     }
198     my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
199                 -id => 'sort2',
200                 -values   => \@select,
201                 -size     => 1,
202                 -multiple => 0 );
203     
204     my @mime = ( C4::Context->preference("MIME") );
205 #       foreach my $mime (@mime){
206 #               warn "".$mime;
207 #       }
208     
209     my $CGIextChoice=CGI::scrolling_list(
210                 -name     => 'MIME',
211                 -id       => 'MIME',
212                 -values   => \@mime,
213                 -size     => 1,
214                 -multiple => 0 );
215     
216     my $CGIsepChoice=GetDelimiterChoices;
217     
218     $template->param(
219                     CGIBorCat => $CGIBorCat,
220                     CGIItemType => $CGIItemTypes,
221                     CGIBranch => $CGIBranch,
222                     hassort1=> $hassort1,
223                     hassort2=> $hassort2,
224                     HlghtSort2 => $hglghtsort2,
225                     CGISort1 => $CGISort1,
226                     CGISort2 => $CGISort2,
227                     CGIextChoice => $CGIextChoice,
228                     CGIsepChoice => $CGIsepChoice
229                     );
230 output_html_with_http_headers $input, $cookie, $template->output;
231 }
232
233
234
235
236 sub calculate {
237     my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
238     my @mainloop;
239     my @loopfooter;
240     my @loopcol;
241     my @loopline;
242     my @looprow;
243     my %globalline;
244     my $grantotal =0;
245 # extract parameters
246     my $dbh = C4::Context->dbh;
247
248 # Filters
249 # Checking filters
250 #
251     my @loopfilter;
252     for (my $i=0;$i<=6;$i++) {
253         my %cell;
254         if ( @$filters[$i] ) {
255             if (($i==1) and (@$filters[$i-1])) {
256                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
257             }
258             # format the dates filters, otherwise just fill as is
259             if ($i>=4) {
260                 $cell{filter} .= @$filters[$i];
261             } else {
262                 $cell{filter} .= format_date(@$filters[$i]);
263             }
264             $cell{crit} .="Issue From" if ($i==0);
265             $cell{crit} .="Issue To" if ($i==1);
266             $cell{crit} .="Issue Month" if ($i==2);
267             $cell{crit} .="Issue Day" if ($i==3);
268             $cell{crit} .="Return From" if ($i==4);
269             $cell{crit} .="Return To" if ($i==5);
270             $cell{crit} .="Return Month" if ($i==6);
271             $cell{crit} .="Return Day" if ($i==7);
272             $cell{crit} .="Borrower Cat" if ($i==8);
273             $cell{crit} .="Doc Type" if ($i==9);
274             $cell{crit} .="Branch" if ($i==10);
275             $cell{crit} .="Sort1" if ($i==11);
276             $cell{crit} .="Sort2" if ($i==12);
277             push @loopfilter, \%cell;
278         }
279     }
280     push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
281     push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
282
283     
284     
285     my @linefilter;
286 #       warn "filtres ".@filters[0];
287 #       warn "filtres ".@filters[1];
288 #       warn "filtres ".@filters[2];
289 #       warn "filtres ".@filters[3];
290     $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
291     $line = "biblioitems.".$line if $line=~/itemtype/;
292     
293     $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
294     $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
295     $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
296     $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
297     $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
298     $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
299     $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
300     $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
301     $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
302     $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ )  ;
303     $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
304 #       $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
305     $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
306     $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
307 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
308
309     $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
310     $column = "biblioitems.".$column if $column=~/itemtype/;
311     my @colfilter ;
312     $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
313     $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
314     $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
315     $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
316     $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
317     $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
318     $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
319     $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
320     $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
321     $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ )  ;
322     $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
323 #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
324     $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
325     $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
326 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
327                                             
328 # 1st, loop rows.                             
329     my $linefield;
330     my $lineorder;                               
331     if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
332         #Display by day
333         $linefield .="dayname($line)";  
334         $lineorder .="weekday($line)";  
335     } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
336         #Display by Month
337         $linefield .="monthname($line)";  
338         $lineorder .="month($line)";  
339     } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
340         #Display by Year
341         $linefield .="Year($line)";
342         $lineorder .= $line;  
343     } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
344         $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
345         $lineorder .= $line;  
346     } else {
347         $linefield .= $line;
348         $lineorder .= $line;  
349     }  
350     
351     my $strsth;
352     $strsth .= "select distinctrow $linefield 
353                 FROM `old_issues` 
354                 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
355                 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
356                 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
357                 LEFT JOIN issuingrules ON 
358                     (issuingrules.branchcode=old_issues.branchcode
359                     AND  issuingrules.itemtype=biblioitems.itemtype 
360                     AND  issuingrules.categorycode=borrowers.categorycode) 
361                 WHERE 1";
362     
363     if (($line=~/timestamp/) or ($line=~/returndate/)){
364         if ($linefilter[1] and ($linefilter[0])){
365             $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
366         } elsif ($linefilter[1]) {
367                 $strsth .= " AND $line < \'$linefilter[1]\' " ;
368         } elsif ($linefilter[0]) {
369             $strsth .= " AND $line > \'$linefilter[0]\' " ;
370         }
371         if ($linefilter[2]){
372             $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
373         }
374         if ($linefilter[3]){
375             $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
376         }
377     } elsif ($linefilter[0]) {
378         $linefilter[0] =~ s/\*/%/g;
379         $strsth .= " AND $line LIKE '$linefilter[0]' " ;
380     }
381     $strsth .=" GROUP BY $linefield";
382     $strsth .=" ORDER BY $lineorder";
383    
384     my $sth = $dbh->prepare( $strsth );
385     $sth->execute;
386
387     
388     while ( my ($celvalue) = $sth->fetchrow) {
389         my %cell;
390         if ($celvalue) {
391             $cell{rowtitle} = $celvalue;
392         } else {
393             $cell{rowtitle} = "";
394         }
395         $cell{totalrow} = 0;
396         push @loopline, \%cell;
397     }
398
399 # 2nd, loop cols.
400     my $colfield;
401     my $colorder;                               
402     if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
403         #Display by day
404         $colfield .="dayname($column)";  
405         $colorder .="weekday($column)";
406     } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
407         #Display by Month
408         $colfield .="monthname($column)";  
409         $colorder .="month($column)";  
410     } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
411         #Display by Year
412         $colfield .="Year($column)";
413         $colorder .= $column;
414     } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
415         $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
416         $colorder .= $column;
417     } else {
418         $colfield .= $column;
419         $colorder .= $column;
420     }  
421     
422     my $strsth2;
423     $strsth2 .= "SELECT distinctrow $colfield 
424                   FROM `old_issues`
425                   LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
426                   LEFT JOIN items  ON items.itemnumber=old_issues.itemnumber  
427                   LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
428                   LEFT JOIN issuingrules ON 
429                     (issuingrules.branchcode=old_issues.branchcode 
430                     AND  issuingrules.itemtype=biblioitems.itemtype 
431                     AND  issuingrules.categorycode=borrowers.categorycode) 
432                   WHERE 1";
433     
434     if (($column=~/timestamp/) or ($column=~/returndate/)){
435         if ($colfilter[1] and ($colfilter[0])){
436             $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
437         } elsif ($colfilter[1]) {
438                 $strsth2 .= " AND $column < '$colfilter[1]' " ;
439         } elsif ($colfilter[0]) {
440             $strsth2 .= " AND $column > '$colfilter[0]' " ;
441         }
442         if ($colfilter[2]){
443             $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
444         }
445         if ($colfilter[3]){
446             $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
447         }
448     } elsif ($colfilter[0]) {
449         $colfilter[0] =~ s/\*/%/g;
450         $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
451     }
452     $strsth2 .=" GROUP BY $colfield";
453     $strsth2 .=" ORDER BY $colorder";
454     
455     my $sth2 = $dbh->prepare( $strsth2 );
456     if (( @colfilter ) and ($colfilter[1])){
457         $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
458     } elsif ($colfilter[0]) {
459         $sth2->execute($colfilter[0]);
460     } else {
461         $sth2->execute;
462     }
463     
464
465     while (my ($celvalue) = $sth2->fetchrow) {
466         my %cell;
467         my %ft;
468 #               warn "coltitle :".$celvalue;
469         $cell{coltitle} = $celvalue;
470         $ft{totalcol} = 0;
471         push @loopcol, \%cell;
472     }
473 #       warn "fin des titres colonnes";
474
475     my $i=0;
476     my @totalcol;
477     my $hilighted=-1;
478     
479     #Initialization of cell values.....
480     my %table;
481     my %wgttable;
482     my %cnttable;
483     
484 #       warn "init table";
485     foreach my $row ( @loopline ) {
486         foreach my $col ( @loopcol ) {
487 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
488             $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
489         }
490         $table{$row->{rowtitle}}->{totalrow}=0;
491     }
492
493 # preparing calculation
494     my $strcalc ;
495     
496 # Processing average loanperiods
497     $strcalc .= "SELECT $linefield, $colfield, ";
498     $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";
499
500     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
501     $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
502     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
503     $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
504     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
505     $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
506     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
507     $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
508     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
509     $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
510     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
511     $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
512     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
513     $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
514     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
515     $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
516     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
517     $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
518     $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
519     $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
520     $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
521     $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
522     
523     $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
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;