diff --git a/app/assets/javascripts/admin/index_utils/filters/attr_filter.js.coffee b/app/assets/javascripts/admin/index_utils/filters/attr_filter.js.coffee index c645b507f1..14255f3f85 100644 --- a/app/assets/javascripts/admin/index_utils/filters/attr_filter.js.coffee +++ b/app/assets/javascripts/admin/index_utils/filters/attr_filter.js.coffee @@ -1,12 +1,16 @@ # Used like a regular angular filter where an object is passed # Adds the additional special case that a value of 0 for the filter # acts as a bypass for that particular attribute + +# NOTE the name doesn't reflect what the filter does, it only fiters on the variant.producer_id angular.module("admin.indexUtils").filter "attrFilter", ($filter) -> return (objects, filters) -> - Object.keys(filters).reduce (filtered, attr) -> - filter = filters[attr] - return filtered if !filter? || filter == 0 - return $filter('filter')(filtered, (object) -> - object[attr] == filter - ) - , objects + filter = filters["producer_id"] + + return objects if !filter? || filter == 0 + + return $filter('filter')(objects, (product) -> + for variant in product.variants + return true if variant["producer_id"] == filter + false + , true) diff --git a/app/assets/javascripts/admin/variant_overrides/directives/track_inheritance.js.coffee b/app/assets/javascripts/admin/variant_overrides/directives/track_inheritance.js.coffee index ecc1cdac29..6c908ea641 100644 --- a/app/assets/javascripts/admin/variant_overrides/directives/track_inheritance.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/directives/track_inheritance.js.coffee @@ -2,10 +2,10 @@ angular.module("admin.variantOverrides").directive "trackInheritance", (VariantO require: "ngModel" link: (scope, element, attrs, ngModel) -> # This is a bit hacky, but it allows us to load the inherit property on the VO, but then not submit it - scope.inherit = angular.equals scope.variantOverrides[scope.hub_id][scope.variant.id], VariantOverrides.newFor scope.hub_id, scope.variant.id + scope.inherit = angular.equals scope.variantOverrides[scope.hub_id][scope.variant.id], VariantOverrides.newFor scope.hub_id, scope.variant ngModel.$parsers.push (viewValue) -> if ngModel.$dirty && viewValue - DirtyVariantOverrides.inherit scope.hub_id, scope.variant.id, scope.variantOverrides[scope.hub_id][scope.variant.id].id + DirtyVariantOverrides.inherit scope.hub_id, scope.variant, scope.variantOverrides[scope.hub_id][scope.variant.id].id scope.displayDirty() viewValue diff --git a/app/assets/javascripts/admin/variant_overrides/filters/hub_permissions_filter.js.coffee b/app/assets/javascripts/admin/variant_overrides/filters/hub_permissions_filter.js.coffee index b1434fb21e..7818fadc78 100644 --- a/app/assets/javascripts/admin/variant_overrides/filters/hub_permissions_filter.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/filters/hub_permissions_filter.js.coffee @@ -2,4 +2,8 @@ angular.module("admin.variantOverrides").filter "hubPermissions", ($filter) -> return (products, hubPermissions, hub_id) -> return [] if !hub_id return [] if !hubPermissions[hub_id] - return $filter('filter')(products, ((product) -> hubPermissions[hub_id].indexOf(product.producer_id) > -1), true) + + return $filter('filter')(products, ((product) -> + for variant in product.variants + return hubPermissions[hub_id].indexOf(variant.producer_id) > -1 + ), true) diff --git a/app/assets/javascripts/admin/variant_overrides/services/dirty_variant_overrides.js.coffee b/app/assets/javascripts/admin/variant_overrides/services/dirty_variant_overrides.js.coffee index 537fb484dc..0d5512979f 100644 --- a/app/assets/javascripts/admin/variant_overrides/services/dirty_variant_overrides.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/services/dirty_variant_overrides.js.coffee @@ -12,11 +12,11 @@ angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http @add(hub_id, variant_id, vo_id) @dirtyVariantOverrides[hub_id][variant_id][attr] = value - inherit: (hub_id, variant_id, vo_id) -> - @add(hub_id, variant_id, vo_id) - blankVo = angular.copy(VariantOverrides.inherit(hub_id, variant_id)) + inherit: (hub_id, variant, vo_id) -> + @add(hub_id, variant.id, vo_id) + blankVo = angular.copy(VariantOverrides.inherit(hub_id, variant)) delete blankVo[attr] for attr, value of blankVo when attr not in @requiredAttrs() - @dirtyVariantOverrides[hub_id][variant_id] = blankVo + @dirtyVariantOverrides[hub_id][variant.id] = blankVo count: -> count = 0 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 94a4d093eb..351dbb89f5 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 @@ -13,17 +13,18 @@ angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOve @variantOverrides[hub.id] ||= {} for product in products for variant in product.variants - @inherit(hub.id, variant.id) unless @variantOverrides[hub.id][variant.id] + @inherit(hub.id, variant) unless @variantOverrides[hub.id][variant.id] - inherit: (hub_id, variant_id) -> + inherit: (hub_id, variant) -> # This method is called from the trackInheritance directive, to reinstate inheritance - @variantOverrides[hub_id][variant_id] ||= {} - angular.extend @variantOverrides[hub_id][variant_id], @newFor hub_id, variant_id + @variantOverrides[hub_id][variant.id] ||= {} + angular.extend @variantOverrides[hub_id][variant.id], @newFor(hub_id, variant) - newFor: (hub_id, variant_id) -> + newFor: (hub_id, variant) -> # These properties need to match those checked in VariantOverrideSet.deletable? hub_id: hub_id - variant_id: variant_id + variant_id: variant.id + producer_id: variant.producer_id sku: null price: null count_on_hand: null diff --git a/app/serializers/api/admin/variant_simple_serializer.rb b/app/serializers/api/admin/variant_simple_serializer.rb index d94421321f..3a2944aff0 100644 --- a/app/serializers/api/admin/variant_simple_serializer.rb +++ b/app/serializers/api/admin/variant_simple_serializer.rb @@ -6,7 +6,7 @@ module Api attributes :id, :name, :import_date, :options_text, :unit_value, :unit_description, :unit_to_display, :display_as, :display_name, :name_to_display, - :price, :on_demand, :on_hand + :price, :on_demand, :on_hand, :producer_id has_many :variant_overrides @@ -27,6 +27,10 @@ module Api def price object.price.nil? ? 0.to_f : object.price end + + def producer_id + object.supplier_id + end end end end diff --git a/app/views/admin/variant_overrides/_products_product.html.haml b/app/views/admin/variant_overrides/_products_product.html.haml index 188f332e26..30ca0ed176 100644 --- a/app/views/admin/variant_overrides/_products_product.html.haml +++ b/app/views/admin/variant_overrides/_products_product.html.haml @@ -1,5 +1,5 @@ %tr.product.even - %td.producer{ "ng-show": 'columns.producer.visible', "ng-bind-html": '::producersByID[product.producer_id].name' } + %td.producer{ "ng-show": 'columns.producer.visible' } %td.product{ "ng-show": 'columns.product.visible', "ng-bind": '::product.name' } %td.sku{ "ng-show": 'columns.sku.visible' } %td.price{ "ng-show": 'columns.price.visible' } diff --git a/app/views/admin/variant_overrides/_products_variants.html.haml b/app/views/admin/variant_overrides/_products_variants.html.haml index 43decfe33f..c924044c9b 100644 --- a/app/views/admin/variant_overrides/_products_variants.html.haml +++ b/app/views/admin/variant_overrides/_products_variants.html.haml @@ -1,5 +1,5 @@ %tr.variant{ id: "v_{{variant.id}}", "ng-repeat": 'variant in product.variants | inventoryVariants:hub_id:views' } - %td.producer{ "ng-show": 'columns.producer.visible' } + %td.producer{ "ng-show": 'columns.producer.visible', "ng-bind": '::producersByID[variant.producer_id].name' } %td.product{ "ng-show": 'columns.product.visible' } %span{ "ng-bind": '::variant.display_name || ""' } .variant-override-unit{ "ng-bind": '::variant.unit_to_display' } diff --git a/app/views/admin/variant_overrides/index.html.haml b/app/views/admin/variant_overrides/index.html.haml index adfcc8f71c..f80ad3ae04 100644 --- a/app/views/admin/variant_overrides/index.html.haml +++ b/app/views/admin/variant_overrides/index.html.haml @@ -7,6 +7,7 @@ = render 'admin/variant_overrides/loading_flash' = render 'admin/variant_overrides/controls' = render 'admin/variant_overrides/no_results' + // filteredProducts is defined in admin/variant_overrides/products %div{ "ng-cloak": true, "ng-show": 'hub_id && filteredProducts.length > 0' } = render 'admin/variant_overrides/new_products' = render 'admin/variant_overrides/hidden_products' 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 26f6d1ce93..b2a4bcd728 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, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - {id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - {id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + {id: 1, hub_id: 10, variant_id: 100, producer_id: 500, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + {id: 2, hub_id: 10, variant_id: 200, producer_id: 500, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + {id: 3, hub_id: 20, variant_id: 300, producer_id: 500, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } ] beforeEach -> @@ -19,38 +19,44 @@ 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, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 200: {id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 100: {id: 1, hub_id: 10, variant_id: 100, producer_id: 500, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 200: {id: 2, hub_id: 10, variant_id: 200, producer_id: 500, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } 20: - 300: {id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 300: {id: 3, hub_id: 20, variant_id: 300, producer_id: 500, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } it "ensures blank data available for some products", -> hubs = [{id: 10}, {id: 20}, {id: 30}] products = [ { id: 1 - variants: [{id: 100}, {id: 200}, {id: 300}, {id: 400}, {id: 500}] + variants: [ + {id: 100, producer_id: 1000}, + {id: 200, producer_id: 1000}, + {id: 300, producer_id: 1000}, + {id: 400, producer_id: 1000}, + {id: 500, producer_id: 1001} + ] } ] VariantOverrides.ensureDataFor hubs, products expect(VariantOverrides.variantOverrides[10]).toEqual - 100: { id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 200: { id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 300: { hub_id: 10, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 400: { hub_id: 10, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 500: { hub_id: 10, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 100: { id: 1, hub_id: 10, variant_id: 100, producer_id: 500, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 200: { id: 2, hub_id: 10, variant_id: 200, producer_id: 500, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 300: { hub_id: 10, variant_id: 300, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 400: { hub_id: 10, variant_id: 400, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 500: { hub_id: 10, variant_id: 500, producer_id: 1001, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } expect(VariantOverrides.variantOverrides[20]).toEqual - 100: { hub_id: 20, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 200: { hub_id: 20, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 300: { id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 400: { hub_id: 20, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []} - 500: { hub_id: 20, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 100: { hub_id: 20, variant_id: 100, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 200: { hub_id: 20, variant_id: 200, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 300: { id: 3, hub_id: 20, variant_id: 300, producer_id: 500, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 400: { hub_id: 20, variant_id: 400, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []} + 500: { hub_id: 20, variant_id: 500, producer_id: 1001, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } expect(VariantOverrides.variantOverrides[30]).toEqual - 100: { hub_id: 30, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 200: { hub_id: 30, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 300: { hub_id: 30, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []} - 400: { hub_id: 30, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } - 500: { hub_id: 30, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 100: { hub_id: 30, variant_id: 100, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 200: { hub_id: 30, variant_id: 200, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 300: { hub_id: 30, variant_id: 300, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []} + 400: { hub_id: 30, variant_id: 400, producer_id: 1000, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } + 500: { hub_id: 30, variant_id: 500, producer_id: 1001, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] } it "updates the IDs of variant overrides", -> VariantOverrides.variantOverrides[2] = {} diff --git a/spec/system/admin/variant_overrides_spec.rb b/spec/system/admin/variant_overrides_spec.rb index 3f48ae7602..ce81548573 100644 --- a/spec/system/admin/variant_overrides_spec.rb +++ b/spec/system/admin/variant_overrides_spec.rb @@ -48,31 +48,31 @@ RSpec.describe " context "when inventory_items exist for variants" do let!(:product) { - create(:simple_product, supplier: producer, variant_unit: 'weight', variant_unit_scale: 1) + create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', variant_unit_scale: 1) } let!(:variant) { create(:variant, product:, unit_value: 1, price: 1.23, on_hand: 12) } let!(:inventory_item) { create(:inventory_item, enterprise: hub, variant: ) } let!(:product_managed) { - create(:simple_product, supplier: producer_managed, variant_unit: 'weight', + create(:simple_product, supplier_id: producer_managed.id, variant_unit: 'weight', variant_unit_scale: 1) } let!(:variant_managed) { - create(:variant, product: product_managed, unit_value: 3, price: 3.65, on_hand: 2) + create(:variant, product: product_managed, supplier: producer_managed, unit_value: 3, price: 3.65, on_hand: 2) } let!(:inventory_item_managed) { create(:inventory_item, enterprise: hub, variant: variant_managed ) } - let!(:product_related) { create(:simple_product, supplier: producer_related) } + let!(:product_related) { create(:simple_product, supplier_id: producer_related.id) } let!(:variant_related) { - create(:variant, product: product_related, unit_value: 2, price: 2.34, on_hand: 23) + create(:variant, product: product_related, supplier: producer_related, unit_value: 2, price: 2.34, on_hand: 23) } let!(:inventory_item_related) { create(:inventory_item, enterprise: hub, variant: variant_related ) } - let!(:product_unrelated) { create(:simple_product, supplier: producer_unrelated) } + let!(:product_unrelated) { create(:simple_product, supplier_id: producer_unrelated.id) } context "when a hub is selected" do before do @@ -83,12 +83,18 @@ RSpec.describe " context "with no overrides" do it "displays the list of products with variants" do expect(page).to have_table_row ['Producer', 'Product', 'Price', 'On Hand', 'On Demand?'] - expect(page).to have_table_row [producer.name, product.name, '', '', ''] + expect(page).to have_table_row ['', product.name, '', '', ''] + within "tr#v_#{variant.id}" do |tr| + expect(tr).to have_content(producer.name) + end expect(page).to have_input "variant-overrides-#{variant.id}-price", placeholder: '1.23' expect(page).to have_input "variant-overrides-#{variant.id}-count_on_hand", placeholder: '12' - expect(page).to have_table_row [producer_related.name, product_related.name, '', '', ''] + expect(page).to have_table_row ['', product_related.name, '', '', ''] + within "tr#v_#{variant_related.id}" do |tr| + expect(tr).to have_content(producer_related.name) + end expect(page).to have_input "variant-overrides-#{variant_related.id}-price", placeholder: '2.34' expect(page).to have_input "variant-overrides-#{variant_related.id}-count_on_hand", @@ -253,7 +259,7 @@ RSpec.describe " create(:variant_override, variant:, hub: hub2, price: 1, count_on_hand: 2) } let!(:product2) { - create(:simple_product, supplier: producer, variant_unit: 'weight', + create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', variant_unit_scale: 1) } let!(:variant2) { @@ -470,7 +476,7 @@ RSpec.describe " describe "when inventory_items do not exist for variants" do let!(:product) { - create(:simple_product, supplier: producer, variant_unit: 'weight', variant_unit_scale: 1) + create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', variant_unit_scale: 1) } let!(:variant1) { create(:variant, product:, unit_value: 1, price: 1.23, on_hand: 12) @@ -523,7 +529,7 @@ RSpec.describe " it "shows more than 100 products in my inventory" do supplier = create(:supplier_enterprise, sells: "own") inventory_items = (1..101).map do - product = create(:simple_product, supplier:) + product = create(:simple_product, supplier_id: supplier.id) InventoryItem.create!( enterprise: supplier, variant: product.variants.first