Stop creating stock movements

Spree added stock movements to track the movements between stock
locations. But we got rid of stock locations and the only stock
movements we have now are just records of stock level changes.

These records were not created in all cases though and there were also
not created for variant overrides (inventory items). And since these
records aren't visible anywhere, I think it's best we remove them
altogether.

I do think that some kind of log would be useful but I don't think that
AR records like this are the best solution for that. And the
StockMovement model just added complexity to our already complex stock
level storage. The actual adjustment of the count_on_hand attribute of
the StockItem was performed in an after_create hook of the
StockMovement. Now we call it explicitely.
This commit is contained in:
Maikel Linke
2025-05-09 15:27:35 +10:00
parent 9034eaa049
commit 2197656606
3 changed files with 3 additions and 22 deletions

View File

@@ -108,13 +108,12 @@ module VariantStock
# only one stock item per variant
#
# This enables us to override this behaviour for variant overrides
def move(quantity, originator = nil)
def move(quantity, _originator = nil)
return if deleted_at
raise_error_if_no_stock_item_available
# Creates a stock movement: it updates stock_item.count_on_hand and fills backorders
stock_item.stock_movements.create!(quantity:, originator:)
stock_item.adjust_count_on_hand(quantity)
end
# There shouldn't be any other stock items, because we should
@@ -141,10 +140,6 @@ module VariantStock
end
# Overwrites stock_item.count_on_hand
#
# Calling stock_item.adjust_count_on_hand will bypass filling backorders
# and creating stock movements
# If that was required we could call self.move
def overwrite_stock_levels(new_level)
stock_item.adjust_count_on_hand(new_level.to_i - stock_item.count_on_hand)
end

View File

@@ -92,7 +92,7 @@ module Spree
def process_return
inventory_units.each do |iu|
iu.return!
Spree::StockMovement.create!(stock_item_id: iu.find_stock_item.id, quantity: 1)
iu.find_stock_item.adjust_count_on_hand(1)
end
Adjustment.create(

View File

@@ -51,13 +51,6 @@ RSpec.describe Spree::OrderInventory do
expect(units['backordered'].size).to eq 2
expect(units['on_hand'].size).to eq 3
end
it 'should create stock_movement' do
expect(subject.__send__(:add_to_shipment, shipment, variant, 5)).to eq 5
movement = variant.stock_item.stock_movements.last
expect(movement.quantity).to eq(-5)
end
end
context 'when order has too many inventory units' do
@@ -101,13 +94,6 @@ RSpec.describe Spree::OrderInventory do
end
end
it 'should create stock_movement' do
expect(subject.__send__(:remove_from_shipment, shipment, variant, 1, true)).to eq 1
movement = variant.stock_item.stock_movements.last
expect(movement.quantity).to eq 1
end
it 'should destroy backordered units first' do
allow(shipment).to receive_messages(inventory_units_for: [
build(:inventory_unit,