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