Cleaned up regex's looking for defunct CVS files
[koha.git] / reports / acquisitions_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 # test comment
22
23 use strict;
24 use C4::Auth;
25 use CGI;
26 use C4::Context;
27
28 use C4::Output;
29 use C4::Koha;
30 use C4::Circulation;
31 use C4::Date;
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 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 $filters[2]=format_date_in_iso($filters[2]);
52 $filters[3]=format_date_in_iso($filters[3]);
53 my $podsp          = $input->param("PlacedOnDisplay");
54 my $rodsp          = $input->param("ReceivedOnDisplay");
55 my $aodsp          = $input->param("AcquiredOnDisplay");    ##added by mason.
56 my $calc           = $input->param("Cellvalue");
57 my $output         = $input->param("output");
58 my $basename       = $input->param("basename");
59 my $mime           = $input->param("MIME");
60 my $del            = $input->param("sep");
61
62 #warn "calcul : ".$calc;
63 my ($template, $borrowernumber, $cookie)
64         = get_template_and_user({template_name => $fullreportname,
65                                 query => $input,
66                                 type => "intranet",
67                                 authnotrequired => 0,
68                                 flagsrequired => {reports => 1},
69                                 debug => 1,
70                                 });
71 $template->param(do_it => $do_it,
72         DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
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     => 'supplier',
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       => 'itemtypes',
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       => 'budget',
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       => 'sort1',
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       => 'sort2',
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             # format the dates filters, otherwise just fill as is
300             if ($i>=4) {
301                 $cell{filter} .= @$filters[$i];
302             } else {
303                 $cell{filter} .= format_date(@$filters[$i]);
304             }
305             $cell{crit}   .= "Placed On From" if ( $i == 0 );
306             $cell{crit}   .= "Placed On To" if ( $i == 1 );
307             $cell{crit}   .= "Received On From" if ( $i == 2 );
308             $cell{crit}   .= "Received On To" if ( $i == 3 );
309
310             $cell{crit} .= "Acquired On From" if ( $i == 4 );
311             $cell{crit} .= "Acquired On To"   if ( $i == 5 );
312
313             $cell{crit} .= "BookSeller" if ( $i == 6 );
314             $cell{crit} .= "Doc Type"   if ( $i == 7 );
315             $cell{crit} .= "Budget"     if ( $i == 8 );
316             $cell{crit} .= "Sort1"      if ( $i == 9 );
317             $cell{crit} .= "Sort2"      if ( $i == 10 );
318             push @loopfilter, \%cell;
319         }
320     }
321
322     my @linefilter;
323
324     #       warn "filtres ".@filters[0];
325     #       warn "filtres ".@filters[1];
326     #       warn "filtres ".@filters[2];
327     #       warn "filtres ".@filters[3];
328
329     $linefilter[0] = @$filters[0] if ( $line =~ /closedate/ );
330     $linefilter[1] = @$filters[1] if ( $line =~ /closedate/ );
331     $linefilter[0] = @$filters[2] if ( $line =~ /received/ );
332     $linefilter[1] = @$filters[3] if ( $line =~ /received/ );
333
334     $linefilter[0] = @$filters[4] if ( $line =~ /acquired/ );
335     $linefilter[1] = @$filters[5] if ( $line =~ /acquired/ );
336
337     $linefilter[0] = @$filters[6]  if ( $line =~ /bookseller/ );
338     $linefilter[0] = @$filters[7]  if ( $line =~ /itemtype/ );
339     $linefilter[0] = @$filters[8]  if ( $line =~ /bookfund/ );
340     $linefilter[0] = @$filters[9]  if ( $line =~ /sort1/ );
341     $linefilter[0] = @$filters[10] if ( $line =~ /sort2/ );
342
343     #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
344     #
345     my @colfilter;
346     $colfilter[0] = @$filters[0] if ( $column =~ /closedate/ );
347     $colfilter[1] = @$filters[1] if ( $column =~ /closedate/ );
348     $colfilter[0] = @$filters[2] if ( $column =~ /received/ );
349     $colfilter[1] = @$filters[3] if ( $column =~ /received/ );
350
351     $colfilter[0] = @$filters[4] if ( $column =~ /acquired/ );
352     $colfilter[1] = @$filters[5] if ( $column =~ /acquired/ );
353
354     $colfilter[0] = @$filters[6]  if ( $column =~ /bookseller/ );
355     $colfilter[0] = @$filters[7]  if ( $column =~ /itemtype/ );
356     $colfilter[0] = @$filters[8]  if ( $column =~ /bookfund/ );
357     $colfilter[0] = @$filters[9]  if ( $column =~ /sort1/ );
358     $colfilter[0] = @$filters[10] if ( $column =~ /sort2/ );
359
360     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
361
362     #warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
363
364     # 1st, loop rows.
365     my $linefield;
366     if ( ( $line =~ /closedate/ ) and ( $podsp == 1 ) ) {
367
368         #Display by day
369         $linefield .= "dayname($line)";
370     }
371     elsif ( ( $line =~ /closedate/ ) and ( $podsp == 2 ) ) {
372
373         #Display by Month
374         $linefield .= "monthname($line)";
375     }
376     elsif ( ( $line =~ /closedate/ ) and ( $podsp == 3 ) ) {
377
378         #Display by Year
379         $linefield .= "Year($line)";
380
381     }
382     elsif ( ( $line =~ /received/ ) and ( $rodsp == 1 ) ) {
383
384         #Display by day
385         $linefield .= "dayname($line)";
386     }
387     elsif ( ( $line =~ /received/ ) and ( $rodsp == 2 ) ) {
388
389         #Display by Month
390         $linefield .= "monthname($line)";
391     }
392     elsif ( ( $line =~ /received/ ) and ( $rodsp == 3 ) ) {
393
394         #Display by Year
395         $linefield .= "Year($line)";
396
397     }
398     elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 1 ) ) {
399
400         #Display by day
401         $linefield .= "dayname($line)";
402     }
403     elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 2 ) ) {
404
405         #Display by Month
406         $linefield .= "monthname($line)";
407     }
408     elsif ( ( $line =~ /acquired/ ) and ( $aodsp == 3 ) ) {
409
410         #Display by Year
411         $linefield .= "Year($line)";
412
413     }
414     else {
415         $linefield .= $line;
416     }
417
418     my $strsth;
419     $strsth .=
420       "SELECT DISTINCTROW $linefield FROM (aqorders, aqbasket,aqorderbreakdown)
421                 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
422                 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
423                 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
424                 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
425                 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
426
427     if (@linefilter) {
428         if ( $linefilter[1] ) {
429             if ( $linefilter[0] ) {
430                 $strsth .= " AND $line BETWEEN ? AND ? ";
431             }
432             else {
433                 $strsth .= " AND $line < ? ";
434             }
435         }
436         elsif (
437             ( $linefilter[0] )
438             and (  ( $line =~ /closedate/ )
439                 or ( $line =~ /received/ )
440                 or ( $line =~ /acquired/ ) )
441           )
442         {
443             $strsth .= " AND $line > ? ";
444         }
445         elsif ( $linefilter[0] ) {
446             $linefilter[0] =~ s/\*/%/g;
447             $strsth .= " AND $line LIKE ? ";
448         }
449     }
450     $strsth .= " GROUP BY $linefield";
451     $strsth .= " ORDER BY $linefield";
452
453     #warn "377:strsth= $strsth";
454
455     my $sth = $dbh->prepare($strsth);
456     if ( (@linefilter) and ( $linefilter[1] ) ) {
457         $sth->execute( "'" . $linefilter[0] . "'", "'" . $linefilter[1] . "'" );
458     }
459     elsif ( $linefilter[0] ) {
460         $sth->execute( $linefilter[0] );
461     }
462     else {
463         $sth->execute;
464     }
465
466     while ( my ($celvalue) = $sth->fetchrow ) {
467         my %cell;
468         if ($celvalue) {
469             $cell{rowtitle} = $celvalue;
470
471             #               } else {
472             #                       $cell{rowtitle} = "";
473         }
474         $cell{totalrow} = 0;
475         push @loopline, \%cell;
476     }
477
478     #warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
479
480     # 2nd, loop cols.
481     my $colfield;
482     if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
483
484         #Display by day
485         $colfield .= "dayname($column)";
486     }
487     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
488
489         #Display by Month
490         $colfield .= "monthname($column)";
491     }
492     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
493
494         #Display by Year
495         $colfield .= "Year($column)";
496
497     }
498     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
499
500         #Display by day
501         $colfield .= "dayname($column)";
502     }
503     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
504
505         #Display by Month
506         $colfield .= "monthname($column)";
507     }
508     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
509
510         #Display by Year
511         $colfield .= "Year($column)";
512
513     }
514     elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
515
516         #Display by day
517         $colfield .= "dayname($column)";
518     }
519     elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
520
521         #Display by Month
522         $colfield .= "monthname($column)";
523     }
524     elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
525
526         #Display by Year
527         $colfield .= "Year($column)";
528
529     }
530     else {
531         $colfield .= $column;
532     }
533
534     my $strsth2;
535     $strsth2 .=
536       "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
537                  LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
538                  LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
539                  LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
540                  LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
541                  WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
542                  AND $column IS NOT NULL";
543
544     if (@colfilter) {
545         if ( $colfilter[1] ) {
546             if ( $colfilter[0] ) {
547                 $strsth2 .= " AND $column BETWEEN  ? AND ? ";
548             }
549             else {
550                 $strsth2 .= " AND $column < ? ";
551             }
552         }
553         elsif (
554             ( $colfilter[0] )
555             and (  ( $column =~ /closedate/ )
556                 or ( $line =~ /received/ )
557                 or ( $line =~ /acquired/ ) )
558           )
559         {
560             $strsth2 .= " AND $column > ? ";
561         }
562         elsif ( $colfilter[0] ) {
563             $colfilter[0] =~ s/\*/%/g;
564             $strsth2 .= " AND $column LIKE ? ";
565         }
566     }
567     $strsth2 .= " GROUP BY $colfield";
568     $strsth2 .= " ORDER BY $colfield";
569
570     #        warn "MASON:. $strsth2";
571
572     my $sth2 = $dbh->prepare($strsth2);
573     if ( (@colfilter) and ( $colfilter[1] ) ) {
574
575         #                warn "from : ".$colfilter[0]." To  :".$colfilter[1];
576         $sth2->execute( "'" . $colfilter[0] . "'", "'" . $colfilter[1] . "'" );
577     }
578     elsif ( $colfilter[0] ) {
579         $sth2->execute( $colfilter[0] );
580     }
581     else {
582         $sth2->execute;
583     }
584
585     while ( my ($celvalue) = $sth2->fetchrow ) {
586         my %cell;
587         if ($celvalue) {
588
589             #               warn "coltitle :".$celvalue;
590             $cell{coltitle} = $celvalue;
591         }
592         push @loopcol, \%cell;
593     }
594
595     #       warn "fin des titres colonnes";
596
597     my $i = 0;
598     my @totalcol;
599     my $hilighted = -1;
600
601     #Initialization of cell values.....
602     my %table;
603
604     #       warn "init table";
605     foreach my $row (@loopline) {
606         foreach my $col (@loopcol) {
607
608 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
609             $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
610         }
611         $table{ $row->{rowtitle} }->{totalrow} = 0;
612     }
613
614     # preparing calculation
615     my $strcalc;
616     $strcalc .= "SELECT $linefield, $colfield, ";
617     $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
618     $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
619       if ( $process == 2 );
620     $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
621                  LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
622                  LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
623                  LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
624                  LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
625                       AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
626
627     @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
628     $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] . "'"
629       if ( @$filters[0] );
630     @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
631     $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] . "'"
632       if ( @$filters[1] );
633     @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
634     $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] . "'"
635       if ( @$filters[2] );
636     @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
637     $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] . "'"
638       if ( @$filters[3] );
639     @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
640     $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] . "'"
641       if ( @$filters[4] );
642     @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
643     $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] . "'"
644       if ( @$filters[5] );
645     @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
646     $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] . "'"
647       if ( @$filters[6] );
648     @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
649     $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] . "'"
650       if ( @$filters[7] );
651     @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
652     $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] . "'"
653       if ( @$filters[8] );
654     @$filters[9] =~ s/\*/%/g if ( @$filters[9] );
655     $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] . "'"
656       if ( @$filters[9] );
657     @$filters[10] =~ s/\*/%/g if ( @$filters[10] );
658     $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] . "'"
659       if ( @$filters[10] );
660     $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
661
662     #        warn "/n/n". $strcalc;
663     my $dbcalc = $dbh->prepare($strcalc);
664     $dbcalc->execute;
665
666     #       warn "filling table";
667     my $emptycol;
668     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
669
670         #              warn "filling table $row / $col / $value ";
671         $emptycol = 1         if ( $col eq undef );
672         $col      = "zzEMPTY" if ( $col eq undef );
673         $row      = "zzEMPTY" if ( $row eq undef );
674
675         $table{$row}->{$col}     += $value;
676         $table{$row}->{totalrow} += $value;
677         $grantotal               += $value;
678     }
679
680     push @loopcol, { coltitle => "NULL" } if ($emptycol);
681
682     foreach my $row ( sort keys %table ) {
683         my @loopcell;
684
685         #@loopcol ensures the order for columns is common with column titles
686         # and the number matches the number of columns
687         foreach my $col (@loopcol) {
688             my $value = $table{$row}->{
689                 ( $col->{coltitle} eq "NULL" )
690                 ? "zzEMPTY"
691                 : $col->{coltitle}
692               };
693             push @loopcell, { value => $value };
694         }
695         push @looprow,
696           {
697             'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
698             'loopcell'  => \@loopcell,
699             'hilighted' => ( $hilighted > 0 ),
700             'totalrow'  => $table{$row}->{totalrow}
701           };
702         $hilighted = -$hilighted;
703     }
704
705     #       warn "footer processing";
706     foreach my $col (@loopcol) {
707         my $total = 0;
708         foreach my $row (@looprow) {
709             $total += $table{
710                 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
711                 : $row->{rowtitle}
712               }->{
713                 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
714                 : $col->{coltitle}
715               };
716
717 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
718         }
719
720         #               warn "summ for column ".$col->{coltitle}."  = ".$total;
721         push @loopfooter, { 'totalcol' => $total };
722     }
723
724     # the header of the table
725     #        $globalline{loopfilter}=\@loopfilter;
726     # the core of the table
727     $globalline{looprow} = \@looprow;
728     $globalline{loopcol} = \@loopcol;
729
730     #       # the foot (totals by borrower type)
731     $globalline{loopfooter} = \@loopfooter;
732     $globalline{total}      = $grantotal;
733     $globalline{line}       = $line;
734     $globalline{column}     = $column;
735     push @mainloop, \%globalline;
736     return \@mainloop;
737 }
738
739 1;
740