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