kohabug 2224 Corrects display descrepancies when applying filters
[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 aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
419                 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
420                 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
421
422     if (@linefilter) {
423         if ( $linefilter[1] ) {
424             if ( $linefilter[0] ) {
425                 $strsth .= " AND $line BETWEEN ? AND ? ";
426             }
427             else {
428                 $strsth .= " AND $line <= ? ";
429             }
430         }
431         elsif (
432             ( $linefilter[0] )
433             and (  ( $line =~ /closedate/ )
434                 or ( $line =~ /received/ ))
435 #                or ( $line =~ /acquired/ ) )
436           )
437         {
438             $strsth .= " AND $line >= ? ";
439         }
440         elsif ( $linefilter[0] ) {
441             $linefilter[0] =~ s/\*/%/g;
442             $strsth .= " AND $line LIKE ? ";
443         }
444     }
445     $strsth .= " GROUP BY $linefield";
446     $strsth .= " ORDER BY $linefield";
447
448     #warn "377:strsth= $strsth";
449
450     my $sth = $dbh->prepare($strsth);
451     if ( (@linefilter) and ( $linefilter[1] ) ) {
452         $sth->execute( $linefilter[0], $linefilter[1] );
453     }
454     elsif ( $linefilter[0] ) {
455         $sth->execute( $linefilter[0] );
456     }
457     else {
458         $sth->execute;
459     }
460         while ( my ($celvalue) = $sth->fetchrow ) {
461                 my %cell;
462                 if ($celvalue) {
463                         $cell{rowtitle} = $celvalue;
464                         push @loopline, \%cell;
465                 }
466                 $cell{totalrow} = 0;
467         }
468 #    warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
469
470     # 2nd, loop cols.
471     my $colfield;
472     if ( ( $column =~ /closedate/ ) and ( $podsp == 1 ) ) {
473
474         #Display by day
475         $colfield .= "dayname($column)";
476     }
477     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 2 ) ) {
478
479         #Display by Month
480         $colfield .= "monthname($column)";
481     }
482     elsif ( ( $column =~ /closedate/ ) and ( $podsp == 3 ) ) {
483
484         #Display by Year
485         $colfield .= "Year($column)";
486
487     }
488     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 1 ) ) {
489
490         #Display by day
491         $colfield .= "dayname($column)";
492     }
493     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 2 ) ) {
494
495         #Display by Month
496         $colfield .= "monthname($column)";
497     }
498     elsif ( ( $column =~ /deliverydate/ ) and ( $rodsp == 3 ) ) {
499
500         #Display by Year
501         $colfield .= "Year($column)";
502
503     }
504 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 1 ) ) {
505 #
506 #        #Display by day
507 #        $colfield .= "dayname($column)";
508 #    }
509 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 2 ) ) {
510 #
511 #        #Display by Month
512 #        $colfield .= "monthname($column)";
513 #    }
514 #    elsif ( ( $column =~ /dateaccessioned/ ) and ( $aodsp == 3 ) ) {
515 #
516 #        #Display by Year
517 #        $colfield .= "Year($column)";
518 #
519 #    }
520     else {
521         $colfield .= $column;
522     }
523
524     my $strsth2;
525     $strsth2 .=
526       "SELECT distinctrow $colfield FROM (aqorders, aqbasket,aqorderbreakdown)
527                  LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
528                  LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
529                  LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
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     if (@colfilter) {
535         if ( $colfilter[1] ) {
536             if ( $colfilter[0] ) {
537                 $strsth2 .= " AND $column BETWEEN  ? AND ? ";
538             }
539             else {
540                 $strsth2 .= " AND $column <= ? ";
541             }
542         }
543         elsif (
544             ( $colfilter[0] )
545             and (  ( $column =~ /closedate/ )
546                 or ( $line =~ /received/ ))
547 #                or ( $line =~ /acquired/ ) )
548           )
549         {
550             $strsth2 .= " AND $column >= ? ";
551         }
552         elsif ( $colfilter[0] ) {
553             $colfilter[0] =~ s/\*/%/g;
554             $strsth2 .= " AND $column LIKE ? ";
555         }
556     }
557     $strsth2 .= " GROUP BY $colfield";
558     $strsth2 .= " ORDER BY $colfield";
559
560 #       warn "MASON:. $strsth2";
561
562     my $sth2 = $dbh->prepare($strsth2);
563     if ( (@colfilter) and ($colfilter[1]) ) {
564         $sth2->execute( $colfilter[0], $colfilter[1] );
565     }
566     elsif ( $colfilter[0] ) {
567         $sth2->execute( $colfilter[0] );
568     }
569     else {
570         $sth2->execute;
571     }
572         while ( my $celvalue = $sth2->fetchrow ) {
573                 my %cell;
574                 if ($celvalue) {
575                         $cell{coltitle} = $celvalue;
576                         #warn "\$cell{coltitle} = $celvalue\n";
577                         push @loopcol, \%cell;
578                 }
579         }
580
581
582     #       warn "fin des titres colonnes";
583
584     my $i = 0;
585     my @totalcol;
586     my $hilighted = -1;
587
588     #Initialization of cell values.....
589     my %table;
590
591 #       warn "init table...\n";
592     foreach my $row (@loopline) {
593         foreach my $col (@loopcol) {
594                         #warn " init table : $row->{rowtitle} / $col->{coltitle} \n";
595             $table{ $row->{rowtitle} }->{ $col->{coltitle} } = 0;
596         }
597         $table{ $row->{rowtitle} }->{totalrow} = 0;
598     }
599
600     # preparing calculation
601     my $strcalc;
602     $strcalc .= "SELECT $linefield, $colfield, ";
603     $strcalc .= "SUM( aqorders.quantity ) " if ( $process == 1 );
604     $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) "
605       if ( $process == 2 );
606     $strcalc .= "FROM (aqorders, aqbasket,aqorderbreakdown)
607                  LEFT JOIN items ON (aqorders.biblionumber= items.biblionumber)
608                  LEFT JOIN biblioitems ON (aqorders.biblionumber= biblioitems.biblionumber)
609                  LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
610                  LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
611                       AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
612
613     @$filters[0] =~ s/\*/%/g if ( @$filters[0] );
614     $strcalc .= " AND aqbasket.closedate >= '" . @$filters[0] . "'"
615       if ( @$filters[0] );
616     @$filters[1] =~ s/\*/%/g if ( @$filters[1] );
617     $strcalc .= " AND aqbasket.closedate <= '" . @$filters[1] . "'"
618       if ( @$filters[1] );
619     @$filters[2] =~ s/\*/%/g if ( @$filters[2] );
620     $strcalc .= " AND aqorderdelivery.deliverydate >= '" . @$filters[2] . "'"
621       if ( @$filters[2] );
622     @$filters[3] =~ s/\*/%/g if ( @$filters[3] );
623     $strcalc .= " AND aqorderdelivery.deliverydate <= '" . @$filters[3] . "'"
624       if ( @$filters[3] );
625 #    @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
626 #    $strcalc .= " AND aqbasket.closedate >= '" . @$filters[4] . "'"
627 #      if ( @$filters[4] );
628 #    @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
629 #    $strcalc .= " AND aqbasket.closedate <= '" . @$filters[5] . "'"
630 #      if ( @$filters[5] );
631     @$filters[4] =~ s/\*/%/g if ( @$filters[4] );
632     $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[4] . "'"
633       if ( @$filters[4] );
634     @$filters[5] =~ s/\*/%/g if ( @$filters[5] );
635     $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[5] . "'"
636       if ( @$filters[5] );
637     @$filters[6] =~ s/\*/%/g if ( @$filters[6] );
638     $strcalc .= " AND aqorderbreakdown.bookfundid LIKE '" . @$filters[6] . "'"
639       if ( @$filters[6] );
640     @$filters[7] =~ s/\*/%/g if ( @$filters[7] );
641     $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[7] . "'"
642       if ( @$filters[7] );
643     @$filters[8] =~ s/\*/%/g if ( @$filters[8] );
644     $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[8] . "'"
645       if ( @$filters[8] );
646     $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
647
648 #       warn $strcalc . "\n";
649     my $dbcalc = $dbh->prepare($strcalc);
650     $dbcalc->execute;
651
652     #       warn "filling table";
653     my $emptycol;
654     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
655                 #warn "filling table $row / $col / $value ";
656         $emptycol = 1         if ( $col eq undef );
657         $col      = "zzEMPTY" if ( $col eq undef );
658         $row      = "zzEMPTY" if ( $row eq undef );
659
660         $table{$row}->{$col}     += $value;
661         $table{$row}->{totalrow} += $value;
662         $grantotal               += $value;
663     }
664
665     push @loopcol, { coltitle => "NULL" } if ($emptycol);
666
667     foreach my $row ( sort keys %table ) {
668         my @loopcell;
669         #@loopcol ensures the order for columns is common with column titles
670         # and the number matches the number of columns
671         foreach my $col (@loopcol) {
672             my $value = $table{$row}->{ ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY" : $col->{coltitle} };
673             push @loopcell, { value => $value };
674         }
675         push @looprow,
676           {
677             'rowtitle' => ( $row eq "zzEMPTY" ) ? "NULL" : $row,
678             'loopcell'  => \@loopcell,
679             'hilighted' => ( $hilighted > 0 ),
680             'totalrow'  => $table{$row}->{totalrow}
681           };
682         $hilighted = -$hilighted;
683     }
684
685     #       warn "footer processing";
686     foreach my $col (@loopcol) {
687         my $total = 0;
688         foreach my $row (@looprow) {
689             $total += $table{
690                 ( $row->{rowtitle} eq "NULL" ) ? "zzEMPTY"
691                 : $row->{rowtitle}
692               }->{
693                 ( $col->{coltitle} eq "NULL" ) ? "zzEMPTY"
694                 : $col->{coltitle}
695               };
696
697 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
698         }
699
700         #               warn "summ for column ".$col->{coltitle}."  = ".$total;
701         push @loopfooter, { 'totalcol' => $total };
702     }
703
704     # the header of the table
705         $globalline{loopfilter}=\@loopfilter;
706     # the core of the table
707     $globalline{looprow} = \@looprow;
708     $globalline{loopcol} = \@loopcol;
709
710     #       # the foot (totals by borrower type)
711     $globalline{loopfooter} = \@loopfooter;
712     $globalline{total}      = $grantotal;
713     $globalline{line}       = $line;
714     $globalline{column}     = $column;
715     push @mainloop, \%globalline;
716     return \@mainloop;
717 }
718
719 1;
720