From e7a909e828dbf8234cdebcbacd8591801caf7518 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 2 Oct 2018 12:25:57 +0100 Subject: [PATCH 1/9] Rename supplier to enterprise --- .../import_form_controller.js.coffee | 4 +- .../controllers/import_options_form.js.coffee | 2 +- .../filters/filter_entries.js.coffee | 8 +-- .../services/product_import_service.js.coffee | 8 +-- .../admin/product_import_controller.rb | 2 +- app/models/product_import/entry_processor.rb | 34 +++++------ app/models/product_import/entry_validator.rb | 56 +++++++++---------- app/models/product_import/product_importer.rb | 22 ++++---- app/models/product_import/settings.rb | 4 +- app/models/product_import/spreadsheet_data.rb | 18 +++--- .../product_import/spreadsheet_entry.rb | 4 +- .../product_import/_import_options.html.haml | 4 +- config/locales/en.yml | 2 +- 13 files changed, 84 insertions(+), 84 deletions(-) diff --git a/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee b/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee index d9b73cdabf..95e6a67a5d 100644 --- a/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee +++ b/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee @@ -3,7 +3,7 @@ angular.module("admin.productImport").controller "ImportFormCtrl", ($scope, $htt $scope.entries = {} $scope.update_counts = {} $scope.reset_counts = {} - $scope.supplier_product_counts = ams_data.supplier_product_counts + $scope.enterprise_product_counts = ams_data.enterprise_product_counts $scope.updates = {} $scope.updated_total = 0 @@ -19,7 +19,7 @@ angular.module("admin.productImport").controller "ImportFormCtrl", ($scope, $htt } $scope.countResettable = () -> - angular.forEach $scope.supplier_product_counts, (value, key) -> + angular.forEach $scope.enterprise_product_counts, (value, key) -> $scope.reset_counts[key] = value if $scope.update_counts[key] $scope.reset_counts[key] -= $scope.update_counts[key] diff --git a/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee b/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee index 79764ecc30..67037014d5 100644 --- a/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee +++ b/app/assets/javascripts/admin/product_import/controllers/import_options_form.js.coffee @@ -17,7 +17,7 @@ angular.module("admin.productImport").controller "ImportOptionsFormCtrl", ($scop confirmed = confirm t('js.product_import.confirmation') if checked if confirmed or !checked - ProductImportService.updateResetAbsent($scope.supplierId, $scope.reset_counts[$scope.supplierId], checked) + ProductImportService.updateResetAbsent($scope.enterpriseId, $scope.reset_counts[$scope.enterpriseId], checked) else $scope.settings['reset_all_absent'] = false diff --git a/app/assets/javascripts/admin/product_import/filters/filter_entries.js.coffee b/app/assets/javascripts/admin/product_import/filters/filter_entries.js.coffee index efbcf5652b..d5b68e8a45 100644 --- a/app/assets/javascripts/admin/product_import/filters/filter_entries.js.coffee +++ b/app/assets/javascripts/admin/product_import/filters/filter_entries.js.coffee @@ -18,15 +18,15 @@ angular.module("admin.productImport").filter 'entriesFilterValid', -> filtered -angular.module("admin.productImport").filter 'entriesFilterSupplier', -> - (entries, supplier) -> - if supplier == 'all' +angular.module("admin.productImport").filter 'entriesFilterEnterprise', -> + (entries, enterprise) -> + if enterprise == 'all' return entries filtered = {} angular.forEach entries, (entry, line_number) -> - if supplier == entry.attributes['supplier'] + if enterprise == entry.attributes['enterprise'] filtered[line_number] = entry filtered diff --git a/app/assets/javascripts/admin/product_import/services/product_import_service.js.coffee b/app/assets/javascripts/admin/product_import/services/product_import_service.js.coffee index af0f464df1..045438f32e 100644 --- a/app/assets/javascripts/admin/product_import/services/product_import_service.js.coffee +++ b/app/assets/javascripts/admin/product_import/services/product_import_service.js.coffee @@ -1,15 +1,15 @@ angular.module("admin.productImport").factory "ProductImportService", ($rootScope) -> new class ProductImportService - suppliers: {} + enterprises: {} resetTotal: 0 settings: {} - updateResetAbsent: (supplierId, resetCount, resetAbsent) -> + updateResetAbsent: (enterpriseId, resetCount, resetAbsent) -> if resetAbsent - @suppliers[supplierId] = resetCount + @enterprises[enterpriseId] = resetCount @resetTotal += resetCount else - @suppliers[supplierId] = null + @enterprises[enterpriseId] = null @resetTotal -= resetCount $rootScope.resetTotal = @resetTotal diff --git a/app/controllers/admin/product_import_controller.rb b/app/controllers/admin/product_import_controller.rb index e8ae8951e9..aa55fac115 100644 --- a/app/controllers/admin/product_import_controller.rb +++ b/app/controllers/admin/product_import_controller.rb @@ -90,7 +90,7 @@ module Admin { filepath: @filepath, item_count: @importer.item_count, - supplier_product_counts: @importer.supplier_products, + enterprise_product_counts: @importer.enterprise_products, import_url: main_app.admin_product_import_process_async_path, save_url: main_app.admin_product_import_save_async_path, reset_url: main_app.admin_product_import_reset_async_path, diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 22bae53907..66c0d40d8f 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -5,8 +5,8 @@ module ProductImport class EntryProcessor attr_reader :inventory_created, :inventory_updated, :products_created, - :variants_created, :variants_updated, :supplier_products, - :total_supplier_products, :products_reset_count + :variants_created, :variants_updated, :enterprise_products, + :total_enterprise_products, :products_reset_count def initialize(importer, validator, import_settings, spreadsheet_data, editable_enterprises, import_time, updated_ids) @importer = importer @@ -23,8 +23,8 @@ module ProductImport @variants_created = 0 @variants_updated = 0 @products_reset_count = 0 - @supplier_products = {} - @total_supplier_products = 0 + @enterprise_products = {} + @total_enterprise_products = 0 end def save_all(entries) @@ -40,24 +40,24 @@ module ProductImport end def count_existing_items - @spreadsheet_data.suppliers_index.each do |_supplier_name, attrs| - supplier_id = attrs[:id] - next unless supplier_id && permission_by_id?(supplier_id) + @spreadsheet_data.enterprises_index.each do |_enterprise_name, attrs| + enterprise_id = attrs[:id] + next unless enterprise_id && permission_by_id?(enterprise_id) products_count = if settings.importing_into_inventory? - VariantOverride.where('variant_overrides.hub_id IN (?)', supplier_id).count + VariantOverride.where('variant_overrides.hub_id IN (?)', enterprise_id).count else Spree::Variant. not_deleted. not_master. joins(:product). - where('spree_products.supplier_id IN (?)', supplier_id). + where('spree_products.supplier_id IN (?)', enterprise_id). count end - @supplier_products[supplier_id] = products_count - @total_supplier_products += products_count + @enterprise_products[enterprise_id] = products_count + @total_enterprise_products += products_count end end @@ -88,8 +88,8 @@ module ProductImport @products_created + @variants_created + @variants_updated + @inventory_created + @inventory_updated end - def permission_by_id?(supplier_id) - @editable_enterprises.value?(Integer(supplier_id)) + def permission_by_id?(enterprise_id) + @editable_enterprises.value?(Integer(enterprise_id)) end private @@ -122,7 +122,7 @@ module ProductImport end def import_into_inventory?(entry) - entry.supplier_id && settings.importing_into_inventory? + entry.enterprise_id && settings.importing_into_inventory? end def save_new_inventory_item(entry) @@ -158,8 +158,8 @@ module ProductImport # If we've already added a new product with these attributes # from this spreadsheet, mark this entry as a new variant with # the new product id, as this is a now variant of that product... - if @already_created[entry.supplier_id] && @already_created[entry.supplier_id][entry.name] - product_id = @already_created[entry.supplier_id][entry.name] + if @already_created[entry.enterprise_id] && @already_created[entry.enterprise_id][entry.name] + product_id = @already_created[entry.enterprise_id][entry.name] @validator.mark_as_new_variant(entry, product_id) return end @@ -176,7 +176,7 @@ module ProductImport @importer.errors.add("#{I18n.t('admin.product_import.model.line')} #{line_number}:", product.errors.full_messages) end - @already_created[entry.supplier_id] = { entry.name => product.id } + @already_created[entry.enterprise_id] = { entry.name => product.id } end def save_variant(entry) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index 700c1e4308..a4574bffda 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -27,10 +27,10 @@ module ProductImport def validate_all(entries) entries.each do |entry| - supplier_validation(entry) + enterprise_validation(entry) unit_fields_validation(entry) - next if entry.supplier_id.blank? + next if entry.enterprise_id.blank? if import_into_inventory? producer_validation(entry) @@ -59,37 +59,37 @@ module ProductImport private - def supplier_validation(entry) + def enterprise_validation(entry) return if name_presence_error entry return if enterprise_not_found_error entry return if permissions_error entry return if primary_producer_error entry - entry.supplier_id = @spreadsheet_data.suppliers_index[entry.supplier][:id] + entry.enterprise_id = @spreadsheet_data.enterprises_index[entry.enterprise][:id] end def name_presence_error(entry) - return if entry.supplier.present? - mark_as_invalid(entry, attribute: "supplier", error: I18n.t(:error_required)) + return if entry.enterprise.present? + mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_required)) true end def enterprise_not_found_error(entry) - return if @spreadsheet_data.suppliers_index[entry.supplier][:id] - mark_as_invalid(entry, attribute: "supplier", error: I18n.t(:error_not_found_in_database, name: entry.supplier)) + return if @spreadsheet_data.enterprises_index[entry.enterprise][:id] + mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_not_found_in_database, name: entry.enterprise)) true end def permissions_error(entry) - return if permission_by_name?(entry.supplier) - mark_as_invalid(entry, attribute: "supplier", error: I18n.t(:error_no_permission_for_enterprise, name: entry.supplier)) + return if permission_by_name?(entry.enterprise) + mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_no_permission_for_enterprise, name: entry.enterprise)) true end def primary_producer_error(entry) return if import_into_inventory? - return if @spreadsheet_data.suppliers_index[entry.supplier][:is_primary_producer] - mark_as_invalid(entry, attribute: "supplier", error: I18n.t(:error_not_primary_producer, name: entry.supplier)) + return if @spreadsheet_data.enterprises_index[entry.enterprise][:is_primary_producer] + mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_not_primary_producer, name: entry.enterprise)) true end @@ -126,7 +126,7 @@ module ProductImport return end - unless inventory_permission?(entry.supplier_id, @spreadsheet_data.producers_index[producer_name]) + unless inventory_permission?(entry.enterprise_id, @spreadsheet_data.producers_index[producer_name]) mark_as_invalid(entry, attribute: "producer", error: "\"#{producer_name}\": #{I18n.t('admin.product_import.model.inventory_no_permission')}") return end @@ -186,7 +186,7 @@ module ProductImport end def product_validation(entry) - products = Spree::Product.where(supplier_id: entry.supplier_id, name: entry.name, deleted_at: nil) + products = Spree::Product.where(supplier_id: entry.enterprise_id, name: entry.name, deleted_at: nil) if products.empty? mark_as_new_product(entry) @@ -222,7 +222,7 @@ module ProductImport if existing_variant.valid? entry.product_object = existing_variant entry.validates_as = 'existing_variant' unless entry.errors? - updates_count_per_supplier(entry.supplier_id) unless entry.errors? + updates_count_per_enterprise(entry.enterprise_id) unless entry.errors? else mark_as_invalid(entry, product_validations: existing_variant.errors) end @@ -243,16 +243,16 @@ module ProductImport existing_product.public_send(attribute).blank? && entry.public_send(attribute).blank? end - def permission_by_name?(supplier_name) - @editable_enterprises.key?(supplier_name) + def permission_by_name?(enterprise_name) + @editable_enterprises.key?(enterprise_name) end - def permission_by_id?(supplier_id) - @editable_enterprises.value?(Integer(supplier_id)) + def permission_by_id?(enterprise_id) + @editable_enterprises.value?(Integer(enterprise_id)) end - def inventory_permission?(supplier_id, producer_id) - @current_user.admin? || ( @inventory_permissions[supplier_id] && @inventory_permissions[supplier_id].include?(producer_id) ) + def inventory_permission?(enterprise_id, producer_id) + @current_user.admin? || ( @inventory_permissions[enterprise_id] && @inventory_permissions[enterprise_id].include?(producer_id) ) end def mark_as_invalid(entry, options = {}) @@ -273,9 +273,9 @@ module ProductImport end def create_inventory_item(entry, existing_variant) - existing_variant_override = VariantOverride.where(variant_id: existing_variant.id, hub_id: entry.supplier_id).first + existing_variant_override = VariantOverride.where(variant_id: existing_variant.id, hub_id: entry.enterprise_id).first - variant_override = existing_variant_override || VariantOverride.new(variant_id: existing_variant.id, hub_id: entry.supplier_id) + 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')) @@ -287,18 +287,18 @@ module ProductImport if variant_override.id entry.validates_as = 'existing_inventory_item' entry.product_object = variant_override - updates_count_per_supplier(entry.supplier_id) unless entry.errors? + updates_count_per_enterprise(entry.enterprise_id) unless entry.errors? else entry.validates_as = 'new_inventory_item' entry.product_object = variant_override end end - def updates_count_per_supplier(supplier_id) - if @reset_counts[supplier_id] && @reset_counts[supplier_id][:updates_count] - @reset_counts[supplier_id][:updates_count] += 1 + def updates_count_per_enterprise(enterprise_id) + if @reset_counts[enterprise_id] && @reset_counts[enterprise_id][:updates_count] + @reset_counts[enterprise_id][:updates_count] += 1 else - @reset_counts[supplier_id] = { updates_count: 1 } + @reset_counts[enterprise_id] = { updates_count: 1 } end end diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 6cabcc9e2f..87ae50f73b 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -65,27 +65,27 @@ module ProductImport end def reset_counts - # Return indexed data about existing product count, reset count, and updates count per supplier - @reset_counts.each do |supplier_id, values| + # Return indexed data about existing product count, reset count, and updates count per enterprise + @reset_counts.each do |enterprise_id, values| values[:updates_count] = 0 if values[:updates_count].blank? if values[:updates_count] && values[:existing_products] - @reset_counts[supplier_id][:reset_count] = values[:existing_products] - values[:updates_count] + @reset_counts[enterprise_id][:reset_count] = values[:existing_products] - values[:updates_count] end end @reset_counts end - def suppliers_index - @spreadsheet_data.suppliers_index + def enterprises_index + @spreadsheet_data.enterprises_index end - def supplier_products - @processor.andand.supplier_products + def enterprise_products + @processor.andand.enterprise_products end - def total_supplier_products - @processor.total_supplier_products + def total_enterprise_products + @processor.total_enterprise_products end def all_entries @@ -165,8 +165,8 @@ module ProductImport @processor.reset_absent_items end - def permission_by_id?(supplier_id) - @editable_enterprises.value?(Integer(supplier_id)) + def permission_by_id?(enterprise_id) + @editable_enterprises.value?(Integer(enterprise_id)) end private diff --git a/app/models/product_import/settings.rb b/app/models/product_import/settings.rb index 4a0df965cb..c0028138f8 100644 --- a/app/models/product_import/settings.rb +++ b/app/models/product_import/settings.rb @@ -6,8 +6,8 @@ module ProductImport def defaults(entry) @import_settings.key?(:settings) && - settings[entry.supplier_id.to_s] && - settings[entry.supplier_id.to_s]['defaults'] + settings[entry.enterprise_id.to_s] && + settings[entry.enterprise_id.to_s]['defaults'] end def settings diff --git a/app/models/product_import/spreadsheet_data.rb b/app/models/product_import/spreadsheet_data.rb index 04203606b0..de4dc65ab4 100644 --- a/app/models/product_import/spreadsheet_data.rb +++ b/app/models/product_import/spreadsheet_data.rb @@ -11,8 +11,8 @@ module ProductImport @entries = entries end - def suppliers_index - @suppliers_index || create_suppliers_index + def enterprises_index + @enterprises_index || create_enterprises_index end def producers_index @@ -33,15 +33,15 @@ module ProductImport private - def create_suppliers_index - @suppliers_index = {} + def create_enterprises_index + @enterprises_index = {} @entries.each do |entry| - supplier_name = entry.supplier - next if @suppliers_index.key? supplier_name - enterprise = Enterprise.find_by_name(supplier_name, select: 'id, name, is_primary_producer') - @suppliers_index[supplier_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) } + enterprise_name = entry.enterprise + next if @enterprises_index.key? enterprise_name + enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, name, is_primary_producer') + @enterprises_index[enterprise_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) } end - @suppliers_index + @enterprises_index end def create_producers_index diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb index fed45a6645..f74e11e8b8 100644 --- a/app/models/product_import/spreadsheet_entry.rb +++ b/app/models/product_import/spreadsheet_entry.rb @@ -11,7 +11,7 @@ module ProductImport attr_accessor :line_number, :valid, :validates_as, :product_object, :product_validations, :on_hand_nil, :has_overrides, :units, :unscaled_units, :unit_type, :tax_category, :shipping_category - attr_accessor :id, :product_id, :producer, :producer_id, :supplier, :supplier_id, :name, :display_name, :sku, + attr_accessor :id, :product_id, :producer, :producer_id, :enterprise, :enterprise_id, :name, :display_name, :sku, :unit_value, :unit_description, :variant_unit, :variant_unit_scale, :variant_unit_name, :display_as, :category, :primary_taxon_id, :price, :on_hand, :count_on_hand, :on_demand, :tax_category_id, :shipping_category_id, :description, :import_date @@ -77,7 +77,7 @@ module ProductImport end def non_display_attributes - ['id', 'product_id', 'unscaled_units', 'variant_id', 'supplier_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] + ['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] end def non_product_attributes diff --git a/app/views/admin/product_import/_import_options.html.haml b/app/views/admin/product_import/_import_options.html.haml index 2debc9a86c..c5c3e32c38 100644 --- a/app/views/admin/product_import/_import_options.html.haml +++ b/app/views/admin/product_import/_import_options.html.haml @@ -1,7 +1,7 @@ %h5= t('admin.product_import.import.options_and_defaults') %br -- @importer.suppliers_index.each do |name, attrs| +- @importer.enterprises_index.each do |name, attrs| - if name and attrs[:id] and @importer.permission_by_id?(attrs[:id]) %div.panel-section.import-settings %div.panel-header{ng: {click: 'togglePanel()', class: '{active: active}'}} @@ -32,7 +32,7 @@ %i.fa.fa-warning %div.header-description = t('admin.product_import.import.no_name') - %span.header-error= " - #{t('admin.product_import.import.blank_supplier')}" + %span.header-error= " - #{t('admin.product_import.import.blank_enterprise')}" %br.panels.clearfix %br diff --git a/config/locales/en.yml b/config/locales/en.yml index ea6a746d97..a6fcd94028 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -554,7 +554,7 @@ en: no_permission: you do not have permission to manage this enterprise not_found: enterprise could not be found in database no_name: No name - blank_supplier: some products have blank supplier name + blank_enterprise: some products do not have an enterprise defined reset_absent?: Reset absent products reset_absent_tip: Set stock to zero for all exiting products not present in the file overwrite_all: Overwrite all From 5c2b5377bc4d636a9d32a4ffa48f23cfefb79e39 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 2 Oct 2018 13:15:18 +0100 Subject: [PATCH 2/9] Assign supplier_id when saving new product to products list --- app/models/product_import/entry_processor.rb | 1 + app/models/product_import/entry_validator.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 66c0d40d8f..68ecf0f19d 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -166,6 +166,7 @@ module ProductImport product = Spree::Product.new product.assign_attributes(entry.attributes.except('id')) + product.supplier_id = entry.enterprise_id assign_defaults(product, entry) if product.save diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index a4574bffda..71d1497da7 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -207,6 +207,7 @@ module ProductImport def mark_as_new_product(entry) new_product = Spree::Product.new new_product.assign_attributes(entry.attributes.except('id')) + new_product.supplier_id = entry.enterprise_id if new_product.valid? entry.validates_as = 'new_product' unless entry.errors? From 2c9dc8899a1e58f9e2f1b655839213612a3b7a95 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 2 Oct 2018 13:16:55 +0100 Subject: [PATCH 3/9] Update fields in specs --- spec/features/admin/product_import_spec.rb | 16 +++++----- spec/models/product_importer_spec.rb | 36 +++++++++++----------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/spec/features/admin/product_import_spec.rb b/spec/features/admin/product_import_spec.rb index 4d3480d32d..5fb763c918 100644 --- a/spec/features/admin/product_import_spec.rb +++ b/spec/features/admin/product_import_spec.rb @@ -33,7 +33,7 @@ feature "Product Import", js: true do it "validates entries and saves them if they are all valid and allows viewing new items in Bulk Products" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -76,7 +76,7 @@ feature "Product Import", js: true do it "displays info about invalid entries but no save button if all items are invalid" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Bad Carrots", "Unkown Enterprise", "Mouldy vegetables", "666", "3.20", "", "g"] csv << ["Bad Potatoes", "", "Vegetables", "6", "6", "6", ""] end @@ -100,7 +100,7 @@ feature "Product Import", js: true do it "handles saving of named tax and shipping categories" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.name, shipping_category.name] end File.write('/tmp/test.csv', csv_data) @@ -129,7 +129,7 @@ feature "Product Import", js: true do it "records a timestamp on import that can be viewed and filtered under Bulk Edit Products" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -176,7 +176,7 @@ feature "Product Import", js: true do it "can reset product stock to zero for products not present in the CSV" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "500", "3.20", "500", "g"] end File.write('/tmp/test.csv', csv_data) @@ -203,7 +203,7 @@ feature "Product Import", js: true do it "can save a new product and variant of that product at the same time, add variant to existing product" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"] csv << ["Beans", "User Enterprise", "Vegetables", "7", "2.50", "250", "g", nil] @@ -241,7 +241,7 @@ feature "Product Import", js: true do it "can import items into inventory" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "producer", "category", "on_hand", "price", "units"] + csv << ["name", "enterprise", "producer", "category", "on_hand", "price", "units"] csv << ["Beans", "Another Enterprise", "User Enterprise", "Vegetables", "5", "3.20", "500"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "Vegetables", "6", "6.50", "500"] csv << ["Cabbage", "Another Enterprise", "User Enterprise", "Vegetables", "2001", "1.50", "500"] @@ -338,7 +338,7 @@ feature "Product Import", js: true do it "only allows product import into enterprises the user is permitted to manage" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 538532471e..efd87d434e 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -45,7 +45,7 @@ describe ProductImport::ProductImporter do describe "importing products from a spreadsheet" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", "", ""] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "2", "kg", "", ""] csv << ["Pea Soup", "User Enterprise", "Vegetables", "8", "5.50", "750", "ml", "", "0"] @@ -135,7 +135,7 @@ describe ProductImport::ProductImporter do describe "when uploading a spreadsheet with some invalid entries" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Good Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Bad Potatoes", "", "Vegetables", "6", "6.50", "1", ""] end @@ -176,7 +176,7 @@ describe ProductImport::ProductImporter do describe "when enterprises are not valid" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Product 1", "Non-existent Enterprise", "Vegetables", "5", "5.50", "500", "g"] csv << ["Product 2", "Non-Producer", "Vegetables", "5", "5.50", "500", "g"] end @@ -191,15 +191,15 @@ describe ProductImport::ProductImporter do @importer.validate_entries entries = JSON.parse(@importer.entries_json) - expect(entries['2']['errors']['supplier']).to include "not found in database" - expect(entries['3']['errors']['supplier']).to include "not enabled as a producer" + expect(entries['2']['errors']['enterprise']).to include "not found in database" + expect(entries['3']['errors']['enterprise']).to include "not enabled as a producer" end end describe "adding new variants to existing products and updating exiting products" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "5", "5.50", "500", "g", "Preexisting Banana"] csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "6", "3.50", "500", "g", "Emergent Coffee"] end @@ -245,7 +245,7 @@ describe ProductImport::ProductImporter do describe "adding new product and sub-variant at the same time" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"] end @@ -289,7 +289,7 @@ describe ProductImport::ProductImporter do describe "updating various fields" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"] csv << ["Beetroot", "And Another Enterprise", "Vegetables", "5", "3.50", "500", "g", "0", nil] csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "g", "1", "TOMS"] end @@ -331,7 +331,7 @@ describe ProductImport::ProductImporter do describe "updating non-updatable fields on existing products" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Beetroot", "And Another Enterprise", "Meat", "5", "3.50", "500", "g"] csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "Kg"] end @@ -358,7 +358,7 @@ describe ProductImport::ProductImporter do describe "when more than one product of the same name already exists with multiple variants each" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "enterprise", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Oats", "User Enterprise", "Cereal", "", "50", "3.50", "500", "g", "Rolled Oats"] # Update csv << ["Oats", "User Enterprise", "Cereal", "", "80", "3.75", "500", "g", "Flaked Oats"] # Update csv << ["Oats", "User Enterprise", "Cereal", "", "60", "5.50", "500", "g", "Magic Oats"] # Add @@ -398,7 +398,7 @@ describe ProductImport::ProductImporter do describe "when importer processes create and update across multiple stages" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Bag of Oats", "User Enterprise", "Cereal", "60", "5.50", "500", "g", "Magic Oats"] # Add csv << ["Bag of Oats", "User Enterprise", "Cereal", "70", "8.50", "500", "g", "French Oats"] # Add csv << ["Bag of Oats", "User Enterprise", "Cereal", "80", "9.50", "500", "g", "Organic Oats"] # Add @@ -467,7 +467,7 @@ describe ProductImport::ProductImporter do describe "importing items into inventory" do before do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "producer", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "producer", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "Another Enterprise", "User Enterprise", "5", "3.20", "500", "g"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "6", "6.50", "500", "g"] csv << ["Cabbage", "Another Enterprise", "User Enterprise", "2001", "1.50", "500", "g"] @@ -517,7 +517,7 @@ describe ProductImport::ProductImporter do it "only allows product import into enterprises the user is permitted to manage" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -545,7 +545,7 @@ describe ProductImport::ProductImporter do it "allows creating inventories for producers that a user's hub has permission for" do csv_data = CSV.generate do |csv| - csv << ["name", "producer", "supplier", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "enterprise", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "User Enterprise", "Another Enterprise", "777", "3.20", "500", "g"] end File.write('/tmp/test-m.csv', csv_data) @@ -572,7 +572,7 @@ describe ProductImport::ProductImporter do it "does not allow creating inventories for producers that a user's hubs don't have permission for" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "User Enterprise", "5", "3.20", "500", "g"] csv << ["Sprouts", "User Enterprise", "6", "6.50", "500", "g"] end @@ -601,7 +601,7 @@ describe ProductImport::ProductImporter do it "can reset all products for an enterprise that are not present in the uploaded file to zero stock" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Beans", "User Enterprise", "Vegetables", "6", "6.50", "500", "g"] end @@ -640,7 +640,7 @@ describe ProductImport::ProductImporter do it "can reset all inventory items for an enterprise that are not present in the uploaded file to zero stock" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "producer", "on_hand", "price", "units", "unit_type"] + csv << ["name", "enterprise", "producer", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "Another Enterprise", "User Enterprise", "6", "3.20", "500", "g"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "7", "6.50", "500", "g"] end @@ -681,7 +681,7 @@ describe ProductImport::ProductImporter do it "can overwrite fields with selected defaults when importing to product list" do csv_data = CSV.generate do |csv| - csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"] + csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.id, ""] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg", "", ""] end From 1c85de6f5835fd6a025e0cfb25a8f7e02117e560 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 2 Oct 2018 15:06:37 +0100 Subject: [PATCH 4/9] Use producer and distributor for field names --- app/models/product_import/entry_processor.rb | 2 +- app/models/product_import/entry_validator.rb | 22 ++++++++---- app/models/product_import/product_importer.rb | 2 +- app/models/product_import/spreadsheet_data.rb | 9 +++-- .../product_import/spreadsheet_entry.rb | 6 ++-- spec/features/admin/product_import_spec.rb | 16 ++++----- spec/models/product_importer_spec.rb | 36 +++++++++---------- 7 files changed, 54 insertions(+), 39 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 68ecf0f19d..f3c5b0a8e5 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -166,7 +166,7 @@ module ProductImport product = Spree::Product.new product.assign_attributes(entry.attributes.except('id')) - product.supplier_id = entry.enterprise_id + product.supplier_id = entry.producer_id assign_defaults(product, entry) if product.save diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index 71d1497da7..c53b4fbe2c 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -27,6 +27,7 @@ module ProductImport def validate_all(entries) entries.each do |entry| + assign_enterprise_field(entry) enterprise_validation(entry) unit_fields_validation(entry) @@ -44,6 +45,14 @@ module ProductImport end end + def assign_enterprise_field(entry) + entry.enterprise = entry.public_send(enterprise_field) + end + + def enterprise_field + import_into_inventory? ? :distributor : :producer + end + def mark_as_new_variant(entry, product_id) new_variant = Spree::Variant.new(entry.attributes.except('id', 'product_id')) new_variant.product_id = product_id @@ -66,30 +75,31 @@ module ProductImport return if primary_producer_error entry entry.enterprise_id = @spreadsheet_data.enterprises_index[entry.enterprise][:id] + entry.public_send "#{enterprise_field}_id=", @spreadsheet_data.enterprises_index[entry.enterprise][:id] end def name_presence_error(entry) return if entry.enterprise.present? - mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_required)) + mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_required)) true end def enterprise_not_found_error(entry) return if @spreadsheet_data.enterprises_index[entry.enterprise][:id] - mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_not_found_in_database, name: entry.enterprise)) + mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_found_in_database, name: entry.enterprise)) true end def permissions_error(entry) return if permission_by_name?(entry.enterprise) - mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_no_permission_for_enterprise, name: entry.enterprise)) + mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_no_permission_for_enterprise, name: entry.enterprise)) true end def primary_producer_error(entry) return if import_into_inventory? return if @spreadsheet_data.enterprises_index[entry.enterprise][:is_primary_producer] - mark_as_invalid(entry, attribute: "enterprise", error: I18n.t(:error_not_primary_producer, name: entry.enterprise)) + mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_primary_producer, name: entry.enterprise)) true end @@ -207,7 +217,7 @@ module ProductImport def mark_as_new_product(entry) new_product = Spree::Product.new new_product.assign_attributes(entry.attributes.except('id')) - new_product.supplier_id = entry.enterprise_id + new_product.supplier_id = entry.producer_id if new_product.valid? entry.validates_as = 'new_product' unless entry.errors? @@ -262,7 +272,7 @@ module ProductImport end def import_into_inventory? - @import_settings[:settings]['import_into'] == 'inventories' + @import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories' end def validate_inventory_item(entry, variant_override) diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 87ae50f73b..35a490c8d6 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -180,7 +180,7 @@ module ProductImport build_entries end - @spreadsheet_data = SpreadsheetData.new(@entries) + @spreadsheet_data = SpreadsheetData.new(@entries, @import_settings) @validator = EntryValidator.new(@current_user, @import_time, @spreadsheet_data, @editable_enterprises, @inventory_permissions, @reset_counts, @import_settings) @processor = EntryProcessor.new(self, @validator, @import_settings, @spreadsheet_data, @editable_enterprises, @import_time, @updated_ids) diff --git a/app/models/product_import/spreadsheet_data.rb b/app/models/product_import/spreadsheet_data.rb index de4dc65ab4..8903944284 100644 --- a/app/models/product_import/spreadsheet_data.rb +++ b/app/models/product_import/spreadsheet_data.rb @@ -7,8 +7,9 @@ module ProductImport class SpreadsheetData - def initialize(entries) + def initialize(entries, import_settings) @entries = entries + @import_settings = import_settings end def enterprises_index @@ -33,10 +34,14 @@ module ProductImport private + def import_into_inventory? + @import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories' + end + def create_enterprises_index @enterprises_index = {} @entries.each do |entry| - enterprise_name = entry.enterprise + enterprise_name = import_into_inventory? ? entry.distributor : entry.producer next if @enterprises_index.key? enterprise_name enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, name, is_primary_producer') @enterprises_index[enterprise_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) } diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb index f74e11e8b8..dcfb35e65f 100644 --- a/app/models/product_import/spreadsheet_entry.rb +++ b/app/models/product_import/spreadsheet_entry.rb @@ -11,10 +11,10 @@ module ProductImport attr_accessor :line_number, :valid, :validates_as, :product_object, :product_validations, :on_hand_nil, :has_overrides, :units, :unscaled_units, :unit_type, :tax_category, :shipping_category - attr_accessor :id, :product_id, :producer, :producer_id, :enterprise, :enterprise_id, :name, :display_name, :sku, + attr_accessor :id, :product_id, :producer, :producer_id, :distributor, :distributor_id, :name, :display_name, :sku, :unit_value, :unit_description, :variant_unit, :variant_unit_scale, :variant_unit_name, :display_as, :category, :primary_taxon_id, :price, :on_hand, :count_on_hand, :on_demand, - :tax_category_id, :shipping_category_id, :description, :import_date + :tax_category_id, :shipping_category_id, :description, :import_date, :enterprise, :enterprise_id def initialize(attrs) @validates_as = '' @@ -77,7 +77,7 @@ module ProductImport end def non_display_attributes - ['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] + ['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise', 'enterprise_id', 'producer_id', 'distributor_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] end def non_product_attributes diff --git a/spec/features/admin/product_import_spec.rb b/spec/features/admin/product_import_spec.rb index 5fb763c918..3a337d93dd 100644 --- a/spec/features/admin/product_import_spec.rb +++ b/spec/features/admin/product_import_spec.rb @@ -33,7 +33,7 @@ feature "Product Import", js: true do it "validates entries and saves them if they are all valid and allows viewing new items in Bulk Products" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -76,7 +76,7 @@ feature "Product Import", js: true do it "displays info about invalid entries but no save button if all items are invalid" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Bad Carrots", "Unkown Enterprise", "Mouldy vegetables", "666", "3.20", "", "g"] csv << ["Bad Potatoes", "", "Vegetables", "6", "6", "6", ""] end @@ -100,7 +100,7 @@ feature "Product Import", js: true do it "handles saving of named tax and shipping categories" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.name, shipping_category.name] end File.write('/tmp/test.csv', csv_data) @@ -129,7 +129,7 @@ feature "Product Import", js: true do it "records a timestamp on import that can be viewed and filtered under Bulk Edit Products" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -176,7 +176,7 @@ feature "Product Import", js: true do it "can reset product stock to zero for products not present in the CSV" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "500", "3.20", "500", "g"] end File.write('/tmp/test.csv', csv_data) @@ -203,7 +203,7 @@ feature "Product Import", js: true do it "can save a new product and variant of that product at the same time, add variant to existing product" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"] csv << ["Beans", "User Enterprise", "Vegetables", "7", "2.50", "250", "g", nil] @@ -241,7 +241,7 @@ feature "Product Import", js: true do it "can import items into inventory" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "producer", "category", "on_hand", "price", "units"] + csv << ["name", "distributor", "producer", "category", "on_hand", "price", "units"] csv << ["Beans", "Another Enterprise", "User Enterprise", "Vegetables", "5", "3.20", "500"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "Vegetables", "6", "6.50", "500"] csv << ["Cabbage", "Another Enterprise", "User Enterprise", "Vegetables", "2001", "1.50", "500"] @@ -338,7 +338,7 @@ feature "Product Import", js: true do it "only allows product import into enterprises the user is permitted to manage" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index efd87d434e..f05816e538 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -45,7 +45,7 @@ describe ProductImport::ProductImporter do describe "importing products from a spreadsheet" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", "", ""] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "2", "kg", "", ""] csv << ["Pea Soup", "User Enterprise", "Vegetables", "8", "5.50", "750", "ml", "", "0"] @@ -135,7 +135,7 @@ describe ProductImport::ProductImporter do describe "when uploading a spreadsheet with some invalid entries" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Good Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Bad Potatoes", "", "Vegetables", "6", "6.50", "1", ""] end @@ -176,7 +176,7 @@ describe ProductImport::ProductImporter do describe "when enterprises are not valid" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Product 1", "Non-existent Enterprise", "Vegetables", "5", "5.50", "500", "g"] csv << ["Product 2", "Non-Producer", "Vegetables", "5", "5.50", "500", "g"] end @@ -191,15 +191,15 @@ describe ProductImport::ProductImporter do @importer.validate_entries entries = JSON.parse(@importer.entries_json) - expect(entries['2']['errors']['enterprise']).to include "not found in database" - expect(entries['3']['errors']['enterprise']).to include "not enabled as a producer" + expect(entries['2']['errors']['producer']).to include "not found in database" + expect(entries['3']['errors']['producer']).to include "not enabled as a producer" end end describe "adding new variants to existing products and updating exiting products" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "5", "5.50", "500", "g", "Preexisting Banana"] csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "6", "3.50", "500", "g", "Emergent Coffee"] end @@ -245,7 +245,7 @@ describe ProductImport::ProductImporter do describe "adding new product and sub-variant at the same time" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"] end @@ -289,7 +289,7 @@ describe ProductImport::ProductImporter do describe "updating various fields" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"] csv << ["Beetroot", "And Another Enterprise", "Vegetables", "5", "3.50", "500", "g", "0", nil] csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "g", "1", "TOMS"] end @@ -331,7 +331,7 @@ describe ProductImport::ProductImporter do describe "updating non-updatable fields on existing products" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Beetroot", "And Another Enterprise", "Meat", "5", "3.50", "500", "g"] csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "Kg"] end @@ -358,7 +358,7 @@ describe ProductImport::ProductImporter do describe "when more than one product of the same name already exists with multiple variants each" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "producer", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Oats", "User Enterprise", "Cereal", "", "50", "3.50", "500", "g", "Rolled Oats"] # Update csv << ["Oats", "User Enterprise", "Cereal", "", "80", "3.75", "500", "g", "Flaked Oats"] # Update csv << ["Oats", "User Enterprise", "Cereal", "", "60", "5.50", "500", "g", "Magic Oats"] # Add @@ -398,7 +398,7 @@ describe ProductImport::ProductImporter do describe "when importer processes create and update across multiple stages" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "display_name"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"] csv << ["Bag of Oats", "User Enterprise", "Cereal", "60", "5.50", "500", "g", "Magic Oats"] # Add csv << ["Bag of Oats", "User Enterprise", "Cereal", "70", "8.50", "500", "g", "French Oats"] # Add csv << ["Bag of Oats", "User Enterprise", "Cereal", "80", "9.50", "500", "g", "Organic Oats"] # Add @@ -467,7 +467,7 @@ describe ProductImport::ProductImporter do describe "importing items into inventory" do before do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "producer", "on_hand", "price", "units", "unit_type"] + csv << ["name", "distributor", "producer", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "Another Enterprise", "User Enterprise", "5", "3.20", "500", "g"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "6", "6.50", "500", "g"] csv << ["Cabbage", "Another Enterprise", "User Enterprise", "2001", "1.50", "500", "g"] @@ -517,7 +517,7 @@ describe ProductImport::ProductImporter do it "only allows product import into enterprises the user is permitted to manage" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"] end @@ -545,7 +545,7 @@ describe ProductImport::ProductImporter do it "allows creating inventories for producers that a user's hub has permission for" do csv_data = CSV.generate do |csv| - csv << ["name", "producer", "enterprise", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "distributor", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "User Enterprise", "Another Enterprise", "777", "3.20", "500", "g"] end File.write('/tmp/test-m.csv', csv_data) @@ -572,7 +572,7 @@ describe ProductImport::ProductImporter do it "does not allow creating inventories for producers that a user's hubs don't have permission for" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "User Enterprise", "5", "3.20", "500", "g"] csv << ["Sprouts", "User Enterprise", "6", "6.50", "500", "g"] end @@ -601,7 +601,7 @@ describe ProductImport::ProductImporter do it "can reset all products for an enterprise that are not present in the uploaded file to zero stock" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"] csv << ["Beans", "User Enterprise", "Vegetables", "6", "6.50", "500", "g"] end @@ -640,7 +640,7 @@ describe ProductImport::ProductImporter do it "can reset all inventory items for an enterprise that are not present in the uploaded file to zero stock" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "producer", "on_hand", "price", "units", "unit_type"] + csv << ["name", "distributor", "producer", "on_hand", "price", "units", "unit_type"] csv << ["Beans", "Another Enterprise", "User Enterprise", "6", "3.20", "500", "g"] csv << ["Sprouts", "Another Enterprise", "User Enterprise", "7", "6.50", "500", "g"] end @@ -681,7 +681,7 @@ describe ProductImport::ProductImporter do it "can overwrite fields with selected defaults when importing to product list" do csv_data = CSV.generate do |csv| - csv << ["name", "enterprise", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"] + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"] csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.id, ""] csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg", "", ""] end From bd4f0507cee458a51893600adf0c01bc8bcdc25d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 8 Oct 2018 17:00:55 +0100 Subject: [PATCH 5/9] Adjustments for new Settings and ResetAbsent classes --- app/models/product_import/inventory_reset_strategy.rb | 10 +++++----- app/models/product_import/products_reset_strategy.rb | 10 +++++----- spec/models/product_import/settings_spec.rb | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/models/product_import/inventory_reset_strategy.rb b/app/models/product_import/inventory_reset_strategy.rb index c22cb6218a..a8647ce250 100644 --- a/app/models/product_import/inventory_reset_strategy.rb +++ b/app/models/product_import/inventory_reset_strategy.rb @@ -4,10 +4,10 @@ module ProductImport @excluded_items_ids = excluded_items_ids end - def reset(supplier_ids) - @supplier_ids = supplier_ids + def reset(enterprise_ids) + @enterprise_ids = enterprise_ids - if supplier_ids.present? + if enterprise_ids.present? relation.update_all(count_on_hand: 0) else 0 @@ -16,10 +16,10 @@ module ProductImport private - attr_reader :excluded_items_ids, :supplier_ids + attr_reader :excluded_items_ids, :enterprise_ids def relation - relation = VariantOverride.where(hub_id: supplier_ids) + relation = VariantOverride.where(hub_id: enterprise_ids) return relation if excluded_items_ids.blank? relation.where('id NOT IN (?)', excluded_items_ids) diff --git a/app/models/product_import/products_reset_strategy.rb b/app/models/product_import/products_reset_strategy.rb index 80dd6a448c..c1ce9af695 100644 --- a/app/models/product_import/products_reset_strategy.rb +++ b/app/models/product_import/products_reset_strategy.rb @@ -4,10 +4,10 @@ module ProductImport @excluded_items_ids = excluded_items_ids end - def reset(supplier_ids) - @supplier_ids = supplier_ids + def reset(enterprise_ids) + @enterprise_ids = enterprise_ids - if supplier_ids.present? + if enterprise_ids.present? relation.update_all(count_on_hand: 0) else 0 @@ -16,13 +16,13 @@ module ProductImport private - attr_reader :excluded_items_ids, :supplier_ids + attr_reader :excluded_items_ids, :enterprise_ids def relation relation = Spree::Variant .joins(:product) .where( - spree_products: { supplier_id: supplier_ids }, + spree_products: { supplier_id: enterprise_ids }, spree_variants: { is_master: false, deleted_at: nil } ) diff --git a/spec/models/product_import/settings_spec.rb b/spec/models/product_import/settings_spec.rb index a7f823b2d8..b485127f0b 100644 --- a/spec/models/product_import/settings_spec.rb +++ b/spec/models/product_import/settings_spec.rb @@ -15,7 +15,7 @@ describe ProductImport::Settings do context 'when there are settings' do let(:entry) do - instance_double(ProductImport::SpreadsheetEntry, supplier_id: 1) + instance_double(ProductImport::SpreadsheetEntry, enterprise_id: 1) end let(:import_settings) { { settings: {} } } From 8fcaa54de7c2371b25d38359ac5f7cfd72fe3ded Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 8 Oct 2018 17:06:37 +0100 Subject: [PATCH 6/9] Update CSV templates --- public/inventory_template.csv | 2 +- public/product_list_template.csv | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/inventory_template.csv b/public/inventory_template.csv index dd7329a89c..d52cecded6 100644 --- a/public/inventory_template.csv +++ b/public/inventory_template.csv @@ -1 +1 @@ -producer,supplier,name,display_name,units,unit_type,price,on_hand +producer,distributor,name,display_name,units,unit_type,price,on_hand diff --git a/public/product_list_template.csv b/public/product_list_template.csv index f97e4f80d7..6eeae3701e 100644 --- a/public/product_list_template.csv +++ b/public/product_list_template.csv @@ -1 +1 @@ -supplier,sku,name,display_name,category,units,unit_type,variant_unit_name,price,on_hand,available_on,on_demand,shipping_category,tax_category +producer,sku,name,display_name,category,units,unit_type,variant_unit_name,price,on_hand,available_on,on_demand,shipping_category,tax_category From 3cb43121d5e901b8bcf1d395694eb4fce47e38b0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 8 Oct 2018 17:41:27 +0100 Subject: [PATCH 7/9] Refactor long lines for new character limit --- app/models/product_import/entry_processor.rb | 25 ++++++-- app/models/product_import/entry_validator.rb | 63 ++++++++++++++----- app/models/product_import/product_importer.rb | 6 +- app/models/product_import/spreadsheet_data.rb | 11 +++- .../product_import/spreadsheet_entry.rb | 25 +++++--- 5 files changed, 98 insertions(+), 32 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index f3c5b0a8e5..d96a9d36c7 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -46,7 +46,10 @@ module ProductImport products_count = if settings.importing_into_inventory? - VariantOverride.where('variant_overrides.hub_id IN (?)', enterprise_id).count + VariantOverride.where( + 'variant_overrides.hub_id IN (?)', + enterprise_id + ).count else Spree::Variant. not_deleted. @@ -158,7 +161,9 @@ module ProductImport # If we've already added a new product with these attributes # from this spreadsheet, mark this entry as a new variant with # the new product id, as this is a now variant of that product... - if @already_created[entry.enterprise_id] && @already_created[entry.enterprise_id][entry.name] + if @already_created[entry.enterprise_id] && + @already_created[entry.enterprise_id][entry.name] + product_id = @already_created[entry.enterprise_id][entry.name] @validator.mark_as_new_variant(entry, product_id) return @@ -207,7 +212,10 @@ module ProductImport when 'overwrite_all' object.assign_attributes(attribute => setting['value']) when 'overwrite_empty' - if object.public_send(attribute).blank? || ((attribute == 'on_hand' || attribute == 'count_on_hand') && entry.on_hand_nil) + if object.public_send(attribute).blank? || + ((attribute == 'on_hand' || attribute == 'count_on_hand') && + entry.on_hand_nil) + object.assign_attributes(attribute => setting['value']) end end @@ -216,7 +224,10 @@ module ProductImport def display_in_inventory(variant_override, is_new = false) unless is_new - existing_item = InventoryItem.where(variant_id: variant_override.variant_id, enterprise_id: variant_override.hub_id).first + existing_item = InventoryItem.where( + variant_id: variant_override.variant_id, + enterprise_id: variant_override.hub_id + ).first if existing_item existing_item.assign_attributes(visible: true) @@ -225,7 +236,11 @@ module ProductImport end end - InventoryItem.new(variant_id: variant_override.variant_id, enterprise_id: variant_override.hub_id, visible: true).save + InventoryItem.new( + variant_id: variant_override.variant_id, + enterprise_id: variant_override.hub_id, + visible: true + ).save end def ensure_variant_updated(product, entry) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index c53b4fbe2c..b7f055e445 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -74,32 +74,50 @@ module ProductImport return if permissions_error entry return if primary_producer_error entry - entry.enterprise_id = @spreadsheet_data.enterprises_index[entry.enterprise][:id] - entry.public_send "#{enterprise_field}_id=", @spreadsheet_data.enterprises_index[entry.enterprise][:id] + entry.enterprise_id = + @spreadsheet_data.enterprises_index[entry.enterprise][:id] + + entry.public_send( + "#{enterprise_field}_id=", + @spreadsheet_data.enterprises_index[entry.enterprise][:id] + ) end def name_presence_error(entry) return if entry.enterprise.present? - mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_required)) + mark_as_invalid(entry, + attribute: enterprise_field, + error: I18n.t(:error_required)) true end def enterprise_not_found_error(entry) return if @spreadsheet_data.enterprises_index[entry.enterprise][:id] - mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_found_in_database, name: entry.enterprise)) + mark_as_invalid(entry, + attribute: enterprise_field, + error: I18n.t(:error_not_found_in_database, + name: entry.enterprise)) true end def permissions_error(entry) return if permission_by_name?(entry.enterprise) - mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_no_permission_for_enterprise, name: entry.enterprise)) + mark_as_invalid(entry, + attribute: enterprise_field, + error: I18n.t(:error_no_permission_for_enterprise, + name: entry.enterprise)) true end def primary_producer_error(entry) return if import_into_inventory? - return if @spreadsheet_data.enterprises_index[entry.enterprise][:is_primary_producer] - mark_as_invalid(entry, attribute: enterprise_field, error: I18n.t(:error_not_primary_producer, name: entry.enterprise)) + return if @spreadsheet_data. + enterprises_index[entry.enterprise][:is_primary_producer] + + mark_as_invalid(entry, + attribute: enterprise_field, + error: I18n.t(:error_not_primary_producer, + name: entry.enterprise)) true end @@ -136,7 +154,11 @@ module ProductImport return end - unless inventory_permission?(entry.enterprise_id, @spreadsheet_data.producers_index[producer_name]) + unless inventory_permission?( + entry.enterprise_id, + @spreadsheet_data.producers_index[producer_name] + ) + mark_as_invalid(entry, attribute: "producer", error: "\"#{producer_name}\": #{I18n.t('admin.product_import.model.inventory_no_permission')}") return end @@ -196,7 +218,9 @@ module ProductImport end def product_validation(entry) - products = Spree::Product.where(supplier_id: entry.enterprise_id, name: entry.name, deleted_at: nil) + products = Spree::Product.where(supplier_id: entry.enterprise_id, + name: entry.name, + deleted_at: nil) if products.empty? mark_as_new_product(entry) @@ -263,7 +287,9 @@ module ProductImport end def inventory_permission?(enterprise_id, producer_id) - @current_user.admin? || ( @inventory_permissions[enterprise_id] && @inventory_permissions[enterprise_id].include?(producer_id) ) + @current_user.admin? || + ( @inventory_permissions[enterprise_id] && + @inventory_permissions[enterprise_id].include?(producer_id) ) end def mark_as_invalid(entry, options = {}) @@ -272,7 +298,7 @@ module ProductImport end def import_into_inventory? - @import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories' + @import_settings[:settings].andand['import_into'] == 'inventories' end def validate_inventory_item(entry, variant_override) @@ -284,9 +310,16 @@ 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 + existing_variant_override = VariantOverride.where( + variant_id: existing_variant.id, + hub_id: entry.enterprise_id + ).first + + variant_override = existing_variant_override || VariantOverride.new( + variant_id: existing_variant.id, + hub_id: entry.enterprise_id + ) - 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')) @@ -306,7 +339,9 @@ module ProductImport end def updates_count_per_enterprise(enterprise_id) - if @reset_counts[enterprise_id] && @reset_counts[enterprise_id][:updates_count] + if @reset_counts[enterprise_id] && + @reset_counts[enterprise_id][:updates_count] + @reset_counts[enterprise_id][:updates_count] += 1 else @reset_counts[enterprise_id] = { updates_count: 1 } diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 35a490c8d6..3bc66fa815 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -65,12 +65,14 @@ module ProductImport end def reset_counts - # Return indexed data about existing product count, reset count, and updates count per enterprise + # Return indexed data about existing product count, reset count, and + # updates count per enterprise @reset_counts.each do |enterprise_id, values| values[:updates_count] = 0 if values[:updates_count].blank? if values[:updates_count] && values[:existing_products] - @reset_counts[enterprise_id][:reset_count] = values[:existing_products] - values[:updates_count] + @reset_counts[enterprise_id][:reset_count] = + values[:existing_products] - values[:updates_count] end end @reset_counts diff --git a/app/models/product_import/spreadsheet_data.rb b/app/models/product_import/spreadsheet_data.rb index 8903944284..1f8ff56ad3 100644 --- a/app/models/product_import/spreadsheet_data.rb +++ b/app/models/product_import/spreadsheet_data.rb @@ -35,16 +35,21 @@ module ProductImport private def import_into_inventory? - @import_settings[:settings] && @import_settings[:settings]['import_into'] == 'inventories' + @import_settings[:settings].andand['import_into'] == 'inventories' end def create_enterprises_index @enterprises_index = {} @entries.each do |entry| enterprise_name = import_into_inventory? ? entry.distributor : entry.producer + next if @enterprises_index.key? enterprise_name - enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, name, is_primary_producer') - @enterprises_index[enterprise_name] = { id: enterprise.try(:id), is_primary_producer: enterprise.try(:is_primary_producer) } + + enterprise = Enterprise.find_by_name(enterprise_name, select: 'id, is_primary_producer') + + @enterprises_index[enterprise_name] = + { id: enterprise.try(:id), + is_primary_producer: enterprise.try(:is_primary_producer) } end @enterprises_index end diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb index dcfb35e65f..c87f46ec0e 100644 --- a/app/models/product_import/spreadsheet_entry.rb +++ b/app/models/product_import/spreadsheet_entry.rb @@ -8,13 +8,17 @@ module ProductImport include ActiveModel::Conversion include ActiveModel::Validations - attr_accessor :line_number, :valid, :validates_as, :product_object, :product_validations, :on_hand_nil, - :has_overrides, :units, :unscaled_units, :unit_type, :tax_category, :shipping_category + attr_accessor :line_number, :valid, :validates_as, :product_object, + :product_validations, :on_hand_nil, :has_overrides, :units, + :unscaled_units, :unit_type, :tax_category, :shipping_category - attr_accessor :id, :product_id, :producer, :producer_id, :distributor, :distributor_id, :name, :display_name, :sku, - :unit_value, :unit_description, :variant_unit, :variant_unit_scale, :variant_unit_name, - :display_as, :category, :primary_taxon_id, :price, :on_hand, :count_on_hand, :on_demand, - :tax_category_id, :shipping_category_id, :description, :import_date, :enterprise, :enterprise_id + attr_accessor :id, :product_id, :producer, :producer_id, :distributor, + :distributor_id, :name, :display_name, :sku, :unit_value, + :unit_description, :variant_unit, :variant_unit_scale, + :variant_unit_name, :display_as, :category, :primary_taxon_id, + :price, :on_hand, :count_on_hand, :on_demand, + :tax_category_id, :shipping_category_id, :description, + :import_date, :enterprise, :enterprise_id def initialize(attrs) @validates_as = '' @@ -77,11 +81,16 @@ module ProductImport end def non_display_attributes - ['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise', 'enterprise_id', 'producer_id', 'distributor_id', 'primary_taxon', 'primary_taxon_id', 'category_id', 'shipping_category_id', 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] + ['id', 'product_id', 'unscaled_units', 'variant_id', 'enterprise', + 'enterprise_id', 'producer_id', 'distributor_id', 'primary_taxon', + 'primary_taxon_id', 'category_id', 'shipping_category_id', + 'tax_category_id', 'variant_unit_scale', 'variant_unit', 'unit_value'] end def non_product_attributes - ['line_number', 'valid', 'errors', 'product_object', 'product_validations', 'inventory_validations', 'validates_as', 'save_type', 'on_hand_nil', 'has_overrides'] + ['line_number', 'valid', 'errors', 'product_object', + 'product_validations', 'inventory_validations', 'validates_as', + 'save_type', 'on_hand_nil', 'has_overrides'] end end end From 123d1963adbbfb932b3eda4ce78d53ad91401506 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 9 Oct 2018 11:18:52 +0100 Subject: [PATCH 8/9] Remove duplicate method --- app/models/product_import/entry_processor.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index d96a9d36c7..dd6a8c3b3d 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -29,7 +29,7 @@ module ProductImport def save_all(entries) entries.each do |entry| - if import_into_inventory?(entry) + if settings.importing_into_inventory? save_to_inventory(entry) else save_to_product_list(entry) @@ -124,10 +124,6 @@ module ProductImport @variants_updated += 1 end - def import_into_inventory?(entry) - entry.enterprise_id && settings.importing_into_inventory? - end - def save_new_inventory_item(entry) new_item = entry.product_object assign_defaults(new_item, entry) From a2828ea3054bf85127afe2bbbf33cb1514c8202d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 9 Oct 2018 11:21:21 +0100 Subject: [PATCH 9/9] Use :for_hubs scope --- app/models/product_import/entry_processor.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index dd6a8c3b3d..2e8ed9592c 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -46,10 +46,7 @@ module ProductImport products_count = if settings.importing_into_inventory? - VariantOverride.where( - 'variant_overrides.hub_id IN (?)', - enterprise_id - ).count + VariantOverride.for_hubs([enterprise_id]).count else Spree::Variant. not_deleted.