Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope variant to take overrides into account in packer #7461

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ def package
order.line_items.each do |line_item|
next unless stock_location.stock_item(line_item.variant)

on_hand, backordered = stock_location.fill_status(line_item.variant, line_item.quantity)
package.add line_item.variant, on_hand, :on_hand if on_hand.positive?
package.add line_item.variant, backordered, :backordered if backordered.positive?
variant = line_item.variant
OpenFoodNetwork::ScopeVariantToHub.new(order.distributor).scope(variant)

on_hand, backordered = stock_location.fill_status(variant, line_item.quantity)
package.add variant, on_hand, :on_hand if on_hand.positive?
package.add variant, backordered, :backordered if backordered.positive?
end
package
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module OrderManagement
module Stock
describe Packer do
let(:order) { create(:order_with_line_items, line_items_count: 5) }
let(:distributor) { create(:distributor_enterprise) }
let(:order) { create(:order_with_line_items, line_items_count: 5, distributor: distributor) }
let(:stock_location) { create(:stock_location) }

subject { Packer.new(stock_location, order) }
Expand All @@ -26,6 +27,18 @@ module Stock
expect(package.on_hand.size).to eq 5
expect(package.backordered.size).to eq 5
end

it "accounts for variant overrides" do
variant = order.line_items.first.variant
variant.on_hand = 0
variant.on_demand = false
variant.save
expect {
create(:variant_override, variant: variant, hub: distributor, count_on_hand: 10)
}.to change {
subject.package.on_hand.size
}.from(4).to(5)
end
end
end
end
57 changes: 57 additions & 0 deletions spec/features/admin/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,63 @@ def new_order_with_distribution(distributor, order_cycle)
expect(order.reload.line_items.first.quantity).to eq(1000)
end

# Regression test for #7337
context "creating a new order with a variant override" do
let!(:override) { create(:variant_override, hub: distributor, variant: product.variants.first,
count_on_hand: 100) }

before do
product.variants.first.update(on_demand: false, on_hand: 0)

login_as user
new_order_with_distribution(distributor, order_cycle)
expect(page).to have_content I18n.t('spree.add_product').upcase
end

it "creates order and shipment successfully and allows proceeding to payment" do
select2_select product.name, from: 'add_variant_id', search: true

within("table.stock-levels") do
expect(page).to have_selector("#stock_item_quantity")
fill_in "stock_item_quantity", with: 50
find("button.add_variant").click
end

expect(page).to_not have_selector("table.stock-levels")
expect(page).to have_selector("table.stock-contents")

within("tr.stock-item") do
expect(page).to have_text("50 x")
end

order = Spree::Order.last
expect(order.line_items.first.quantity).to eq(50)
expect(order.shipments.count).to eq(1)

click_button "Update And Recalculate Fees"
expect(page).to have_selector 'h1', text: "Customer Details"

fill_in "order_email", with: "[email protected]"
check "order_use_billing"
fill_in "order_bill_address_attributes_firstname", with: "xxx"
fill_in "order_bill_address_attributes_lastname", with: "xxx"
fill_in "order_bill_address_attributes_address1", with: "xxx"
fill_in "order_bill_address_attributes_city", with: "xxx"
fill_in "order_bill_address_attributes_zipcode", with: "xxx"
select "Australia", from: "order_bill_address_attributes_country_id"
select "Victoria", from: "order_bill_address_attributes_state_id"
fill_in "order_bill_address_attributes_phone", with: "xxx"

click_button "Update"

expect(page).to have_content "Customer Details updated"

click_link "Payments"

expect(page).to have_content "New Payment"
end
end

scenario "can't change distributor or order cycle once order has been finalized" do
login_as_admin_and_visit spree.edit_admin_order_path(order)

Expand Down