From 428a33ad6d0e01621717840d2db4861f4bee6c21 Mon Sep 17 00:00:00 2001
From: Mark Wells <mark@freeside.biz>
Date: Thu, 19 Feb 2015 15:45:22 -0800
Subject: [PATCH] fix problems with tax calculation, #33587, from #18509

---
 FS/FS/cust_bill_pkg.pm    | 11 ++++++++---
 FS/FS/cust_main_county.pm |  7 +++++--
 FS/FS/tax_rate.pm         |  6 ++++--
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/FS/FS/cust_bill_pkg.pm b/FS/FS/cust_bill_pkg.pm
index 13f027b07..2aa2a6fab 100644
--- a/FS/FS/cust_bill_pkg.pm
+++ b/FS/FS/cust_bill_pkg.pm
@@ -954,6 +954,7 @@ sub disintegrate {
 
   my $usage_total;
   foreach my $classnum ($self->usage_classes) {
+    next if $classnum eq ''; # null-class usage is included in 'recur'
     my $amount = $self->usage($classnum);
     next if $amount == 0; # though if so we shouldn't be here
     my $usage_item = FS::cust_bill_pkg->new({
@@ -1013,7 +1014,11 @@ sub usage {
 
     my $sql = 'SELECT SUM(COALESCE(amount,0)) FROM cust_bill_pkg_detail '.
               ' WHERE billpkgnum = '. $self->billpkgnum;
-    $sql .= " AND classnum = $classnum" if defined($classnum);
+    if ($classnum =~ /^(\d+)$/) {
+      $sql .= " AND classnum = $1";
+    } elsif (defined($classnum) and $classnum eq '') {
+      $sql .= " AND classnum IS NULL";
+    }
 
     my $sth = dbh->prepare($sql) or die dbh->errstr;
     $sth->execute or die $sth->errstr;
@@ -1128,7 +1133,7 @@ sub tax_locationnum {
   my $self = shift;
   if ( $self->pkgnum ) { # normal sales
     return $self->cust_pkg->tax_locationnum;
-  } elsif ( $self->feepart ) { # fees
+  } elsif ( $self->feepart and $self->invnum ) { # fees
     return $self->cust_bill->cust_main->ship_locationnum;
   } else { # taxes
     return '';
@@ -1139,7 +1144,7 @@ sub tax_location {
   my $self = shift;
   if ( $self->pkgnum ) { # normal sales
     return $self->cust_pkg->tax_location;
-  } elsif ( $self->feepart ) { # fees
+  } elsif ( $self->feepart and $self->invnum ) { # fees
     return $self->cust_bill->cust_main->ship_location;
   } else { # taxes
     return;
diff --git a/FS/FS/cust_main_county.pm b/FS/FS/cust_main_county.pm
index d9cd634d2..6d1ce77fd 100644
--- a/FS/FS/cust_main_county.pm
+++ b/FS/FS/cust_main_county.pm
@@ -309,8 +309,11 @@ sub taxline {
       # pertain to this tax def
       $taxable_charged -= $_->amount;
     }
- 
-    my $locationnum = $cust_bill_pkg->tax_locationnum;
+
+    # can't determine the tax_locationnum directly for fees; they're not
+    # yet linked to an invoice
+    my $locationnum = $cust_bill_pkg->tax_locationnum
+                   || $cust_main->ship_locationnum;
 
     ### Monthly capped exemptions ### 
     if ( $self->exempt_amount && $self->exempt_amount > 0 
diff --git a/FS/FS/tax_rate.pm b/FS/FS/tax_rate.pm
index ab1a69eab..12a3e987f 100644
--- a/FS/FS/tax_rate.pm
+++ b/FS/FS/tax_rate.pm
@@ -575,9 +575,11 @@ sub taxline {
 
       } elsif ( $self->unittype == 2 ) {
 
+        my $locationnum = $cust_bill_pkg->tax_locationnum
+                       || $cust_main->ship_locationnum;
         # per account
-        $units = 1 unless $seen{$cust_bill_pkg->tax_locationnum};
-        $seen{$cust_bill_pkg->tax_locationnum} = 1;
+        $units = 1 unless $seen{$locationnum};
+        $seen{$locationnum} = 1;
 
       } else {
         # Unittype 19 is used for prepaid wireless E911 charges in many states.
-- 
2.20.1