Update issues_stats to use authorised_values instead of distinct(sortn); add col...
[koha.git] / reports / issues_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::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Members;
31 use Date::Manip;
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 $debug = 1;
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/issues_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 my $podsp = $input->param("DisplayBy");
53 my $type = $input->param("PeriodTypeSel");
54 my $daysel = $input->param("PeriodDaySel");
55 my $monthsel = $input->param("PeriodMonthSel");
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 my $del = $input->param("sep");
61 #warn "calcul : ".$calc;
62 my ($template, $borrowernumber, $cookie)
63     = get_template_and_user({template_name => $fullreportname,
64                             query => $input,
65                             type => "intranet",
66                             authnotrequired => 0,
67                             flagsrequired => {reports => 1},
68                             debug => 0,
69                             });
70 $template->param(do_it => $do_it,
71         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
72                 );
73
74 my $itemtypes = GetItemTypes();
75 my $categoryloop = GetBorrowercategoryList;
76
77 my $ccodes = GetKohaAuthorisedValues("items.ccode");
78 my $locations = GetKohaAuthorisedValues("items.location");
79
80 my $Bsort1 = GetAuthorisedValues("Bsort1");
81 my $Bsort2 = GetAuthorisedValues("Bsort2");
82 my ($hassort1,$hassort2);
83 $hassort1=1 if $Bsort1;
84 $hassort2=1 if $Bsort2;
85
86
87 if ($do_it) {
88 # Displaying results
89     my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
90     if ($output eq "screen"){
91 # Printing results to screen
92             $template->param(mainloop => $results);
93             output_html_with_http_headers $input, $cookie, $template->output;
94             exit(1);
95     } else {
96 # Printing to a csv file
97         print $input->header(-type => 'application/vnd.sun.xml.calc',
98                             -encoding    => 'utf-8',
99                             -attachment=>"$basename.csv",
100                             -filename=>"$basename.csv" );
101         my $cols = @$results[0]->{loopcol};
102         my $lines = @$results[0]->{looprow};
103         my $sep;
104         $sep =C4::Context->preference("delimiter");
105 # header top-right
106         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
107 # Other header
108         foreach my $col ( @$cols ) {
109                 print $col->{coltitle}.$sep;
110         }
111         print "Total\n";
112 # Table
113         foreach my $line ( @$lines ) {
114                 my $x = $line->{loopcell};
115                 print $line->{rowtitle}.$sep;
116                 foreach my $cell (@$x) {
117                         print $cell->{value}.$sep;
118                 }
119                 print $line->{totalrow};
120                 print "\n";
121         }
122 # footer
123         print "TOTAL";
124         $cols = @$results[0]->{loopfooter};
125         foreach my $col ( @$cols ) {
126                 print $sep.$col->{totalcol};
127         }
128         print $sep.@$results[0]->{total};
129         exit(1);
130     }
131 # Displaying choices
132 } else {
133     my $dbh = C4::Context->dbh;
134     my @values;
135     my %labels;
136     my %select;
137
138    # create itemtype arrayref for <select>.
139    my @itemtypeloop;
140     for my $itype ( keys(%$itemtypes)) {
141                 push @itemtypeloop, { code => $itype , description => $itemtypes->{$itype}->{description} } ;
142         }
143     
144     my $branches=GetBranches();
145         my @branchloop;
146     foreach (keys %$branches) {
147         my $thisbranch = ''; 
148         my %row = (branchcode => $_,
149             selected => ($thisbranch eq $_ ? 1 : 0),
150             code => $branches->{$_}->{'branchcode'},
151             description => $branches->{$_}->{'branchname'},
152         );
153         push @branchloop, \%row;
154     }
155
156     # location list
157     my @locations;
158     foreach (sort keys %$locations) {
159         push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
160     }
161     
162     my @ccodes;
163     foreach (keys %$ccodes) {
164         push @ccodes, { code => $_, description => $ccodes->{$_} };
165     }
166
167     # various
168     my @mime = ( C4::Context->preference("MIME") );
169     
170     my $CGIextChoice=CGI::scrolling_list(
171                             -name     => 'MIME',
172                             -id       => 'MIME',
173                             -values   => \@mime,
174                             -size     => 1,
175                             -multiple => 0 );
176     
177     my @dels = ( C4::Context->preference("delimiter") );
178     my $CGIsepChoice=CGI::scrolling_list(
179                             -name     => 'sep',
180                             -id       => 'sep',
181                             -values   => \@dels,
182                             -size     => 1,
183                             -multiple => 0 );
184  
185     $template->param(
186         categoryloop => $categoryloop,
187                 itemtypeloop => \@itemtypeloop,
188                 hassort1=> $hassort1,
189         hassort2=> $hassort2,
190                 Bsort1 => $Bsort1,
191                 Bsort2 => $Bsort2,
192         CGIextChoice => $CGIextChoice,
193         CGIsepChoice => $CGIsepChoice,
194         locationloop => \@locations,
195                 ccodeloop => \@ccodes,
196         branchloop => \@branchloop,
197         );
198     output_html_with_http_headers $input, $cookie, $template->output;
199 }
200
201
202
203
204 sub calculate {
205         my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
206         my @mainloop;
207         my @loopfooter;
208         my @loopcol;
209         my @loopline;
210         my @looprow;
211         my %globalline;
212         my $grantotal =0;
213 # extract parameters
214         my $dbh = C4::Context->dbh;
215
216 # Filters
217 # Checking filters
218 #
219         my @loopfilter;
220         for (my $i=0;$i<=10;$i++) {
221                 my %cell;
222                 if ( @$filters[$i] ) {
223                         if (($i==1) and (@$filters[$i-1])) {
224                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
225                             }
226                         # format the dates filters, otherwise just fill as is
227                         if ($i>=2) {
228                             $cell{filter} .= @$filters[$i];
229                         } else {
230                             $cell{filter} .= format_date(@$filters[$i]);
231                         }
232                         $cell{crit} .="Period From" if ($i==0);
233                         $cell{crit} .="Period To" if ($i==1);
234                         $cell{crit} .="Patron Category=" if ($i==2);
235                         $cell{crit} .="Item Type=" if ($i==3);
236                         $cell{crit} .="Library=" if ($i==4);
237                         $cell{crit} .="Collection=" if ($i==5);
238                         $cell{crit} .="Location=" if ($i==6);
239                         $cell{crit} .="Item callnumber>=" if ($i==7);
240                         $cell{crit} .="Item callnumber<" if ($i==8);
241                         $cell{crit} .="sort1=" if ($i==9);
242                         $cell{crit} .="sort2=" if ($i==10);
243                                                 # FIXME - no translation mechanism !
244                         push @loopfilter, \%cell;
245                 }
246         }
247         push @loopfilter,{crit=>"Event",filter=>$type};
248         push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
249         push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
250         push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
251         
252         
253         my @linefilter;
254       $debug and warn "filtres ". join "|", @filters;
255         my ($colsource, $linesource);
256         $linefilter[0] = @$filters[0] if ($line =~ /datetime/ )  ;
257         $linefilter[1] = @$filters[1] if ($line =~ /datetime/ )  ;
258                 if ($line =~ /category/ )  {
259                 $linefilter[0] = @$filters[2] ;
260                 }
261                 if ($line =~ /itemtype/ ) {
262                 $linefilter[0] = @$filters[3] ;
263                 }
264         $linefilter[0] = @$filters[4] if ($line =~ /branch/ )  ;
265                 if ($line =~ /ccode/ ) {
266                 $linefilter[0] = @$filters[5] ;
267                         $linesource = 'items';
268                 }
269                 if ($line =~ /location/ ) {
270                 $linefilter[0] = @$filters[6] ;
271                         $linesource = 'items';
272                 }
273         $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
274         $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
275
276         my @colfilter ;
277         $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
278         $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
279         $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
280         $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
281         $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
282                 if ($column =~ /ccode/ ) {
283                 $colfilter[0] = @$filters[5] ;
284                         $colsource = 'items';
285                 }
286                 if ($column =~ /location/ ) {
287                 $colfilter[0] = @$filters[6] ;
288                         $colsource = 'items';
289                 }
290         $colfilter[0] = @$filters[9] if ($column =~ /sort1/  )  ;
291         $colfilter[0] = @$filters[10] if ($column =~ /sort2/  )  ;
292 # 1st, loop rows.                             
293         my $linefield;                               
294         if (($line =~/datetime/) and ($dsp == 1)) {
295                 #Display by day
296                 $linefield .="dayname($line)";  
297         } elsif (($line=~/datetime/) and ($dsp == 2)) {
298                 #Display by Month
299                 $linefield .="monthname($line) ";  
300         } elsif (($line=~/datetime/) and ($dsp == 3)) {
301                 #Display by Year
302                 $linefield .="Year($line)";
303         } elsif ($line=~/datetime/) {
304                 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
305         } else {
306                 $linefield .= $line;
307         }  
308         my $lineorder = $linefield;
309         $lineorder = "weekday($line)" if $linefield =~ /dayname/;
310         $lineorder = "month($line)" if $linefield =~ "^month";
311         $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
312
313         my $strsth;
314         $strsth .= "select distinctrow $linefield from statistics, ";
315                 # get stats on items if ccode or location, otherwise borrowers.
316                 $strsth .= ($linesource eq 'items' ) ? 
317                                                 "items where (statistics.itemnumber=items.itemnumber) " 
318                                                 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
319                 $strsth .= " and $line is not null ";
320         
321         if ($line=~/datetime/) {
322                 if ($linefilter[1] and ($linefilter[0])){
323                         $strsth .= " and $line between ? and ? " ;
324                 } elsif ($linefilter[1]) {
325                                 $strsth .= " and $line < ? " ;
326                 } elsif ($linefilter[0]) {
327                         $strsth .= " and $line > ? " ;
328                 }
329                 $strsth .= " and type ='".$type."' " if $type;
330                 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
331                 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
332         } elsif ($linefilter[0]) {
333                 $linefilter[0] =~ s/\*/%/g;
334                 $strsth .= " and $line LIKE ? " ;
335         }
336         $strsth .=" group by $linefield";
337         $strsth .=" order by $lineorder";
338         $debug and warn $strsth;
339         
340         my $sth = $dbh->prepare( $strsth );
341         if (( @linefilter ) and ($linefilter[1])){
342                 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
343         } elsif ($linefilter[0]) {
344                 $sth->execute($linefilter[0]);
345         } else {
346                 $sth->execute;
347         }
348         
349         while ( my ($celvalue) = $sth->fetchrow) {
350                 my %cell;
351                                 if($line =~ /ccode/) {
352                                         $cell{rowtitle_display} = $ccodes->{$celvalue};
353                                 } elsif($line=~/location/) {
354                                          $cell{rowtitle_display} = $locations->{$celvalue};
355                                 } elsif($line=~/sort1/) {
356                                         for my $s (@$Bsort1) {
357                                                 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
358                                         }
359                                         $cell{rowtitle_display} = $celvalue unless  $cell{rowtitle_display};
360                                 } elsif($line=~/sort2/) {
361                                         for my $s (@$Bsort2) {
362                                                 $cell{rowtitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
363                                         }
364                                         $cell{rowtitle_display} = $celvalue unless  $cell{rowtitle_display};
365                                 } elsif($line=~/categorycode/) {
366                                         for my $s (@$categoryloop) { 
367                                                 $cell{rowtitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
368                                         }
369                                         $cell{rowtitle_display} = $celvalue unless  $cell{rowtitle_display};
370                                 } elsif($line=~/itemtype/) {
371                                         $cell{rowtitle_display} = $itemtypes->{$celvalue}->{description};
372                                 } else {
373                         $cell{rowtitle_display} = $celvalue;
374                                 }                                       
375                 if ($celvalue) {
376                         $cell{rowtitle} = $celvalue;
377                 } else {
378                         $cell{rowtitle} = "";
379                 }
380                 $cell{totalrow} = 0;
381                 push @loopline, \%cell;
382         }
383
384 # 2nd, loop cols.
385         my $colfield;
386         my $colorder;                               
387         if (($column =~/datetime/) and ($dsp == 1)) {
388                 #Display by day
389                 $colfield .="dayname($column)";  
390         } elsif (($column=~/datetime/) and ($dsp == 2)) {
391                 #Display by Month
392                 $colfield .="monthname($column)";  
393         } elsif (($column=~/datetime/) and ($dsp == 3)) {
394                 #Display by Year
395                 $colfield .="Year($column)";
396         } elsif ($column=~/datetime/) {
397                 $colfield .='date_format(`datetime`,"%Y-%m-%d")';       
398         } else {
399                 $colfield .= $column;
400         }  
401         $colorder = "weekday($line)" if $colfield =~ "^dayname";
402         $colorder = "month($line)" if $colfield =~ "^month";
403         $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
404         
405         my $strsth2; 
406         $strsth2 .= "select distinctrow $colfield from statistics, ";
407                 # get stats on items if ccode or location, otherwise borrowers.
408                 $strsth2 .= ($colsource eq 'items' ) ? 
409                                                 "items where (statistics.itemnumber=items.itemnumber) " 
410                                                 : " borrowers where (statistics.borrowernumber=borrowers.borrowernumber) ";
411                 $strsth2 .= " and $column is not null ";
412         
413         if ($column=~/datetime/){
414                 if (($colfilter[1]) and ($colfilter[0])){
415                         $strsth2 .= " and $column between ? and ? " ;
416                 } elsif ($colfilter[1]) {
417                         $strsth2 .= " and $column < ? " ;
418                 } elsif ($colfilter[0]) {
419                         $strsth2 .= " and $column > ? " ;
420                 }
421                 $strsth2 .= " and type ='".$type."' " if $type;
422                 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
423                 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
424         } elsif ($colfilter[0]) {
425                 $colfilter[0] =~ s/\*/%/g;
426                 $strsth2 .= " and $column LIKE ? " ;
427         }
428         $strsth2 .=" group by $colfield";
429         $strsth2 .=" order by $colorder";
430         
431         my $sth2 = $dbh->prepare( $strsth2 );
432         if (( @colfilter ) and ($colfilter[1])){
433                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
434         } elsif ($colfilter[0]) {
435                 $sth2->execute($colfilter[0]);
436         } else {
437                 $sth2->execute;
438         }
439         
440
441         while (my ($celvalue) = $sth2->fetchrow) {
442                 my %cell;
443                 my %ft;
444                                 if($column =~ /ccode/) {
445                                         $cell{coltitle_display} = $ccodes->{$celvalue};
446                                 } elsif($column=~/location/) {
447                                          $cell{coltitle_display} = $locations->{$celvalue};
448                                 } elsif($column=~/itemtype/) {
449                                         $cell{coltitle_display} = $itemtypes->{$celvalue}->{description};
450                                 } elsif($column=~/sort1/) {
451                                         for my $s (@$Bsort1) {
452                                                 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
453                                         }
454                                         $cell{coltitle_display} = $celvalue unless  $cell{coltitle_display};
455                                 } elsif($column=~/sort2/) {
456                                         for my $s (@$Bsort2) {
457                                                 $cell{coltitle_display} = $s->{lib} if ($celvalue eq $s->{authorised_value});
458                                         }
459                                         $cell{coltitle_display} = $celvalue unless  $cell{coltitle_display};
460                                 } elsif($column=~/category/) {
461                                         for my $s (@$categoryloop) {
462                                                 $cell{coltitle_display} = $s->{description} if ($celvalue eq $s->{categorycode});
463                                         }
464                                         $cell{coltitle_display} = $celvalue unless  $cell{coltitle_display};
465                                 } else {
466                         $cell{coltitle_display} = $celvalue;
467                                 }                                       
468                 $cell{coltitle} = $celvalue;
469                                 # we leave this as 'coltitle' since we use it as hash key when filling the table, and add a title_display key.
470                 $ft{totalcol} = 0;
471                 push @loopcol, \%cell;
472         }
473 #       warn "fin des titres colonnes";
474
475         my $i=0;
476         my @totalcol;
477         
478         #Initialization of cell values.....
479         my %table;
480 #       warn "init table";
481         foreach my $row ( @loopline ) {
482                 foreach my $col ( @loopcol ) {
483                                 $debug and warn " init table : $row->{rowtitle} ( $row->{rowtitle_display} ) / $col->{coltitle} ( $col->{coltitle_display} )  ";
484                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
485                 }
486                 $table{$row->{rowtitle}}->{totalrow}=0;
487         }
488
489 # preparing calculation
490         my $strcalc ;
491
492         $strcalc .= "SELECT $linefield, $colfield, ";
493         $strcalc .= "COUNT( * ) " if ($process ==1);
494         if ($process ==2){
495                 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
496         }
497         if ($process ==3){
498                 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
499         }
500         if ($process ==4){
501                 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
502                 $rqbookcount->execute;
503                 my ($bookcount) = $rqbookcount->fetchrow;
504                 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
505         }
506         $strcalc .= "FROM statistics ";
507         $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
508         $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if ( ($colsource eq 'items') || @$filters[5] || @$filters[6] ||@$filters[7] || @$filters[8] );
509         
510         $strcalc .= "WHERE 1=1 ";
511         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
512         $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
513         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
514         $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
515         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
516         $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
517         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
518         $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
519         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
520         $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
521         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
522         $strcalc .= " AND items.ccode like '" . @$filters[5] ."'" if ( @$filters[5] );
523         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
524         $strcalc .= " AND items.location like '" . @$filters[6] ."'" if ( @$filters[6] );
525         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
526         $strcalc .= " AND items.itemcallnumber >='" . @$filters[7] ."'" if ( @$filters[7] );
527         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
528         $strcalc .= " AND items.itemcallnumber <'" . @$filters[8] ."'" if ( @$filters[8] );
529         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
530         $strcalc .= " AND borrowers.sort1 like '" . @$filters[9] ."'" if ( @$filters[9] );
531         @$filters[10]=~ s/\*/%/g if (@$filters[10]);
532         $strcalc .= " AND borrowers.sort2 like '" . @$filters[10] ."'" if ( @$filters[10] );
533         $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
534         $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
535         $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
536         
537         $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
538         ($debug) and warn "". $strcalc;
539         my $dbcalc = $dbh->prepare($strcalc);
540         $dbcalc->execute;
541 #       warn "filling table";
542         my ($emptycol,$emptyrow); 
543         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
544                 ($debug) and warn "filling table $row / $col / $value ";
545                                 if ($col eq undef) {
546                         $emptycol = 1; 
547                         $col = "zzEMPTY" ;
548                                 }
549                                 if ($row eq undef) {
550                                         $emptyrow = 1;
551                         $row = "zzEMPTY"; 
552                 }
553                 $table{$row}->{$col}+=$value;
554                 $table{$row}->{totalrow}+=$value;
555                 $grantotal += $value;
556         }
557         push @loopcol,{coltitle => "NULL", coltitle_display => 'NULL'} if ($emptycol);
558         push @loopline,{rowtitle => "NULL", rowtitle_display => 'NULL'} if ($emptyrow);
559
560         foreach my $row (@loopline) {
561                 my @loopcell;
562                 #@loopcol ensures the order for columns is common with column titles
563                 # and the number matches the number of columns
564                 foreach my $col ( @loopcol ) {
565                         my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
566                         push @loopcell, {value => $value  } ;
567                 }
568                 push @looprow,  { 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
569                                                                 'rowtitle_display' => ($row->{rowtitle_display} eq "NULL")?"NULL":$row->{rowtitle_display},
570                                 'loopcell' => \@loopcell,
571                                 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
572                                                 };
573         }
574 #       warn "footer processing";
575         for my $col ( @loopcol ) {
576                 my $total=0;
577                 foreach my $row ( @looprow ) {
578                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
579                         $debug and warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
580                 }
581 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
582                 push @loopfooter, {'totalcol' => $total};
583         }
584                         
585
586         # the header of the table
587         $globalline{loopfilter}=\@loopfilter;
588         # the core of the table
589         $globalline{looprow} = \@looprow;
590         $globalline{loopcol} = \@loopcol;
591 #       # the foot (totals by borrower type)
592         $globalline{loopfooter} = \@loopfooter;
593         $globalline{total}= $grantotal;
594         $globalline{line} = $line;
595         $globalline{column} = $column;
596         push @mainloop,\%globalline;
597         return \@mainloop;
598 }
599
600 1;