fix total line links on tax report when using report_group kludge? (possibly not...
[freeside.git] / httemplate / search / report_tax.cgi
1 <% include("/elements/header.html", "$agentname Tax Report - ".
2               ( $beginning
3                   ? time2str('%h %o %Y ', $beginning )
4                   : ''
5               ).
6               'through '.
7               ( $ending == 4294967295
8                   ? 'now'
9                   : time2str('%h %o %Y', $ending )
10               )
11           )
12 %>
13
14 <% include('/elements/table-grid.html') %>
15
16   <TR>
17     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
18     <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
19     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
20     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
21     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
22     <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
23 % unless ( $cgi->param('show_taxclasses') ) { 
24       <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
25 % } 
26   </TR>
27
28   <TR>
29     <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
30     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
31     <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
32     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
33     <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
34     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
35     <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
36     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
37     <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
38   </TR>
39
40 % my $bgcolor1 = '#eeeeee';
41 % my $bgcolor2 = '#ffffff';
42 % my $bgcolor;
43 %
44 % foreach my $region ( @regions ) {
45 %
46 %   my $link = '';
47 %   if ( $region->{'label'} eq $out ) {
48 %     $link = ';out=1';
49 %   } else {
50 %     $link = ';'. $region->{'url_param'}
51 %       if $region->{'url_param'};
52 %   }
53 %
54 %   if ( $bgcolor eq $bgcolor1 ) {
55 %     $bgcolor = $bgcolor2;
56 %   } else {
57 %     $bgcolor = $bgcolor1;
58 %   }
59 %
60 %   #my $diff = 0;
61 %   my $hicolor = $bgcolor;
62 %   unless ( $cgi->param('show_taxclasses') ) {
63 %     my $diff = abs(   sprintf( '%.2f', $region->{'owed'} )
64 %                     - sprintf( '%.2f', $region->{'tax'}  )
65 %                   );
66 %     if ( $diff > 0.02 ) {
67 %     #  $hicolor = $hicolor eq '#eeeeee' ? '#eeee66' : '#ffff99';
68 %     #} elsif ( $diff ) {
69 %       $hicolor = $hicolor eq '#eeeeee' ? '#eeee99' : '#ffffcc';
70 %     }
71 %   }
72 %
73 %
74 %   my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
75 %   my $tdh = qq(TD CLASS="grid" BGCOLOR="$hicolor");
76 %   my $bigmath = '<FONT FACE="sans-serif" SIZE="+1"><B>';
77 %   my $bme = '</B></FONT>';
78
79     <TR>
80       <<%$td%>><% $region->{'label'} %></TD>
81       <<%$td%> ALIGN="right">
82         <A HREF="<% $baselink. $link %>;nottax=1"
83         ><% &$money_sprintf( $region->{'total'} ) %></A>
84       </TD>
85       <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
86       <<%$td%> ALIGN="right">
87         <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"
88         ><% &$money_sprintf( $region->{'exempt_cust'} ) %></A>
89       </TD>
90       <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
91       <<%$td%> ALIGN="right">
92         <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"
93         ><% &$money_sprintf( $region->{'exempt_pkg'} ) %></A>
94       </TD>
95       <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
96       <<%$td%> ALIGN="right">
97         <A HREF="<% $exemptlink. $link %>"
98         ><% &$money_sprintf( $region->{'exempt_monthly'} ) %></A>
99         </TD>
100       <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
101       <<%$td%> ALIGN="right">
102         <A HREF="<% $baselink. $link %>;nottax=1;taxable=1"
103         ><% &$money_sprintf( $region->{'taxable'} ) %></A>
104       </TD>
105       <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath X $bme" %></TD>
106       <<%$td%> ALIGN="right"><% $region->{'rate'} %></TD>
107       <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath = $bme" %></TD>
108       <<%$tdh%> ALIGN="right">
109         <% &$money_sprintf( $region->{'owed'} ) %>
110       </TD>
111
112 % unless ( $cgi->param('show_taxclasses') ) { 
113         <<%$tdh%> ALIGN="right">
114           <A HREF="<% $baselink. $link %>;istax=1"
115           ><% &$money_sprintf( $region->{'tax'} ) %></A>
116         </TD>
117 % } 
118
119     </TR>
120 % } 
121
122 </TABLE>
123
124 % if ( $cgi->param('show_taxclasses') ) {
125
126     <BR>
127     <% include('/elements/table-grid.html') %>
128     <TR>
129       <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
130       <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
131     </TR>
132
133 %   #some false laziness w/above
134 %   $bgcolor1 = '#eeeeee';
135 %   $bgcolor2 = '#ffffff';
136 %
137 %   foreach my $region ( @base_regions ) {
138 %
139 %     my $link = '';
140 %     if ( $region->{'label'} eq $out ) {
141 %       $link = ';out=1';
142 %     } else {
143 %       $link = ';'. $region->{'url_param'}
144 %         if $region->{'url_param'};
145 %     }
146 %
147 %     if ( $bgcolor eq $bgcolor1 ) {
148 %       $bgcolor = $bgcolor2;
149 %     } else {
150 %       $bgcolor = $bgcolor1;
151 %     }
152 %     my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
153
154       <TR>
155         <<%$td%>><% $region->{'label'} %></TD>
156         <<%$td%> ALIGN="right">
157           <A HREF="<% $baselink. $link %>;istax=1"
158           ><% &$money_sprintf( $region->{'tax'} ) %></A>
159         </TD>
160       </TR>
161
162 % } 
163
164 % if ( $bgcolor eq $bgcolor1 ) {
165 %   $bgcolor = $bgcolor2;
166 % } else {
167 %   $bgcolor = $bgcolor1;
168 % }
169 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
170
171   <TR>
172    <<%$td%>>Total</TD>
173    <<%$td%> ALIGN="right">
174      <A HREF="<% $baselink %>;istax=1"
175      ><% &$money_sprintf( $tot_tax ) %></A>
176    </TD>
177   </TR>
178
179   </TABLE>
180
181 % } 
182
183 <% include('/elements/footer.html') %>
184
185 <%init>
186
187 die "access denied"
188   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
189
190 my $conf = new FS::Conf;
191
192 my $user = getotaker;
193
194 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
195
196 my $join_cust =     '     JOIN cust_bill      USING ( invnum  ) 
197                       LEFT JOIN cust_main     USING ( custnum ) ';
198 my $join_cust_pkg = $join_cust.
199                     ' LEFT JOIN cust_pkg      USING ( pkgnum  )
200                       LEFT JOIN part_pkg      USING ( pkgpart ) ';
201 $join_cust_pkg .=   ' LEFT JOIN cust_location USING ( locationnum )'
202   if $conf->exists('tax-pkg_address');
203
204 my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg "; 
205
206 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
207
208 my( $location_sql, @base_param ) = FS::cust_pkg->location_sql;
209 $where .= " AND $location_sql ";
210
211 my $agentname = '';
212 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
213   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
214   die "agent not found" unless $agent;
215   $agentname = $agent->agent;
216   $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
217 }
218
219 sub gotcust {
220   my $table = shift;
221   my $prefix = @_ ? shift : '';
222   "
223         ( $table.${prefix}county  = cust_main_county.county
224           OR cust_main_county.county = ''
225           OR cust_main_county.county IS NULL )
226     AND ( $table.${prefix}state   = cust_main_county.state
227           OR cust_main_county.state = ''
228           OR cust_main_county.state IS NULL )
229     AND ( $table.${prefix}country = cust_main_county.country )
230   ";
231 }
232
233 my $gotcust;
234 if ( $conf->exists('tax-ship_address') ) {
235
236   $gotcust = "
237                (    cust_main_county.country = cust_main.country
238                  OR cust_main_county.country = cust_main.ship_country
239                )
240
241                AND
242
243                ( 
244                  (     ( ship_last IS NULL     OR  ship_last = '' )
245                    AND ". gotcust('cust_main'). "
246                  )
247                  OR
248                  (       ship_last IS NOT NULL AND ship_last != ''
249                    AND ". gotcust('cust_main', 'ship_'). "
250                  )
251                )
252   ";
253
254 } else {
255
256   $gotcust = gotcust('cust_main');
257
258 }
259 if ( $conf->exists('tax-pkg_address') ) {
260   $gotcust = "
261        ( cust_pkg.locationnum IS     NULL AND $gotcust)
262     OR ( cust_pkg.locationnum IS NOT NULL AND ". gotcust('cust_location'). " )";
263   $gotcust =
264     "WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
265                                  LEFT JOIN cust_main USING ( custnum )
266                                  LEFT JOIN cust_location USING ( locationnum )
267                    WHERE $gotcust
268                    LIMIT 1
269                )
270     ";
271 } else {
272   $gotcust =
273     "WHERE 0 < ( SELECT COUNT(*) FROM cust_main WHERE $gotcust LIMIT 1 )";
274 }
275
276 my $out = 'Out of taxable region(s)';
277 my %regions = ();
278 my %taxclasses = ();
279
280 foreach my $r ( qsearch({ 'table'     => 'cust_main_county',
281                           'extra_sql' => $gotcust,
282                        })
283               )
284 {
285   #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
286
287   my $label = getlabel($r);
288   $regions{$label}->{'label'} = $label;
289   $regions{$label}->{'url_param'} =
290     join(';', map "$_=".uri_escape($r->$_()),
291                   qw( county state country taxname )
292         );
293
294   my @param = @base_param;
295   my $mywhere = $where;
296
297   if ( $r->taxclass ) {
298
299     $mywhere .= " AND taxclass = ? ";
300     push @param, 'taxclass';
301     $regions{$label}->{'url_param'} .= ';taxclass='. uri_escape($r->taxclass);
302     #no, always#  if $cgi->param('show_taxclasses');
303
304     $taxclasses{$r->taxclass} = 1;
305
306   } else {
307
308     $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
309       if $cgi->param('show_taxclasses');
310
311     my $same_sql = $r->sql_taxclass_sameregion;
312     $mywhere .= " AND $same_sql" if $same_sql;
313
314   }
315
316   my $fromwhere = "$from_join_cust_pkg $mywhere"; # AND payby != 'COMP' ";
317
318 #  my $label = getlabel($r);
319 #  $regions{$label}->{'label'} = $label;
320
321   my $nottax = 'pkgnum != 0';
322
323   ## calculate total for this region
324
325   my $t_sql =
326    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax";
327   my $t = scalar_sql($r, \@param, $t_sql);
328   $regions{$label}->{'total'} += $t;
329
330   #if ( $label eq $out ) # && $t ) {
331   #  warn "adding $t for ".
332   #       join('/', map $r->$_, qw( taxclass county state country ) ). "\n";
333   #  #warn $t_sql if $r->state eq 'FL';
334   #}
335
336   ## calculate customer-exemption for this region
337
338 ##  my $taxable = $t;
339
340 #  my($taxable, $x_cust) = (0, 0);
341 #  foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
342 #                       qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
343 #    $taxable += scalar_sql($r, \@param, 
344 #      "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
345 #    );
346 #
347 #    $x_cust += scalar_sql($r, \@param, 
348 #      "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
349 #    );
350 #  }
351
352   my $x_cust = scalar_sql($r, \@param,
353     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
354      $fromwhere AND $nottax AND tax = 'Y' "
355   );
356
357   $regions{$label}->{'exempt_cust'} += $x_cust;
358   
359   ## calculate package-exemption for this region
360
361   my $x_pkg = scalar_sql($r, \@param,
362     "SELECT SUM(
363                  ( CASE WHEN part_pkg.setuptax = 'Y'
364                         THEN cust_bill_pkg.setup
365                         ELSE 0
366                    END
367                  )
368                  +
369                  ( CASE WHEN part_pkg.recurtax = 'Y'
370                         THEN cust_bill_pkg.recur
371                         ELSE 0
372                    END
373                  )
374                )
375        $fromwhere
376        AND $nottax
377        AND (
378                 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
379              OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
380            )
381        AND ( tax != 'Y' OR tax IS NULL )
382     "
383   );
384   $regions{$label}->{'exempt_pkg'} += $x_pkg;
385
386   ## calculate monthly exemption (texas tax) for this region
387
388   # count up all the cust_tax_exempt_pkg records associated with
389   # the actual line items.
390
391   my $x_monthly = scalar_sql($r, \@param,
392     "SELECT SUM(amount)
393        FROM cust_tax_exempt_pkg
394        JOIN cust_bill_pkg USING ( billpkgnum )
395        $join_cust_pkg
396      $mywhere"
397   );
398   $regions{$label}->{'exempt_monthly'} += $x_monthly;
399
400   my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
401   $regions{$label}->{'taxable'} += $taxable;
402
403   $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
404
405   if ( defined($regions{$label}->{'rate'})
406        && $regions{$label}->{'rate'} != $r->tax.'%' ) {
407     $regions{$label}->{'rate'} = 'variable';
408   } else {
409     $regions{$label}->{'rate'} = $r->tax.'%';
410   }
411
412 }
413
414 my $distinct = "country, state, county,
415                 CASE WHEN taxname IS NULL THEN '' ELSE taxname END AS taxname";
416 my $taxclass_distinct = 
417   #a little bit unsure of this part... test?
418   #ah, it looks like it winds up being irrelevant as ->{'tax'} 
419   # from $regions is not displayed when show_taxclasses is on
420   ( $cgi->param('show_taxclasses')
421       ? " CASE WHEN taxclass IS NULL THEN '' ELSE taxclass END "
422       : " '' "
423   )." AS taxclass";
424
425
426 my %qsearch = (
427   'select'    => "DISTINCT $distinct, $taxclass_distinct",
428   'table'     => 'cust_main_county',
429   'hashref'   => {},
430   'extra_sql' => $gotcust,
431 );
432
433 my $taxfromwhere = " FROM cust_bill_pkg $join_cust ";
434 my $taxwhere = $where;
435 if ( $conf->exists('tax-pkg_address') ) {
436
437   $taxfromwhere .= 'LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
438                     LEFT JOIN cust_location USING ( locationnum ) ';
439
440   #quelle kludge
441   $taxwhere =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
442
443 }
444 $taxfromwhere .= " $taxwhere "; #AND payby != 'COMP' ";
445 my @taxparam = @base_param;
446
447 #should i be a cust_main_county method or something
448 #need to pass in $taxfromwhere & @taxparam???
449 my $_taxamount_sub = sub {
450   my $r = shift;
451
452   #match itemdesc if necessary!
453   my $named_tax =
454     $r->taxname
455       ? 'AND itemdesc = '. dbh->quote($r->taxname)
456       : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
457
458   my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
459             " $taxfromwhere AND cust_bill_pkg.pkgnum = 0 $named_tax";
460
461   scalar_sql($r, \@taxparam, $sql );
462 };
463
464 #tax-report_groups filtering
465 my($group_op, $group_value) = ( '', '' );
466 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) {
467   ( $group_op, $group_value ) = ( $1, $2 );
468 }
469 my $group_test = sub {
470   my $label = shift;
471   return 1 unless $group_op; #in case we get called inadvertantly
472   if ( $label eq $out ) { #don't display "out of taxable region" in this case
473     0;
474   } elsif ( $group_op eq '=' ) {
475     $label =~ /^$group_value/;
476   } elsif ( $group_op eq '!=' ) {
477     $label !~ /^$group_value/;
478   } else {
479     die "guru meditation #00de: group_op $group_op\n";
480   }
481 };
482
483 my $tot_tax = 0;
484 #foreach my $label ( keys %regions ) {
485 foreach my $r ( qsearch(\%qsearch) ) {
486
487   #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
488
489   my $label = getlabel($r);
490   if ( $group_op ) {
491     next unless &{$group_test}($label);
492   }
493
494   #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
495   #my @param = @base_param; 
496
497   my $x = &{$_taxamount_sub}($r);
498
499   $regions{$label}->{'tax'} += $x;
500   $tot_tax += $x unless $cgi->param('show_taxclasses');
501
502 }
503
504 my %base_regions = ();
505 if ( $cgi->param('show_taxclasses') ) {
506
507   $qsearch{'select'} = "DISTINCT $distinct";
508   foreach my $r ( qsearch(\%qsearch) ) {
509
510     my $x = &{$_taxamount_sub}($r);
511
512     my $base_label = getlabel($r, 'no_taxclass'=>1 );
513     $base_regions{$base_label}->{'label'} = $base_label;
514
515     $base_regions{$base_label}->{'url_param'} =
516       join(';', map "$_=". uri_escape($r->$_()),
517                      qw( county state country taxname )
518           );
519
520     $base_regions{$base_label}->{'tax'} += $x;
521     $tot_tax += $x;
522   }
523
524 }
525
526 my @regions = keys %regions;
527
528 #tax-report_groups filtering
529 @regions = grep &{$group_test}($_), @regions
530   if $group_op;
531
532 #ordering
533 @regions =
534   map $regions{$_},
535   sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
536   @regions;
537
538 my @base_regions =
539   map $base_regions{$_},
540   sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
541   keys %base_regions;
542
543 #totals
544 my( $total, $tot_taxable, $tot_owed ) = ( 0, 0, 0 );
545 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0, 0 );
546 foreach (@regions) {
547   $total          += $regions{$_}->{'total'};
548   $tot_taxable    += $regions{$_}->{'taxable'};
549   $tot_owed       += $regions{$_}->{'owed'};
550   $exempt_cust    += $regions{$_}->{'exempt_cust'};
551   $exempt_pkg     += $regions{$_}->{'exempt_pkg'};
552   $exempt_monthly += $regions{$_}->{'exempt_monthly'};
553 }
554
555 my $total_url_param = '';
556
557 if ( $group_op ) {
558   $total_url_param = 'report_group='.uri_escape("$group_op $group_value").';'.
559                      join(';', 'taxclass = '.dbh->quote($_), keys %taxclasses );
560 }
561
562 push @regions, {
563   'label'          => 'Total',
564   'url_param'      => $total_url_param,
565   'total'          => $total,
566   'exempt_cust'    => $exempt_cust,
567   'exempt_pkg'     => $exempt_pkg,
568   'exempt_monthly' => $exempt_monthly,
569   'taxable'        => $tot_taxable,
570   'rate'           => '',
571   'owed'           => $tot_owed,
572   'tax'            => $tot_tax,
573 };
574
575 #-- 
576
577 my $money_char = $conf->config('money_char') || '$';
578 my $money_sprintf = sub {
579   $money_char. sprintf('%.2f', shift );
580 };
581
582 sub getlabel {
583   my $r = shift;
584   my %opt = @_;
585
586   my $label;
587   if (
588     $r->tax == 0 
589     && ! scalar( qsearch('cust_main_county', { 'state'   => $r->state,
590                                                'county'  => $r->county,
591                                                'country' => $r->country,
592                                                'tax' => { op=>'>', value=>0 },
593                                              }
594                         )
595                )
596
597   ) {
598     #kludge to avoid "will not stay shared" warning
599     my $out = 'Out of taxable region(s)';
600     $label = $out;
601 #  } elsif ( $r->taxname && count_taxname($r->taxname) == 1 ) {
602 #    $label = $r->taxname;
603 ##    $regions{$label}->{'taxname'} = $label;
604 ##    push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
605   } else {
606     $label = $r->country;
607     $label = $r->state.", $label" if $r->state;
608     $label = $r->county." county, $label" if $r->county;
609     $label = "$label (". $r->taxclass. ")"
610       if $r->taxclass
611       && $cgi->param('show_taxclasses')
612       && ! $opt{'no_taxclass'};
613     $label = $r->taxname. " ($label)" if $r->taxname;
614   }
615   return $label;
616 }
617
618 #my %count_taxname = (); #cache
619 #sub count_taxname {
620 #  my $taxname = shift;
621 #  return $count_taxname{$taxname} if exists $count_taxname{$taxname};
622 #  my $sql = 'SELECT COUNT(*) FROM cust_main_county WHERE taxname = ?';
623 #  my $sth = dbh->prepare($sql) or die dbh->errstr;
624 #  $sth->execute( $taxname )
625 #    or die "Unexpected error executing statement $sql: ". $sth->errstr;
626 #  $count_taxname{$taxname} = $sth->fetchrow_arrayref->[0];
627 #}
628
629 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
630 #to FS::Report or FS::Record or who the fuck knows where)
631 sub scalar_sql {
632   my( $r, $param, $sql ) = @_;
633   #warn "$sql\n";
634   my $sth = dbh->prepare($sql) or die dbh->errstr;
635   $sth->execute( map $r->$_(), @$param )
636     or die "Unexpected error executing statement $sql: ". $sth->errstr;
637   $sth->fetchrow_arrayref->[0] || 0;
638 }
639
640 my $dateagentlink = "begin=$beginning;end=$ending";
641 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
642   if length($agentname);
643 my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
644 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
645
646 </%init>