[fix] generate item code for variant

This commit is contained in:
Anand Doshi 2015-08-19 10:21:24 +05:30
parent 2538e51887
commit 90fd6fee23
2 changed files with 54 additions and 18 deletions

View File

@ -9,7 +9,7 @@ frappe.ui.form.on("Item", {
if (frm.doc.variant_of){
frm.fields_dict["attributes"].grid.set_column_disp("attribute_value", true);
}
},
refresh: function(frm) {
@ -34,7 +34,7 @@ frappe.ui.form.on("Item", {
frm.add_custom_button(__("Show Variants"), function() {
frappe.set_route("List", "Item", {"variant_of": frm.doc.name});
}, "icon-list", "btn-default");
frm.add_custom_button(__("Make Variant"), function() {
erpnext.item.make_variant()
}, "icon-list", "btn-default");
@ -57,7 +57,7 @@ frappe.ui.form.on("Item", {
}
erpnext.item.toggle_reqd(frm);
erpnext.item.toggle_attributes(frm);
},
@ -93,7 +93,7 @@ frappe.ui.form.on("Item", {
is_stock_item: function(frm) {
erpnext.item.toggle_reqd(frm);
},
has_variants: function(frm) {
erpnext.item.toggle_attributes(frm);
}
@ -193,10 +193,10 @@ $.extend(erpnext.item, {
validated = 0;
}
},
make_variant: function(doc) {
var fields = []
for(var i=0;i< cur_frm.doc.attributes.length;i++){
var fieldtype, desc;
var row = cur_frm.doc.attributes[i];
@ -221,8 +221,8 @@ $.extend(erpnext.item, {
title: __("Make Variant"),
fields: fields
});
d.set_primary_action(__("Make"), function() {
d.set_primary_action(__("Make"), function() {
args = d.get_values();
if(!args) return;
frappe.call({
@ -234,8 +234,8 @@ $.extend(erpnext.item, {
callback: function(r) {
// returns variant item
if (r.message) {
var variant = r.message[0];
var msgprint_dialog = frappe.msgprint(__("Item Variant {0} already exists with same attributes",
var variant = r.message;
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>', {
item_encoded: encodeURIComponent(variant),
item: variant
@ -251,7 +251,7 @@ $.extend(erpnext.item, {
method:"erpnext.stock.doctype.item.item.create_variant",
args: {
"item": cur_frm.doc.name,
"param": d.get_values()
"args": d.get_values()
},
callback: function(r) {
var doclist = frappe.model.sync(r.message);
@ -262,17 +262,17 @@ $.extend(erpnext.item, {
}
});
});
d.show();
$.each(d.fields_dict, function(i, field) {
if(field.df.fieldtype !== "Data") {
return;
}
$(field.input_area).addClass("ui-front");
field.$input.autocomplete({
minLength: 0,
minChars: 0,

View File

@ -352,8 +352,8 @@ class Item(WebsiteGenerator):
args[d.attribute] = d.attribute_value
variant = get_variant(self.variant_of, args)
if variant and variant[0][0] != self.name:
frappe.throw(_("Item variant {0} exists with same attributes").format(variant[0][0]), ItemVariantExistsError)
if variant and variant != self.name:
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):
if not end_of_life:
@ -572,8 +572,10 @@ def find_variant(item, args):
return variant.name
@frappe.whitelist()
def create_variant(item, param):
args = json.loads(param)
def create_variant(item, args):
if isinstance(args, basestring):
args = json.loads(args)
variant = frappe.new_doc("Item")
variant_attributes = []
for d in args:
@ -581,9 +583,12 @@ def create_variant(item, param):
"attribute": d,
"attribute_value": args[d]
})
variant.set("attributes", variant_attributes)
template = frappe.get_doc("Item", item)
copy_attributes_to_variant(template, variant)
make_variant_item_code(template, variant)
return variant
def copy_attributes_to_variant(item, variant):
@ -600,3 +605,34 @@ def copy_attributes_to_variant(item, variant):
variant.description += "\n"
for d in variant.attributes:
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