summaryrefslogtreecommitdiff
path: root/lib/super_good
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-20 13:29:46 -0800
committerJared Norman <jared@super.gd>2019-01-20 13:29:46 -0800
commit8912e81c695ec1aafe3fdb03203d31fdee71e9a6 (patch)
tree59858092475f04efe7e537cc53cfced67ca863f8 /lib/super_good
parent6488fd15856d4db6204f8726d6b5f5c8d1abf14b (diff)
Improve tax handling
This is partially a refactor, and also handles the unlikely scenario where there is a shipping tax, but no breakdown.
Diffstat (limited to 'lib/super_good')
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb36
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb
index 30279bf..4efa367 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -12,7 +12,6 @@ module SuperGood
def calculate
return no_tax if order.tax_address.empty?
- return no_tax unless taxjar_breakdown
Spree::Tax::OrderTax.new(
order_id: order.id,
@@ -26,26 +25,29 @@ module SuperGood
attr_reader :order, :api
def line_item_taxes
- taxjar_breakdown.line_items.map do |line_item|
- Spree::Tax::ItemTax.new(
- item_id: line_item.id.to_i,
- label: "Sales Tax",
- tax_rate: tax_rate,
- amount: line_item.tax_collectable,
- included_in_price: false
- )
- end
+ @line_item_taxes ||=
+ if taxjar_breakdown
+ taxjar_breakdown.line_items.map do |line_item|
+ Spree::Tax::ItemTax.new(
+ item_id: line_item.id.to_i,
+ label: "Sales Tax",
+ tax_rate: tax_rate,
+ amount: line_item.tax_collectable,
+ included_in_price: false
+ )
+ end
+ else
+ []
+ end
end
def shipment_taxes
@shipment_taxes ||=
- begin
+ if (total_shipping_tax = taxjar_tax.shipping) != 0
# Distribute shipping tax across shipments:
- # TaxJar does not provide a breakdown, so we have to proportionally
- # distribute the tax across the shipments, accounting for rounding
- # errors.
- total_shipping_tax = taxjar_tax.shipping
-
+ # TaxJar does not provide a breakdown of shipping taxes, so we have
+ # to proportionally distribute the tax across the shipments,
+ # accounting for rounding errors.
tax_items = []
remaining_tax = total_shipping_tax
shipments = order.shipments.to_a
@@ -74,6 +76,8 @@ module SuperGood
)
tax_items
+ else
+ []
end
end