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