Improve and simplify VO count on hand suggestions

Set variant override count_on_hand in UI to variant count_on_hand value
if on_demand is changed and both specify limited stock.
This commit is contained in:
Kristina Lim
2018-11-29 16:33:34 +08:00
parent 4f2d96d763
commit cff4eb0005
3 changed files with 74 additions and 31 deletions

View File

@@ -120,8 +120,20 @@ angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl",
else
variant.on_hand
$scope.clearCountOnHandUnlessLimitedStock = (hubId, variantId) ->
variantOverride = $scope.variantOverrides[hubId][variantId]
unless variantOverride.on_demand == false && variantOverride.count_on_hand?
variantOverride.count_on_hand = null
DirtyVariantOverrides.set hubId, variantId, variantOverride.id, 'count_on_hand', null
# This method should only be used when the variant override on_demand is changed.
#
# Change the count_on_hand value to a suggested value.
$scope.updateCountOnHand = (variant, hubId) ->
variantOverride = $scope.variantOverrides[hubId][variant.id]
suggested = $scope.countOnHandSuggestion(variant, hubId)
unless suggested == variantOverride.count_on_hand
variantOverride.count_on_hand = suggested
DirtyVariantOverrides.set hubId, variant.id, variantOverride.id, 'count_on_hand', suggested
# Suggest producer count_on_hand if variant has limited stock and variant override forces limited
# stock. Otherwise, clear whatever value is set.
$scope.countOnHandSuggestion = (variant, hubId) ->
variantOverride = $scope.variantOverrides[hubId][variant.id]
return null unless !variant.on_demand && variantOverride.on_demand == false
variant.on_hand

View File

@@ -10,7 +10,7 @@
%td.on_hand{ ng: { show: 'columns.on_hand.visible' } }
%input{name: 'variant-overrides-{{ variant.id }}-count_on_hand', type: 'text', ng: { model: 'variantOverrides[hub_id][variant.id].count_on_hand', readonly: 'variantOverrides[hub_id][variant.id].on_demand != false' }, placeholder: '{{ countOnHandPlaceholder(variant, hub_id) }}', 'ofn-track-variant-override' => 'count_on_hand'}
%td.on_demand{ ng: { show: 'columns.on_demand.visible' } }
%select{ name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub_id][variant.id].on_demand', change: 'clearCountOnHandUnlessLimitedStock(hub_id, variant.id)', options: 'option.value as option.description for option in onDemandOptions' }, 'ofn-track-variant-override' => 'on_demand' }
%select{ name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub_id][variant.id].on_demand', change: 'updateCountOnHand(variant, hub_id)', options: 'option.value as option.description for option in onDemandOptions' }, 'ofn-track-variant-override' => 'on_demand' }
%option{ value: '' }= t(".on_demand.use_producer_settings")
%td.reset{ ng: { show: 'columns.reset.visible' } }
%input{name: 'variant-overrides-{{ variant.id }}-resettable', type: 'checkbox', ng: {model: 'variantOverrides[hub_id][variant.id].resettable'}, placeholder: '{{ variant.resettable }}', 'ofn-track-variant-override' => 'resettable'}

View File

@@ -88,38 +88,69 @@ describe "VariantOverridesCtrl", ->
expect(VariantOverrides.updateData).toHaveBeenCalledWith variant_overrides_mock
expect(StatusMessage.display).toHaveBeenCalledWith 'success', 'Stocks reset to defaults.'
describe "ensuring that on demand and count on hand settings are compatible", ->
describe "clearing count on hand when not limited stock", ->
describe "suggesting count_on_hand when on_demand is changed", ->
variant = null
beforeEach ->
scope.variantOverrides = {123: {}}
describe "when variant is on demand", ->
beforeEach ->
scope.variantOverrides = {123: {2: {id: 5, count_on_hand: 1}}}
# Ideally, count_on_hand is blank when the variant is on demand. However, this rule is not
# enforced.
variant = {id: 2, on_demand: true, count_on_hand: 20, on_hand: "On demand"}
describe "when on demand is false", ->
beforeEach ->
scope.variantOverrides[123][2].on_demand = false
it "clears count_on_hand when variant override uses producer stock settings", ->
scope.variantOverrides[123][2] = {on_demand: null, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
it "does not clear count on hand", ->
scope.clearCountOnHandUnlessLimitedStock(123, 2)
expect(scope.variantOverrides[123][2].count_on_hand).toEqual(1)
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
describe "when on demand is true", ->
beforeEach ->
scope.variantOverrides[123][2].on_demand = true
it "clears count_on_hand when variant override forces on demand", ->
scope.variantOverrides[123][2] = {on_demand: true, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
it "clears count on hand and registers dirty attribute", ->
scope.clearCountOnHandUnlessLimitedStock(123, 2)
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
describe "when on demand is null", ->
beforeEach ->
scope.variantOverrides[123][2].on_demand = null
it "clears count_on_hand when variant override forces limited stock", ->
scope.variantOverrides[123][2] = {on_demand: false, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
it "clears count on hand and registers dirty attribute", ->
scope.clearCountOnHandUnlessLimitedStock(123, 2)
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
describe "when variant has limited stock", ->
beforeEach ->
variant = {id: 2, on_demand: false, count_on_hand: 20, on_hand: 20}
it "clears count_on_hand when variant override uses producer stock settings", ->
scope.variantOverrides[123][2] = {on_demand: null, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
it "clears count_on_hand when variant override forces on demand", ->
scope.variantOverrides[123][2] = {on_demand: true, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
expect(scope.variantOverrides[123][2].count_on_hand).toBeNull()
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBeNull()
it "sets to producer count_on_hand when variant override forces limited stock", ->
scope.variantOverrides[123][2] = {on_demand: false, count_on_hand: 1}
scope.updateCountOnHand(variant, 123)
expect(scope.variantOverrides[123][2].count_on_hand).toBe(20)
dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2]
expect(dirtyVariantOverride.count_on_hand).toBe(20)
describe "count on hand placeholder", ->
beforeEach ->