From 55cc66a4192ff80c06288c884c8458473dfe36ff Mon Sep 17 00:00:00 2001 From: Jared Norman Date: Thu, 10 Jan 2019 14:51:19 -0800 Subject: Flesh out Calculator behaviour This adds the functionality to the calculator required to map per line item taxes coming from the TaxJar API to the taxes required for each line item in an order. The calculator does not yet support shipping taxes. --- lib/super_good/solidus_taxjar/tax_calculator.rb | 48 ++++++++++++++++++++----- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index e2a398e..e498da0 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -1,28 +1,60 @@ module SuperGood module SolidusTaxJar class TaxCalculator - def initialize(order) + def self.default_api + ::SuperGood::SolidusTaxJar::API.new + end + + def initialize(order, api: self.class.default_api) @order = order + @api = api end def calculate + return no_tax if order.tax_address.empty? + return no_tax unless taxjar_breakdown + Spree::Tax::OrderTax.new( order_id: order.id, - line_item_taxes: line_item_rates, - shipment_taxes: shipment_rates + line_item_taxes: line_item_taxes, + shipment_taxes: [] ) end private - attr_reader :order + 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 + end + + def taxjar_breakdown + @taxjar_breakdown ||= taxjar_tax.breakdown + end + + def taxjar_tax + @taxjar_taxes ||= api.tax_for(order) + end - def line_item_rates - [] + def no_tax + Spree::Tax::OrderTax.new( + order_id: order.id, + line_item_taxes: [], + shipment_taxes: [] + ) end - def shipment_rates - [] + def tax_rate + Spree::TaxRate.find_by(name: "Sales Tax") end end end -- cgit v1.2.3