Head & rel_2_2 merged
[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                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
69                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
70                 IntranetNav => C4::Context->preference("IntranetNav"),
71                 );
72 if ($do_it) {
73 # Displaying results
74         my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
75         if ($output eq "screen"){
76 # Printing results to screen
77                 $template->param(mainloop => $results);
78                 output_html_with_http_headers $input, $cookie, $template->output;
79                 exit(1);
80         } else {
81 # Printing to a csv file
82                 print $input->header(-type => 'application/vnd.sun.xml.calc',
83                         -attachment=>"$basename.csv",
84                         -filename=>"$basename.csv" );
85                 my $cols = @$results[0]->{loopcol};
86                 my $lines = @$results[0]->{looprow};
87                 my $sep;
88                 $sep =C4::Context->preference("delimiter");
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 => 'Filter',
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 @dels = ( C4::Context->preference("delimiter") );
216         my $CGIsepChoice=CGI::scrolling_list(
217                                 -name     => 'sep',
218                                 -id       => 'sep',
219                                 -values   => \@dels,
220                                 -size     => 1,
221                                 -multiple => 0 );
222         
223         $template->param(
224                                         CGIBorCat => $CGIBorCat,
225                                         CGIItemType => $CGIItemTypes,
226                                         CGIBranch => $CGIBranch,
227                                         hassort1=> $hassort1,
228                                         hassort2=> $hassort2,
229                                         HlghtSort2 => $hglghtsort2,
230                                         CGISort1 => $CGISort1,
231                                         CGISort2 => $CGISort2,
232                                         CGIextChoice => $CGIextChoice,
233                                         CGIsepChoice => $CGIsepChoice
234                                         );
235 output_html_with_http_headers $input, $cookie, $template->output;
236 }
237
238
239
240
241 sub calculate {
242         my ($line, $column, $rodsp, $podsp, $process, $filters) = @_;
243         my @mainloop;
244         my @loopfooter;
245         my @loopcol;
246         my @loopline;
247         my @looprow;
248         my %globalline;
249         my $grantotal =0;
250 # extract parameters
251         my $dbh = C4::Context->dbh;
252
253 # Filters
254 # Checking filters
255 #
256         my @loopfilter;
257         for (my $i=0;$i<=6;$i++) {
258                 my %cell;
259                 if ( @$filters[$i] ) {
260                         if (($i==1) and (@$filters[$i-1])) {
261                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
262                         }
263                         $cell{filter} .= @$filters[$i];
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 = "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 = "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 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";
353         
354         if (($line=~/timestamp/) or ($line=~/returndate/)){
355                 if ($linefilter[1] and ($linefilter[0])){
356                         $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
357                 } elsif ($linefilter[1]) {
358                                 $strsth .= " and $line < '$linefilter[1]' " ;
359                 } elsif ($linefilter[0]) {
360                         $strsth .= " and $line > '$linefilter[0]' " ;
361                 }
362                 if ($linefilter[2]){
363                         $strsth .= " and dayname($line) = '$linefilter[2]' " ;
364                 }
365                 if ($linefilter[3]){
366                         $strsth .= " and monthname($line) = '$linefilter[3]' " ;
367                 }
368         } elsif ($linefilter[0]) {
369                 $linefilter[0] =~ s/\*/%/g;
370                 $strsth .= " and $line LIKE '$linefilter[0]' " ;
371         }
372         $strsth .=" group by $linefield";
373         $strsth .=" order by $lineorder";
374         warn "". $strsth;
375         
376         my $sth = $dbh->prepare( $strsth );
377         $sth->execute;
378
379         
380         while ( my ($celvalue) = $sth->fetchrow) {
381                 my %cell;
382                 if ($celvalue) {
383                         $cell{rowtitle} = $celvalue;
384                 } else {
385                         $cell{rowtitle} = "";
386                 }
387                 $cell{totalrow} = 0;
388                 push @loopline, \%cell;
389         }
390
391 # 2nd, loop cols.
392         my $colfield;
393         my $colorder;                               
394         if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
395                 #Display by day
396                 $colfield .="dayname($column)";  
397                 $colorder .="weekday($column)";
398         } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
399                 #Display by Month
400                 $colfield .="monthname($column)";  
401                 $colorder .="month($column)";  
402         } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
403                 #Display by Year
404                 $colfield .="Year($column)";
405                 $colorder .= $column;
406         } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
407                 $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
408                 $colorder .= $column;
409         } else {
410                 $colfield .= $column;
411                 $colorder .= $column;
412         }  
413         
414         my $strsth2;
415         $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";
416         
417         if (($column=~/timestamp/) or ($column=~/returndate/)){
418                 if ($colfilter[1] and ($colfilter[0])){
419                         $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
420                 } elsif ($colfilter[1]) {
421                                 $strsth2 .= " and $column < '$colfilter[1]' " ;
422                 } elsif ($colfilter[0]) {
423                         $strsth2 .= " and $column > '$colfilter[0]' " ;
424                 }
425                 if ($colfilter[2]){
426                         $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
427                 }
428                 if ($colfilter[3]){
429                         $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
430                 }
431         } elsif ($colfilter[0]) {
432                 $colfilter[0] =~ s/\*/%/g;
433                 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
434         }
435         $strsth2 .=" group by $colfield";
436         $strsth2 .=" order by $colorder";
437         warn "". $strsth2;
438         
439         my $sth2 = $dbh->prepare( $strsth2 );
440         if (( @colfilter ) and ($colfilter[1])){
441                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
442         } elsif ($colfilter[0]) {
443                 $sth2->execute($colfilter[0]);
444         } else {
445                 $sth2->execute;
446         }
447         
448
449         while (my ($celvalue) = $sth2->fetchrow) {
450                 my %cell;
451                 my %ft;
452 #               warn "coltitle :".$celvalue;
453                 $cell{coltitle} = $celvalue;
454                 $ft{totalcol} = 0;
455                 push @loopcol, \%cell;
456         }
457 #       warn "fin des titres colonnes";
458
459         my $i=0;
460         my @totalcol;
461         my $hilighted=-1;
462         
463         #Initialization of cell values.....
464         my %table;
465         my %wgttable;
466         my %cnttable;
467         
468 #       warn "init table";
469         foreach my $row ( @loopline ) {
470                 foreach my $col ( @loopcol ) {
471 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
472                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
473                 }
474                 $table{$row->{rowtitle}}->{totalrow}=0;
475         }
476
477 # preparing calculation
478         my $strcalc ;
479         
480 # Processing average loanperiods
481         $strcalc .= "SELECT $linefield, $colfield, ";
482         $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";
483
484         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
485         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
486         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
487         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
488         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
489         $strcalc .= " AND issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
490         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
491         $strcalc .= " AND issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
492         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
493         $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
494         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
495         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
496         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
497         $strcalc .= " AND issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
498         @$filters[11]=~ s/\*/%/g if (@$filters[11]);
499         $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
500         @$filters[12]=~ s/\*/%/g if (@$filters[12]);
501         $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
502         $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
503         $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
504         $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
505         $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
506         
507         $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
508         warn "SQL :". $strcalc;
509         
510         my $dbcalc = $dbh->prepare($strcalc);
511         $dbcalc->execute;
512 #       warn "filling table";
513         my $issues_count=0;
514         my $previous_row; 
515         my $previous_col;
516         my $loanlength; 
517         my $err;
518         my $emptycol;
519         my $weightrow;
520         
521         while (my  @data = $dbcalc->fetchrow) {
522                 my ($row, $col, $issuedate, $returndate, $weight)=@data;
523 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
524                 $emptycol=1 if ($col eq undef);
525                 $col = "zzEMPTY" if ($col eq undef);
526                 $row = "zzEMPTY" if ($row eq undef);
527 #               warn "506 row :".$row." column :".$col;
528                 my @result =split /:/,DateCalc($returndate,$issuedate) ;
529 #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
530 #  and seconds between the two
531                 $loanlength = $result[2]*7+$result[3];
532 #               warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
533 #               warn "513 row :".$row." column :".$col;
534                 $table{$row}->{$col}+=$weight*$loanlength;
535 #               $table{$row}->{totalrow}+=$weight*$loanlength;
536                 $cnttable{$row}->{$col}= 1;
537                 $wgttable{$row}->{$col}+=$weight;
538         }
539         
540         push @loopcol,{coltitle => "NULL"} if ($emptycol);
541         
542         foreach my $row ( sort keys %table ) {
543                 my @loopcell;
544         #@loopcol ensures the order for columns is common with column titles
545         # and the number matches the number of columns
546                 my $colcount=0;
547                 foreach my $col ( @loopcol ) {
548                         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}});
549
550                         $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
551                         $table{$row}->{totalrow}+=$value;
552                         #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
553                         $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
554                         push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
555                 }
556                 #warn "row : $row colcount:$colcount";
557                 my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
558                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
559                                                 'loopcell' => \@loopcell,
560                                                 'hilighted' => ($hilighted >0),
561                                                 'totalrow' => ($total)?sprintf("%.2f",$total):0
562                                         };
563                 $hilighted = -$hilighted;
564         }
565 #       
566 # #     warn "footer processing";
567         foreach my $col ( @loopcol ) {
568                 my $total=0;
569                 my $nbrow=0;
570                 foreach my $row ( @looprow ) {
571                         $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}};
572                         $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
573 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
574                 }
575 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
576                 $total = $total/$nbrow if ($nbrow);
577                 push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
578         
579         }
580                         
581
582         # the header of the table
583         $globalline{loopfilter}=\@loopfilter;
584         # the core of the table
585         $globalline{looprow} = \@looprow;
586         $globalline{loopcol} = \@loopcol;
587 #       # the foot (totals by borrower type)
588         $globalline{loopfooter} = \@loopfooter;
589         $globalline{total}= $grantotal;
590         $globalline{line} = $line;
591         $globalline{column} = $column;
592         push @mainloop,\%globalline;
593         return \@mainloop;
594 }
595
596 1;