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
Give a fulfillment an association for the ledger rows it caused
Łukasz Adamczykd0f67ecc76b6
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rbindex b420f011a6..765003f5ff 100644--- a/spree/core/app/models/spree/fulfillment.rb+++ b/spree/core/app/models/spree/fulfillment.rb@@ -32,6 +32,13 @@ module Spreehas_many :fees, class_name: 'Spree::Fee', dependent: :destroy, inverse_of: :fulfillmenthas_many :delivery_methods, through: :delivery_rateshas_many :variants, through: :fulfillment_items+ # The ledger rows this fulfillment caused. Deliberately no `dependent:`+ # option: a movement is a committed fact about stock that outlives the+ # fulfillment behind it, and every option Rails offers would either delete+ # those rows or rewrite their cause. Draining a fulfillment already hands+ # its promise to the one taking the units over, so nothing is left holding+ # stock by the time it is destroyed.+ has_many :stock_movements, class_name: 'Spree::StockMovement', inverse_of: :fulfillmenthas_one :selected_delivery_rate, -> { where(selected: true).order(:cost) }, class_name: 'Spree::DeliveryRate'after_save :update_adjustments@@ -348,12 +355,12 @@ module Spree## @return [Hash{Integer => Integer}]def allocated_quantities+ # An unsaved fulfillment has caused nothing.return {} unless persisted?totals = Hash.new(0)- Spree::StockMovement.- where(fulfillment_id: id).+ stock_movements.joins(:stock_level).group("#{Spree::StockLevel.table_name}.variant_id", :kind).sum(:quantity).diff --git a/spree/core/app/models/spree/stock_transfer.rb b/spree/core/app/models/spree/stock_transfer.rbindex 3a1ccc18d2..d0e81b6c87 100644--- a/spree/core/app/models/spree/stock_transfer.rb+++ b/spree/core/app/models/spree/stock_transfer.rb@@ -9,7 +9,9 @@ module Spreepublishes_lifecycle_events- has_many :stock_movements, class_name: 'Spree::StockMovement', inverse_of: :stock_transfer, dependent: :nullify+ # No `dependent:` option, for the reason given on Spree::Fulfillment's own+ # movements: the ledger outlives the record that caused it.+ has_many :stock_movements, class_name: 'Spree::StockMovement', inverse_of: :stock_transferaccepts_nested_attributes_for :stock_movements, reject_if: proc { |attributes|attributes[:quantity] = attributes[:quantity].to_iattributes[:quantity].blank? || attributes[:quantity].zero? || attributes[:stock_level_id].blank?