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