diff options
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 36 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/tax_calculator_spec.rb | 7 |
2 files changed, 25 insertions, 18 deletions
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index 30279bf..4efa367 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -12,7 +12,6 @@ module SuperGood def calculate return no_tax if order.tax_address.empty? - return no_tax unless taxjar_breakdown Spree::Tax::OrderTax.new( order_id: order.id, @@ -26,26 +25,29 @@ module SuperGood 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 + @line_item_taxes ||= + if taxjar_breakdown + 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 + else + [] + end end def shipment_taxes @shipment_taxes ||= - begin + if (total_shipping_tax = taxjar_tax.shipping) != 0 # 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 - + # TaxJar does not provide a breakdown of shipping taxes, so we have + # to proportionally distribute the tax across the shipments, + # accounting for rounding errors. tax_items = [] remaining_tax = total_shipping_tax shipments = order.shipments.to_a @@ -74,6 +76,8 @@ module SuperGood ) tax_items + else + [] end end diff --git a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb index 19841a8..b97f53b 100644 --- a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb @@ -79,12 +79,12 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do instance_double( ::Taxjar::Tax, breakdown: breakdown, - shipping: 10.0 + shipping: shipping_tax ) ) end - context "and there is a breakdown" do + context "and there is tax" do let!(:tax_rate) do ::Spree::TaxRate.create!( name: "Sales Tax", @@ -93,6 +93,8 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do ) end + let(:shipping_tax) { 10 } + let(:breakdown) do instance_double ::Taxjar::Breakdown, line_items: [taxjar_line_item] end @@ -141,6 +143,7 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do context "and there is not a breakdown" do let(:breakdown) { nil } + let(:shipping_tax) { 0 } it "returns no taxes" do expect(subject.order_id).to eq order.id |