finish basic implemention of tax exemption by tax name hack, RT#5127
authorivan <ivan>
Mon, 22 Jun 2009 10:06:11 +0000 (10:06 +0000)
committerivan <ivan>
Mon, 22 Jun 2009 10:06:11 +0000 (10:06 +0000)
FS/FS/Conf.pm
FS/FS/cust_main.pm
httemplate/search/cust_bill_pkg.cgi
httemplate/search/report_tax.cgi

index 543c770..a9b891c 100644 (file)
@@ -2830,7 +2830,7 @@ worry that config_items is freeside-specific and icky.
   {
     'key'         => 'tax-cust_exempt-groups',
     'section'     => '',
-    'description' => 'List of grouping possibilities for tax names, for per-customer exemption purposes, one tax name prefix for line.  For example, "GST" would indicate the ability to exempt customers individually from taxes starting with "GST" (but not other taxes).',
+    'description' => 'List of grouping possibilities for tax names, for per-customer exemption purposes, one tax name per line.  For example, "GST" would indicate the ability to exempt customers individually from taxes named "GST" (but not other taxes).',
     'type'        => 'textarea',
   },
 
index 51ba209..ed16e1b 100644 (file)
@@ -2972,6 +2972,10 @@ sub _handle_taxes {
         @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
       }
 
+      @taxes = grep { ! $_->taxname or ! $self->tax_exemption($_->taxname) }
+                    @taxes
+        if $self->cust_main_exemption; #just to be safe
+
       if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
         foreach (@taxes) {
           $_->set('pkgnum',      $cust_pkg->pkgnum );
@@ -2984,12 +2988,12 @@ sub _handle_taxes {
       $taxes{'recur'} = [ @taxes ];
       $taxes{$_} = [ @taxes ] foreach (@classes);
 
-      # maybe eliminate this entirely, along with all the 0% records
-      unless ( @taxes ) {
-        return
-          "fatal: can't find tax rate for state/county/country/taxclass ".
-          join('/', map $taxhash{$_}, qw(state county country taxclass) );
-      }
+      # maybe eliminate this entirely, along with all the 0% records
+      unless ( @taxes ) {
+        return
+          "fatal: can't find tax rate for state/county/country/taxclass ".
+          join('/', map $taxhash{$_}, qw(state county country taxclass) );
+      }
 
     } #if $conf->exists('enable_taxproducts') ...
 
@@ -6384,12 +6388,20 @@ sub tax_exemption {
   my( $self, $taxname ) = @_;
 
   qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
-                                     'taxname' => { 'op'    => 'LIKE',
-                                                    'value' => $taxname.'%' },
-                                    },
+                                     'taxname' => $taxname,
+                                   },
           );
 }
 
+=item cust_main_exemption
+
+=cut
+
+sub cust_main_exemption {
+  my $self = shift;
+  qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
+}
+
 =item invoicing_list [ ARRAYREF ]
 
 If an arguement is given, sets these email addresses as invoice recipients
index 4654425..4a31611 100644 (file)
@@ -274,7 +274,24 @@ if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
 push @where, 'cust_bill_pkg.pkgnum  = 0' if $cgi->param('istax');
 
-push @where, " tax = 'Y' " if $cgi->param('cust_tax');
+if ( $cgi->param('cust_tax') ) {
+  #false laziness -ish w/report_tax.cgi
+  my $cust_exempt;
+  if ( $cgi->param('taxname') ) {
+    my $q_taxname = dbh->quote($cgi->param('taxname'));
+    $cust_exempt =
+      "( tax = 'Y'
+         OR EXISTS ( SELECT 1 FROM cust_main_exemption
+                       WHERE cust_main_exemption.custnum = cust_main.custnum
+                         AND cust_main_exemption.taxname = $q_taxname )
+       )
+      ";
+  } else {
+    $cust_exempt = " tax = 'Y' ";
+  }
+
+  push @where, $cust_exempt;
+}
 
 my $count_query;
 if ( $cgi->param('pkg_tax') ) {
index bcceaf1..e89c665 100755 (executable)
@@ -355,9 +355,25 @@ foreach my $r ( qsearch({ 'table'     => 'cust_main_county',
 #    );
 #  }
 
+  #false laziness -ish w/report_tax.cgi
+  my $cust_exempt;
+  if ( $r->taxname ) {
+    my $q_taxname = dbh->quote($r->taxname);
+    $cust_exempt =
+      "( tax = 'Y'
+         OR EXISTS ( SELECT 1 FROM cust_main_exemption
+                       WHERE cust_main_exemption.custnum = cust_main.custnum
+                         AND cust_main_exemption.taxname = $q_taxname
+                   )
+       )
+      ";
+  } else {
+    $cust_exempt = " tax = 'Y' ";
+  }
+
   my $x_cust = scalar_sql($r, \@param,
     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
-     $fromwhere AND $nottax AND tax = 'Y' "
+     $fromwhere AND $nottax AND $cust_exempt "
   );
 
   $regions{$label}->{'exempt_cust'} += $x_cust;