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