From 351cd0a842afc664d02161ff9c6a8fc0186eae27 Mon Sep 17 00:00:00 2001 From: Noah Silvera Date: Fri, 9 Apr 2021 13:59:41 -0700 Subject: Consider promotions in default shipping calculator Previously, the shipment_total method on order was used, which does not take into account shipment promotions. Instead, we should manually sum the shipment cost taking into account any adjustments. Co-authored-by: Nick Van Doorn --- lib/super_good/solidus_taxjar.rb | 2 +- spec/super_good/solidus_taxjar/api_params_spec.rb | 4 +++- spec/super_good/solidus_taxjar_spec.rb | 9 +++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 36b5d85..a3cb730 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -44,7 +44,7 @@ module SuperGood } self.line_item_tax_label_maker = ->(taxjar_line_item, spree_line_item) { "Sales Tax" } self.logging_enabled = false - self.shipping_calculator = ->(order) { order.shipment_total } + self.shipping_calculator = ->(order) { order.shipments.sum(&:total_before_tax) } self.shipping_tax_label_maker = ->(shipment, shipping_tax) { "Sales Tax" } self.taxable_address_check = ->(address) { true } self.taxable_order_check = ->(order) { true } diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb index 6c13e03..1e879e7 100644 --- a/spec/super_good/solidus_taxjar/api_params_spec.rb +++ b/spec/super_good/solidus_taxjar/api_params_spec.rb @@ -8,9 +8,9 @@ RSpec.describe SuperGood::SolidusTaxjar::ApiParams do line_items: [line_item], number: "R111222333", ship_address: ship_address, - shipment_total: BigDecimal("3.01"), store: store, total: order_total, + shipments: [shipment], user_id: 12345 ).tap do |order| order.update! completed_at: DateTime.new(2018, 3, 6, 12, 10, 33) @@ -119,6 +119,8 @@ RSpec.describe SuperGood::SolidusTaxjar::ApiParams do ) end + let(:shipment) { Spree::Shipment.create!(cost: BigDecimal("3.01")) } + describe "#order_params" do subject { described_class.order_params(order) } diff --git a/spec/super_good/solidus_taxjar_spec.rb b/spec/super_good/solidus_taxjar_spec.rb index 7fe0dee..d9ef1d4 100644 --- a/spec/super_good/solidus_taxjar_spec.rb +++ b/spec/super_good/solidus_taxjar_spec.rb @@ -77,9 +77,14 @@ RSpec.describe SuperGood::SolidusTaxjar do describe ".shipping_calculator" do subject { described_class.shipping_calculator.call(order) } - let(:order) { instance_double(Spree::Order, shipment_total: 10) } + let(:order) { create :order } + let(:shipment) { create :shipment, order: order, cost: 20 } - it "returns the shipment total" do + before do + create :adjustment, order: order, adjustable: shipment, amount: -10, eligible: true, source: create(:shipping_rate, shipment: shipment) + end + + it "returns the shipment total including promotions" do expect(subject).to eq(10) end end -- cgit v1.2.3