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