summaryrefslogtreecommitdiff
path: root/lib/super_good/solidus_taxjar
diff options
context:
space:
mode:
Diffstat (limited to 'lib/super_good/solidus_taxjar')
-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