Bug Fix : 1898 Replaces the previous commit : More fixes.
[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::Dates qw/format_date format_date_in_iso/;
30 use Date::Calc qw(Delta_Days);
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38 =over 2
39
40 =cut
41
42 my $input = new CGI;
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_avg_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso($filters[0]);
49 $filters[1]=format_date_in_iso($filters[1]);
50 $filters[2]=format_date_in_iso($filters[2]);
51 $filters[3]=format_date_in_iso($filters[3]);
52 my $podsp = $input->param("IssueDisplay");
53 my $rodsp = $input->param("ReturnDisplay");
54 my $calc = $input->param("Cellvalue");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 my $del = $input->param("sep");
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 $template->param(do_it => $do_it,
69         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
70     );
71 if ($do_it) {
72 # Displaying results
73     my $results = calculate($line, $column, $rodsp, $podsp, $calc, \@filters);
74     if ($output eq "screen"){
75 # Printing results to screen
76         $template->param(mainloop => $results);
77         output_html_with_http_headers $input, $cookie, $template->output;
78         exit(1);
79     } else {
80 # Printing to a csv file
81         print $input->header(-type => 'application/vnd.sun.xml.calc',
82                                     -encoding    => 'utf-8',
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 => '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 @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             # format the dates filters, otherwise just fill as is
264             if ($i>=4) {
265                 $cell{filter} .= @$filters[$i];
266             } else {
267                 $cell{filter} .= format_date(@$filters[$i]);
268             }
269             $cell{crit} .="Issue From" if ($i==0);
270             $cell{crit} .="Issue To" if ($i==1);
271             $cell{crit} .="Issue Month" if ($i==2);
272             $cell{crit} .="Issue Day" if ($i==3);
273             $cell{crit} .="Return From" if ($i==4);
274             $cell{crit} .="Return To" if ($i==5);
275             $cell{crit} .="Return Month" if ($i==6);
276             $cell{crit} .="Return Day" if ($i==7);
277             $cell{crit} .="Borrower Cat" if ($i==8);
278             $cell{crit} .="Doc Type" if ($i==9);
279             $cell{crit} .="Branch" if ($i==10);
280             $cell{crit} .="Sort1" if ($i==11);
281             $cell{crit} .="Sort2" if ($i==12);
282             push @loopfilter, \%cell;
283         }
284     }
285     push @loopfilter,{crit=>"Issue Display",filter=>$rodsp} if ($rodsp);
286     push @loopfilter,{crit=>"Return Display",filter=>$podsp} if ($podsp);
287
288     
289     
290     my @linefilter;
291 #       warn "filtres ".@filters[0];
292 #       warn "filtres ".@filters[1];
293 #       warn "filtres ".@filters[2];
294 #       warn "filtres ".@filters[3];
295     $line = "old_issues.".$line if ($line=~/branchcode/) or ($line=~/timestamp/);
296     $line = "biblioitems.".$line if $line=~/itemtype/;
297     
298     $linefilter[0] = @$filters[0] if ($line =~ /timestamp/ )  ;
299     $linefilter[1] = @$filters[1] if ($line =~ /timestamp/ )  ;
300     $linefilter[2] = @$filters[2] if ($line =~ /timestamp/ )  ;
301     $linefilter[3] = @$filters[3] if ($line =~ /timestamp/ )  ;
302     $linefilter[0] = @$filters[4] if ($line =~ /returndate/ )  ;
303     $linefilter[1] = @$filters[5] if ($line =~ /returndate/ )  ;
304     $linefilter[2] = @$filters[6] if ($line =~ /returndate/ )  ;
305     $linefilter[3] = @$filters[7] if ($line =~ /returndate/ )  ;
306     $linefilter[0] = @$filters[8] if ($line =~ /category/ )  ;
307     $linefilter[0] = @$filters[9] if ($line =~ /itemtype/ )  ;
308     $linefilter[0] = @$filters[10] if ($line =~ /branch/ )  ;
309 #       $linefilter[0] = @$filters[11] if ($line =~ /sort2/ ) ;
310     $linefilter[0] = @$filters[11] if ($line =~ /sort1/ ) ;
311     $linefilter[0] = @$filters[12] if ($line =~ /sort2/ ) ;
312 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
313
314     $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
315     $column = "biblioitems.".$column if $column=~/itemtype/;
316     my @colfilter ;
317     $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
318     $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
319     $colfilter[2] = @$filters[2] if ($column =~ /timestamp/ )  ;
320     $colfilter[3] = @$filters[3] if ($column =~ /timestamp/ )  ;
321     $colfilter[0] = @$filters[4] if ($column =~ /returndate/ )  ;
322     $colfilter[1] = @$filters[5] if ($column =~ /returndate/ )  ;
323     $colfilter[2] = @$filters[6] if ($column =~ /returndate/ )  ;
324     $colfilter[3] = @$filters[7] if ($column =~ /returndate/ )  ;
325     $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
326     $colfilter[0] = @$filters[9] if ($column =~ /itemtype/ )  ;
327     $colfilter[0] = @$filters[10] if ($column =~ /branch/ )  ;
328 #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
329     $colfilter[0] = @$filters[11] if ($column =~ /sort1/ ) ;
330     $colfilter[0] = @$filters[12] if ($column =~ /sort2/ ) ;
331 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
332                                             
333 # 1st, loop rows.                             
334     my $linefield;
335     my $lineorder;                               
336     if ((($line =~/timestamp/) and ($podsp == 1)) or  (($line =~/returndate/) and ($rodsp == 1))) {
337         #Display by day
338         $linefield .="dayname($line)";  
339         $lineorder .="weekday($line)";  
340     } elsif ((($line =~/timestamp/) and ($podsp == 2)) or  (($line =~/returndate/) and ($rodsp == 2))) {
341         #Display by Month
342         $linefield .="monthname($line)";  
343         $lineorder .="month($line)";  
344     } elsif ((($line =~/timestamp/) and ($podsp == 3)) or  (($line =~/returndate/) and ($rodsp == 3))) {
345         #Display by Year
346         $linefield .="Year($line)";
347         $lineorder .= $line;  
348     } elsif (($line=~/timestamp/) or ($line=~/returndate/)){
349         $linefield .= "date_format(\'$line\',\"%Y-%m-%d\")";
350         $lineorder .= $line;  
351     } else {
352         $linefield .= $line;
353         $lineorder .= $line;  
354     }  
355     
356     my $strsth;
357     $strsth .= "select distinctrow $linefield 
358                 FROM `old_issues` 
359                 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
360                 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
361                 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
362                 LEFT JOIN issuingrules ON 
363                     (issuingrules.branchcode=old_issues.branchcode
364                     AND  issuingrules.itemtype=biblioitems.itemtype 
365                     AND  issuingrules.categorycode=borrowers.categorycode) 
366                 WHERE returndate is not null";
367     
368     if (($line=~/timestamp/) or ($line=~/returndate/)){
369         if ($linefilter[1] and ($linefilter[0])){
370             $strsth .= " and $line between '$linefilter[0]' and '$linefilter[1]' " ;
371         } elsif ($linefilter[1]) {
372                 $strsth .= " and $line < \'$linefilter[1]\' " ;
373         } elsif ($linefilter[0]) {
374             $strsth .= " and $line > \'$linefilter[0]\' " ;
375         }
376         if ($linefilter[2]){
377             $strsth .= " and dayname($line) = '$linefilter[2]' " ;
378         }
379         if ($linefilter[3]){
380             $strsth .= " and monthname($line) = '$linefilter[3]' " ;
381         }
382     } elsif ($linefilter[0]) {
383         $linefilter[0] =~ s/\*/%/g;
384         $strsth .= " and $line LIKE '$linefilter[0]' " ;
385     }
386     $strsth .=" group by $linefield";
387     $strsth .=" order by $lineorder";
388    
389     my $sth = $dbh->prepare( $strsth );
390     $sth->execute;
391
392     
393     while ( my ($celvalue) = $sth->fetchrow) {
394         my %cell;
395         if ($celvalue) {
396             $cell{rowtitle} = $celvalue;
397         } else {
398             $cell{rowtitle} = "";
399         }
400         $cell{totalrow} = 0;
401         push @loopline, \%cell;
402     }
403
404 # 2nd, loop cols.
405     my $colfield;
406     my $colorder;                               
407     if ((($column =~/timestamp/) and ($podsp == 1)) or  (($column =~/returndate/) and ($rodsp == 1))) {
408         #Display by day
409         $colfield .="dayname($column)";  
410         $colorder .="weekday($column)";
411     } elsif ((($column =~/timestamp/) and ($podsp == 2)) or  (($column =~/returndate/) and ($rodsp == 2))) {
412         #Display by Month
413         $colfield .="monthname($column)";  
414         $colorder .="month($column)";  
415     } elsif ((($column =~/timestamp/) and ($podsp == 3)) or  (($column =~/returndate/) and ($rodsp == 3))) {
416         #Display by Year
417         $colfield .="Year($column)";
418         $colorder .= $column;
419     } elsif (($column=~/timestamp/) or ($column=~/returndate/)){
420         $colfield .= 'date_format( '."'".$column."'". ', "%Y-%m-%d")';
421         $colorder .= $column;
422     } else {
423         $colfield .= $column;
424         $colorder .= $column;
425     }  
426     
427     my $strsth2;
428     $strsth2 .= "SELECT distinctrow $colfield 
429                   FROM `old_issues`
430                   LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
431                   LEFT JOIN items  ON items.itemnumber=old_issues.itemnumber  
432                   LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) 
433                   LEFT JOIN issuingrules ON 
434                     (issuingrules.branchcode=old_issues.branchcode 
435                     AND  issuingrules.itemtype=biblioitems.itemtype 
436                     AND  issuingrules.categorycode=borrowers.categorycode) 
437                   WHERE returndate is not null";
438     
439     if (($column=~/timestamp/) or ($column=~/returndate/)){
440         if ($colfilter[1] and ($colfilter[0])){
441             $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
442         } elsif ($colfilter[1]) {
443                 $strsth2 .= " and $column < '$colfilter[1]' " ;
444         } elsif ($colfilter[0]) {
445             $strsth2 .= " and $column > '$colfilter[0]' " ;
446         }
447         if ($colfilter[2]){
448             $strsth2 .= " and dayname($column) = '$colfilter[2]' " ;
449         }
450         if ($colfilter[3]){
451             $strsth2 .= " and monthname($column) = '$colfilter[3]' " ;
452         }
453     } elsif ($colfilter[0]) {
454         $colfilter[0] =~ s/\*/%/g;
455         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
456     }
457     $strsth2 .=" group by $colfield";
458     $strsth2 .=" order by $colorder";
459     warn "". $strsth2;
460     
461     my $sth2 = $dbh->prepare( $strsth2 );
462     if (( @colfilter ) and ($colfilter[1])){
463         $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
464     } elsif ($colfilter[0]) {
465         $sth2->execute($colfilter[0]);
466     } else {
467         $sth2->execute;
468     }
469     
470
471     while (my ($celvalue) = $sth2->fetchrow) {
472         my %cell;
473         my %ft;
474 #               warn "coltitle :".$celvalue;
475         $cell{coltitle} = $celvalue;
476         $ft{totalcol} = 0;
477         push @loopcol, \%cell;
478     }
479 #       warn "fin des titres colonnes";
480
481     my $i=0;
482     my @totalcol;
483     my $hilighted=-1;
484     
485     #Initialization of cell values.....
486     my %table;
487     my %wgttable;
488     my %cnttable;
489     
490 #       warn "init table";
491     foreach my $row ( @loopline ) {
492         foreach my $col ( @loopcol ) {
493 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
494             $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
495         }
496         $table{$row->{rowtitle}}->{totalrow}=0;
497     }
498
499 # preparing calculation
500     my $strcalc ;
501     
502 # Processing average loanperiods
503     $strcalc .= "SELECT $linefield, $colfield, ";
504     $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";
505
506     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
507     $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
508     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
509     $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
510     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
511     $strcalc .= " AND old_issues.returndate > '" . @$filters[4] ."'" if ( @$filters[4] );
512     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
513     $strcalc .= " AND old_issues.returndate < '" . @$filters[5] ."'" if ( @$filters[5] );
514     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
515     $strcalc .= " AND borrowers.categorycode like '" . @$filters[8] ."'" if ( @$filters[8] );
516     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
517     $strcalc .= " AND biblioitems.itemtype like '" . @$filters[9] ."'" if ( @$filters[9] );
518     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
519     $strcalc .= " AND old_issues.branchcode like '" . @$filters[10] ."'" if ( @$filters[10] );
520     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
521     $strcalc .= " AND borrowers.sort1 like '" . @$filters[11] ."'" if ( @$filters[11] );
522     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
523     $strcalc .= " AND borrowers.sort2 like '" . @$filters[12] ."'" if ( @$filters[12] );
524     $strcalc .= " AND dayname(timestamp) like '" . @$filters[2]."'" if (@$filters[2]);
525     $strcalc .= " AND monthname(timestamp) like '" . @$filters[3] ."'" if ( @$filters[3] );
526     $strcalc .= " AND dayname(returndate) like '" . @$filters[5]."'" if (@$filters[5]);
527     $strcalc .= " AND monthname(returndate) like '" . @$filters[6] ."'" if ( @$filters[6] );
528     
529     $strcalc .= " group by  $linefield, $colfield, issuedate, returndate order by $linefield, $colfield";
530     warn "SQL :". $strcalc;
531     
532     my $dbcalc = $dbh->prepare($strcalc);
533     $dbcalc->execute;
534 #       warn "filling table";
535     my $issues_count=0;
536     my $previous_row; 
537     my $previous_col;
538     my $loanlength; 
539     my $err;
540     my $emptycol;
541     my $weightrow;
542     
543     while (my  @data = $dbcalc->fetchrow) {
544         my ($row, $col, $issuedate, $returndate, $weight)=@data;
545 #               warn "filling table $row / $col / $issuedate / $returndate /$weight";
546         $emptycol=1 if ($col eq undef);
547         $col = "zzEMPTY" if ($col eq undef);
548         $row = "zzEMPTY" if ($row eq undef);
549         # fill returndate to avoid an error with date calc (needed for all non returned issues)
550         $returndate= join '-',Date::Calc::Today if $returndate eq '0000-00-00';
551     #  DateCalc returns => 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
552     #  and seconds between the two
553         $loanlength = Delta_Days(split(/-/,$issuedate),split (/-/,$returndate)) ;
554     #           warn "512 Same row and col DateCalc returns :$loanlength with return ". $returndate ."issue ". $issuedate ."weight : ". $weight;
555     #           warn "513 row :".$row." column :".$col;
556         $table{$row}->{$col}+=$weight*$loanlength;
557     #           $table{$row}->{totalrow}+=$weight*$loanlength;
558         $cnttable{$row}->{$col}= 1;
559         $wgttable{$row}->{$col}+=$weight;
560     }
561     
562     push @loopcol,{coltitle => "NULL"} if ($emptycol);
563     
564     foreach my $row ( sort keys %table ) {
565         my @loopcell;
566     #@loopcol ensures the order for columns is common with column titles
567     # and the number matches the number of columns
568         my $colcount=0;
569         foreach my $col ( @loopcol ) {
570             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}});
571
572             $table{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}} = $value;
573             $table{$row}->{totalrow}+=$value;
574             #warn "row : $row col:$col  $cnttable{$row}->{(($col->{coltitle} eq \"NULL\")or ($col->{coltitle} eq \"\"))?\"zzEMPTY\":$col->{coltitle}}";
575             $colcount+=$cnttable{$row}->{(($col->{coltitle} eq "NULL")or ($col->{coltitle} eq ""))?"zzEMPTY":$col->{coltitle}};
576             push @loopcell, {value => ($value)?sprintf("%.2f",$value):0  } ;
577         }
578         #warn "row : $row colcount:$colcount";
579         my $total = $table{$row}->{totalrow}/$colcount if ($colcount>0);
580         push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
581                         'loopcell' => \@loopcell,
582                         'hilighted' => ($hilighted >0),
583                         'totalrow' => ($total)?sprintf("%.2f",$total):0
584                     };
585         $hilighted = -$hilighted;
586     }
587 #       
588 # #     warn "footer processing";
589     foreach my $col ( @loopcol ) {
590         my $total=0;
591         my $nbrow=0;
592         foreach my $row ( @looprow ) {
593             $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}};
594             $nbrow +=$cnttable{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};;
595 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
596         }
597 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
598         $total = $total/$nbrow if ($nbrow);
599         push @loopfooter, {'totalcol' => ($total)?sprintf("%.2f",$total):0};
600     
601     }
602             
603
604     # the header of the table
605     $globalline{loopfilter}=\@loopfilter;
606     # the core of the table
607     $globalline{looprow} = \@looprow;
608     $globalline{loopcol} = \@loopcol;
609 #       # the foot (totals by borrower type)
610     $globalline{loopfooter} = \@loopfooter;
611     $globalline{total}= $grantotal;
612     $globalline{line} = $line;
613     $globalline{column} = $column;
614     push @mainloop,\%globalline;
615     return \@mainloop;
616 }
617
618 1;