Skip to content

Commit

Permalink
Allow on-demand VariantOverride to track stock
Browse files Browse the repository at this point in the history
We allowed this for producer stock and need to do the same for inventory
stock. This will allow us to create backorders for missing, but promised
stock.
  • Loading branch information
mkllnk committed Aug 2, 2024
1 parent cd8dc41 commit b6c4079
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 46 deletions.
13 changes: 2 additions & 11 deletions app/models/concerns/stock_settings_override_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
# `count_on_hand` can either be: nil or a number
#
# This means that a variant override can be in six different stock states
# but only three of them are valid.
# but only four of them are valid.
#
# | on_demand | count_on_hand | stock_overridden? | use_producer_stock_settings? | valid? |
# |-----------|---------------|-------------------|------------------------------|--------|
# | 1 | nil | false | false | true |
# | 0 | x | true | false | true |
# | nil | nil | false | true | true |
# | 1 | x | ? | ? | false |
# | 1 | x | true | false | true |
# | 0 | nil | ? | ? | false |
# | nil | x | ? | ? | false |
#
Expand All @@ -27,7 +27,6 @@ module StockSettingsOverrideValidation

def require_compatible_on_demand_and_count_on_hand
disallow_count_on_hand_if_using_producer_stock_settings
disallow_count_on_hand_if_on_demand
require_count_on_hand_if_limited_stock
end

Expand All @@ -39,14 +38,6 @@ def disallow_count_on_hand_if_using_producer_stock_settings
errors.add(:count_on_hand, error_message)
end

def disallow_count_on_hand_if_on_demand
return unless on_demand? && count_on_hand.present?

error_message = I18n.t("count_on_hand.on_demand_but_count_on_hand_set",
scope: i18n_scope_for_stock_settings_override_validation_error)
errors.add(:count_on_hand, error_message)
end

def require_count_on_hand_if_limited_stock
return unless on_demand == false && count_on_hand.blank?

Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ en:
variant_override:
count_on_hand:
using_producer_stock_settings_but_count_on_hand_set: "must be blank because using producer stock settings"
on_demand_but_count_on_hand_set: "must be blank if on demand"
limited_stock_but_no_count_on_hand: "must be specified because forcing limited stock"
messages:
confirmation: "doesn't match %{attribute}"
Expand Down
2 changes: 0 additions & 2 deletions spec/models/spree/variant_stock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
end

it "reduces stock when on demand" do
pending "VariantOverride allowing stock with on_demand"

override.update!(on_demand: true, count_on_hand: 7)

expect {
Expand Down
7 changes: 2 additions & 5 deletions spec/models/variant_override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@
context "when count_on_hand is set" do
let(:count_on_hand) { 1 }

it "is invalid" do
expect(variant_override).not_to be_valid
error_message = I18n.t("on_demand_but_count_on_hand_set",
scope: [i18n_scope_for_error, "count_on_hand"])
expect(variant_override.errors[:count_on_hand]).to eq([error_message])
it "is valid" do
expect(variant_override).to be_valid
end
end
end
Expand Down
27 changes: 0 additions & 27 deletions spec/system/admin/product_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,33 +624,6 @@
expect(page).not_to have_content "line 3: Sprouts"
end

it "handles on_demand and on_hand validations with inventory - With both values set" do
csv_data = <<~CSV
name, distributor, producer, category, on_hand, price, units, on_demand
Beans, Another Enterprise, User Enterprise, Vegetables, 6, 3.20, 500, 1
Sprouts, Another Enterprise, User Enterprise, Vegetables, 6, 6.50, 500, 1
Cabbage, Another Enterprise, User Enterprise, Vegetables, 0, 1.50, 500, 1
CSV
File.write('/tmp/test.csv', csv_data)

visit main_app.admin_product_import_path
select 'Inventories', from: "settings_import_into"
attach_file 'file', '/tmp/test.csv'
click_button 'Upload'

proceed_to_validation

expect(page).to have_selector '.item-count', text: "3"
expect(page).to have_selector '.invalid-count', text: "3"

find('div.header-description', text: 'Items contain errors').click
expect(page).to have_content "line 2: Beans - Count_on_hand must be blank if on demand"
expect(page).to have_content "line 3: Sprouts - Count_on_hand must be blank if on demand"
expect(page).to have_content "line 4: Cabbage - Count_on_hand must be blank if on demand"
expect(page).to have_content "Imported file contains invalid entries"
expect(page).not_to have_selector 'input[type=submit][value="Save"]'
end

it "imports lines with all allowed units" do
csv_data = CSV.generate do |csv|
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type",
Expand Down

0 comments on commit b6c4079

Please sign in to comment.