mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #13312 from mkllnk/stock-movement
Stop creating stock movements
This commit is contained in:
@@ -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)
|
||||
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
|
||||
|
||||
@@ -35,7 +35,6 @@ module Spree
|
||||
can [:read, :update, :destroy], Spree::User, id: user.id
|
||||
can [:index, :read], State
|
||||
can [:index, :read], StockItem
|
||||
can [:index, :read], StockMovement
|
||||
can [:index, :read], Taxon
|
||||
can [:index, :read], Variant
|
||||
can [:index, :read], Zone
|
||||
|
||||
@@ -77,7 +77,7 @@ module Spree
|
||||
back_order.times { shipment.set_up_inventory('backordered', variant, order) }
|
||||
|
||||
if order.completed?
|
||||
variant.move(-quantity, shipment)
|
||||
variant.move(-quantity)
|
||||
end
|
||||
|
||||
quantity
|
||||
@@ -101,7 +101,7 @@ module Spree
|
||||
shipment.destroy if shipment.inventory_units.reload.count == 0
|
||||
|
||||
if order.completed? && restock_item
|
||||
variant.move(removed_quantity, shipment)
|
||||
variant.move(removed_quantity)
|
||||
end
|
||||
|
||||
removed_quantity
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -313,11 +313,11 @@ module Spree
|
||||
end
|
||||
|
||||
def manifest_unstock(item)
|
||||
item.variant.move(-1 * item.quantity, self)
|
||||
item.variant.move(-1 * item.quantity)
|
||||
end
|
||||
|
||||
def manifest_restock(item)
|
||||
item.variant.move(item.quantity, self)
|
||||
item.variant.move(item.quantity)
|
||||
end
|
||||
|
||||
def generate_shipment_number
|
||||
|
||||
@@ -7,7 +7,6 @@ module Spree
|
||||
acts_as_paranoid
|
||||
|
||||
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :stock_items
|
||||
has_many :stock_movements, dependent: :destroy
|
||||
|
||||
validates :variant_id, uniqueness: { scope: [:deleted_at] }
|
||||
validates :count_on_hand, numericality: { greater_than_or_equal_to: 0, unless: :backorderable? }
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
class StockMovement < ApplicationRecord
|
||||
belongs_to :stock_item, class_name: 'Spree::StockItem'
|
||||
belongs_to :originator, polymorphic: true, optional: true
|
||||
|
||||
after_create :update_stock_item_quantity
|
||||
|
||||
validates :quantity, presence: true
|
||||
|
||||
scope :recent, -> { order('created_at DESC') }
|
||||
|
||||
private
|
||||
|
||||
def update_stock_item_quantity
|
||||
stock_item.adjust_count_on_hand quantity
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -41,9 +41,8 @@ module OpenFoodNetwork
|
||||
|
||||
# If it is an variant override with a count_on_hand value:
|
||||
# - updates variant_override.count_on_hand
|
||||
# - does not create stock_movement
|
||||
# - does not update stock_item.count_on_hand
|
||||
def move(quantity, originator = nil)
|
||||
def move(quantity)
|
||||
if @variant_override&.stock_overridden?
|
||||
@variant_override.move_stock! quantity
|
||||
else
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :stock_movement, class: Spree::StockMovement do
|
||||
quantity { 1 }
|
||||
action { 'sold' }
|
||||
end
|
||||
end
|
||||
@@ -119,13 +119,6 @@ RSpec.describe Spree::Ability do
|
||||
end
|
||||
end
|
||||
|
||||
context 'for StockMovement' do
|
||||
let(:resource) { Spree::StockMovement.new }
|
||||
context 'requested by any user' do
|
||||
it_should_behave_like 'read only'
|
||||
end
|
||||
end
|
||||
|
||||
context 'for Taxons' do
|
||||
let(:resource) { Spree::Taxon.new }
|
||||
context 'requested by any user' do
|
||||
|
||||
@@ -25,7 +25,7 @@ RSpec.describe Spree::InventoryUnit do
|
||||
]
|
||||
}
|
||||
|
||||
it "should create a stock movement" do
|
||||
it "finalizes pending units" do
|
||||
Spree::InventoryUnit.finalize_units!(inventory_units)
|
||||
expect(inventory_units.any?(&:pending)).to be_falsy
|
||||
end
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -84,17 +84,6 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
context "has stock movements" do
|
||||
let(:product) { create(:product) }
|
||||
let(:variant) { product.variants.first }
|
||||
let(:stock_item) { variant.stock_items.first }
|
||||
|
||||
it "doesnt raise ReadOnlyRecord error" do
|
||||
Spree::StockMovement.create!(stock_item:, quantity: 1)
|
||||
expect { product.destroy }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "associations" do
|
||||
it { is_expected.to have_one(:image) }
|
||||
|
||||
|
||||
@@ -324,7 +324,7 @@ RSpec.describe Spree::Shipment do
|
||||
allow(shipment).to receive_message_chain(:inventory_units,
|
||||
:group_by,
|
||||
map: [unit])
|
||||
expect(variant).to receive(:move).with(1, shipment)
|
||||
expect(variant).to receive(:move).with(1)
|
||||
shipment.after_cancel
|
||||
end
|
||||
end
|
||||
@@ -347,7 +347,7 @@ RSpec.describe Spree::Shipment do
|
||||
allow(shipment).to receive_message_chain(:inventory_units,
|
||||
:group_by,
|
||||
map: [unit])
|
||||
expect(variant).to receive(:move).with(-1, shipment)
|
||||
expect(variant).to receive(:move).with(-1)
|
||||
shipment.after_resume
|
||||
end
|
||||
|
||||
|
||||
@@ -101,13 +101,5 @@ RSpec.describe Spree::StockItem do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with stock movements" do
|
||||
before { Spree::StockMovement.create(stock_item: subject, quantity: 1) }
|
||||
|
||||
it "does not destroy stock_movements when destroyed" do
|
||||
expect { subject.destroy }.to change { Spree::StockMovement.count }.by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Spree::StockMovement do
|
||||
let(:stock_item) { create(:variant, on_hand: 15).stock_item }
|
||||
subject { build(:stock_movement, stock_item:) }
|
||||
|
||||
it 'should belong to a stock item' do
|
||||
expect(subject).to respond_to(:stock_item)
|
||||
end
|
||||
|
||||
context "when quantity is negative" do
|
||||
context "after save" do
|
||||
it "should decrement the stock item count on hand" do
|
||||
subject.quantity = -1
|
||||
subject.save
|
||||
stock_item.reload
|
||||
expect(stock_item.count_on_hand).to eq 14
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when quantity is positive" do
|
||||
context "after save" do
|
||||
it "should increment the stock item count on hand" do
|
||||
subject.quantity = 1
|
||||
subject.save
|
||||
stock_item.reload
|
||||
expect(stock_item.count_on_hand).to eq 16
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user