From 6488fd15856d4db6204f8726d6b5f5c8d1abf14b Mon Sep 17 00:00:00 2001 From: Jared Norman Date: Fri, 18 Jan 2019 16:27:53 -0800 Subject: Support shipping taxes --- lib/super_good/solidus_taxjar/tax_calculator.rb | 42 ++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'lib/super_good/solidus_taxjar') 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 -- cgit v1.2.3