Fixes for Manage Variants
This commit is contained in:
parent
274dd4ada0
commit
0988440fd9
@ -2,11 +2,15 @@ import frappe
|
|||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
frappe.reload_doctype("Item")
|
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}):
|
for d in frappe.get_list("Item", filters={"has_variants":1}):
|
||||||
manage_variant = frappe.new_doc("Manage Variants")
|
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 \
|
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)
|
from `tabItem Variant` where parent = %s", d.name, as_dict=1)
|
||||||
manage_variant.generate_combinations()
|
if manage_variant.attributes:
|
||||||
manage_variant.create_variants()
|
manage_variant.generate_combinations()
|
||||||
frappe.delete_doc("doctype", "Item Variant")
|
manage_variant.create_variants()
|
||||||
|
frappe.delete_doc("DocType", "Item Variant")
|
@ -86,7 +86,7 @@ frappe.ui.form.on("Item", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
manage_variants: function(frm) {
|
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");
|
frappe.set_route("List", "Manage Variants");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -332,14 +332,14 @@ class Item(WebsiteGenerator):
|
|||||||
updated = []
|
updated = []
|
||||||
variants = frappe.db.get_all("Item", fields=["item_code"], filters={"variant_of": self.name })
|
variants = frappe.db.get_all("Item", fields=["item_code"], filters={"variant_of": self.name })
|
||||||
for d in variants:
|
for d in variants:
|
||||||
update_variant(self.item_code, d)
|
update_variant(self.name, d)
|
||||||
updated.append(d.item_code)
|
updated.append(d.item_code)
|
||||||
frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
|
frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
|
||||||
|
|
||||||
def validate_has_variants(self):
|
def validate_has_variants(self):
|
||||||
if not self.has_variants and frappe.db.get_value("Item", self.name, "has_variants"):
|
if not self.has_variants and frappe.db.get_value("Item", self.name, "has_variants"):
|
||||||
if frappe.db.exists("Item", {"variant_of": self.name}):
|
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):
|
def validate_stock_for_template_must_be_zero(self):
|
||||||
if self.has_variants:
|
if self.has_variants:
|
||||||
|
@ -43,7 +43,7 @@ frappe.ui.form.on("Manage Variants", {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
item:function(frm) {
|
item_code:function(frm) {
|
||||||
return frappe.call({
|
return frappe.call({
|
||||||
method: "get_item_details",
|
method: "get_item_details",
|
||||||
doc:frm.doc,
|
doc:frm.doc,
|
||||||
|
@ -9,9 +9,9 @@
|
|||||||
"document_type": "",
|
"document_type": "",
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
"fieldname": "item",
|
"fieldname": "item_code",
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"label": "Item",
|
"label": "Item Code",
|
||||||
"options": "Item",
|
"options": "Item",
|
||||||
"permlevel": 0,
|
"permlevel": 0,
|
||||||
"precision": "",
|
"precision": "",
|
||||||
@ -75,7 +75,7 @@
|
|||||||
"is_submittable": 0,
|
"is_submittable": 0,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"istable": 0,
|
"istable": 0,
|
||||||
"modified": "2015-06-30 03:18:13.787883",
|
"modified": "2015-06-30 13:40:59.946655",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Manage Variants",
|
"name": "Manage Variants",
|
||||||
|
@ -15,7 +15,7 @@ class ManageVariants(Document):
|
|||||||
|
|
||||||
def get_item_details(self):
|
def get_item_details(self):
|
||||||
self.clear_tables()
|
self.clear_tables()
|
||||||
if self.item:
|
if self.item_code:
|
||||||
self.get_attributes()
|
self.get_attributes()
|
||||||
self.get_variants()
|
self.get_variants()
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ class ManageVariants(Document):
|
|||||||
attributes = {}
|
attributes = {}
|
||||||
self.set('attributes', [])
|
self.set('attributes', [])
|
||||||
for d in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` as attribute,
|
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)
|
attributes.setdefault(d.attribute, []).append(d.attribute_value)
|
||||||
for d in attributes:
|
for d in attributes:
|
||||||
attribute_values = set(attributes[d])
|
attribute_values = set(attributes[d])
|
||||||
@ -46,24 +46,25 @@ class ManageVariants(Document):
|
|||||||
|
|
||||||
def get_variants(self):
|
def get_variants(self):
|
||||||
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_code})]
|
||||||
|
data = frappe.db.sql("""select parent, attribute, attribute_value from `tabVariant Attribute`""", as_dict=1)
|
||||||
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 data:
|
||||||
variant_attributes += attribute[1] + " | "
|
if attribute.parent == d:
|
||||||
attributes.append([attribute[0], attribute[1]])
|
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)})
|
self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -3], "attributes": json.dumps(attributes)})
|
||||||
|
|
||||||
def validate_attributes(self):
|
def validate_attributes(self):
|
||||||
if not self.attributes:
|
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):
|
def validate_template_item(self):
|
||||||
template_item = frappe.get_doc("Item", self.item)
|
if not frappe.db.get_value("Item", self.item_code, "has_variants"):
|
||||||
if not template_item.has_variants:
|
|
||||||
frappe.throw(_("Selected Item cannot have 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"))
|
frappe.throw(_("Item cannot be a variant of a variant"))
|
||||||
|
|
||||||
def validate_attribute_values(self):
|
def validate_attribute_values(self):
|
||||||
@ -111,7 +112,7 @@ class ManageVariants(Document):
|
|||||||
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[: -3]})
|
"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):
|
def sync_variants(self):
|
||||||
variant_item_codes = []
|
variant_item_codes = []
|
||||||
@ -122,7 +123,7 @@ class ManageVariants(Document):
|
|||||||
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_code})]
|
||||||
|
|
||||||
for d in existing_variants:
|
for d in existing_variants:
|
||||||
attributes = []
|
attributes = []
|
||||||
@ -147,10 +148,10 @@ class ManageVariants(Document):
|
|||||||
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:
|
||||||
if item_code not in new_variant_name:
|
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)
|
inserted.append(item_code)
|
||||||
else:
|
else:
|
||||||
update_variant(self.item, item_code, self.variants)
|
update_variant(self.item_code, item_code, self.variants)
|
||||||
updated.append(item_code)
|
updated.append(item_code)
|
||||||
|
|
||||||
if inserted:
|
if inserted:
|
||||||
@ -168,14 +169,12 @@ class ManageVariants(Document):
|
|||||||
def make_variant(item, variant_code, variant_attribute):
|
def make_variant(item, variant_code, variant_attribute):
|
||||||
variant = frappe.new_doc("Item")
|
variant = frappe.new_doc("Item")
|
||||||
variant.item_code = variant_code
|
variant.item_code = variant_code
|
||||||
template = frappe.get_doc("Item", item)
|
copy_attributes_to_variant(item, variant, variant_attribute, insert=True)
|
||||||
copy_attributes_to_variant(template, variant, variant_attribute, insert=True)
|
|
||||||
variant.insert()
|
variant.insert()
|
||||||
|
|
||||||
def update_variant(item, variant_code, variant_attribute=None):
|
def update_variant(item, variant_code, variant_attribute=None):
|
||||||
variant = frappe.get_doc("Item", variant_code)
|
variant = frappe.get_doc("Item", variant_code)
|
||||||
template = frappe.get_doc("Item", item)
|
copy_attributes_to_variant(item, 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):
|
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):
|
def delete_variant(variant_code):
|
||||||
frappe.delete_doc("Item", 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
|
from frappe.model import no_value_fields
|
||||||
for field in template.meta.fields:
|
for field in template.meta.fields:
|
||||||
if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\
|
if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\
|
||||||
|
Loading…
x
Reference in New Issue
Block a user