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