diff --git a/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee b/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee index 72caecbcc1..1a4a445d0e 100644 --- a/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee @@ -20,7 +20,7 @@ angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOve count_on_hand: null on_demand: null default_stock: null - enable_reset: false + resettable: false updateIds: (updatedVos) -> for vo in updatedVos diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index efac7e5bd6..ec3adebdaf 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -56,7 +56,7 @@ class VariantOverride < ActiveRecord::Base end def reset_stock! - if enable_reset + if resettable if default_stock? self.attributes = { count_on_hand: default_stock } self.save diff --git a/app/models/variant_override_set.rb b/app/models/variant_override_set.rb index f1aaf1074c..54124983b5 100644 --- a/app/models/variant_override_set.rb +++ b/app/models/variant_override_set.rb @@ -7,7 +7,7 @@ class VariantOverrideSet < ModelSet attrs['price'].blank? && attrs['count_on_hand'].blank? && attrs['default_stock'].blank? && - attrs['enable_reset'].blank? && + attrs['resettable'].blank? && attrs['sku'].nil? && attrs['on_demand'].nil? end diff --git a/app/serializers/api/admin/variant_override_serializer.rb b/app/serializers/api/admin/variant_override_serializer.rb index fa989af96e..c1e6e0038c 100644 --- a/app/serializers/api/admin/variant_override_serializer.rb +++ b/app/serializers/api/admin/variant_override_serializer.rb @@ -1,3 +1,3 @@ class Api::Admin::VariantOverrideSerializer < ActiveModel::Serializer - attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand, :default_stock, :enable_reset + attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand, :default_stock, :resettable end diff --git a/app/views/admin/variant_overrides/_products_variants.html.haml b/app/views/admin/variant_overrides/_products_variants.html.haml index 9115a079a2..7e441dbd64 100644 --- a/app/views/admin/variant_overrides/_products_variants.html.haml +++ b/app/views/admin/variant_overrides/_products_variants.html.haml @@ -15,6 +15,6 @@ %input{name: 'variant-overrides-{{ variant.id }}-count-on-hand', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].count_on_hand'}, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'} %td - %input{name: 'variant-overrides-{{ variant.id }}-enable-reset', type: 'checkbox', ng: {model: 'variantOverrides[hub.id][variant.id].enable_reset'}, placeholder: '{{ variant.enable_reset }}', 'ofn-track-variant-override' => 'enable_reset'} + %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'} %td %input{name: 'variant-overrides-{{ variant.id }}-default-stock', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock ? variant.default_stock : "Default stock"}}', 'ofn-track-variant-override' => 'default_stock'} diff --git a/db/schema.rb b/db/schema.rb index 60f3819562..08cc2bb643 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20151126235409) do +ActiveRecord::Schema.define(:version => 20151128185900) do create_table "account_invoices", :force => true do |t| t.integer "user_id", :null => false @@ -1162,7 +1162,7 @@ ActiveRecord::Schema.define(:version => 20151126235409) do t.string "sku" t.boolean "on_demand" t.integer "default_stock" - t.boolean "enable_reset" + t.boolean "resettable" end add_index "variant_overrides", ["variant_id", "hub_id"], :name => "index_variant_overrides_on_variant_id_and_hub_id" diff --git a/spec/controllers/admin/variant_overrides_spec.rb b/spec/controllers/admin/variant_overrides_spec.rb index 882b1759ca..2eb887b961 100644 --- a/spec/controllers/admin/variant_overrides_spec.rb +++ b/spec/controllers/admin/variant_overrides_spec.rb @@ -6,8 +6,8 @@ module Admin let!(:hub_owner) { create :user, enterprise_limit: 2 } let!(:v1) { create(:variant) } let!(:v2) { create(:variant) } - let!(:vo1) { create(:variant_override, hub: hub, variant: v1, price: "6.0", count_on_hand: 5, default_stock: 7, enable_reset: true) } - let!(:vo2) { create(:variant_override, hub: hub, variant: v2, price: "6.0", count_on_hand: 2, default_stock: 1, enable_reset: false) } + let!(:vo1) { create(:variant_override, hub: hub, variant: v1, price: "6.0", count_on_hand: 5, default_stock: 7, resettable: true) } + let!(:vo2) { create(:variant_override, hub: hub, variant: v2, price: "6.0", count_on_hand: 2, default_stock: 1, resettable: false) } before do controller.stub spree_current_user: hub_owner diff --git a/spec/factories.rb b/spec/factories.rb index f6eeff4680..dd8d02e7bf 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -95,7 +95,7 @@ FactoryGirl.define do price 77.77 count_on_hand 11111 default_stock 2000 - enable_reset false + resettable false end factory :enterprise, :class => Enterprise do diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index 955e02054c..40398033ca 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -181,9 +181,11 @@ feature %q{ end context "with overrides" do - let!(:vo) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, enable_reset: true) } + let!(:vo) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, resettable: true) } let!(:vo_no_auth) { create(:variant_override, variant: variant, hub: hub3, price: 1, count_on_hand: 2) } - + let!(:product2) { create(:simple_product, supplier: producer, variant_unit: 'weight', variant_unit_scale: 1) } + let!(:variant2) { create(:variant, product: product2, unit_value: 8, price: 1.00, on_hand: 12) } + let!(:vo_no_reset) { create(:variant_override, variant: variant2, hub: hub, price: 3.99, count_on_hand: 40, default_stock: 100, resettable: false) } before do visit '/admin/variant_overrides' select2_select hub.name, from: 'hub_id' @@ -211,12 +213,12 @@ feature %q{ vo.count_on_hand.should == 8888 end - # This fails for me and can't find where this automatic deletion is implemented? + # Any new fields added to the VO model need to be added to this test it "deletes overrides when values are cleared" do fill_in "variant-overrides-#{variant.id}-price", with: '' fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '' fill_in "variant-overrides-#{variant.id}-default-stock", with: '' - page.uncheck "variant-overrides-#{variant.id}-enable-reset" + page.uncheck "variant-overrides-#{variant.id}-resettable" page.should have_content "Changes to one override remain unsaved." expect do @@ -235,6 +237,14 @@ feature %q{ vo.count_on_hand.should == 1000 end + it "doesn't reset stock levels if the behaviour is disabled" do + click_button 'Reset Stock to Defaults' + vo_no_reset.reload + page.should have_input "variant-overrides-#{variant2.id}-count-on-hand", with: '40', placeholder: '12' + vo_no_reset.count_on_hand.should == 40 + end + + it "prompts to save changes before reset if any are pending" do fill_in "variant-overrides-#{variant.id}-price", with: '200' click_button 'Reset Stock to Defaults' diff --git a/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee index 5ffd71cd64..9dcae28a94 100644 --- a/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee @@ -1,9 +1,9 @@ describe "VariantOverrides service", -> VariantOverrides = $httpBackend = null variantOverrides = [ - {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} - {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} - {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} + {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: '', resettable: false} + {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: '', resettable: false} + {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: '', resettable: false} ] beforeEach -> @@ -19,10 +19,10 @@ describe "VariantOverrides service", -> it "indexes variant overrides by hub_id -> variant_id", -> expect(VariantOverrides.variantOverrides).toEqual 10: - 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} - 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} + 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: '', resettable: false} + 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: '', resettable: false} 20: - 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} + 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: '', resettable: false} it "ensures blank data available for some products", -> hubs = [{id: 10}, {id: 20}, {id: 30}] @@ -35,32 +35,32 @@ describe "VariantOverrides service", -> VariantOverrides.ensureDataFor hubs, products expect(VariantOverrides.variantOverrides).toEqual 10: - 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} - 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} - 300: { hub_id: 10, variant_id: 300, price: '', count_on_hand: '', default_stock: ''} - 400: { hub_id: 10, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} - 500: { hub_id: 10, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} + 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: '', resettable: false} + 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: '', resettable: false} + 300: { hub_id: 10, variant_id: 300, price: '', count_on_hand: '', default_stock: '', resettable: false} + 400: { hub_id: 10, variant_id: 400, price: '', count_on_hand: '', default_stock: '', resettable: false} + 500: { hub_id: 10, variant_id: 500, price: '', count_on_hand: '', default_stock: '', resettable: false} 20: - 100: { hub_id: 20, variant_id: 100, price: '', count_on_hand: '', default_stock: ''} - 200: { hub_id: 20, variant_id: 200, price: '', count_on_hand: '', default_stock: ''} - 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} - 400: { hub_id: 20, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} - 500: { hub_id: 20, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} + 100: { hub_id: 20, variant_id: 100, price: '', count_on_hand: '', default_stock: '', resettable: false} + 200: { hub_id: 20, variant_id: 200, price: '', count_on_hand: '', default_stock: '', resettable: false} + 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: '', resettable: false} + 400: { hub_id: 20, variant_id: 400, price: '', count_on_hand: '', default_stock: '', resettable: false} + 500: { hub_id: 20, variant_id: 500, price: '', count_on_hand: '', default_stock: '', resettable: false} 30: - 100: { hub_id: 30, variant_id: 100, price: '', count_on_hand: '', default_stock: ''} - 200: { hub_id: 30, variant_id: 200, price: '', count_on_hand: '', default_stock: ''} - 300: { hub_id: 30, variant_id: 300, price: '', count_on_hand: '', default_stock: ''} - 400: { hub_id: 30, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} - 500: { hub_id: 30, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} + 100: { hub_id: 30, variant_id: 100, price: '', count_on_hand: '', default_stock: '', resettable: false} + 200: { hub_id: 30, variant_id: 200, price: '', count_on_hand: '', default_stock: '', resettable: false} + 300: { hub_id: 30, variant_id: 300, price: '', count_on_hand: '', default_stock: '', resettable: false} + 400: { hub_id: 30, variant_id: 400, price: '', count_on_hand: '', default_stock: '', resettable: false} + 500: { hub_id: 30, variant_id: 500, price: '', count_on_hand: '', default_stock: '', resettable: false} it "updates the IDs of variant overrides", -> VariantOverrides.variantOverrides[2] = {} - VariantOverrides.variantOverrides[2][3] = {hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: ''} - VariantOverrides.variantOverrides[2][8] = {hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} + VariantOverrides.variantOverrides[2][3] = {hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: '', resettable: false} + VariantOverrides.variantOverrides[2][8] = {hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: '', resettable: false} updatedVos = [ - {id: 1, hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: ''} - {id: 6, hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} + {id: 1, hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: '', resettable: false} + {id: 6, hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: '', resettable: false} ] VariantOverrides.updateIds updatedVos @@ -75,14 +75,14 @@ describe "VariantOverrides service", -> it "updates the variant overrides on the page with new data", -> VariantOverrides.variantOverrides[1] = - 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: 3} - 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} + 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: 3, resettable: true} + 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: '', resettable: false} # Updated count on hand to 3 updatedVos = [ - {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3} + {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3, resettable: true} ] VariantOverrides.updateData(updatedVos) expect(VariantOverrides.variantOverrides[1]).toEqual - 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3} - 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} + 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3, resettable: true} + 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: '', resettable: false} diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index d1b7f0f84a..ed2f2c00f4 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -89,18 +89,18 @@ describe VariantOverride do describe "resetting stock levels" do it "resets the on hand level to the value in the default_stock field" do - vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20, enable_reset: true) + vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20, resettable: true) vo.reset_stock! vo.reload.count_on_hand.should == 20 end it "silently logs an error if the variant override doesn't have a default stock level" do - vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock:nil, enable_reset: true) + vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock:nil, resettable: true) Bugsnag.should_receive(:notify) vo.reset_stock! vo.reload.count_on_hand.should == 12 end it "doesn't reset the level if the behaviour is disabled" do - vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 10, enable_reset: false) + vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 10, resettable: false) vo.reset_stock! vo.reload.count_on_hand.should == 12 end