Bug 2505 - Add commented use warnings where missing in the reports/ directory
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 # test comment
22
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use C4::Auth;
26 use CGI;
27 use C4::Context;
28 use C4::Reports;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Circulation;
32 use C4::Dates qw/format_date format_date_in_iso/;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40 =over 2
41
42 =cut
43
44 my $input          = new CGI;
45 my $do_it          = $input->param('do_it');
46 my $fullreportname = "reports/acquisitions_stats.tmpl";
47 my $line           = $input->param("Line");
48 my $column         = $input->param("Column");
49 my @filters        = $input->param("Filter");
50 $filters[0]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[0]) : undef);
51 $filters[1]= (($line =~ /closedate/ || $column =~ /closedate/) ? format_date_in_iso($filters[1]) : undef);
52 $filters[2]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[2]) : undef);
53 $filters[3]= (($line =~ /delivery/ || $column =~ /delivery/) ? format_date_in_iso($filters[3]) : undef);
54 my $podsp          = $input->param("PlacedOnDisplay");
55 my $rodsp          = $input->param("ReceivedOnDisplay");
56 my $aodsp          = $input->param("AcquiredOnDisplay");    ##added by mason.
57 my $calc           = $input->param("Cellvalue");
58 my $output         = $input->param("output");
59 my $basename       = $input->param("basename");
60 my $mime           = $input->param("MIME");
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 => '*'},
69                                 debug => 1,
70                                 });
71 our $sep     = $input->param("sep");
72 $sep = "\t" if ($sep eq 'tabulation');
73 $template->param(do_it => $do_it,
74         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
75                 );
76 if ($do_it) {
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 budget_code, budget_name FROM aqbudgets ORDER BY budget_name");
158     $req->execute;
159     undef @select;
160     undef %select;
161     push @select, "";
162     $select{''} = "All budgets";
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 =~ /budget/ );
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 =~ /budget/ );
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 )
408                 LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
409                 LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
410                 LEFT JOIN aqbudgets  ON (aqorders.budget_id = aqbudgets.budget_id )
411
412                 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
413                 AND $line IS NOT NULL AND $line <> '' ";
414
415 #                               LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
416     
417         if (@linefilter) {
418         if ( $linefilter[1] ) {
419             if ( $linefilter[0] ) {
420                 $strsth .= " AND $line BETWEEN ? AND ? ";
421             }
422             else {
423                 $strsth .= " AND $line <= ? ";
424             }
425         }
426         elsif (
427             ( $linefilter[0] )
428             and (  ( $line =~ /closedate/ )
429                 or ( $line =~ /received/ ))
430 #                or ( $line =~ /acquired/ ) )
431           )
432         {
433             $strsth .= " AND $line >= ? ";
434         }
435         elsif ( $linefilter[0] ) {
436             $linefilter[0] =~ s/\*/%/g;
437             $strsth .= " AND $line LIKE ? ";
438         }
439     }
440     $strsth .= " GROUP BY $linefield";
441     $strsth .= " ORDER BY $linefield";
442
443     #warn "377:strsth= $strsth";
444
445     my $sth = $dbh->prepare($strsth);
446     if ( (@linefilter) and ( $linefilter[1] ) ) {
447         $sth->execute( $linefilter[0], $linefilter[1] );
448     }
449     elsif ( $linefilter[0] ) {
450         $sth->execute( $linefilter[0] );
451     }
452     else {
453         $sth->execute;
454     }
455         while ( my ($celvalue) = $sth->fetchrow ) {
456                 my %cell;
457                 if ($celvalue) {
458                         $cell{rowtitle} = $celvalue;
459                         push @loopline, \%cell;
460                 }
461                 $cell{totalrow} = 0;
462         }
463 #    warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
464
465     # 2nd, loop cols.
466     my $colfield;
467     if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
468
469         #Display by day
470         $colfield .= "dayname($column)";
471     }
472     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
473
474         #Display by Month
475         $colfield .= "monthname($column)";
476     }
477     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
478
479         #Display by Year
480         $colfield .= "Year($column)";
481
482     }
483     elsif ( ( $column =~ /received/ ) and ( $rodsp == 1 ) ) {
484
485         #Display by day
486         $colfield .= "dayname($column)";
487     }
488     elsif ( ( $column =~ /received/ ) and ( $rodsp == 2 ) ) {
489
490         #Display by Month
491         $colfield .= "monthname($column)";
492     }
493     elsif ( ( $column =~ /received/ ) and ( $rodsp == 3 ) ) {
494
495         #Display by Year
496         $colfield .= "Year($column)";
497
498     }
499 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
500 #
501 #        #Display by day
502 #        $colfield .= "dayname($column)";
503 #    }
504 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
505 #
506 #        #Display by Month
507 #        $colfield .= "monthname($column)";
508 #    }
509 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
510 #
511 #        #Display by Year
512 #        $colfield .= "Year($column)";
513 #
514 #    }
515     else {
516         $colfield .= $column;
517     }
518
519     my $strsth2;
520     $strsth2 .=
521       "SELECT distinctrow $colfield FROM (aqorders, aqbasket )
522                  LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
523                  LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
524                  LEFT JOIN aqbudgets  ON (aqorders.budget_id = aqbudgets.budget_id )
525
526                  LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
527                  WHERE (aqorders.basketno=aqbasket.basketno) AND 
528                  $column IS NOT NULL AND $column <> '' ";
529
530 #                               LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
531
532     if (@colfilter) {
533         if ( $colfilter[1] ) {
534             if ( $colfilter[0] ) {
535                 $strsth2 .= " AND $column BETWEEN  ? AND ? ";
536             }
537             else {
538                 $strsth2 .= " AND $column <= ? ";
539             }
540         }
541         elsif (
542             ( $colfilter[0] )
543             and (  ( $column =~ /closedate/ )
544                 or ( $line =~ /received/ ))
545 #                or ( $line =~ /acquired/ ) )
546           )
547         {
548             $strsth2 .= " AND $column >= ? ";
549         }
550         elsif ( $colfilter[0] ) {
551             $colfilter[0] =~ s/\*/%/g;
552             $strsth2 .= " AND $column LIKE ? ";
553         }
554     }
555
556
557     $strsth2 .= " GROUP BY $colfield";
558     $strsth2 .= " ORDER BY $colfield";
559
560     my $sth2 = $dbh->prepare($strsth2);
561
562     if ( (@colfilter) and ($colfilter[1]) ) {
563         $sth2->execute( $colfilter[0], $colfilter[1] );
564     }
565     elsif ( $colfilter[0] ) {
566         $sth2->execute( $colfilter[0] );
567     }
568     else {
569         $sth2->execute;
570     }
571         while ( my $celvalue = $sth2->fetchrow ) {
572                 my %cell;
573                 if ($celvalue) {
574                         $cell{coltitle} = $celvalue;
575                         push @loopcol, \%cell;
576                 }
577         }
578
579     #       warn "fin des titres colonnes";
580
581     my $i = 0;
582     my @totalcol;
583     my $hilighted = -1;
584
585     #Initialization of cell values.....
586     my %table;
587
588 #       warn "init table...\n";
589     foreach my $row (@loopline) {
590         foreach my $col (@loopcol) {
591             $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
592         }
593         $table{ $row->{rowtitle} }->{totalrow} = 0;
594     }
595
596     # preparing calculation
597     my $strcalc;
598     $strcalc .= "SELECT $linefield, $colfield, ";
599     $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
600     $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
601       if ( $process == 2 );
602     $strcalc .= "FROM (aqorders, aqbasket )
603                  LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
604                  LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
605                  LEFT JOIN aqbudgets  ON (aqorders.budget_id = aqbudgets.budget_id )
606
607                  LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) 
608                  WHERE (aqorders.basketno=aqbasket.basketno) ";
609
610 #                 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
611     
612         @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
613     $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
614       if ( @$filters[0] );
615     @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
616     $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
617       if ( @$filters[1] );
618     @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
619     $strcalc .= " AND aqorders.datereceived >= '" . @$filters[2] . "'"
620       if ( @$filters[2] );
621     @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
622     $strcalc .= " AND aqorders.datereceived <= '" . @$filters[3] . "'"
623       if ( @$filters[3] );
624 #    @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
625 #    $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
626 #      if ( @$filters[4] );
627 #    @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
628 #    $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
629 #      if ( @$filters[5] );
630     @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
631     $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
632       if ( @$filters[4] );
633     @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
634     $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
635       if ( @$filters[5] );
636     @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
637     $strcalc .= " AND aqbudgets.budget_code LIKE '" . @$filters[6] . "'"
638       if ( @$filters[6] );
639     @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
640     $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
641       if ( @$filters[7] );
642     @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
643     $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
644       if ( @$filters[8] );
645
646     $strcalc .= " AND aqorders.datecancellationprinted is NULL ";
647
648     $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
649
650 #       warn $strcalc . "\n";
651
652     my $dbcalc = $dbh->prepare($strcalc);
653     $dbcalc->execute;
654
655     #       warn "filling table";
656     my $emptycol;
657     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
658                 next if ($row eq undef || $col eq undef);
659
660 # warn "filling table $row / $col / $value ";
661
662         $emptycol = 1         if ( !defined($col) );
663         $col      = "zzEMPTY" if ( !defined($col) );
664         $row      = "zzEMPTY" if ( !defined($row) );
665
666         $table{$row}->{$col}     += $value;
667         $table{$row}->{totalrow} += $value;
668         $grantotal               += $value;
669     }
670
671     push @loopcol, { coltitle => "NULL" } if ($emptycol);
672
673     foreach my $row ( sort keys %table ) {
674         my @loopcell;
675         #@loopcol ensures the order for columns is common with column titles
676         # and the number matches the number of columns
677         foreach my $col (@loopcol) {
678             my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
679             push @loopcell, { value => $value };
680         }
681         push @looprow,
682           {
683             'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
684             'loopcell'  => \@loopcell,
685             'hilighted' => ( $hilighted > 0 ),
686             'totalrow'  => $table{$row}->{totalrow}
687           };
688         $hilighted = -$hilighted;
689     }
690
691     #       warn "footer processing";
692     foreach my $col (@loopcol) {
693         my $total = 0;
694         foreach my $row (@looprow) {
695             $total += $table{
696                 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
697                 : $row->{rowtitle}
698               }->{
699                 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
700                 : $col->{coltitle}
701               };
702
703 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
704         }
705
706         #               warn "summ for column ".$col->{coltitle}."  = ".$total;
707         push @loopfooter, { 'totalcol' => $total };
708     }
709
710     # the header of the table
711         $globalline{loopfilter}=\@loopfilter;
712     # the core of the table
713     $globalline{looprow} = \@looprow;
714     $globalline{loopcol} = \@loopcol;
715
716     #       # the foot (totals by borrower type)
717     $globalline{loopfooter} = \@loopfooter;
718     $globalline{total}      = $grantotal;
719     $globalline{line}       = $line;
720     $globalline{column}     = $column;
721     push @mainloop, \%globalline;
722     return \@mainloop;
723 }
724
725 1;
726