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