summaryrefslogtreecommitdiff
path: root/lib/super_good
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-18 16:27:53 -0800
committerJared Norman <jared@super.gd>2019-01-18 16:31:11 -0800
commit6488fd15856d4db6204f8726d6b5f5c8d1abf14b (patch)
treed4d87a0cef8e997c8e3e347e1da85910149280c5 /lib/super_good
parent55cc66a4192ff80c06288c884c8458473dfe36ff (diff)
Support shipping taxes
Diffstat (limited to 'lib/super_good')
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb42
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb
index e498da0..30279bf 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -17,7 +17,7 @@ module SuperGood
Spree::Tax::OrderTax.new(
order_id: order.id,
line_item_taxes: line_item_taxes,
- shipment_taxes: []
+ shipment_taxes: shipment_taxes
)
end
@@ -37,6 +37,46 @@ module SuperGood
end
end
+ def shipment_taxes
+ @shipment_taxes ||=
+ begin
+ # 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
+
+ tax_items = []
+ remaining_tax = total_shipping_tax
+ shipments = order.shipments.to_a
+ total_shipping_cost = shipments.sum(&:total_before_tax)
+
+ shipments[0...-1].each do |shipment|
+ percentage_of_tax = shipment.total_before_tax / total_shipping_cost
+ shipping_tax = (percentage_of_tax * total_shipping_tax).round(2)
+ remaining_tax -= shipping_tax
+
+ tax_items << ::Spree::Tax::ItemTax.new(
+ item_id: shipment.id,
+ label: "Sales Tax",
+ tax_rate: tax_rate,
+ amount: shipping_tax,
+ included_in_price: false
+ )
+ end
+
+ tax_items << ::Spree::Tax::ItemTax.new(
+ item_id: shipments.last.id,
+ label: "Sales Tax",
+ tax_rate: tax_rate,
+ amount: remaining_tax,
+ included_in_price: false
+ )
+
+ tax_items
+ end
+ end
+
def taxjar_breakdown
@taxjar_breakdown ||= taxjar_tax.breakdown
end