From 56cf43f1c14a28492f611b86e6b95f9b4d34dcd1 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Thu, 22 Apr 2021 11:45:12 -0700 Subject: [PATCH 1/4] scope variant to take overrides into account in packer --- .../app/services/order_management/stock/packer.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/engines/order_management/app/services/order_management/stock/packer.rb b/engines/order_management/app/services/order_management/stock/packer.rb index 49141d3fef6..87fe891c724 100644 --- a/engines/order_management/app/services/order_management/stock/packer.rb +++ b/engines/order_management/app/services/order_management/stock/packer.rb @@ -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 From 5e21480ae0e2c25f90feb746f25be802daf22baa Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 23 Apr 2021 09:30:55 -0700 Subject: [PATCH 2/4] add packer spec --- .../order_management/stock/packer_spec.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/engines/order_management/spec/services/order_management/stock/packer_spec.rb b/engines/order_management/spec/services/order_management/stock/packer_spec.rb index 53b6505231b..d77fe1e071c 100644 --- a/engines/order_management/spec/services/order_management/stock/packer_spec.rb +++ b/engines/order_management/spec/services/order_management/stock/packer_spec.rb @@ -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) } @@ -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 From de02acad6434fc3066217956aad5e9ba8210d1d6 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 26 Apr 2021 15:42:36 -0700 Subject: [PATCH 3/4] add feature spec --- spec/features/admin/order_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/features/admin/order_spec.rb b/spec/features/admin/order_spec.rb index 17d8a996589..f41b1df2e1f 100644 --- a/spec/features/admin/order_spec.rb +++ b/spec/features/admin/order_spec.rb @@ -174,6 +174,30 @@ def new_order_with_distribution(distributor, order_cycle) expect(order.reload.line_items.first.quantity).to eq(1000) end + scenario "there is a variant override" do + # Move the order back to the cart state + order.state = 'cart' + order.completed_at = nil + variant = order.line_items.first.variant + variant.update_attribute(:on_demand, false) + variant.update_attribute(:on_hand, 0) + override = create(:variant_override, hub: order.distributor, variant: variant, count_on_hand: 100) + + login_as_admin_and_visit spree.edit_admin_order_path(order) + + within("tr.stock-item", text: order.products.first.name) do + find("a.edit-item").click + expect(page).to have_input(:quantity) + fill_in(:quantity, with: 50) + find("a.save-item").click + end + + within("tr.stock-item", text: order.products.first.name) do + expect(page).to have_text("50 x") + end + expect(order.reload.line_items.first.quantity).to eq(50) + 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) From c7e65185eeee742a7910acaa5519d9e1e3bee02c Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Apr 2021 10:44:29 +0100 Subject: [PATCH 4/4] Regression test for new order with variant overrides --- spec/features/admin/order_spec.rb | 67 +++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/spec/features/admin/order_spec.rb b/spec/features/admin/order_spec.rb index f41b1df2e1f..34921c68d11 100644 --- a/spec/features/admin/order_spec.rb +++ b/spec/features/admin/order_spec.rb @@ -174,28 +174,61 @@ def new_order_with_distribution(distributor, order_cycle) expect(order.reload.line_items.first.quantity).to eq(1000) end - scenario "there is a variant override" do - # Move the order back to the cart state - order.state = 'cart' - order.completed_at = nil - variant = order.line_items.first.variant - variant.update_attribute(:on_demand, false) - variant.update_attribute(:on_hand, 0) - override = create(:variant_override, hub: order.distributor, variant: variant, count_on_hand: 100) + # 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) } - login_as_admin_and_visit spree.edit_admin_order_path(order) + before do + product.variants.first.update(on_demand: false, on_hand: 0) - within("tr.stock-item", text: order.products.first.name) do - find("a.edit-item").click - expect(page).to have_input(:quantity) - fill_in(:quantity, with: 50) - find("a.save-item").click + login_as user + new_order_with_distribution(distributor, order_cycle) + expect(page).to have_content I18n.t('spree.add_product').upcase end - within("tr.stock-item", text: order.products.first.name) do - expect(page).to have_text("50 x") + 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: "test@test.com" + 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 - expect(order.reload.line_items.first.quantity).to eq(50) end scenario "can't change distributor or order cycle once order has been finalized" do