From bd9745ba729235416b5da8616a447ba0da123328 Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Fri, 29 May 2015 17:15:55 +0530 Subject: [PATCH] Rename Item Variant from Manage Variants feature Added. --- .../manage_variants/manage_variants.js | 9 ++- .../manage_variants/manage_variants.json | 10 +-- .../manage_variants/manage_variants.py | 61 ++++++++++++++----- .../doctype/variant_item/variant_item.json | 4 +- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.js b/erpnext/stock/doctype/manage_variants/manage_variants.js index 992e2a9df1..5388cc2a92 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.js +++ b/erpnext/stock/doctype/manage_variants/manage_variants.js @@ -39,8 +39,14 @@ frappe.ui.form.on("Manage Variants", { refresh: function(frm) { frm.disable_save(); + frm.page.set_primary_action(__("Create Variants"), function() { + frappe.call({ + method: "create_variants", + doc:frm.doc + }) + }); }, - + item:function(frm) { return frappe.call({ method: "get_item_details", @@ -51,5 +57,4 @@ frappe.ui.form.on("Manage Variants", { } }) } - }); diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.json b/erpnext/stock/doctype/manage_variants/manage_variants.json index bcad045c07..1ad938d05b 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.json +++ b/erpnext/stock/doctype/manage_variants/manage_variants.json @@ -58,14 +58,6 @@ "options": "Variant Item", "permlevel": 0, "precision": "" - }, - { - "fieldname": "create_variants", - "fieldtype": "Button", - "label": "Create Variants", - "options": "create_variants", - "permlevel": 0, - "precision": "" } ], "hide_heading": 0, @@ -75,7 +67,7 @@ "is_submittable": 0, "issingle": 1, "istable": 0, - "modified": "2015-05-27 04:43:52.051367", + "modified": "2015-05-28 06:18:03.238411", "modified_by": "Administrator", "module": "Stock", "name": "Manage Variants", diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.py b/erpnext/stock/doctype/manage_variants/manage_variants.py index 8b96ff9dd2..834b9ef9d9 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.py +++ b/erpnext/stock/doctype/manage_variants/manage_variants.py @@ -15,8 +15,10 @@ class ItemTemplateCannotHaveStock(frappe.ValidationError): pass class ManageVariants(Document): def get_item_details(self): - self.get_attributes() - self.get_variants() + self.clear_tables() + if self.item: + self.get_attributes() + self.get_variants() def generate_combinations(self): self.validate_attributes() @@ -29,6 +31,10 @@ class ManageVariants(Document): def create_variants(self): self.sync_variants() + def clear_tables(self): + self.set('attributes', []) + self.set('variants', []) + def get_attributes(self): attributes = {} self.set('attributes', []) @@ -41,15 +47,14 @@ class ManageVariants(Document): self.append('attributes',{"attribute": d, "attribute_value": value}) def get_variants(self): - self.set('variants', []) variants = [d.name for d in frappe.get_all("Item", filters={"variant_of":self.item})] for d in variants: variant_attributes, attributes = "", [] 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]]) - 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): if not self.attributes: @@ -112,33 +117,50 @@ class ManageVariants(Document): else: variant_attributes = "" for d in _my_attributes: - variant_attributes += d[1] + " " + variant_attributes += d[1] + " | " 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) def sync_variants(self): variant_item_codes = [] + item_variants_attributes = {} + inserted, updated, renamed_old, renamed_new, deleted = [], [], [], [], [] + for v in self.variants: variant_item_codes.append(v.variant) existing_variants = [d.name for d in frappe.get_all("Item", filters={"variant_of":self.item})] + + 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) - inserted, updated, deleted = [], [], [] for existing_variant in existing_variants: if existing_variant not in variant_item_codes: - frappe.delete_doc("Item", existing_variant) - deleted.append(existing_variant) + att = item_variants_attributes[existing_variant][0] + 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: if item_code not in existing_variants: - make_variant(self.item, item_code, self.variants) - inserted.append(item_code) + if item_code not in renamed_new: + make_variant(self.item, item_code, self.variants) + inserted.append(item_code) else: - update_variant(self.item, existing_variant, self.variants) - updated.append(existing_variant) + update_variant(self.item, item_code, self.variants) + updated.append(item_code) if inserted: frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted))) @@ -146,6 +168,9 @@ class ManageVariants(Document): if 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: 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) 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): from frappe.model import no_value_fields for field in template.meta.fields: diff --git a/erpnext/stock/doctype/variant_item/variant_item.json b/erpnext/stock/doctype/variant_item/variant_item.json index 9f10482720..13c46ac0f7 100644 --- a/erpnext/stock/doctype/variant_item/variant_item.json +++ b/erpnext/stock/doctype/variant_item/variant_item.json @@ -47,7 +47,7 @@ { "fieldname": "attributes", "fieldtype": "Text", - "hidden": 1, + "hidden": 0, "label": "attributes", "permlevel": 0, "precision": "", @@ -62,7 +62,7 @@ "is_submittable": 0, "issingle": 0, "istable": 1, - "modified": "2015-05-28 04:58:20.495616", + "modified": "2015-05-29 02:09:33.622631", "modified_by": "Administrator", "module": "Stock", "name": "Variant Item",