Skip to content

Commit

Permalink
fix: item variant with manufacturer (backport #38845) (backport #38847)…
Browse files Browse the repository at this point in the history
… (#38851)

fix: item variant with manufacturer (backport #38845) (#38847)

* fix: item variant with manufacturer (#38845)

(cherry picked from commit e0c8ff1)

* chore: fix test case

---------

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
(cherry picked from commit 4aa960b)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mergify[bot] authored Dec 19, 2023
1 parent 637343f commit 7320440
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
20 changes: 17 additions & 3 deletions erpnext/controllers/item_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,24 @@ def make_variant_based_on_manufacturer(template, manufacturer, manufacturer_part

copy_attributes_to_variant(template, variant)

variant.manufacturer = manufacturer
variant.manufacturer_part_no = manufacturer_part_no

variant.item_code = append_number_if_name_exists("Item", template.name)
variant.flags.ignore_mandatory = True
variant.save()

if not frappe.db.exists(
"Item Manufacturer", {"item_code": variant.name, "manufacturer": manufacturer}
):
manufacturer_doc = frappe.new_doc("Item Manufacturer")
manufacturer_doc.update(
{
"item_code": variant.name,
"manufacturer": manufacturer,
"manufacturer_part_no": manufacturer_part_no,
}
)

manufacturer_doc.flags.ignore_mandatory = True
manufacturer_doc.save(ignore_permissions=True)

return variant

Expand Down
44 changes: 15 additions & 29 deletions erpnext/stock/doctype/item/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,39 +522,25 @@ def test_uom_conv_base_case(self):
self.assertEqual(factor, 1.0)

def test_item_variant_by_manufacturer(self):
fields = [{"field_name": "description"}, {"field_name": "variant_based_on"}]
set_item_variant_settings(fields)
template = make_item(
"_Test Item Variant By Manufacturer", {"has_variants": 1, "variant_based_on": "Manufacturer"}
).name

if frappe.db.exists("Item", "_Test Variant Mfg"):
frappe.delete_doc("Item", "_Test Variant Mfg")
if frappe.db.exists("Item", "_Test Variant Mfg-1"):
frappe.delete_doc("Item", "_Test Variant Mfg-1")
if frappe.db.exists("Manufacturer", "MSG1"):
frappe.delete_doc("Manufacturer", "MSG1")

template = frappe.get_doc(
dict(
doctype="Item",
item_code="_Test Variant Mfg",
has_variant=1,
item_group="Products",
variant_based_on="Manufacturer",
)
).insert()
for manufacturer in ["DFSS", "DASA", "ASAAS"]:
if not frappe.db.exists("Manufacturer", manufacturer):
m_doc = frappe.new_doc("Manufacturer")
m_doc.short_name = manufacturer
m_doc.insert()

manufacturer = frappe.get_doc(dict(doctype="Manufacturer", short_name="MSG1")).insert()
self.assertFalse(frappe.db.exists("Item Manufacturer", {"manufacturer": "DFSS"}))
variant = get_variant(template, manufacturer="DFSS", manufacturer_part_no="DFSS-123")

variant = get_variant(template.name, manufacturer=manufacturer.name)
self.assertEqual(variant.item_code, "_Test Variant Mfg-1")
self.assertEqual(variant.description, "_Test Variant Mfg")
self.assertEqual(variant.manufacturer, "MSG1")
variant.insert()
item_manufacturer = frappe.db.exists(
"Item Manufacturer", {"manufacturer": "DFSS", "item_code": variant.name}
)
self.assertTrue(item_manufacturer)

variant = get_variant(template.name, manufacturer=manufacturer.name, manufacturer_part_no="007")
self.assertEqual(variant.item_code, "_Test Variant Mfg-2")
self.assertEqual(variant.description, "_Test Variant Mfg")
self.assertEqual(variant.manufacturer, "MSG1")
self.assertEqual(variant.manufacturer_part_no, "007")
frappe.delete_doc("Item Manufacturer", item_manufacturer)

def test_stock_exists_against_template_item(self):
stock_item = frappe.get_all("Stock Ledger Entry", fields=["item_code"], limit=1)
Expand Down

0 comments on commit 7320440

Please sign in to comment.