Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Stop the completion copy from re-estimating tax
Łukasz Adamczyk5a799d336389
diff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rbindex 4e885b3e89..cea8cb2611 100644--- a/spree/core/app/models/spree/line_item.rb+++ b/spree/core/app/models/spree/line_item.rb@@ -60,7 +60,12 @@ module Spreeafter_save :update_inventoryafter_save :update_adjustments- after_create :update_tax_charge+ # Set on copies whose tax is already known — the completion copy carries the+ # cart's tax rows over verbatim, so re-estimating would discard the answer+ # and, with an external engine, spend a remote call per line item.+ attr_accessor :skip_tax_estimation++ after_create :update_tax_charge, unless: :skip_tax_estimationdelegate :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, :option_values, to: :variantdelegate :name, :description, :brand, :category, to: :productdiff --git a/spree/core/app/workflows/spree/carts/complete.rb b/spree/core/app/workflows/spree/carts/complete.rbindex c13722faee..5717f0b0e1 100644--- a/spree/core/app/workflows/spree/carts/complete.rb+++ b/spree/core/app/workflows/spree/carts/complete.rb@@ -236,10 +236,18 @@ module Spree'customer'end+ # Copies carry skip_tax_estimation: the cart's tax rows are copied onto the+ # order a few lines below, so estimating per copied item would ask the+ # provider a question whose answer is discarded — and for an external+ # engine that is a billable remote call per line item, inside the+ # completion lock.def copy_line_items!(cart, order)cart.line_items.reload.index_with do |cart_line_item|attributes = cart_line_item.attributes.except('id', 'cart_id', 'created_at', 'updated_at')- order.line_items.create!(attributes.merge('order_id' => order.id))+ line_item = order.line_items.new(attributes.merge('order_id' => order.id))+ line_item.skip_tax_estimation = true+ line_item.save!+ line_itemendend@@ -271,6 +279,8 @@ module Spreeendend+ # The cart's rows are the record of what the sale was costed at, so the+ # order receives them verbatim rather than being re-estimated.def copy_typed_lines!(cart, order, line_item_map, fulfillment_map)line_item_id_map = line_item_map.transform_keys(&:id).transform_values(&:id)diff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rbindex 1fc81a5497..85781e75a5 100644--- a/spree/core/spec/workflows/spree/carts/complete_spec.rb+++ b/spree/core/spec/workflows/spree/carts/complete_spec.rb@@ -95,6 +95,62 @@ module Spreeexpect(provider).to have_received(:commit).with(order)end++ it 'does not re-estimate while copying the cart onto the order' do+ provider = instance_double(Spree::TaxProvider::Internal, estimate: nil, commit: nil)+ allow_any_instance_of(Spree::Order).to receive(:tax_provider).and_return(provider)++ described_class.call(cart: ready_cart)++ # Only commit files the sale. An estimate here would be a remote call per+ # line item for an external engine, and its answer is discarded anyway.+ expect(provider).not_to have_received(:estimate)+ end++ # The copy is the source of truth: whatever the cart was taxed is what the+ # order owes. Left to itself, creating the order's line items fires+ # LineItem#update_tax_charge and writes a second row per item — the order's+ # own totals still read correctly, so the damage lands on whatever reads+ # the rows: an invoice, an e-invoicing export, a tax return.+ context 'when the cart carries tax rows' do+ let(:tax_category) { create(:tax_category) }+ let!(:rate) do+ create(:tax_rate, store: store, country: ready_cart.tax_country, amount: 0.1,+ included_in_price: false, tax_category: tax_category)+ end++ before do+ # The line item copies its category from the variant on every save, so+ # the variant is where it has to be set.+ ready_cart.line_items.each do |line_item|+ line_item.variant.update!(tax_category: tax_category)+ line_item.reload.update!(tax_category: tax_category)+ end+ ready_cart.recalculate_totals!+ end++ # Queried directly rather than through order.tax_lines: the association+ # is loaded during the copy and reports the cart's rows here.+ def order_rows(order)+ Spree::TaxLine.where(order_id: order.id)+ end++ it 'gives the order exactly one tax row per taxed line item' do+ expect(ready_cart.tax_lines.reload.where.not(line_item_id: nil).count).to eq(1)++ order = described_class.call(cart: ready_cart).value+ rows = order_rows(order).where.not(line_item_id: nil)++ expect(rows.count).to eq(1)+ expect(rows.pluck(:line_item_id).uniq.length).to eq(1)+ end++ it 'keeps the summed rows equal to the order additional tax total' do+ order = described_class.call(cart: ready_cart).value++ expect(order_rows(order).sum(:amount)).to eq(order.additional_tax_total)+ end+ endenddescribe 'business customer' do