diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index a6a2277afd..f720af4aa5 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -161,7 +161,7 @@ module ProductImport end def unit_fields_validation(entry) - unit_types = ['g', 'oz', 'lb', 'kg', 't', 'ml', 'l', 'kl', ''] + unit_types = ['mg', 'g', 'kg', 'oz', 'lb', 't', 'ml', 'cl', 'dl', 'l', 'kl', 'gal', ''] if entry.units.blank? mark_as_invalid(entry, attribute: 'units', @@ -297,7 +297,7 @@ module ProductImport unscaled_units = entry.unscaled_units.to_f || 0 entry.unit_value = unscaled_units * unit_scale unless unit_scale.nil? - if entry.match_inventory_variant?(existing_variant) + if entry.match_variant?(existing_variant) variant_override = create_inventory_item(entry, existing_variant) return validate_inventory_item(entry, variant_override) end diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb index 23743e6f90..dc02a91fba 100644 --- a/app/models/product_import/spreadsheet_entry.rb +++ b/app/models/product_import/spreadsheet_entry.rb @@ -85,10 +85,6 @@ module ProductImport end def match_variant?(variant) - match_display_name?(variant) && variant.unit_value.to_d == unscaled_units.to_d - end - - def match_inventory_variant?(variant) match_display_name?(variant) && variant.unit_value.to_d == unit_value.to_d end diff --git a/app/models/product_import/unit_converter.rb b/app/models/product_import/unit_converter.rb index 88cd408d34..33e3ac43cf 100644 --- a/app/models/product_import/unit_converter.rb +++ b/app/models/product_import/unit_converter.rb @@ -32,14 +32,18 @@ module ProductImport def unit_scales { + 'mg' => { scale: 0.001, unit: 'weight' }, 'g' => { scale: 1, unit: 'weight' }, 'kg' => { scale: 1000, unit: 'weight' }, 'oz' => { scale: 28.35, unit: 'weight' }, 'lb' => { scale: 453.6, unit: 'weight' }, 't' => { scale: 1_000_000, unit: 'weight' }, 'ml' => { scale: 0.001, unit: 'volume' }, + 'cl' => { scale: 0.01, unit: 'volume' }, + 'dl' => { scale: 0.1, unit: 'volume' }, 'l' => { scale: 1, unit: 'volume' }, - 'kl' => { scale: 1000, unit: 'volume' } + 'kl' => { scale: 1000, unit: 'volume' }, + 'gal' => { scale: 4.54609, unit: 'volume' }, } end diff --git a/spec/models/product_import/spreadsheet_entry_spec.rb b/spec/models/product_import/spreadsheet_entry_spec.rb index f2e98c86c0..06ed7d68f3 100644 --- a/spec/models/product_import/spreadsheet_entry_spec.rb +++ b/spec/models/product_import/spreadsheet_entry_spec.rb @@ -20,16 +20,15 @@ RSpec.describe ProductImport::SpreadsheetEntry do } let(:display_name) { "" } - # TODO test match on display_name describe "#match_variant?" do it "returns true if matching" do - variant = create(:variant, unit_value: 500) + variant = create(:variant, unit_value: 500_000) expect(entry.match_variant?(variant)).to be(true) end it "returns false if not machting" do - variant = create(:variant, unit_value: 250) + variant = create(:variant, unit_value: 500) expect(entry.match_variant?(variant)).to be(false) end @@ -38,7 +37,7 @@ RSpec.describe ProductImport::SpreadsheetEntry do let(:display_name) { "Good" } it "returns true" do - variant = create(:variant, unit_value: 500, display_name: "Good") + variant = create(:variant, unit_value: 500_000, display_name: "Good") expect(entry.match_variant?(variant)).to be(true) end @@ -48,44 +47,10 @@ RSpec.describe ProductImport::SpreadsheetEntry do let(:display_name) { "Bad" } it "returns false" do - variant = create(:variant, unit_value: 500, display_name: "Good") + variant = create(:variant, unit_value: 500_000, display_name: "Good") expect(entry.match_variant?(variant)).to be(false) end end end - - describe "#match_inventory_variant?" do - it "returns true if matching" do - variant = create(:variant, unit_value: 500_000) - - expect(entry.match_inventory_variant?(variant)).to be(true) - end - - it "returns false if not machting" do - variant = create(:variant, unit_value: 500) - - expect(entry.match_inventory_variant?(variant)).to be(false) - end - - context "with same display_name" do - let(:display_name) { "Good" } - - it "returns true" do - variant = create(:variant, unit_value: 500_000, display_name: "Good") - - expect(entry.match_inventory_variant?(variant)).to be(true) - end - end - - context "with different display_name" do - let(:display_name) { "Bad" } - - it "returns false" do - variant = create(:variant, unit_value: 500_000, display_name: "Good") - - expect(entry.match_inventory_variant?(variant)).to be(false) - end - end - end end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 60d42c6154..a2a2917f20 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -578,11 +578,11 @@ RSpec.describe ProductImport::ProductImporter do describe "updating non-updatable fields on existing variants" do let(:csv_data) { CSV.generate do |csv| - csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", + csv << ["name", "producer", "category", "on_hand", "price", "units", "variant_unit_name", "shipping_category"] - csv << ["Beetroot", enterprise3.name, "Vegetables", "5", "3.50", "500", "Kg", + csv << ["Beetroot", enterprise3.name, "Vegetables", "5", "3.50", "500", "Half", shipping_category.name] - csv << ["Tomato", enterprise3.name, "Vegetables", "6", "5.50", "500", "Kg", + csv << ["Tomato", enterprise3.name, "Vegetables", "6", "5.50", "500", "Half", shipping_category.name] end }