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