remove editor settings
[koha.git] / reports / issues_avg_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Dates qw/format_date format_date_in_iso/;
31 use Date::Calc qw(Delta_Days);
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =over 2
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 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 $filters[2]=format_date_in_iso($filters[2]);
52 $filters[3]=format_date_in_iso($filters[3]);
53 my $podsp = $input->param("IssueDisplay");
54 my $rodsp = $input->param("ReturnDisplay");
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61     = get_template_and_user({template_name => $fullreportname,
62                 query => $input,
63                 type => "intranet",
64                 authnotrequired => 0,
65                 flagsrequired => {reports => 1},
66                 debug => 1,
67                     });
68 our $sep     = $input->param("sep");
69 $sep = "\t" if ($sep eq 'tabulation');
70 $template->param(do_it => $do_it,
71         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
72     );
73 if ($do_it) {
74 # Displaying results
75     my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
76     if ($output eq "screen"){
77 # Printing results to screen
78         $template->param(mainloop => $results);
79         output_html_with_http_headers $input, $cookie, $template->output;
80         exit(1);
81     } else {
82 # Printing to a csv file
83         print $input->header(-type => 'application/vnd.sun.xml.calc',
84                                     -encoding    => 'utf-8',
85             -attachment=>"$basename.csv",
86             -filename=>"$basename.csv" );
87         my $cols = @$results[0]->{loopcol};
88         my $lines = @$results[0]->{looprow};
89 # header top-right
90         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
91 # Other header
92         foreach my $col ( @$cols ) {
93             print $col->{coltitle}.$sep;
94         }
95         print "Total\n";
96 # Table
97         foreach my $line ( @$lines ) {
98             my $x = $line->{loopcell};
99             print $line->{rowtitle}.$sep;
100             foreach my $cell (@$x) {
101                 print $cell->{value}.$sep;
102             }
103             print $line->{totalrow};
104             print "\n";
105         }
106 # footer
107         print "TOTAL";
108         $cols = @$results[0]->{loopfooter};
109         foreach my $col ( @$cols ) {
110             print $sep.$col->{totalcol};
111         }
112         print $sep.@$results[0]->{total};
113         exit(1);
114     }
115 # Displaying choices
116 } else {
117     my $dbh = C4::Context->dbh;
118     my @values;
119     my %labels;
120     my %select;
121     my $req;
122     $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
123     $req->execute;
124     my @select;
125     push @select,"";
126     $select{""}="";
127     while (my ($value, $desc) =$req->fetchrow) {
128         push @select, $value;
129         $select{$value}=$desc;
130     }
131     my $CGIBorCat=CGI::scrolling_list( -name     => 'Filter',
132                 -id => 'borcat',
133                 -values   => \@select,
134                 -labels   => \%select,
135                 -size     => 1,
136                 -multiple => 0 );
137     
138     $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
139     $req->execute;
140     undef @select;
141     undef %select;
142     push @select,"";
143     $select{""}="";
144     while (my ($value,$desc) =$req->fetchrow) {
145         push @select, $value;
146         $select{$value}=$desc;
147     }
148     my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
149                 -id => 'itemtypes',
150                 -values   => \@select,
151                 -labels    => \%select,
152                 -size     => 1,
153                 -multiple => 0 );
154     
155     $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
156     $req->execute;
157     undef @select;
158     push @select,"";
159     my $hassort1;
160     while (my ($value) =$req->fetchrow) {
161         $hassort1 =1 if ($value);
162         push @select, $value;
163     }
164     my $branches=GetBranches();
165     my @select_branch;
166     my %select_branches;
167     push @select_branch,"";
168     $select_branches{""} = "";
169     foreach my $branch (keys %$branches) {
170         push @select_branch, $branch;
171         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
172     }
173     my $CGIBranch=CGI::scrolling_list( -name     => 'Filter',
174                 -id => 'branch',
175                 -values   => \@select_branch,
176                 -labels   => \%select_branches,
177                 -size     => 1,
178                 -multiple => 0 );
179     
180     my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
181                 -id => 'sort1',
182                 -values   => \@select,
183                 -size     => 1,
184                 -multiple => 0 );
185     
186     $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
187     $req->execute;
188     undef @select;
189     push @select,"";
190     my $hassort2;
191     my $hglghtsort2;
192     while (my ($value) =$req->fetchrow) {
193         $hassort2 =1 if ($value);
194         $hglghtsort2= !($hassort1);
195         push @select, $value;
196     }
197     my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
198                 -id => 'sort2',
199                 -values   => \@select,
200                 -size     => 1,
201                 -multiple => 0 );
202     
203     my @mime = ( C4::Context->preference("MIME") );
204 #       foreach my $mime (@mime){
205 #               warn "".$mime;
206 #       }
207     
208     my $CGIextChoice=CGI::scrolling_list(
209                 -name     => 'MIME',
210                 -id       => 'MIME',
211                 -values   => \@mime,
212                 -size     => 1,
213                 -multiple => 0 );
214     
215     my $CGIsepChoice=GetDelimiterChoices;
216     
217     $template->param(
218                     CGIBorCat => $CGIBorCat,
219                     CGIItemType => $CGIItemTypes,
220                     CGIBranch => $CGIBranch,
221                     hassort1=> $hassort1,
222                     hassort2=> $hassort2,
223                     HlghtSort2 => $hglghtsort2,
224                     CGISort1 => $CGISort1,
225                     CGISort2 => $CGISort2,
226                     CGIextChoice => $CGIextChoice,
227                     CGIsepChoice => $CGIsepChoice
228                     );
229 output_html_with_http_headers $input, $cookie, $template->output;
230 }
231
232
233
234
235 sub calculate {
236     my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
237     my @mainloop;
238     my @loopfooter;
239     my @loopcol;
240     my @loopline;
241     my @looprow;
242     my %globalline;
243     my $grantotal =0;
244 # extract parameters
245     my $dbh = C4::Context->dbh;
246
247 # Filters
248 # Checking filters
249 #
250     my @loopfilter;
251     for (my $i=0;$i<=6;$i++) {
252         my %cell;
253         if ( @$filters[$i] ) {
254             if (($i==1) and (@$filters[$i-1])) {
255                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
256             }
257             # format the dates filters, otherwise just fill as is
258             if ($i>=4) {
259                 $cell{filter} .= @$filters[$i];
260             } else {
261                 $cell{filter} .= format_date(@$filters[$i]);
262             }
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 = "old_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 = "old_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 
352                 FROM `old_issues` 
353                 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
354                 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
355                 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
356                 LEFT JOIN issuingrules ON 
357                     (issuingrules.branchcode=old_issues.branchcode
358                     AND  issuingrules.itemtype=biblioitems.itemtype 
359                     AND  issuingrules.categorycode=borrowers.categorycode) 
360                 WHERE 1";
361     
362     if (($line=~/timestamp/) or ($line=~/returndate/)){
363         if ($linefilter[1] and ($linefilter[0])){
364             $strsth .= " AND $line BETWEEN '$linefilter[0]' AND '$linefilter[1]' " ;
365         } elsif ($linefilter[1]) {
366                 $strsth .= " AND $line < \'$linefilter[1]\' " ;
367         } elsif ($linefilter[0]) {
368             $strsth .= " AND $line > \'$linefilter[0]\' " ;
369         }
370         if ($linefilter[2]){
371             $strsth .= " AND dayname($line) = '$linefilter[2]' " ;
372         }
373         if ($linefilter[3]){
374             $strsth .= " AND monthname($line) = '$linefilter[3]' " ;
375         }
376     } elsif ($linefilter[0]) {
377         $linefilter[0] =~ s/\*/%/g;
378         $strsth .= " AND $line LIKE '$linefilter[0]' " ;
379     }
380     $strsth .=" GROUP BY $linefield";
381     $strsth .=" ORDER BY $lineorder";
382    
383     my $sth = $dbh->prepare( $strsth );
384     $sth->execute;
385
386     
387     while ( my ($celvalue) = $sth->fetchrow) {
388         my %cell;
389         if ($celvalue) {
390             $cell{rowtitle} = $celvalue;
391         } else {
392             $cell{rowtitle} = "";
393         }
394         $cell{totalrow} = 0;
395         push @loopline, \%cell;
396     }
397
398 # 2nd, loop cols.
399     my $colfield;
400     my $colorder;                               
401     if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
402         #Display by day
403         $colfield .="dayname($column)";  
404         $colorder .="weekday($column)";
405     } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
406         #Display by Month
407         $colfield .="monthname($column)";  
408         $colorder .="month($column)";  
409     } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
410         #Display by Year
411         $colfield .="Year($column)";
412         $colorder .= $column;
413     } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
414         $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
415         $colorder .= $column;
416     } else {
417         $colfield .= $column;
418         $colorder .= $column;
419     }  
420     
421     my $strsth2;
422     $strsth2 .= "SELECT distinctrow $colfield 
423                   FROM `old_issues`
424                   LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
425                   LEFT JOIN items  ON items.itemnumber=old_issues.itemnumber  
426                   LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
427                   LEFT JOIN issuingrules ON 
428                     (issuingrules.branchcode=old_issues.branchcode 
429                     AND  issuingrules.itemtype=biblioitems.itemtype 
430                     AND  issuingrules.categorycode=borrowers.categorycode) 
431                   WHERE 1";
432     
433     if (($column=~/timestamp/) or ($column=~/returndate/)){
434         if ($colfilter[1] and ($colfilter[0])){
435             $strsth2 .= " AND $column BETWEEN '$colfilter[0]' AND '$colfilter[1]' " ;
436         } elsif ($colfilter[1]) {
437                 $strsth2 .= " AND $column < '$colfilter[1]' " ;
438         } elsif ($colfilter[0]) {
439             $strsth2 .= " AND $column > '$colfilter[0]' " ;
440         }
441         if ($colfilter[2]){
442             $strsth2 .= " AND dayname($column) = '$colfilter[2]' " ;
443         }
444         if ($colfilter[3]){
445             $strsth2 .= " AND monthname($column) = '$colfilter[3]' " ;
446         }
447     } elsif ($colfilter[0]) {
448         $colfilter[0] =~ s/\*/%/g;
449         $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
450     }
451     $strsth2 .=" GROUP BY $colfield";
452     $strsth2 .=" ORDER BY $colorder";
453     warn "". $strsth2;
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     warn "SQL :". $strcalc;
525     
526     my $dbcalc = $dbh->prepare($strcalc);
527     $dbcalc->execute;
528 #       warn "filling table";
529     my $issues_count=0;
530     my $previous_row; 
531     my $previous_col;
532     my $loanlength; 
533     my $err;
534     my $emptycol;
535     my $weightrow;
536     
537     while (my  @data = $dbcalc->fetchrow) {
538         my ($row, $col, $issuedate, $returndate, $weight)=@data;
539 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
540         $emptycol=1 if (!defined($col));
541         $col = "zzEMPTY" if (!defined($col));
542         $row = "zzEMPTY" if (!defined($row));
543         # fill returndate to avoid an error with date calc (needed for all non returned issues)
544         $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
545     #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
546     #  and seconds between the two
547         $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
548     #           warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
549     #           warn "513 row :".$row." column :".$col;
550         $table{$row}->{$col}+=$weight*$loanlength;
551     #           $table{$row}->{totalrow}+=$weight*$loanlength;
552         $cnttable{$row}->{$col}= 1;
553         $wgttable{$row}->{$col}+=$weight;
554     }
555     
556     push @loopcol,{coltitle => "NULL"} if ($emptycol);
557     
558     foreach my $row ( sort keys %table ) {
559         my @loopcell;
560     #@loopcol ensures the order for columns is common with column titles
561     # and the number matches the number of columns
562         my $colcount=0;
563         foreach my $col ( @loopcol ) {
564             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}});
565
566             $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
567             $table{$row}->{totalrow}+=$value;
568             #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
569             $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
570             push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
571         }
572         #warn "row : $row colcount:$colcount";
573         my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
574         push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
575                         'loopcell' => \@loopcell,
576                         'hilighted' => ($hilighted >0),
577                         'totalrow' => ($total)?sprintf("%.2f",$total):0
578                     };
579         $hilighted = -$hilighted;
580     }
581 #       
582 # #     warn "footer processing";
583     foreach my $col ( @loopcol ) {
584         my $total=0;
585         my $nbrow=0;
586         foreach my $row ( @looprow ) {
587             $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}};
588             $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
589 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
590         }
591 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
592         $total = $total/$nbrow if ($nbrow);
593         push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
594     
595     }
596             
597
598     # the header of the table
599     $globalline{loopfilter}=\@loopfilter;
600     # the core of the table
601     $globalline{looprow} = \@looprow;
602     $globalline{loopcol} = \@loopcol;
603 #       # the foot (totals by borrower type)
604     $globalline{loopfooter} = \@loopfooter;
605     $globalline{total}= $grantotal;
606     $globalline{line} = $line;
607     $globalline{column} = $column;
608     push @mainloop,\%globalline;
609     return \@mainloop;
610 }
611
612 1;