From 21976566069034f3c3df799fc99cf5575ef1c0c3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 9 May 2025 15:27:35 +1000 Subject: [PATCH] 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. --- app/models/concerns/variant_stock.rb | 9 ++------- app/models/spree/return_authorization.rb | 2 +- spec/models/spree/order_inventory_spec.rb | 14 -------------- 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/app/models/concerns/variant_stock.rb b/app/models/concerns/variant_stock.rb index 7e40bd2afa..d8d7f1419a 100644 --- a/app/models/concerns/variant_stock.rb +++ b/app/models/concerns/variant_stock.rb @@ -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 diff --git a/app/models/spree/return_authorization.rb b/app/models/spree/return_authorization.rb index 9410899763..b7f7849c61 100644 --- a/app/models/spree/return_authorization.rb +++ b/app/models/spree/return_authorization.rb @@ -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( diff --git a/spec/models/spree/order_inventory_spec.rb b/spec/models/spree/order_inventory_spec.rb index cc7d520bca..234e9d5207 100644 --- a/spec/models/spree/order_inventory_spec.rb +++ b/spec/models/spree/order_inventory_spec.rb @@ -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,