[fix] generate item code for variant
This commit is contained in:
parent
2538e51887
commit
90fd6fee23
@ -234,7 +234,7 @@ $.extend(erpnext.item, {
|
|||||||
callback: function(r) {
|
callback: function(r) {
|
||||||
// returns variant item
|
// returns variant item
|
||||||
if (r.message) {
|
if (r.message) {
|
||||||
var variant = r.message[0];
|
var variant = r.message;
|
||||||
var msgprint_dialog = frappe.msgprint(__("Item Variant {0} already exists with same attributes",
|
var msgprint_dialog = frappe.msgprint(__("Item Variant {0} already exists with same attributes",
|
||||||
[repl('<a href="#Form/Item/%(item_encoded)s" class="strong variant-click">%(item)s</a>', {
|
[repl('<a href="#Form/Item/%(item_encoded)s" class="strong variant-click">%(item)s</a>', {
|
||||||
item_encoded: encodeURIComponent(variant),
|
item_encoded: encodeURIComponent(variant),
|
||||||
@ -251,7 +251,7 @@ $.extend(erpnext.item, {
|
|||||||
method:"erpnext.stock.doctype.item.item.create_variant",
|
method:"erpnext.stock.doctype.item.item.create_variant",
|
||||||
args: {
|
args: {
|
||||||
"item": cur_frm.doc.name,
|
"item": cur_frm.doc.name,
|
||||||
"param": d.get_values()
|
"args": d.get_values()
|
||||||
},
|
},
|
||||||
callback: function(r) {
|
callback: function(r) {
|
||||||
var doclist = frappe.model.sync(r.message);
|
var doclist = frappe.model.sync(r.message);
|
||||||
|
@ -352,8 +352,8 @@ class Item(WebsiteGenerator):
|
|||||||
args[d.attribute] = d.attribute_value
|
args[d.attribute] = d.attribute_value
|
||||||
|
|
||||||
variant = get_variant(self.variant_of, args)
|
variant = get_variant(self.variant_of, args)
|
||||||
if variant and variant[0][0] != self.name:
|
if variant and variant != self.name:
|
||||||
frappe.throw(_("Item variant {0} exists with same attributes").format(variant[0][0]), ItemVariantExistsError)
|
frappe.throw(_("Item variant {0} exists with same attributes").format(variant), ItemVariantExistsError)
|
||||||
|
|
||||||
def validate_end_of_life(item_code, end_of_life=None, verbose=1):
|
def validate_end_of_life(item_code, end_of_life=None, verbose=1):
|
||||||
if not end_of_life:
|
if not end_of_life:
|
||||||
@ -572,8 +572,10 @@ def find_variant(item, args):
|
|||||||
return variant.name
|
return variant.name
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def create_variant(item, param):
|
def create_variant(item, args):
|
||||||
args = json.loads(param)
|
if isinstance(args, basestring):
|
||||||
|
args = json.loads(args)
|
||||||
|
|
||||||
variant = frappe.new_doc("Item")
|
variant = frappe.new_doc("Item")
|
||||||
variant_attributes = []
|
variant_attributes = []
|
||||||
for d in args:
|
for d in args:
|
||||||
@ -581,9 +583,12 @@ def create_variant(item, param):
|
|||||||
"attribute": d,
|
"attribute": d,
|
||||||
"attribute_value": args[d]
|
"attribute_value": args[d]
|
||||||
})
|
})
|
||||||
|
|
||||||
variant.set("attributes", variant_attributes)
|
variant.set("attributes", variant_attributes)
|
||||||
template = frappe.get_doc("Item", item)
|
template = frappe.get_doc("Item", item)
|
||||||
copy_attributes_to_variant(template, variant)
|
copy_attributes_to_variant(template, variant)
|
||||||
|
make_variant_item_code(template, variant)
|
||||||
|
|
||||||
return variant
|
return variant
|
||||||
|
|
||||||
def copy_attributes_to_variant(item, variant):
|
def copy_attributes_to_variant(item, variant):
|
||||||
@ -600,3 +605,34 @@ def copy_attributes_to_variant(item, variant):
|
|||||||
variant.description += "\n"
|
variant.description += "\n"
|
||||||
for d in variant.attributes:
|
for d in variant.attributes:
|
||||||
variant.description += "<p>" + d.attribute + ": " + cstr(d.attribute_value) + "</p>"
|
variant.description += "<p>" + d.attribute + ": " + cstr(d.attribute_value) + "</p>"
|
||||||
|
|
||||||
|
def make_variant_item_code(template, variant):
|
||||||
|
"""Uses template's item code and abbreviations to make variant's item code"""
|
||||||
|
if variant.item_code:
|
||||||
|
return
|
||||||
|
|
||||||
|
abbreviations = []
|
||||||
|
for attr in variant.attributes:
|
||||||
|
item_attribute = frappe.db.sql("""select i.numeric_values, v.abbr
|
||||||
|
from `tabItem Attribute` i left join `tabItem Attribute Value` v
|
||||||
|
on (i.name=v.parent)
|
||||||
|
where i.name=%(attribute)s and v.attribute_value=%(attribute_value)s""", {
|
||||||
|
"attribute": attr.attribute,
|
||||||
|
"attribute_value": attr.attribute_value
|
||||||
|
}, as_dict=True)
|
||||||
|
|
||||||
|
if not item_attribute:
|
||||||
|
# somehow an invalid item attribute got used
|
||||||
|
return
|
||||||
|
|
||||||
|
if item_attribute[0].numeric_values:
|
||||||
|
# don't generate item code if one of the attributes is numeric
|
||||||
|
return
|
||||||
|
|
||||||
|
abbreviations.append(item_attribute[0].abbr)
|
||||||
|
|
||||||
|
if abbreviations:
|
||||||
|
variant.item_code = "{0}-{1}".format(template.item_code, "-".join(abbreviations))
|
||||||
|
|
||||||
|
if variant.item_code:
|
||||||
|
variant.item_name = variant.item_code
|
||||||
|
Loading…
x
Reference in New Issue
Block a user