diff options
author | Jared Norman <jared@super.gd> | 2019-01-10 14:51:19 -0800 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2019-01-10 14:51:19 -0800 |
commit | 55cc66a4192ff80c06288c884c8458473dfe36ff (patch) | |
tree | a1e78f64f0e51d3ba855e3d32ba9488e678e5ece /lib/super_good | |
parent | 19cf30bbe4739a67cc4009746bdaeade1bdf38ac (diff) |
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.
Diffstat (limited to 'lib/super_good')
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 48 |
1 files changed, 40 insertions, 8 deletions
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 |