Adding Borrowers count feature.
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use HTML::Template;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39
40 =over2
41
42 =cut
43
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/catalogue_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $deweydigits = $input->param("deweydigits");
51 my $lccndigits = $input->param("lccndigits");
52 my $cotedigits = $input->param("cotedigits");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
57
58 my ($template, $borrowernumber, $cookie)
59         = get_template_and_user({template_name => $fullreportname,
60                                 query => $input,
61                                 type => "intranet",
62                                 authnotrequired => 0,
63                                 flagsrequired => {editcatalogue => 1},
64                                 debug => 1,
65                                 });
66 $template->param(do_it => $do_it);
67 if ($do_it) {
68         my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
69         if ($output eq "screen"){
70                 $template->param(mainloop => $results);
71                 output_html_with_http_headers $input, $cookie, $template->output;
72                 exit(1);
73         } else {
74                 print $input->header(-type => 'application/vnd.sun.xml.calc',
75                                                          -attachment=>"$basename.csv",
76                                                          -name=>"$basename.csv" );
77                 my $cols = @$results[0]->{loopcol};
78                 my $lines = @$results[0]->{looprow};
79                 my $sep;
80                 $sep =C4::Context->preference("delimiter");
81                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82                 foreach my $col ( @$cols ) {
83                         print $col->{coltitle}.$sep;
84                 }
85                 print "Total\n";
86                 foreach my $line ( @$lines ) {
87                         my $x = $line->{loopcell};
88                         print $line->{rowtitle}.$sep;
89                         foreach my $cell (@$x) {
90                                 print $cell->{value}.$sep;
91                         }
92                         print $line->{totalrow};
93                         print "\n";
94                 }
95                 print "TOTAL";
96                 $cols = @$results[0]->{loopfooter};
97                 foreach my $col ( @$cols ) {
98                         print $sep.$col->{totalcol};
99                 }
100                 print $sep.@$results[0]->{total};
101                 exit(1);
102         }
103 } else {
104         my $dbh = C4::Context->dbh;
105         my @values;
106         my %labels;
107         my $count=0;
108         my $req;
109         $req = $dbh->prepare("select count(dewey) from biblioitems ");
110         $req->execute;
111         my $hasdewey;
112         my @select;
113 #       push @select,"";
114         while (my ($value) =$req->fetchrow) {
115                 $hasdewey =1 if (($value>2) and (! $hasdewey));
116                 $count++ if (($value>2) and (! $hasdewey));
117 #               push @select, $value;
118         }
119 #       my $CGIdewey=CGI::scrolling_list( -name     => 'Filter',
120 #                               -id => 'Filter',
121 #                               -values   => \@select,
122 #                               -size     => 1,
123 #                               -multiple => 0 );
124         
125         $req = $dbh->prepare( "select count(lccn) from biblioitems ");
126         $req->execute;
127 #       undef @select;
128 #       push @select,"";
129         my $haslccn;
130         my $hlghtlccn;
131         while (my ($value) =$req->fetchrow) {
132                 $hlghtlccn = !($hasdewey);
133                 $haslccn =1 if (($value>2) and (! $haslccn));
134                 $count++ if (($value) and (! $haslccn));
135 #               push @select, $value;
136         }
137 #       my $CGIlccn=CGI::scrolling_list( -name     => 'Filter',
138 #                               -id => 'Filter',
139 #                               -values   => \@select,
140 #                               -size     => 1,
141 #                               -multiple => 0 );
142         
143         $req = $dbh->prepare("select count(itemcallnumber) from items");
144         $req->execute;
145 #       undef @select;
146 #       push @select,"";
147         my $hascote;
148         my $hlghtcote;
149         while (my ($value) =$req->fetchrow) {
150                 $hascote =1 if (($value>2) and (! $hascote));
151                 $count++ if (($value) and (! $hascote));
152                 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
153 #               push @select, $value;
154         }
155 #       my $CGIcote=CGI::scrolling_list( -name     => 'Filter',
156 #                               -id => 'Filter',
157 #                               -values   => \@select,
158 #                               -size     => 1,
159 #                               -multiple => 0 );
160         $count++;
161         my $hglghtDT =$count % 2;
162 #       warn "highlightDT ".$hglghtDT;
163         $count++;
164         my $hglghtPub =$count % 2;
165 #       warn "highlightPub ".$hglghtPub;
166         $count++;
167         my $hglghtPY =$count % 2;
168 #       warn "highlightPY ".$hglghtPY;
169         $count++;
170         my $hglghtHB =$count % 2;
171 #       warn "highlightHB ".$hglghtHB;
172         $count++;
173         my $hglghtLOC =$count % 2;
174 #       warn "highlightLOC ".$hglghtLOC;
175         
176         
177         $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
178         $req->execute;
179         undef @select;
180         push @select,"";
181         while (my ($value) =$req->fetchrow) {
182                 push @select, $value;
183         }
184         my $CGIitemtype=CGI::scrolling_list( -name     => 'Filter',
185                                 -id => 'itemtype',
186                                 -values   => \@select,
187                                 -size     => 1,
188                                 -multiple => 0 );
189         
190 #       $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
191 #       $req->execute;
192 #       undef @select;
193 #       push @select,"";
194 #       while (my ($value) =$req->fetchrow) {
195 #               push @select, $value;
196 #       }
197 #       my $CGIpublisher=CGI::scrolling_list( -name     => 'Filter',
198 #                               -id => 'Filter',
199 #                               -values   => \@select,
200 #                               -size     => 1,
201 #                               -multiple => 0 );
202
203         undef @select;
204         push @select,"";
205         my $branches=getbranches();
206         my %select_branches;
207         $select_branches{""} = "";
208         foreach my $branch (keys %$branches) {
209                 push @select, $branch;
210                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
211         }
212         my $CGIbranch=CGI::scrolling_list( -name     => 'Filter',
213                                 -id => 'branch',
214                                 -values   => \@select,
215                                 -labels   => \%select_branches,
216                                 -size     => 1,
217                                 -multiple => 0 );
218         
219         $req = $dbh->prepare("select distinctrow location from items order by location");
220         $req->execute;
221         undef @select;
222         push @select,"";
223         my $CGIlocation=CGI::scrolling_list( -name     => 'Filter',
224                                 -id => 'location',
225                                 -values   => \@select,
226                                 -size     => 1,
227                                 -multiple => 0 );
228         
229         my @mime = ( C4::Context->preference("MIME") );
230         foreach my $mime (@mime){
231 #               warn "".$mime;
232         }
233         
234         my $CGIextChoice=CGI::scrolling_list(
235                                 -name => 'MIME',
236                                 -id => 'MIME',
237                                 -values   => \@mime,
238                                 -size     => 1,
239                                 -multiple => 0 );
240         
241         my @dels = ( C4::Context->preference("delimiter") );
242         my $CGIsepChoice=CGI::scrolling_list(
243                                 -name => 'sep',
244                                 -id => 'sep',
245                                 -values   => \@dels,
246                                 -size     => 1,
247                                 -multiple => 0 );
248         
249         $template->param(hasdewey=>$hasdewey,
250 #                                       CGIFromDeweyClass => $CGIdewey,
251 #                                       CGIToDeweyClass => $CGIdewey,
252                                         haslccn=> $haslccn,
253                                         hlghtlccn => $hlghtlccn,
254 #                                       CGIFromLoCClass => $CGIlccn,
255 #                                       CGIToLoCClass => $CGIlccn,
256                                         hascote=> $hascote,
257                                         hlghtcote => $hlghtcote,
258                                         hglghtDT => $hglghtDT,
259                                         hglghtPub => $hglghtPub,
260                                         hglghtPY => $hglghtPY,
261                                         hglghtHB => $hglghtHB,
262                                         hglghtLOC => $hglghtLOC,
263 #                                       CGIFromCoteClass => $CGIcote,
264 #                                       CGIToCoteClass => $CGIcote,
265                                         CGIItemType => $CGIitemtype,
266 #                                       CGIFromPublicationYear => $CGIpublicationyear,
267 #                                       CGIToPublicationYear => $CGIpublicationyear,
268 #                                       CGIPublisher => $CGIpublisher,
269                                         CGIBranch => $CGIbranch,
270                                         CGILocation => $CGIlocation,
271                                         CGIextChoice => $CGIextChoice,
272                                         CGIsepChoice => $CGIsepChoice
273                                         );
274
275 }
276 output_html_with_http_headers $input, $cookie, $template->output;
277
278
279
280 sub calculate {
281         my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
282         my @mainloop;
283         my @loopfooter;
284         my @loopcol;
285         my @loopline;
286         my @looprow;
287         my %globalline;
288         my $grantotal =0;
289 # extract parameters
290         my $dbh = C4::Context->dbh;
291
292 # Filters
293 # Checking filters
294 #
295         my @loopfilter;
296         for (my $i=0;$i<=11;$i++) {
297                 my %cell;
298                 if ( @$filters[$i] ) {
299                         if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
300                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
301                         }
302                         $cell{filter} .= @$filters[$i];
303                         $cell{crit} .="Dewey Classification From" if ($i==0);
304                         $cell{crit} .="Dewey Classification To" if ($i==1);
305                         $cell{crit} .="Lccn Classification From" if ($i==2);
306                         $cell{crit} .="Lccn Classification To" if ($i==3);
307                         $cell{crit} .="Cote Classification From" if ($i==4);
308                         $cell{crit} .="Cote Classification To" if ($i==5);
309                         $cell{crit} .="Document type" if ($i==6);
310                         $cell{crit} .="Publisher" if ($i==7);
311                         $cell{crit} .="Publication year From" if ($i==8);
312                         $cell{crit} .="Publication year To" if ($i==9);
313                         $cell{crit} .="Branch :" if ($i==10);
314                         $cell{crit} .="Location:" if ($i==11);
315                         push @loopfilter, \%cell;
316                 }
317         }
318         
319         my @linefilter;
320 #       warn "filtres ".@filters[0];
321 #       warn "filtres ".@filters[1];
322 #       warn "filtres ".@filters[2];
323 #       warn "filtres ".@filters[3];
324         
325         $linefilter[0] = @$filters[0] if ($line =~ /dewey/ )  ;
326         $linefilter[1] = @$filters[1] if ($line =~ /dewey/ )  ;
327         $linefilter[0] = @$filters[2] if ($line =~ /lccn/ )  ;
328         $linefilter[1] = @$filters[3] if ($line =~ /lccn/ )  ;
329         $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ )  ;
330         $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ )  ;
331         @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ )  ;
332         @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
333         $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
334         $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
335         @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
336         @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
337
338         my @colfilter ;
339         $colfilter[0] = @$filters[0] if ($column =~ /dewey/ )  ;
340         $colfilter[1] = @$filters[1] if ($column =~ /dewey/ )  ;
341         $colfilter[0] = @$filters[2] if ($column =~ /lccn/ )  ;
342         $colfilter[1] = @$filters[3] if ($column =~ /lccn/ )  ;
343         $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ )  ;
344         $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ )  ;
345         @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ )  ;
346         @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
347         $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
348         $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
349         @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
350         @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
351
352 # 1st, loop rows.
353         my $linefield;
354         if (($line =~/dewey/)  and ($deweydigits)) {
355                 $linefield .="left($line,$deweydigits)";
356         } elsif (($line=~/lccn/) and ($lccndigits)) {
357                 $linefield .="left($line,$lccndigits)";
358         } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
359                 $linefield .="left($line,$cotedigits)";
360         }else {
361                 $linefield .= $line;
362         }
363         
364         
365         my $strsth;
366         $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
367         if ( @linefilter ) {
368                 if ($linefilter[1]){
369                         $strsth .= " and $line >= ? " ;
370                         $strsth .= " and $line <= ? " ;
371                 } elsif ($linefilter[0]) {
372                         $linefilter[0] =~ s/\*/%/g;
373                         $strsth .= " and $line LIKE ? " ;
374                 }
375         }
376         $strsth .=" order by $linefield";
377         warn "". $strsth;
378         
379         my $sth = $dbh->prepare( $strsth );
380         if (( @linefilter ) and ($linefilter[1])){
381                 $sth->execute($linefilter[0],$linefilter[1]);
382         } elsif ($linefilter[0]) {
383                 $sth->execute($linefilter[0]);
384         } else {
385                 $sth->execute;
386         }
387         while ( my ($celvalue) = $sth->fetchrow) {
388                 my %cell;
389                 if ($celvalue) {
390                         $cell{rowtitle} = $celvalue;
391 #               } else {
392 #                       $cell{rowtitle} = "";
393                 }
394                 $cell{totalrow} = 0;
395                 push @loopline, \%cell;
396         }
397
398 # 2nd, loop cols.
399         my $colfield;
400         if (($column =~/dewey/)  and ($deweydigits)) {
401                 $colfield .="left($column,$deweydigits)";
402         }elsif (($column=~/lccn/) and ($lccndigits)) {
403                 $colfield .="left($column,$lccndigits)";
404         }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
405                 $colfield .="left($column,$cotedigits)";
406         }else {
407                 $colfield .= $column;
408         }
409         
410         my $strsth2;
411         $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
412         if (( @colfilter ) and ($colfilter[1])) {
413                 $strsth2 .= " and $column> ? and $column< ?";
414         }elsif ($colfilter[0]){
415                 $colfilter[0] =~ s/\*/%/g;
416                 $strsth2 .= " and $column LIKE ? ";
417         } 
418         $strsth2 .= " order by $colfield";
419         warn "". $strsth2;
420         my $sth2 = $dbh->prepare( $strsth2 );
421         if ((@colfilter) and ($colfilter[1])) {
422                 $sth2->execute($colfilter[0],$colfilter[1]);
423         } elsif ($colfilter[0]){
424                 $sth2->execute($colfilter[0]);
425         } else {
426                 $sth2->execute;
427         }
428         while (my ($celvalue) = $sth2->fetchrow) {
429                 my %cell;
430                 my %ft;
431                 if ($celvalue) {
432                         $cell{coltitle} = $celvalue;
433 #               } else {
434 #                       $cell{coltitle} = "";
435                 }
436                 $ft{totalcol} = 0;
437                 push @loopcol, \%cell;
438         }
439         
440
441         my $i=0;
442         my @totalcol;
443         my $hilighted=-1;
444         
445         #Initialization of cell values.....
446         my %table;
447 #       warn "init table";
448         foreach my $row ( @loopline ) {
449                 foreach my $col ( @loopcol ) {
450 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
451                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
452                 }
453                 $table{$row->{rowtitle}}->{totalrow}=0;
454         }
455
456 # preparing calculation
457         my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)";
458         my $cond=0;
459         if (@$filters[0]){
460                 @$filters[0]=~ s/\*/%/g;
461                 $strcalc .= " WHERE dewey >" . @$filters[0] ."";
462                 $cond=1; 
463         }
464         if (@$filters[1]){
465                 @$filters[1]=~ s/\*/%/g ;
466                 if ($cond){
467                         $strcalc .= " AND dewey <" . @$filters[1] ."";
468                 } else {
469                         $strcalc .= " WHERE dewey <" . @$filters[1] ."" ;
470                         $cond=1;
471                 }
472                 
473         }
474         if (@$filters[2]){
475                 @$filters[2]=~ s/\*/%/g ;
476                 if ($cond){
477                         $strcalc .= " AND lccn >" . @$filters[2] ."" ;
478                 } else {
479                         $strcalc .= " WHERE lccn > " . @$filters[2] ."" ;
480                         $cond=1;
481                 }
482         }
483         if (@$filters[3]){
484                 @$filters[3]=~ s/\*/%/g;
485                 if ($cond){
486                         $strcalc .= " AND lccn <" . @$filters[3] ."" ;
487                 } else {
488                         $strcalc .= " WHERE lccn <" . @$filters[3] ."" ;
489                         $cond=1;
490                 }
491         }
492         if (@$filters[4]){
493                 @$filters[4]=~ s/\*/%/g ;
494                 if ($cond){
495                         $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" ;
496                 } else {
497                         $strcalc .= " WHERE items.itemcallnumber >" . @$filters[4] ."" ;
498                         $cond=1;
499                 }
500         }
501         
502         if (@$filters[5]){
503                 @$filters[5]=~ s/\*/%/g;
504                 if ($cond){
505                         $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" ;
506                 } else {
507                         $strcalc .= " WHERE items.itemcallnumber <" . @$filters[5] ."" ;
508                         $cond=1;
509                 }
510         }
511         
512         if (@$filters[6]){
513                 @$filters[6]=~ s/\*/%/g;
514                 if ($cond){
515                         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
516                 } else {
517                         $strcalc .= " WHERE biblioitems.itemtype like '" . @$filters[6] ."'";
518                         $cond=1;
519                 }
520         }
521         
522         if (@$filters[7]){
523                 @$filters[7]=~ s/\*/%/g;
524                 @$filters[7].="%" unless @$filters[7]=~/%/;
525                 if ($cond){
526                         $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
527                 } else {
528                         $strcalc .= " WHERE biblioitems.publishercode like \"" . @$filters[7] ."\"";
529                         $cond=1;
530                 }
531         }
532         if (@$filters[8]){
533                 @$filters[8]=~ s/\*/%/g;
534                 if ($cond){
535                         $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
536                 } else {
537                         $strcalc .= " WHERE publicationyear >" . @$filters[8] ."" ;
538                         $cond=1;
539                 }
540         }
541         if (@$filters[9]){
542                 @$filters[9]=~ s/\*/%/g;
543                 if ($cond){
544                         $strcalc .= " AND publicationyear <" . @$filters[9] ."";
545                 } else {
546                         $strcalc .= " WHERE publicationyear <" . @$filters[9] ."";
547                         $cond=1;
548                 }
549         }
550         if (@$filters[10]){
551                 @$filters[10]=~ s/\*/%/g;
552                 if ($cond){
553                         $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
554                 } else {
555                         $strcalc .= " WHERE items.homebranch like '" . @$filters[10] ."'";
556                         $cond=1;
557                 }
558         }
559         if (@$filters[11]){
560                 @$filters[11]=~ s/\*/%/g;
561                 if ($cond){
562                         $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
563                 } else {
564                         $strcalc .= " WHERE items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
565                 }
566         }
567         
568         $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
569         warn "". $strcalc;
570         my $dbcalc = $dbh->prepare($strcalc);
571         $dbcalc->execute;
572 #       warn "filling table";
573         
574         my $emptycol; 
575         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
576 #               warn "filling table $row / $col / $value ";
577                 $emptycol = 1 if ($col eq undef);
578                 $col = "zzEMPTY" if ($col eq undef);
579                 $row = "zzEMPTY" if ($row eq undef);
580                 
581                 $table{$row}->{$col}+=$value;
582                 $table{$row}->{totalrow}+=$value;
583                 $grantotal += $value;
584         }
585
586 #       my %cell = {rowtitle => 'zzROWEMPTY'};
587 #       push @loopline,\%cell;
588 #       undef %cell;
589 #       my %cell;
590 #       %cell = {coltitle => "zzEMPTY"};
591         push @loopcol,{coltitle => "NULL"} if ($emptycol);
592         
593         foreach my $row ( sort keys %table ) {
594                 my @loopcell;
595                 #@loopcol ensures the order for columns is common with column titles
596                 # and the number matches the number of columns
597                 foreach my $col ( @loopcol ) {
598                         my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
599                         push @loopcell, {value => $value  } ;
600                 }
601                 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
602                                                 'loopcell' => \@loopcell,
603                                                 'hilighted' => ($hilighted >0),
604                                                 'totalrow' => $table{$row}->{totalrow}
605                                         };
606                 $hilighted = -$hilighted;
607         }
608         
609 #       warn "footer processing";
610         foreach my $col ( @loopcol ) {
611                 my $total=0;
612                 foreach my $row ( @looprow ) {
613                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
614 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
615                 }
616 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
617                 push @loopfooter, {'totalcol' => $total};
618         }
619                         
620
621         # the header of the table
622         $globalline{loopfilter}=\@loopfilter;
623         # the core of the table
624         $globalline{looprow} = \@looprow;
625         $globalline{loopcol} = \@loopcol;
626 #       # the foot (totals by borrower type)
627         $globalline{loopfooter} = \@loopfooter;
628         $globalline{total}= $grantotal;
629         $globalline{line} = $line;
630         $globalline{column} = $column;
631         push @mainloop,\%globalline;
632         return \@mainloop;
633 }
634
635 1;