mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-04 07:09:14 +00:00
Fix usage of variant.on_hand in subscriptions order factory, we now take on_demand into account
This fixes a problem introduced in 12eab1bfa9 (diff-c3c4192f302cc77e9a8547012fe86ddb), since then variant.on_hand does not return infinity if variant is on_demand
This commit is contained in:
@@ -48,7 +48,7 @@ class OrderFactory
|
||||
attrs[:line_items].each do |li|
|
||||
next unless variant = Spree::Variant.find_by_id(li[:variant_id])
|
||||
scoper.scope(variant)
|
||||
li[:quantity] = stock_limited_quantity(variant.on_hand, li[:quantity])
|
||||
li[:quantity] = stock_limited_quantity(variant.on_demand, variant.on_hand, li[:quantity])
|
||||
li[:price] = variant.price
|
||||
build_item_from(li)
|
||||
end
|
||||
@@ -81,9 +81,9 @@ class OrderFactory
|
||||
@order.payments.create(payment_method_id: attrs[:payment_method_id], amount: @order.reload.total)
|
||||
end
|
||||
|
||||
def stock_limited_quantity(stock, requested)
|
||||
return requested if opts[:skip_stock_check]
|
||||
[stock, requested].min
|
||||
def stock_limited_quantity(variant_on_demand, variant_on_hand, requested)
|
||||
return requested if opts[:skip_stock_check] || variant_on_demand
|
||||
[variant_on_hand, requested].min
|
||||
end
|
||||
|
||||
def scoper
|
||||
|
||||
@@ -31,7 +31,7 @@ describe OrderFactory do
|
||||
attrs
|
||||
end
|
||||
|
||||
it "builds a new order based the provided attributes" do
|
||||
it "builds a new order based on the provided attributes" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.count).to eq 2
|
||||
@@ -78,11 +78,21 @@ describe OrderFactory do
|
||||
end
|
||||
|
||||
context "when skip_stock_check is not requested" do
|
||||
it "initialised the order but limits stock to the available amount" do
|
||||
it "initialises the order but limits stock to the available amount" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 2
|
||||
end
|
||||
|
||||
context "when variant is on_demand" do
|
||||
before { variant1.update_attribute(:on_demand, true) }
|
||||
|
||||
it "initialises the order with the requested quantity regardless of stock" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 5
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when skip_stock_check is requested" do
|
||||
|
||||
Reference in New Issue
Block a user