More work on the multiple organisations
[koha.git] / reports / acquisitions_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 # test comment
23
24 use strict;
25 use C4::Auth;
26 use CGI;
27 use C4::Context;
28 use HTML::Template;
29 use C4::Search;
30 use C4::Output;
31 use C4::Koha;
32 use C4::Interface::CGI::Output;
33 use C4::Circulation::Circ2;
34
35 =head1 NAME
36
37 plugin that shows a stats on borrowers
38
39 =head1 DESCRIPTION
40
41
42 =over2
43
44 =cut
45
46 my $input = new CGI;
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/acquisitions_stats.tmpl";
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my @filters = $input->param("Filter");
52 my $podsp = $input->param("PlacedOnDisplay");
53 my $rodsp = $input->param("ReceivedOnDisplay");
54 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
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 => {editcatalogue => 1},
67                                 debug => 1,
68                                 });
69 $template->param(do_it => $do_it);
70 if ($do_it) {
71 warn "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
72
73         my $results = calculate($line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters);
74         if ($output eq "screen"){
75                 $template->param(mainloop => $results);
76                 output_html_with_http_headers $input, $cookie, $template->output;
77                 exit(1);
78         } else {
79                 print $input->header(-type => 'application/vnd.sun.xml.calc',
80                                                          -attachment=>"$basename.csv",
81                                                          -name=>"$basename.csv" );
82                 my $cols = @$results[0]->{loopcol};
83                 my $lines = @$results[0]->{looprow};
84                 my $sep;
85                 $sep =C4::Context->preference("delimiter");
86                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87                 foreach my $col ( @$cols ) {
88                         print $col->{coltitle}.$sep;
89                 }
90                 print "Total\n";
91                 foreach my $line ( @$lines ) {
92                         my $x = $line->{loopcell};
93                         print $line->{rowtitle}.$sep;
94                         foreach my $cell (@$x) {
95                                 print $cell->{value}.$sep;
96                         }
97                         print $line->{totalrow};
98                         print "\n";
99                 }
100                 print "TOTAL";
101                 $cols = @$results[0]->{loopfooter};
102                 foreach my $col ( @$cols ) {
103                         print $sep.$col->{totalcol};
104                 }
105                 print $sep.@$results[0]->{total};
106                 exit(1);
107         }
108 } else {
109         my $dbh = C4::Context->dbh;
110         my @values;
111         my %labels;
112         my %select;
113         my $req;
114         $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
115         $req->execute;
116         my @select;
117         push @select,"";
118 #       $select{""}="";
119         while (my ($value, $desc) =$req->fetchrow) {
120                 push @select, $desc;
121 #               $select{$value}=$desc;
122         }
123         my $CGIBookSellers=CGI::scrolling_list( -name     => 'Filter',
124                                 -id => 'Filter',
125                                 -values   => \@select,
126 #                               -labels   => \%select,
127                                 -size     => 1,
128                                 -multiple => 0 );
129
130         $req = $dbh->prepare( "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
131         $req->execute;
132         undef @select;
133         undef %select;
134         push @select,"";
135         $select{""}="";
136         while (my ($value,$desc) =$req->fetchrow) {
137                 push @select, $value;
138                 $select{$value}=$desc;
139         }
140         my $CGIItemTypes=CGI::scrolling_list( -name     => 'Filter',
141                                 -id => 'Filter',
142                                 -values   => \@select,
143                                 -labels    => \%select,
144                                 -size     => 1,
145                                 -multiple => 0 );
146
147         $req = $dbh->prepare( "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname");
148         $req->execute;
149         undef @select;
150         undef %select;
151         push @select,"";
152         $select{""}="";
153         while (my ($value,$desc) =$req->fetchrow) {
154                 push @select, $value;
155                 $select{$value}=$desc;
156         }
157         my $CGIBudget=CGI::scrolling_list( -name     => 'Filter',
158                                 -id => 'Filter',
159                                 -values   => \@select,
160                                 -labels    => \%select,
161                                 -size     => 1,
162                                 -multiple => 0 );
163
164         $req = $dbh->prepare("SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1");
165         $req->execute;
166         undef @select;
167         push @select,"";
168         my $hassort1;
169         while (my ($value) =$req->fetchrow) {
170                 $hassort1 =1 if ($value);
171                 push @select, $value;
172         }
173         my $CGISort1=CGI::scrolling_list( -name     => 'Filter',
174                                 -id => 'Filter',
175                                 -values   => \@select,
176                                 -size     => 1,
177                                 -multiple => 0 );
178
179         $req = $dbh->prepare("SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2");
180         $req->execute;
181         undef @select;
182         push @select,"";
183         my $hassort2;
184         my $hglghtsort2;
185         while (my ($value) =$req->fetchrow) {
186                 $hassort2 =1 if ($value);
187                 $hglghtsort2= !($hassort1);
188                 push @select, $value;
189         }
190         my $CGISort2=CGI::scrolling_list( -name     => 'Filter',
191                                 -id => 'Filter',
192                                 -values   => \@select,
193                                 -size     => 1,
194                                 -multiple => 0 );
195
196         my @mime = ( C4::Context->preference("MIME") );
197         foreach my $mime (@mime){
198 #               warn "".$mime;
199         }
200
201         my $CGIextChoice=CGI::scrolling_list(
202                                 -name     => 'MIME',
203                                 -id       => 'MIME',
204                                 -values   => \@mime,
205                                 -size     => 1,
206                                 -multiple => 0 );
207
208         my @dels = ( C4::Context->preference("delimiter") );
209         my $CGIsepChoice=CGI::scrolling_list(
210                                 -name     => 'sep',
211                                 -id       => 'sep',
212                                 -values   => \@dels,
213                                 -size     => 1,
214                                 -multiple => 0 );
215
216         $template->param(
217                                         CGIBookSeller => $CGIBookSellers,
218                                         CGIItemType => $CGIItemTypes,
219                                         CGIBudget => $CGIBudget,
220                                         hassort1=> $hassort1,
221                                         hassort2=> $hassort2,
222                                         HlghtSort2 => $hglghtsort2,
223                                         CGISort1 => $CGISort1,
224                                         CGISort2 => $CGISort2,
225                                         CGIextChoice => $CGIextChoice,
226                                         CGIsepChoice => $CGIsepChoice
227                                         );
228
229 }
230 output_html_with_http_headers $input, $cookie, $template->output;
231
232
233
234 sub calculate {
235         my ($line, $column, $podsp, $rodsp, $aodsp, $process, $filters) = @_;
236         my @mainloop;
237         my @loopfooter;
238         my @loopcol;
239         my @loopline;
240         my @looprow;
241         my %globalline;
242         my $grantotal =0;
243 # extract parameters
244         my $dbh = C4::Context->dbh;
245
246 # Filters
247 # Checking filters
248 #
249         my @loopfilter;
250         for (my $i=0;$i<=8;$i++) {
251                 my %cell;
252                 if ( @$filters[$i] ) {
253                         if ((($i==1) or ($i==3)) and (@$filters[$i-1])) {
254                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
255                         }
256                         $cell{filter} .= @$filters[$i];
257                         $cell{crit} .="Placed On From" if ($i==0);
258                         $cell{crit} .="Placed On To" if ($i==1);
259                         $cell{crit} .="Received On From" if ($i==2);
260                         $cell{crit} .="Received On To" if ($i==3);
261
262                         $cell{crit} .="Acquired On From" if ($i==4);
263                         $cell{crit} .="Acquired On To" if ($i==5);
264
265                         $cell{crit} .="BookSeller" if ($i==6);
266                         $cell{crit} .="Doc Type" if ($i==7);
267                         $cell{crit} .="Budget" if ($i==8);
268                         $cell{crit} .="Sort1" if ($i==9);
269                         $cell{crit} .="Sort2" if ($i==10);
270                         push @loopfilter, \%cell;
271                 }
272         }
273
274         my @linefilter;
275 #       warn "filtres ".@filters[0];
276 #       warn "filtres ".@filters[1];
277 #       warn "filtres ".@filters[2];
278 #       warn "filtres ".@filters[3];
279
280         $linefilter[0] = @$filters[0] if ($line =~ /closedate/ )  ;
281         $linefilter[1] = @$filters[1] if ($line =~ /closedate/ )  ;
282         $linefilter[0] = @$filters[2] if ($line =~ /received/ )  ;
283         $linefilter[1] = @$filters[3] if ($line =~ /received/ )  ;
284
285         $linefilter[0] = @$filters[4] if ($line =~ /acquired/ )  ;
286         $linefilter[1] = @$filters[5] if ($line =~ /acquired/ )  ;
287
288         $linefilter[0] = @$filters[6] if ($line =~ /bookseller/ )  ;
289         $linefilter[0] = @$filters[7] if ($line =~ /itemtype/ )  ;
290         $linefilter[0] = @$filters[8] if ($line =~ /bookfund/ )  ;
291         $linefilter[0] = @$filters[9] if ($line =~ /sort1/ )  ;
292         $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
293 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
294 #
295         my @colfilter ;
296         $colfilter[0] = @$filters[0] if ($column =~ /closedate/ ) ;
297         $colfilter[1] = @$filters[1] if ($column =~ /closedate/ ) ;
298         $colfilter[0] = @$filters[2] if ($column =~ /received/ )  ;
299         $colfilter[1] = @$filters[3] if ($column =~ /received/ )  ;
300
301         $colfilter[0] = @$filters[4] if ($column =~ /acquired/ )  ;
302         $colfilter[1] = @$filters[5] if ($column =~ /acquired/ )  ;
303
304         $colfilter[0] = @$filters[6] if ($column =~ /bookseller/ );
305         $colfilter[0] = @$filters[7] if ($column =~ /itemtype/ )  ;
306         $colfilter[0] = @$filters[8] if ($column =~ /bookfund/ )  ;
307         $colfilter[0] = @$filters[9] if ($column =~ /sort1/ )     ;
308         $colfilter[0] = @$filters[10] if ($column =~ /sort2/ )     ;
309 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
310                                               
311 # 1st, loop rows.                             
312         my $linefield;
313         my $lineorder;                               
314         if (($line =~/closedate/) and ($podsp == 1)) {
315                 #Display by day
316                 $linefield .="dayname($line)";  
317                 $lineorder .="weekday($line)";  
318         } elsif (($line=~/closedate/) and ($podsp == 2)) {
319                 #Display by Month
320                 $linefield .="monthname($line)";  
321                 $lineorder .="month($line)";  
322         } elsif (($line=~/closedate/) and ($podsp == 3)) {
323                 #Display by Year
324                 $linefield .="Year($line)";
325                 $lineorder .=$linefield;  
326         } elsif (($line =~/received/) and ($rodsp == 1)) {
327                 #Display by day
328                 $linefield .="dayname($line)";  
329                 $lineorder .="weekday($line)";  
330         } elsif (($line=~/received/) and ($rodsp == 2)) {
331                 #Display by Month
332                 $linefield .="monthname($line)";  
333                 $lineorder .="month($line)";  
334         } elsif (($line=~/received/) and ($rodsp == 3)) {
335                 #Display by Year
336                 $linefield .="Year($line)";
337                 $lineorder .=$linefield;  
338         } else {
339                 $linefield .= $line;
340                 $lineorder .=$linefield;  
341         }  
342         
343         my $strsth;
344         $strsth .= "select distinctrow $linefield from aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) and $line is not null ";
345         
346         if ( @linefilter ) {
347                 if ($linefilter[1]){
348                         if ($linefilter[0]){
349                                 $strsth .= " and $line between ? and ? " ;
350                         } else {
351                                 $strsth .= " and $line < ? " ;
352                         }
353                 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/))){
354                         $strsth .= " and $line > ? " ;
355                 } elsif ($linefilter[0]) {
356                         $linefilter[0] =~ s/\*/%/g;
357                         $strsth .= " and $line LIKE ? " ;
358                 }
359         }
360         $strsth .=" group by $linefield";
361         $strsth .=" order by $lineorder";
362         warn "rows: ". $strsth;
363         
364         my $sth = $dbh->prepare( $strsth );
365         if (( @linefilter ) and ($linefilter[1])){
366                 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
367         } elsif ($linefilter[0]) {
368                 $sth->execute($linefilter[0]);
369         } else {
370                 $sth->execute;
371         }
372         
373         while ( my ($celvalue) = $sth->fetchrow) {
374                 my %cell;
375                 if ($celvalue) {
376                         $cell{rowtitle} = $celvalue;
377 #               } else {
378 #                       $cell{rowtitle} = "";
379                 }
380                 $cell{totalrow} = 0;
381                 push @loopline, \%cell;
382         }
383
384 warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
385
386 # 1st, loop rows.
387         my $linefield;
388         if (($line =~/closedate/) and ($podsp == 1)) {
389                 #Display by day
390                 $linefield .="dayname($line)";
391         } elsif (($line=~/closedate/) and ($podsp == 2)) {
392                 #Display by Month
393                 $linefield .="monthname($line)";
394         } elsif (($line=~/closedate/) and ($podsp == 3)) {
395                 #Display by Year
396                 $linefield .="Year($line)";
397
398         } elsif (($line =~/received/) and ($rodsp == 1)) {
399                 #Display by day
400                 $linefield .="dayname($line)";
401         } elsif (($line=~/received/) and ($rodsp == 2)) {
402                 #Display by Month
403                 $linefield .="monthname($line)";
404         } elsif (($line=~/received/) and ($rodsp == 3)) {
405                 #Display by Year
406                 $linefield .="Year($line)";
407
408         } elsif (($line =~/acquired/) and ($aodsp == 1)) {
409                 #Display by day
410                 $linefield .="dayname($line)";
411         } elsif (($line=~/acquired/) and ($aodsp == 2)) {
412                 #Display by Month
413                 $linefield .="monthname($line)";
414         } elsif (($line=~/acquired/) and ($aodsp == 3)) {
415                 #Display by Year
416                 $linefield .="Year($line)";
417
418
419         } else {
420                 $linefield .= $line;
421         }
422
423         my $strsth;
424         $strsth .= "SELECT DISTINCTROW $linefield FROM aqorders, aqbasket,aqorderbreakdown
425                 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
426                 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
427                 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
428                 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
429                 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
430
431         if ( @linefilter ) {
432                 if ($linefilter[1]){
433                         if ($linefilter[0]){
434                                 $strsth .= " AND $line BETWEEN ? AND ? " ;
435                         } else {
436                                 $strsth .= " AND $line < ? " ;
437                         }
438                 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/) or ($line=~/acquired/))){
439                         $strsth .= " AND $line > ? " ;
440                 } elsif ($linefilter[0]) {
441                         $linefilter[0] =~ s/\*/%/g;
442                         $strsth .= " AND $line LIKE ? " ;
443                 }
444         }
445         $strsth .=" GROUP BY $linefield";
446         $strsth .=" ORDER BY $linefield";
447         warn "377:strsth= $strsth";
448
449         my $sth = $dbh->prepare( $strsth );
450         if (( @linefilter ) and ($linefilter[1])){
451                 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
452         } elsif ($linefilter[0]) {
453                 $sth->execute($linefilter[0]);
454         } else {
455                 $sth->execute;
456         }
457
458         while ( my ($celvalue) = $sth->fetchrow) {
459                 my %cell;
460                 if ($celvalue) {
461                         $cell{rowtitle} = $celvalue;
462 #               } else {
463 #                       $cell{rowtitle} = "";
464                 }
465                 $cell{totalrow} = 0;
466                 push @loopline, \%cell;
467         }
468
469 warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
470 # 2nd, loop cols.
471         my $colfield;
472         my $colorder;
473         if (($column =~/closedate/) and ($podsp == 1)) {
474                 #Display by day
475                 $colfield .="dayname($column)";
476                 $colorder .="weekday($column)";
477         } elsif (($column=~/closedate/) and ($podsp == 2)) {
478                 #Display by Month
479                 $colfield .="monthname($column)";  
480                 $colorder .="month($column)";
481         } elsif (($column=~/closedate/) and ($podsp == 3)) {
482                 #Display by Year
483                 $colfield .="Year($column)";
484                 $colorder .= $colfield;
485         } elsif (($column =~/received/) and ($rodsp == 1)) {
486                 #Display by day
487                 $colfield .="dayname($column)";  
488                 $colorder .="weekday($column)";
489         } elsif (($column=~/received/) and ($rodsp == 2)) {
490                 #Display by Month
491                 $colfield .="monthname($column)";  
492                 $colorder .="month($column)";
493         } elsif (($column=~/received/) and ($rodsp == 3)) {
494                 #Display by Year
495                 $colfield .="Year($column)";
496                 $colorder .= $colfield;
497         } else {
498                 $colfield .= $column;
499                 $colorder .= $colfield;
500         }  
501         
502         my $strsth2;
503         $strsth2 .= "select distinctrow $colfield from aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) and $column is not null ";
504         
505         if ( @colfilter ) {
506                 if ($colfilter[1]){
507                         if ($colfilter[0]){
508                                 $strsth2 .= " and $column between ? and ? " ;
509                         } else {
510                                 $strsth2 .= " and $column < ? " ;
511                         }
512                 } elsif (($colfilter[0]) and (($column=~/closedate/) or ($column=~/received/))){
513                         $strsth2 .= " and $column > ? " ;
514                 } elsif ($colfilter[0]) {
515                         $colfilter[0] =~ s/\*/%/g;
516                         $strsth2 .= " and $column LIKE ? " ;
517                 }
518         }
519         $strsth2 .=" group by $colfield";
520         $strsth2 .=" order by $colorder";
521         warn "columns : ". $strsth2;
522         
523         my $sth2 = $dbh->prepare( $strsth2 );
524         if (( @colfilter ) and ($colfilter[1])){
525                 warn "from : ".$colfilter[0]." To  :".$colfilter[1];
526                 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
527         } elsif ($colfilter[0]) {
528                 $sth2->execute($colfilter[0]);
529         } else {
530                 $sth2->execute;
531         }
532
533         while (my ($celvalue) = $sth2->fetchrow) {
534                 my %cell;
535                 if ($celvalue){
536 #               warn "coltitle :".$celvalue;
537                         $cell{coltitle} = $celvalue;
538                 }
539                 push @loopcol, \%cell;
540         }
541 #       warn "fin des titres colonnes";
542
543         my $i=0;
544         my @totalcol;
545         my $hilighted=-1;
546         
547         #Initialization of cell values.....
548         my %table;
549 #       warn "init table";
550         foreach my $row ( @loopline ) {
551                 foreach my $col ( @loopcol ) {
552 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
553                         $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
554                 }
555                 $table{$row->{rowtitle}}->{totalrow}=0;
556         }
557
558 # preparing calculation
559         my $strcalc ;
560         $strcalc .= "SELECT $linefield, $colfield, ";
561         $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
562         $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
563         $strcalc .= "FROM aqorders, aqbasket,aqorderbreakdown left join aqorderdelivery on (aqorders.ordernumber =aqorderdelivery.ordernumber ) left join aqbooksellers on (aqbasket.booksellerid=aqbooksellers.id) where (aqorders.basketno=aqbasket.basketno) and (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
564
565         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
566         $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
567         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
568         $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] ."'" if ( @$filters[1] );
569         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
570         $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] ."'" if ( @$filters[2] );
571         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
572         $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] ."'" if ( @$filters[3] );
573         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
574         $strcalc .= " AND aqbooksellers.name like '" . @$filters[4] ."'" if ( @$filters[4] );
575         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
576         $strcalc .= " AND aqorderbreakdown.bookfundid like '" . @$filters[5] ."'" if ( @$filters[5] );
577         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
578         $strcalc .= " AND aqorders.sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
579         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
580         $strcalc .= " AND aqorders.sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
581         $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
582         warn "". $strcalc;
583         my $dbcalc = $dbh->prepare($strcalc);
584         $dbcalc->execute;
585
586 #       warn "filling table";
587         my $emptycol; 
588         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
589 #               warn "filling table $row / $col / $value ";
590                 $emptycol = 1 if ($col eq undef);
591                 $col = "zzEMPTY" if ($col eq undef);
592                 $row = "zzEMPTY" if ($row eq undef);
593                 
594                 $table{$row}->{$col}+=$value;
595                 $table{$row}->{totalrow}+=$value;
596                 $grantotal += $value;
597         }
598
599         push @loopcol,{coltitle => "NULL"} if ($emptycol);
600         
601         foreach my $row (@loopline) {
602                 my @loopcell;
603                 #@loopcol ensures the order for columns is common with column titles
604                 # and the number matches the number of columns
605                 foreach my $col ( @loopcol ) {
606                         my $value =$table{($row->{'rowtitle'} eq "NULL")?"zzEMPTY":$row->{'rowtitle'}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
607                         push @loopcell, {value => $value  } ;
608                 }
609                 push @looprow,{ 'rowtitle' => $row->{'rowtitle'},
610                                                 'loopcell' => \@loopcell,
611                                                 'hilighted' => ($hilighted >0),
612                                                 'totalrow' => $table{($row->{'rowtitle'} eq "NULL")?"zzEMPTY":$row->{'rowtitle'}}->{totalrow}
613                                         };
614                 $hilighted = -$hilighted;
615         }
616         
617 #       warn "footer processing";
618         foreach my $col ( @loopcol ) {
619                 my $total=0;
620                 foreach my $row ( @looprow ) {
621                         $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
622 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
623                 }
624 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
625                 push @loopfooter, {'totalcol' => $total};
626         }
627                         
628
629         # the header of the table
630         $globalline{loopfilter}=\@loopfilter;
631         # the core of the table
632         $globalline{looprow} = \@looprow;
633         $globalline{loopcol} = \@loopcol;
634 #       # the foot (totals by borrower type)
635         $globalline{loopfooter} = \@loopfooter;
636         $globalline{total}= $grantotal;
637         $globalline{line} = $line;
638         $globalline{column} = $column;
639         push @mainloop,\%globalline;
640         return \@mainloop;
641 }
642
643 1;
644