From 0988440fd979c01c5ac6fab774c576af58ea0fbe Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Wed, 1 Jul 2015 12:50:52 +0530 Subject: [PATCH] Fixes for Manage Variants --- erpnext/patches/v5_0/item_variants.py | 12 ++++-- erpnext/stock/doctype/item/item.js | 2 +- erpnext/stock/doctype/item/item.py | 4 +- .../manage_variants/manage_variants.js | 2 +- .../manage_variants/manage_variants.json | 6 +-- .../manage_variants/manage_variants.py | 38 +++++++++---------- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/erpnext/patches/v5_0/item_variants.py b/erpnext/patches/v5_0/item_variants.py index 6e4db194324..62e9ac9e1f0 100644 --- a/erpnext/patches/v5_0/item_variants.py +++ b/erpnext/patches/v5_0/item_variants.py @@ -2,11 +2,15 @@ import frappe def execute(): frappe.reload_doctype("Item") + for dt in ["manage_variants", "manage_variants_item", "variant_attribute"]: + frappe.reload_doc("stock", "doctype", dt) + for d in frappe.get_list("Item", filters={"has_variants":1}): manage_variant = frappe.new_doc("Manage Variants") - manage_variant.item = d.name + manage_variant.item_code = d.name manage_variant.attributes = frappe.db.sql("select item_attribute as attribute, item_attribute_value as attribute_value \ from `tabItem Variant` where parent = %s", d.name, as_dict=1) - manage_variant.generate_combinations() - manage_variant.create_variants() - frappe.delete_doc("doctype", "Item Variant") \ No newline at end of file + if manage_variant.attributes: + manage_variant.generate_combinations() + manage_variant.create_variants() + frappe.delete_doc("DocType", "Item Variant") \ No newline at end of file diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index d2f1d7527c1..58b1adb8db7 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -86,7 +86,7 @@ frappe.ui.form.on("Item", { }, manage_variants: function(frm) { - frappe.route_options = {"item": frm.doc.name }; + frappe.route_options = {"item_code": frm.doc.name }; frappe.set_route("List", "Manage Variants"); } }); diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 22e6b212f59..a24fc0476ad 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -332,14 +332,14 @@ class Item(WebsiteGenerator): updated = [] variants = frappe.db.get_all("Item", fields=["item_code"], filters={"variant_of": self.name }) for d in variants: - update_variant(self.item_code, d) + update_variant(self.name, d) updated.append(d.item_code) frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated))) def validate_has_variants(self): if not self.has_variants and frappe.db.get_value("Item", self.name, "has_variants"): if frappe.db.exists("Item", {"variant_of": self.name}): - frappe.throw("Item has variants.") + frappe.throw(_("Item has variants.")) def validate_stock_for_template_must_be_zero(self): if self.has_variants: diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.js b/erpnext/stock/doctype/manage_variants/manage_variants.js index 82a3da7f4d9..ba5c46e8eb9 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.js +++ b/erpnext/stock/doctype/manage_variants/manage_variants.js @@ -43,7 +43,7 @@ frappe.ui.form.on("Manage Variants", { }); }, - item:function(frm) { + item_code:function(frm) { return frappe.call({ method: "get_item_details", doc:frm.doc, diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.json b/erpnext/stock/doctype/manage_variants/manage_variants.json index 055fc274423..7c61620b083 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.json +++ b/erpnext/stock/doctype/manage_variants/manage_variants.json @@ -9,9 +9,9 @@ "document_type": "", "fields": [ { - "fieldname": "item", + "fieldname": "item_code", "fieldtype": "Link", - "label": "Item", + "label": "Item Code", "options": "Item", "permlevel": 0, "precision": "", @@ -75,7 +75,7 @@ "is_submittable": 0, "issingle": 1, "istable": 0, - "modified": "2015-06-30 03:18:13.787883", + "modified": "2015-06-30 13:40:59.946655", "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 8b1a13b0a17..0131deb0de5 100644 --- a/erpnext/stock/doctype/manage_variants/manage_variants.py +++ b/erpnext/stock/doctype/manage_variants/manage_variants.py @@ -15,7 +15,7 @@ class ManageVariants(Document): def get_item_details(self): self.clear_tables() - if self.item: + if self.item_code: self.get_attributes() self.get_variants() @@ -37,7 +37,7 @@ class ManageVariants(Document): attributes = {} self.set('attributes', []) for d in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` as attribute, - `tabItem` as item where attribute.parent= item.name and item.variant_of = %s""", self.item, as_dict=1): + `tabItem` as item where attribute.parent= item.name and item.variant_of = %s""", self.item_code, as_dict=1): attributes.setdefault(d.attribute, []).append(d.attribute_value) for d in attributes: attribute_values = set(attributes[d]) @@ -46,24 +46,25 @@ class ManageVariants(Document): def get_variants(self): variants = [d.name for d in frappe.get_all("Item", - filters={"variant_of":self.item})] + filters={"variant_of":self.item_code})] + data = frappe.db.sql("""select parent, attribute, attribute_value from `tabVariant Attribute`""", as_dict=1) 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] + " | " - attributes.append([attribute[0], attribute[1]]) + for attribute in data: + if attribute.parent == d: + variant_attributes += attribute.attribute_value + " | " + attributes.append([attribute.attribute, attribute.attribute_value]) self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -3], "attributes": json.dumps(attributes)}) def validate_attributes(self): if not self.attributes: - frappe.throw("Enter atleast one Attribute & its Value in Attribute table.") + frappe.throw(_("Enter atleast one Attribute & its Value in Attribute table.")) def validate_template_item(self): - template_item = frappe.get_doc("Item", self.item) - if not template_item.has_variants: + if not frappe.db.get_value("Item", self.item_code, "has_variants"): frappe.throw(_("Selected Item cannot have Variants.")) - if template_item.variant_of: + if frappe.db.get_value("Item", self.item_code, "variant_of"): frappe.throw(_("Item cannot be a variant of a variant")) def validate_attribute_values(self): @@ -111,7 +112,7 @@ class ManageVariants(Document): variant_attributes += d[1] + " | " self.append('variants', {"variant": item_code + "-" + value.abbr, "attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes[: -3]}) - add_attribute_suffixes(self.item, [], attributes) + add_attribute_suffixes(self.item_code, [], attributes) def sync_variants(self): variant_item_codes = [] @@ -122,7 +123,7 @@ class ManageVariants(Document): variant_item_codes.append(v.variant) existing_variants = [d.name for d in frappe.get_all("Item", - filters={"variant_of":self.item})] + filters={"variant_of":self.item_code})] for d in existing_variants: attributes = [] @@ -147,10 +148,10 @@ class ManageVariants(Document): for item_code in variant_item_codes: if item_code not in existing_variants: if item_code not in new_variant_name: - make_variant(self.item, item_code, self.variants) + make_variant(self.item_code, item_code, self.variants) inserted.append(item_code) else: - update_variant(self.item, item_code, self.variants) + update_variant(self.item_code, item_code, self.variants) updated.append(item_code) if inserted: @@ -168,14 +169,12 @@ class ManageVariants(Document): def make_variant(item, variant_code, variant_attribute): variant = frappe.new_doc("Item") variant.item_code = variant_code - template = frappe.get_doc("Item", item) - copy_attributes_to_variant(template, variant, variant_attribute, insert=True) + copy_attributes_to_variant(item, variant, variant_attribute, insert=True) variant.insert() def update_variant(item, variant_code, variant_attribute=None): variant = frappe.get_doc("Item", variant_code) - template = frappe.get_doc("Item", item) - copy_attributes_to_variant(template, variant, variant_attribute, insert=True) + copy_attributes_to_variant(item, variant, variant_attribute, insert=True) variant.save() def rename_variant(old_variant_code, new_variant_code): @@ -184,7 +183,8 @@ def rename_variant(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(item, variant, variant_attribute=None, insert=False): + template = frappe.get_doc("Item", item) from frappe.model import no_value_fields for field in template.meta.fields: if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\