mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-31 18:59:08 +00:00
Rename Item Variant from Manage Variants feature Added.
This commit is contained in:
@@ -39,6 +39,12 @@ frappe.ui.form.on("Manage Variants", {
|
|||||||
|
|
||||||
refresh: function(frm) {
|
refresh: function(frm) {
|
||||||
frm.disable_save();
|
frm.disable_save();
|
||||||
|
frm.page.set_primary_action(__("Create Variants"), function() {
|
||||||
|
frappe.call({
|
||||||
|
method: "create_variants",
|
||||||
|
doc:frm.doc
|
||||||
|
})
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
item:function(frm) {
|
item:function(frm) {
|
||||||
@@ -51,5 +57,4 @@ frappe.ui.form.on("Manage Variants", {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -58,14 +58,6 @@
|
|||||||
"options": "Variant Item",
|
"options": "Variant Item",
|
||||||
"permlevel": 0,
|
"permlevel": 0,
|
||||||
"precision": ""
|
"precision": ""
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "create_variants",
|
|
||||||
"fieldtype": "Button",
|
|
||||||
"label": "Create Variants",
|
|
||||||
"options": "create_variants",
|
|
||||||
"permlevel": 0,
|
|
||||||
"precision": ""
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"hide_heading": 0,
|
"hide_heading": 0,
|
||||||
@@ -75,7 +67,7 @@
|
|||||||
"is_submittable": 0,
|
"is_submittable": 0,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"istable": 0,
|
"istable": 0,
|
||||||
"modified": "2015-05-27 04:43:52.051367",
|
"modified": "2015-05-28 06:18:03.238411",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Manage Variants",
|
"name": "Manage Variants",
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ class ItemTemplateCannotHaveStock(frappe.ValidationError): pass
|
|||||||
class ManageVariants(Document):
|
class ManageVariants(Document):
|
||||||
|
|
||||||
def get_item_details(self):
|
def get_item_details(self):
|
||||||
self.get_attributes()
|
self.clear_tables()
|
||||||
self.get_variants()
|
if self.item:
|
||||||
|
self.get_attributes()
|
||||||
|
self.get_variants()
|
||||||
|
|
||||||
def generate_combinations(self):
|
def generate_combinations(self):
|
||||||
self.validate_attributes()
|
self.validate_attributes()
|
||||||
@@ -29,6 +31,10 @@ class ManageVariants(Document):
|
|||||||
def create_variants(self):
|
def create_variants(self):
|
||||||
self.sync_variants()
|
self.sync_variants()
|
||||||
|
|
||||||
|
def clear_tables(self):
|
||||||
|
self.set('attributes', [])
|
||||||
|
self.set('variants', [])
|
||||||
|
|
||||||
def get_attributes(self):
|
def get_attributes(self):
|
||||||
attributes = {}
|
attributes = {}
|
||||||
self.set('attributes', [])
|
self.set('attributes', [])
|
||||||
@@ -41,15 +47,14 @@ class ManageVariants(Document):
|
|||||||
self.append('attributes',{"attribute": d, "attribute_value": value})
|
self.append('attributes',{"attribute": d, "attribute_value": value})
|
||||||
|
|
||||||
def get_variants(self):
|
def get_variants(self):
|
||||||
self.set('variants', [])
|
|
||||||
variants = [d.name for d in frappe.get_all("Item",
|
variants = [d.name for d in frappe.get_all("Item",
|
||||||
filters={"variant_of":self.item})]
|
filters={"variant_of":self.item})]
|
||||||
for d in variants:
|
for d in variants:
|
||||||
variant_attributes, attributes = "", []
|
variant_attributes, attributes = "", []
|
||||||
for attribute in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` where parent = %s""", d):
|
for attribute in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` where parent = %s""", d):
|
||||||
variant_attributes += attribute[1] + " "
|
variant_attributes += attribute[1] + " | "
|
||||||
attributes.append([attribute[0], attribute[1]])
|
attributes.append([attribute[0], attribute[1]])
|
||||||
self.append('variants',{"variant": d, "variant_attributes": variant_attributes, "attributes": json.dumps(attributes)})
|
self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -2], "attributes": json.dumps(attributes)})
|
||||||
|
|
||||||
def validate_attributes(self):
|
def validate_attributes(self):
|
||||||
if not self.attributes:
|
if not self.attributes:
|
||||||
@@ -112,33 +117,50 @@ class ManageVariants(Document):
|
|||||||
else:
|
else:
|
||||||
variant_attributes = ""
|
variant_attributes = ""
|
||||||
for d in _my_attributes:
|
for d in _my_attributes:
|
||||||
variant_attributes += d[1] + " "
|
variant_attributes += d[1] + " | "
|
||||||
self.append('variants', {"variant": item_code + "-" + value.abbr,
|
self.append('variants', {"variant": item_code + "-" + value.abbr,
|
||||||
"attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes})
|
"attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes[: -2]})
|
||||||
|
|
||||||
add_attribute_suffixes(self.item, [], attributes)
|
add_attribute_suffixes(self.item, [], attributes)
|
||||||
|
|
||||||
def sync_variants(self):
|
def sync_variants(self):
|
||||||
variant_item_codes = []
|
variant_item_codes = []
|
||||||
|
item_variants_attributes = {}
|
||||||
|
inserted, updated, renamed_old, renamed_new, deleted = [], [], [], [], []
|
||||||
|
|
||||||
for v in self.variants:
|
for v in self.variants:
|
||||||
variant_item_codes.append(v.variant)
|
variant_item_codes.append(v.variant)
|
||||||
|
|
||||||
existing_variants = [d.name for d in frappe.get_all("Item",
|
existing_variants = [d.name for d in frappe.get_all("Item",
|
||||||
filters={"variant_of":self.item})]
|
filters={"variant_of":self.item})]
|
||||||
|
|
||||||
inserted, updated, deleted = [], [], []
|
for d in existing_variants:
|
||||||
|
attributes = []
|
||||||
|
for attribute in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` where parent = %s""", d):
|
||||||
|
attributes.append([attribute[0], attribute[1]])
|
||||||
|
item_variants_attributes.setdefault(d, []).append(attributes)
|
||||||
|
|
||||||
for existing_variant in existing_variants:
|
for existing_variant in existing_variants:
|
||||||
if existing_variant not in variant_item_codes:
|
if existing_variant not in variant_item_codes:
|
||||||
frappe.delete_doc("Item", existing_variant)
|
att = item_variants_attributes[existing_variant][0]
|
||||||
deleted.append(existing_variant)
|
for variant in self.variants:
|
||||||
|
if sorted(json.loads(variant.attributes) ,key=lambda x: x[0]) == \
|
||||||
|
sorted(att ,key=lambda x: x[0]):
|
||||||
|
rename_variant(existing_variant, variant.variant)
|
||||||
|
renamed_old.append(existing_variant)
|
||||||
|
renamed_new.append(variant.variant)
|
||||||
|
|
||||||
|
if existing_variant not in renamed_old:
|
||||||
|
delete_variant(existing_variant)
|
||||||
|
deleted.append(existing_variant)
|
||||||
|
|
||||||
for item_code in variant_item_codes:
|
for item_code in variant_item_codes:
|
||||||
if item_code not in existing_variants:
|
if item_code not in existing_variants:
|
||||||
make_variant(self.item, item_code, self.variants)
|
if item_code not in renamed_new:
|
||||||
inserted.append(item_code)
|
make_variant(self.item, item_code, self.variants)
|
||||||
|
inserted.append(item_code)
|
||||||
else:
|
else:
|
||||||
update_variant(self.item, existing_variant, self.variants)
|
update_variant(self.item, item_code, self.variants)
|
||||||
updated.append(existing_variant)
|
updated.append(item_code)
|
||||||
|
|
||||||
if inserted:
|
if inserted:
|
||||||
frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted)))
|
frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted)))
|
||||||
@@ -146,6 +168,9 @@ class ManageVariants(Document):
|
|||||||
if updated:
|
if updated:
|
||||||
frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
|
frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
|
||||||
|
|
||||||
|
if renamed_old:
|
||||||
|
frappe.msgprint(_("Item Variants {0} renamed").format(", ".join(renamed_old)))
|
||||||
|
|
||||||
if deleted:
|
if deleted:
|
||||||
frappe.msgprint(_("Item Variants {0} deleted").format(", ".join(deleted)))
|
frappe.msgprint(_("Item Variants {0} deleted").format(", ".join(deleted)))
|
||||||
|
|
||||||
@@ -162,6 +187,12 @@ def update_variant(item, variant_code, variant_attribute=None):
|
|||||||
copy_attributes_to_variant(template, variant, variant_attribute, insert=True)
|
copy_attributes_to_variant(template, variant, variant_attribute, insert=True)
|
||||||
variant.save()
|
variant.save()
|
||||||
|
|
||||||
|
def rename_variant(old_variant_code, new_variant_code):
|
||||||
|
frappe.rename_doc("Item", old_variant_code, new_variant_code)
|
||||||
|
|
||||||
|
def delete_variant(variant_code):
|
||||||
|
frappe.delete_doc("Item", variant_code)
|
||||||
|
|
||||||
def copy_attributes_to_variant(template, variant, variant_attribute=None, insert=False):
|
def copy_attributes_to_variant(template, variant, variant_attribute=None, insert=False):
|
||||||
from frappe.model import no_value_fields
|
from frappe.model import no_value_fields
|
||||||
for field in template.meta.fields:
|
for field in template.meta.fields:
|
||||||
|
|||||||
@@ -47,7 +47,7 @@
|
|||||||
{
|
{
|
||||||
"fieldname": "attributes",
|
"fieldname": "attributes",
|
||||||
"fieldtype": "Text",
|
"fieldtype": "Text",
|
||||||
"hidden": 1,
|
"hidden": 0,
|
||||||
"label": "attributes",
|
"label": "attributes",
|
||||||
"permlevel": 0,
|
"permlevel": 0,
|
||||||
"precision": "",
|
"precision": "",
|
||||||
@@ -62,7 +62,7 @@
|
|||||||
"is_submittable": 0,
|
"is_submittable": 0,
|
||||||
"issingle": 0,
|
"issingle": 0,
|
||||||
"istable": 1,
|
"istable": 1,
|
||||||
"modified": "2015-05-28 04:58:20.495616",
|
"modified": "2015-05-29 02:09:33.622631",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Variant Item",
|
"name": "Variant Item",
|
||||||
|
|||||||
Reference in New Issue
Block a user