diff options
author | Jared Norman <jared@super.gd> | 2019-01-20 16:10:35 -0800 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2019-01-20 16:10:35 -0800 |
commit | 3a1aea93e236ec0a52fae0ef4ab949acccd2088f (patch) | |
tree | 9bcf1596ea1f0cb8d69358b7f64b129e46f3ca35 | |
parent | 4ddde7d3b241cd57f935bb74750d1ed574dd90c4 (diff) |
Use correctly value for shipping tax
Oops, was grabbing the total shipping tax for the order and using that
as the tax, instead of grabbing the collectable tax from the breakdown.
That makes more sense.
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 25 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/tax_calculator_spec.rb | 62 |
2 files changed, 48 insertions, 39 deletions
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index 4efa367..445087a 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -12,6 +12,7 @@ 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,24 +27,22 @@ module SuperGood def line_item_taxes @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 - [] + 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 shipment_taxes @shipment_taxes ||= - if (total_shipping_tax = taxjar_tax.shipping) != 0 + if taxjar_breakdown.shipping? && + (total_shipping_tax = taxjar_breakdown.shipping.tax_collectable) != 0 + # Distribute shipping tax across shipments: # TaxJar does not provide a breakdown of shipping taxes, so we have # to proportionally distribute the tax across the shipments, diff --git a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb index b97f53b..bd74292 100644 --- a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb @@ -78,8 +78,7 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do allow(dummy_api).to receive(:tax_for).with(order).and_return( instance_double( ::Taxjar::Tax, - breakdown: breakdown, - shipping: shipping_tax + breakdown: breakdown ) ) end @@ -93,18 +92,22 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do ) end - let(:shipping_tax) { 10 } - let(:breakdown) do - instance_double ::Taxjar::Breakdown, line_items: [taxjar_line_item] + instance_double ::Taxjar::Breakdown, + line_items: [taxjar_line_item], + shipping?: !!shipping_tax_breakdown, + shipping: shipping_tax_breakdown end let(:taxjar_line_item) do instance_double ::Taxjar::BreakdownLineItem, id: "33", tax_collectable: 6.66 end + let(:shipping_tax_breakdown) { nil } + it "returns the taxes" do expect(subject.order_id).to eq order.id + expect(subject.shipment_taxes).to be_empty expect(subject.line_item_taxes.length).to eq 1 item_tax = subject.line_item_taxes.first @@ -115,35 +118,42 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do expect(item_tax.amount).to eq 6.66 expect(item_tax.included_in_price).to eq false end + end - shipment_taxes = subject.shipment_taxes - expect(shipment_taxes.length).to eq 3 + context "when there are shipping taxes" do + let(:shipping_tax_breakdown) do + instance_double ::Taxjar::Shipping, tax_collectable: 10.00 + end - aggregate_failures do - expect(shipment_taxes[0].item_id).to eq 1 - expect(shipment_taxes[0].label).to eq "Sales Tax" - expect(shipment_taxes[0].tax_rate).to eq tax_rate - expect(shipment_taxes[0].amount).to eq 2.33 - expect(shipment_taxes[0].included_in_price).to eq false - - expect(shipment_taxes[1].item_id).to eq 2 - expect(shipment_taxes[1].label).to eq "Sales Tax" - expect(shipment_taxes[1].tax_rate).to eq tax_rate - expect(shipment_taxes[1].amount).to eq 4.33 - expect(shipment_taxes[1].included_in_price).to eq false - - expect(shipment_taxes[2].item_id).to eq 3 - expect(shipment_taxes[2].label).to eq "Sales Tax" - expect(shipment_taxes[2].tax_rate).to eq tax_rate - expect(shipment_taxes[2].amount).to eq 3.34 - expect(shipment_taxes[2].included_in_price).to eq false + it "returns the shipping taxes" do + shipment_taxes = subject.shipment_taxes + expect(shipment_taxes.length).to eq 3 + + aggregate_failures do + expect(shipment_taxes[0].item_id).to eq 1 + expect(shipment_taxes[0].label).to eq "Sales Tax" + expect(shipment_taxes[0].tax_rate).to eq tax_rate + expect(shipment_taxes[0].amount).to eq 2.33 + expect(shipment_taxes[0].included_in_price).to eq false + + expect(shipment_taxes[1].item_id).to eq 2 + expect(shipment_taxes[1].label).to eq "Sales Tax" + expect(shipment_taxes[1].tax_rate).to eq tax_rate + expect(shipment_taxes[1].amount).to eq 4.33 + expect(shipment_taxes[1].included_in_price).to eq false + + expect(shipment_taxes[2].item_id).to eq 3 + expect(shipment_taxes[2].label).to eq "Sales Tax" + expect(shipment_taxes[2].tax_rate).to eq tax_rate + expect(shipment_taxes[2].amount).to eq 3.34 + expect(shipment_taxes[2].included_in_price).to eq false + end end end end 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 |