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:
luisramos0
2019-06-06 18:09:25 +01:00
parent 9b6f1a5e11
commit 7bd32d4967
2 changed files with 16 additions and 6 deletions

View File

@@ -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

View File

@@ -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