Skip to content

Commit

Permalink
Merge pull request #67 from customink/lha/unknown_us_states
Browse files Browse the repository at this point in the history
Add fallback rates for APO addresses, handle unknown US states more gracefully
  • Loading branch information
llhhaa authored Jan 24, 2023
2 parents 5e62a54 + b44b122 commit 4cbfa57
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/vertex_client/rates.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module VertexClient
RATES = {
'US' => {
'AA' => '0.0',
'AE' => '0.0',
'AL' => '0.0914',
'AK' => '0.0',
'AZ' => '0.0',
Expand Down
15 changes: 13 additions & 2 deletions lib/vertex_client/responses/quotation_fallback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,27 @@ def product_for_line_item(product)
)
end

# see lib/vertex_client/rates.rb for hard-coded fallback rates
def tax_amount(price, country, state)
if state.present? && RATES['US'].has_key?(state)
if known_us_state?(state)
price * BigDecimal(RATES['US'][state])
elsif country.present? && RATES.has_key?(country)
elsif known_non_us_country?(country)
price * BigDecimal(RATES[country])
else
BigDecimal('0.0')
end
end

# state is in the United States and we have an explicit fallback
def known_us_state?(state)
state.present? && RATES['US'].has_key?(state)
end

# we have an explicit fallback for the country
def known_non_us_country?(country)
country.present? && country != 'US' && RATES.has_key?(country)
end

def tax_for_line_item(line_item)
price = BigDecimal(line_item[:extended_price].to_s)
state = line_item[:customer][:destination][:main_division]
Expand Down
2 changes: 1 addition & 1 deletion lib/vertex_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module VertexClient
VERSION = '0.10.1'
VERSION = '0.10.2'
end
37 changes: 36 additions & 1 deletion test/responses/quotation_fallback_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
end

describe 'total_tax' do
describe 'for country-less customer' do
let(:countryless_params) do
working_quote_params.tap do |wqp|
wqp[:customer].delete(:country)
wqp[:line_items][1][:customer].delete(:country)
end
end
let(:payload) { VertexClient::Payload::QuotationFallback.new(countryless_params) }

it 'assumes they are from the US' do
assert_equal 8.66, response.total_tax.to_f
end
end

describe 'for US customer' do
it 'is the sum of total_tax from line_items' do
assert_equal 8.66, response.total_tax.to_f
Expand All @@ -35,16 +49,37 @@
working_quote_params[:line_items] = []
assert_equal 0.0, response.total_tax.to_f
end

it 'handle unrecognized states' do
working_quote_params[:customer][:state] = 'XY'
working_quote_params[:line_items][1][:customer][:state] = 'XY'
assert_equal 0.0, response.total_tax.to_f
end
end

describe 'for EU customer' do
let(:payload) { VertexClient::Payload::QuotationFallback.new(working_eu_quote_params) }
let(:response) { VertexClient::Response::QuotationFallback.new(payload) }

it 'is the sum of total_tax from line_items' do
assert_equal 12.54, response.total_tax.to_f
end
end

describe 'for other countries' do
let(:intl_params) do
working_quote_params.tap do |wqp|
wqp[:customer][:country] = 'TZ'
wqp[:customer].delete(:state)
wqp[:line_items][1][:customer][:country] = 'TZ'
wqp[:line_items][1][:customer].delete(:state)
end
end
let(:payload) { VertexClient::Payload::QuotationFallback.new(intl_params) }

it 'is the sum of total_tax from line_items' do
assert_equal 0.0, response.total_tax.to_f
end
end
end

describe 'total' do
Expand Down

0 comments on commit 4cbfa57

Please sign in to comment.