diff --git a/app/models/product_importer.rb b/app/models/product_importer.rb index 7c2bf28208..c3e6a1ad71 100644 --- a/app/models/product_importer.rb +++ b/app/models/product_importer.rb @@ -166,6 +166,7 @@ class ProductImporter inventory_validation(entry) else category_validation(entry) + tax_and_shipping_validation(entry) product_validation(entry) end end @@ -193,6 +194,7 @@ class ProductImporter end build_categories_index build_suppliers_index + build_tax_and_shipping_indexes build_producers_index if importing_into_inventory? #validate_all count_existing_items unless @import_settings.has_key?(:start) @@ -267,6 +269,7 @@ class ProductImporter inventory_validation(entry) else category_validation(entry) + tax_and_shipping_validation(entry) product_validation(entry) end end @@ -420,6 +423,29 @@ class ProductImporter end end + def tax_and_shipping_validation(entry) + tax_validation(entry) + shipping_validation(entry) + end + + def tax_validation(entry) + return unless entry.tax_category.present? + if @tax_index.has_key? entry.tax_category + entry.tax_category_id = @tax_index[entry.tax_category] + else + mark_as_invalid(entry, attribute: "tax_category", error: "#{I18n.t('admin.product_import.model.not_found')}") + end + end + + def shipping_validation(entry) + return unless entry.shipping_category.present? + if @shipping_index.has_key? entry.shipping_category + entry.shipping_category_id = @shipping_index[entry.shipping_category] + else + mark_as_invalid(entry, attribute: "shipping_category", error: "#{I18n.t('admin.product_import.model.not_found')}") + end + end + def category_exists?(category_name) @categories_index[category_name] end @@ -464,6 +490,13 @@ class ProductImporter @categories_index end + def build_tax_and_shipping_indexes + @tax_index = {} + @shipping_index = {} + Spree::TaxCategory.select([:id, :name]).map {|tc| @tax_index[tc.name] = tc.id } + Spree::ShippingCategory.select([:id, :name]).map {|sc| @shipping_index[sc.name] = sc.id } + end + def save_all_valid @entries.each do |entry| if importing_into_inventory? diff --git a/app/models/spreadsheet_entry.rb b/app/models/spreadsheet_entry.rb index ad7a496cea..dd4ffcf46a 100644 --- a/app/models/spreadsheet_entry.rb +++ b/app/models/spreadsheet_entry.rb @@ -7,7 +7,7 @@ class SpreadsheetEntry attr_reader :validates_as attr_accessor :line_number, :valid, :product_object, :product_validations, :on_hand_nil, - :has_overrides, :units, :unscaled_units, :unit_type + :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, :unit_value, :unit_description, :variant_unit, :variant_unit_scale, :variant_unit_name, @@ -153,6 +153,6 @@ class SpreadsheetEntry 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', 'tax_category,' 'shipping_category'] end end