mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
Scope variant to hub only when inventory enabled
This commit is contained in:
@@ -53,7 +53,11 @@ module Admin
|
||||
return unless @order_cycle
|
||||
|
||||
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle)
|
||||
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant)
|
||||
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @shop)
|
||||
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant)
|
||||
end
|
||||
|
||||
@variant.price + fee_calculator.indexed_fees_for(@variant)
|
||||
end
|
||||
|
||||
|
||||
@@ -112,7 +112,11 @@ module Api
|
||||
|
||||
def scoped_variant(variant_id)
|
||||
variant = Spree::Variant.find(variant_id)
|
||||
OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant)
|
||||
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @order.distributor)
|
||||
OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant)
|
||||
end
|
||||
|
||||
variant
|
||||
end
|
||||
|
||||
|
||||
@@ -286,9 +286,15 @@ class OrderCycle < ApplicationRecord
|
||||
user_id: user,
|
||||
distributor_id: distributor,
|
||||
order_cycle_id: self)
|
||||
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
|
||||
items = Spree::LineItem.includes(:variant).joins(:order).merge(orders).to_a
|
||||
items.each { |li| scoper.scope(li.variant) }
|
||||
|
||||
items = Spree::LineItem.includes(:variant).joins(:order).merge(orders)
|
||||
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor)
|
||||
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
|
||||
items.each { |li| scoper.scope(li.variant) }
|
||||
end
|
||||
|
||||
items
|
||||
end
|
||||
|
||||
def distributor_payment_methods
|
||||
|
||||
@@ -164,7 +164,7 @@ module Spree
|
||||
return true if skip_stock_check
|
||||
return true if quantity <= 0
|
||||
|
||||
scoper.scope(variant)
|
||||
scope_variant
|
||||
variant.can_supply?(quantity)
|
||||
end
|
||||
|
||||
@@ -177,7 +177,7 @@ module Spree
|
||||
end
|
||||
|
||||
def cap_quantity_at_stock!
|
||||
scoper.scope(variant)
|
||||
scope_variant
|
||||
return if variant.on_demand
|
||||
|
||||
update!(quantity: variant.on_hand) if quantity > variant.on_hand
|
||||
@@ -263,7 +263,7 @@ module Spree
|
||||
def update_inventory
|
||||
return unless changed?
|
||||
|
||||
scoper.scope(variant)
|
||||
scope_variant
|
||||
Spree::OrderInventory.new(order).verify(self, target_shipment)
|
||||
end
|
||||
|
||||
@@ -294,5 +294,13 @@ module Spree
|
||||
self.final_weight_volume = variant&.unit_value&.* quantity
|
||||
end
|
||||
end
|
||||
|
||||
def scope_variant
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
|
||||
scoper.scope(variant)
|
||||
end
|
||||
|
||||
variant
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -194,7 +194,11 @@ module Spree
|
||||
inventory_units.group_by(&:variant).map do |variant, units|
|
||||
states = {}
|
||||
units.group_by(&:state).each { |state, iu| states[state] = iu.count }
|
||||
scoper.scope(variant)
|
||||
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
|
||||
scoper.scope(variant)
|
||||
end
|
||||
|
||||
OpenStruct.new(variant:, quantity: units.length, states:)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -59,7 +59,10 @@ class CartService
|
||||
end
|
||||
|
||||
def attempt_cart_add(variant, quantity, max_quantity = nil)
|
||||
scoper.scope(variant)
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
|
||||
scoper.scope(variant)
|
||||
end
|
||||
|
||||
return unless valid_variant?(variant)
|
||||
|
||||
cart_add(variant, quantity, max_quantity)
|
||||
|
||||
@@ -51,7 +51,10 @@ module Orders
|
||||
attrs[:line_items].each do |li|
|
||||
next unless variant = Spree::Variant.find_by(id: li[:variant_id])
|
||||
|
||||
scoper.scope(variant)
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, shop)
|
||||
scoper.scope(variant)
|
||||
end
|
||||
|
||||
li[:quantity] = stock_limited_quantity(variant.on_demand, variant.on_hand, li[:quantity])
|
||||
li[:price] = variant.price
|
||||
build_item_from(li)
|
||||
|
||||
@@ -39,7 +39,9 @@ class VariantsStockLevels
|
||||
def scoped_variant(distributor, variant)
|
||||
return variant if distributor.blank?
|
||||
|
||||
scoper(distributor).scope(variant)
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor)
|
||||
scoper(distributor).scope(variant)
|
||||
end
|
||||
variant
|
||||
end
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ RSpec.describe Admin::SubscriptionLineItemsController do
|
||||
end
|
||||
end
|
||||
|
||||
context "where a relevant variant override exists" do
|
||||
context "where a relevant variant override exists", feature: :inventory do
|
||||
let!(:override) {
|
||||
create(:variant_override, hub_id: shop.id, variant_id: variant.id, price: 12.00)
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ RSpec.describe Api::V0::ShipmentsController do
|
||||
expect(order.reload.line_items.first.variant.price).to eq(variant.price)
|
||||
end
|
||||
|
||||
it 'updates existing shipment with variant override if an VO is sent' do
|
||||
it 'updates existing shipment with variant override if an VO is sent', feature: :inventory do
|
||||
hub = create(:distributor_enterprise)
|
||||
order.update_attribute(:distributor, hub)
|
||||
shipment.shipping_method.distributors << hub
|
||||
@@ -346,7 +346,7 @@ RSpec.describe Api::V0::ShipmentsController do
|
||||
expect_error_response
|
||||
end
|
||||
|
||||
it 'adds a variant override to the shipment' do
|
||||
it 'adds a variant override to the shipment', feature: :inventory do
|
||||
hub = create(:distributor_enterprise)
|
||||
order.update_attribute(:distributor, hub)
|
||||
variant_override = create(:variant_override, hub:, variant:)
|
||||
|
||||
@@ -42,7 +42,7 @@ RSpec.describe CartController do
|
||||
end
|
||||
end
|
||||
|
||||
context "handling variant overrides correctly" do
|
||||
context "handling variant overrides correctly", feature: :inventory do
|
||||
let(:product) { create(:simple_product, supplier: producer) }
|
||||
let(:producer) { create(:supplier_enterprise) }
|
||||
let!(:variant_in_the_order) { create(:variant) }
|
||||
|
||||
@@ -477,7 +477,7 @@ RSpec.describe OrderCycle do
|
||||
expect(items).to match_array order.reload.line_items
|
||||
end
|
||||
|
||||
it "returns items with scoped variants" do
|
||||
it "returns items with scoped variants", feature: :inventory do
|
||||
overridden_variant = order.line_items.first.variant
|
||||
create(:variant_override, hub: shop, variant: overridden_variant, count_on_hand: 1000)
|
||||
|
||||
|
||||
@@ -259,7 +259,7 @@ RSpec.describe Spree::LineItem do
|
||||
expect(li.reload.quantity).to eq 0
|
||||
end
|
||||
|
||||
context "when a variant override is in place" do
|
||||
context "when a variant override is in place", feature: :inventory do
|
||||
let!(:hub) { create(:distributor_enterprise) }
|
||||
let!(:vo) { create(:variant_override, hub:, variant: v, count_on_hand: 2) }
|
||||
|
||||
@@ -371,7 +371,7 @@ RSpec.describe Spree::LineItem do
|
||||
let!(:line_item) { order.reload.line_items.first }
|
||||
let!(:variant) { line_item.variant }
|
||||
|
||||
context "when a variant override applies" do
|
||||
context "when a variant override applies", feature: :inventory do
|
||||
let!(:vo) { create(:variant_override, hub: shop, variant:, count_on_hand: 3 ) }
|
||||
|
||||
it "restores stock to the variant override" do
|
||||
@@ -414,7 +414,7 @@ RSpec.describe Spree::LineItem do
|
||||
it { expect(li.sufficient_stock?).to be false }
|
||||
end
|
||||
|
||||
context "when a variant override is in place" do
|
||||
context "when a variant override is in place", feature: :inventory do
|
||||
let!(:vo) { create(:variant_override, hub:, variant: v, count_on_hand: 5) }
|
||||
|
||||
context "and stock on the variant override is sufficient" do
|
||||
|
||||
@@ -86,6 +86,16 @@ RSpec.describe Spree::Shipment do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with variant override", feature: :inventory do
|
||||
let(:order) { create(:order, distributor: variant.supplier) }
|
||||
|
||||
it "returns the scoped variant" do
|
||||
create(:variant_override, hub: variant.supplier, variant:, price: 25.00)
|
||||
|
||||
expect(shipment.manifest.first.variant.price).to eq 25.00
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'shipping_rates' do
|
||||
@@ -276,7 +286,7 @@ RSpec.describe Spree::Shipment do
|
||||
.to change { variant.on_hand }.from(5).to(4)
|
||||
end
|
||||
|
||||
it "reduces stock of a variant override" do
|
||||
it "reduces stock of a variant override", feature: :inventory do
|
||||
variant.on_hand = 5
|
||||
variant_override = VariantOverride.create!(
|
||||
variant:,
|
||||
|
||||
@@ -169,10 +169,12 @@ RSpec.describe CartService do
|
||||
describe "attempt_cart_add" do
|
||||
let!(:variant) { create(:variant, on_hand: 250) }
|
||||
let(:quantity) { 123 }
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
|
||||
before do
|
||||
allow(Spree::Variant).to receive(:find).and_return(variant)
|
||||
allow(VariantOverride).to receive(:for).and_return(nil)
|
||||
allow(order).to receive(:distributor).and_return(distributor)
|
||||
end
|
||||
|
||||
it "performs additional validations" do
|
||||
|
||||
@@ -104,7 +104,7 @@ RSpec.describe Orders::FactoryService do
|
||||
end
|
||||
end
|
||||
|
||||
context "when an override is present" do
|
||||
context "when an override is present", feature: :inventory do
|
||||
let!(:override) {
|
||||
create(:variant_override, hub_id: shop.id, variant_id: variant1.id, count_on_hand: 3)
|
||||
}
|
||||
@@ -137,7 +137,7 @@ RSpec.describe Orders::FactoryService do
|
||||
end
|
||||
end
|
||||
|
||||
context "when an override is present" do
|
||||
context "when an override is present", feature: :inventory do
|
||||
let!(:override) {
|
||||
create(:variant_override, hub_id: shop.id, variant_id: variant1.id, price: 3.0)
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ RSpec.describe VariantsStockLevels do
|
||||
end
|
||||
|
||||
context "when the variant is in the order" do
|
||||
it "returns the on_hand value of the override" do
|
||||
it "returns the on_hand value of the override", feature: :inventory do
|
||||
expect(variant_stock_levels.call(order, [variant_in_the_order.id])).to eq(
|
||||
variant_in_the_order.id => {
|
||||
quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false
|
||||
@@ -86,7 +86,7 @@ RSpec.describe VariantsStockLevels do
|
||||
end
|
||||
|
||||
context "with variants that are not in the order" do
|
||||
it "returns the on_hand value of the override" do
|
||||
it "returns the on_hand value of the override", feature: :inventory do
|
||||
variant_ids = [variant_in_the_order.id, variant_not_in_the_order.id]
|
||||
expect(variant_stock_levels.call(order, variant_ids)).to eq(
|
||||
variant_in_the_order.id => {
|
||||
|
||||
Reference in New Issue
Block a user