Merge pull request #12995 from chahmedejaz/bugfix/12968-product-import-update

Impossible to update product sold by weight with product import
This commit is contained in:
Filipe
2024-11-28 08:38:58 -06:00
committed by GitHub
5 changed files with 14 additions and 49 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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
}