compliance solutions integration: always send a zip, whatever zip we have, RT#78660...
[freeside.git] / FS / FS / TaxEngine / compliance_solutions.pm
index 92ca2ce..2cb9cb5 100644 (file)
@@ -140,18 +140,26 @@ sub build_input_item {
         " recurring charge\n"
         if !$taxproduct;
 
-    # when billing on cancellation there are no units
-    my $units = $self->{cancel} ? 0 : $cust_bill_pkg->units;
-    unshift @items, {
+    my %item = (
       $self->generic_item($cust_bill, $cust_main),
       record_type     => 'S',
       unique_id       => 'cust_bill_pkg '. $cust_bill_pkg->billpkgnum. ' recur',
       charge_amount   => $recur_without_usage,
-      location_a      => $cust_bill_pkg->tax_location->zip,
       productcode     => substr($taxproduct,0,4),
       servicecode     => substr($taxproduct,4,3),
-      units           => $units,
-    };
+    );
+
+    # when billing on cancellation there are no units
+    $item{units} = $self->{cancel} ? 0 : $cust_bill_pkg->units;
+
+    my $location =  $cust_bill_pkg->tax_location
+                 || ( $conf->exists('tax-ship_address')
+                        ? $cust_main->ship_location
+                        : $cust_main->bill_location
+                    );
+    $item{location_a} = $location->zip;
+
+    unshift @items, \%item;
   }
 
   ###
@@ -167,16 +175,24 @@ sub build_input_item {
         " setup charge\n"
         if !$taxproduct;
 
-    unshift @items, {
+    my %item = (
       $self->generic_item($cust_bill, $cust_main),
       record_type     => 'S',
       unique_id       => 'cust_bill_pkg '. $cust_bill_pkg->billpkgnum. ' setup',
       charge_amount   => $cust_bill_pkg->setup,
-      location_a      => $cust_bill_pkg->tax_location->zip,
       productcode     => substr($taxproduct,0,4),
       servicecode     => substr($taxproduct,4,3),
       units           => $cust_bill_pkg->units,
-    };
+    );
+
+    my $location =  $cust_bill_pkg->tax_location
+                 || ( $conf->exists('tax-ship_address')
+                        ? $cust_main->ship_location
+                        : $cust_main->bill_location
+                    );
+    $item{location_a} = $location->zip;
+
+    unshift @items, \%item;
   }
 
   return @items;
@@ -226,6 +242,7 @@ sub make_taxlines {
     }
   );
   warn $request_json if $DEBUG > 1;
+  $cust_bill->taxengine_request($request_json);
 
   my $soap = SOAP::Lite->service("http://tcms1.csilongwood.com/cgi-bin/taxcalc.wsdl");
 
@@ -263,7 +280,7 @@ sub make_taxlines {
     # create a tax rate location if there isn't one yet
     my $taxname = $tax_data->{descript};
     my $tax_rate = FS::tax_rate->new({
-        data_vendor   => 'compliance solutions',
+        data_vendor   => 'compliance_solutions',
         taxname       => $taxname,
         taxclassnum   => '',
         taxauth       => $tax_data->{'taxauthtype'}, # federal / state / city / district
@@ -277,13 +294,16 @@ sub make_taxlines {
     $tax_rate = $tax_rate->replace_old;
 
     my $tax_rate_location = FS::tax_rate_location->new({
-        data_vendor => 'compliance solutions',
-        state       => $tax_data->{'state'},
-        country     => $tax_data->{'country'},
+        data_vendor => 'compliance_solutions',
         geocode     => $tax_data->{'geocode'},
+        district    => $tax_data->{'geo_district'},
+        state       => $tax_data->{'geo_state'},
+        county      => $tax_data->{'geo_county'},
+        country     => 'US',
     });
     $error = $tax_rate_location->find_or_insert;
-    die "error inserting tax_rate_location record: $error\n"
+    die 'error inserting tax_rate_location record for '.  $tax_data->{state}.
+        '/'. $tax_data->{country}. ' ('. $tax_data->{'geocode'}. "): $error\n"
       if $error;
     $tax_rate_location = $tax_rate_location->replace_old;