mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-19 04:49:15 +00:00
Merge branch 'master' into 2-0-stable-jan-8th
This commit is contained in:
41
app/models/concerns/stock_settings_override_validation.rb
Normal file
41
app/models/concerns/stock_settings_override_validation.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
module StockSettingsOverrideValidation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_validation :require_compatible_on_demand_and_count_on_hand
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def disallow_count_on_hand_if_using_producer_stock_settings
|
||||
return unless on_demand.nil? && count_on_hand.present?
|
||||
|
||||
error_message = I18n.t("count_on_hand.using_producer_stock_settings_but_count_on_hand_set",
|
||||
scope: i18n_scope_for_stock_settings_override_validation_error)
|
||||
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?
|
||||
|
||||
error_message = I18n.t("count_on_hand.limited_stock_but_no_count_on_hand",
|
||||
scope: i18n_scope_for_stock_settings_override_validation_error)
|
||||
errors.add(:count_on_hand, error_message)
|
||||
end
|
||||
|
||||
def i18n_scope_for_stock_settings_override_validation_error
|
||||
"activerecord.errors.models.#{self.class.name.underscore}"
|
||||
end
|
||||
end
|
||||
@@ -68,6 +68,18 @@ module ProductImport
|
||||
|
||||
private
|
||||
|
||||
def find_or_initialize_variant_override(entry, existing_variant)
|
||||
existing_variant_override = VariantOverride.where(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
).first
|
||||
|
||||
existing_variant_override || VariantOverride.new(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
)
|
||||
end
|
||||
|
||||
def enterprise_validation(entry)
|
||||
return if name_presence_error entry
|
||||
return if enterprise_not_found_error entry
|
||||
@@ -310,21 +322,12 @@ module ProductImport
|
||||
end
|
||||
|
||||
def create_inventory_item(entry, existing_variant)
|
||||
existing_variant_override = VariantOverride.where(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
).first
|
||||
find_or_initialize_variant_override(entry, existing_variant).tap do |variant_override|
|
||||
check_variant_override_stock_settings(entry, variant_override)
|
||||
|
||||
variant_override = existing_variant_override || VariantOverride.new(
|
||||
variant_id: existing_variant.id,
|
||||
hub_id: entry.enterprise_id
|
||||
)
|
||||
|
||||
variant_override.assign_attributes(count_on_hand: entry.on_hand, import_date: @import_time)
|
||||
check_on_hand_nil(entry, variant_override)
|
||||
variant_override.assign_attributes(entry.attributes.slice('price', 'on_demand'))
|
||||
|
||||
variant_override
|
||||
variant_override.assign_attributes(import_date: @import_time)
|
||||
variant_override.assign_attributes(entry.attributes.slice('price', 'on_demand'))
|
||||
end
|
||||
end
|
||||
|
||||
def mark_as_inventory_item(entry, variant_override)
|
||||
@@ -355,5 +358,11 @@ module ProductImport
|
||||
object.count_on_hand = 0 if object.respond_to?(:count_on_hand)
|
||||
entry.on_hand_nil = true
|
||||
end
|
||||
|
||||
def check_variant_override_stock_settings(entry, object)
|
||||
object.count_on_hand = entry.on_hand.presence
|
||||
object.on_demand = object.count_on_hand.blank? if entry.on_demand.blank?
|
||||
entry.on_hand_nil = object.count_on_hand.blank?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class VariantOverride < ActiveRecord::Base
|
||||
extend Spree::LocalizedNumber
|
||||
include StockSettingsOverrideValidation
|
||||
|
||||
acts_as_taggable
|
||||
|
||||
@@ -58,7 +59,7 @@ class VariantOverride < ActiveRecord::Base
|
||||
def reset_stock!
|
||||
if resettable
|
||||
if default_stock?
|
||||
self.attributes = { count_on_hand: default_stock }
|
||||
self.attributes = { on_demand: false, count_on_hand: default_stock }
|
||||
save
|
||||
else
|
||||
Bugsnag.notify RuntimeError.new "Attempting to reset stock level for a variant with no default stock level."
|
||||
|
||||
Reference in New Issue
Block a user