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