feat: create variant with/without image (#41317)
* feat: create variant with/without image * feat: create variant with/without image * feat: create variant with/without image * feat: create variant with/without image * feat: create variant with/without image * feat: create variant with/without image * fix: change the variable name use_same_image to use_template_image
This commit is contained in:
committed by
GitHub
parent
6d42cd0f4c
commit
66b35ec9fb
@@ -41,7 +41,8 @@ def get_variant(template, args=None, variant=None, manufacturer=None, manufactur
|
|||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
args = json.loads(args)
|
args = json.loads(args)
|
||||||
|
|
||||||
if not args:
|
attribute_args = {k: v for k, v in args.items() if k != "use_template_image"}
|
||||||
|
if not attribute_args:
|
||||||
frappe.throw(_("Please specify at least one attribute in the Attributes table"))
|
frappe.throw(_("Please specify at least one attribute in the Attributes table"))
|
||||||
|
|
||||||
return find_variant(template, args, variant)
|
return find_variant(template, args, variant)
|
||||||
@@ -197,7 +198,8 @@ def find_variant(template, args, variant_item_code=None):
|
|||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def create_variant(item, args):
|
def create_variant(item, args, use_template_image=False):
|
||||||
|
use_template_image = frappe.parse_json(use_template_image)
|
||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
args = json.loads(args)
|
args = json.loads(args)
|
||||||
|
|
||||||
@@ -211,13 +213,18 @@ def create_variant(item, args):
|
|||||||
|
|
||||||
variant.set("attributes", variant_attributes)
|
variant.set("attributes", variant_attributes)
|
||||||
copy_attributes_to_variant(template, variant)
|
copy_attributes_to_variant(template, variant)
|
||||||
|
|
||||||
|
if use_template_image and template.image:
|
||||||
|
variant.image = template.image
|
||||||
|
|
||||||
make_variant_item_code(template.item_code, template.item_name, variant)
|
make_variant_item_code(template.item_code, template.item_name, variant)
|
||||||
|
|
||||||
return variant
|
return variant
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def enqueue_multiple_variant_creation(item, args):
|
def enqueue_multiple_variant_creation(item, args, use_template_image=False):
|
||||||
|
use_template_image = frappe.parse_json(use_template_image)
|
||||||
# There can be innumerable attribute combinations, enqueue
|
# There can be innumerable attribute combinations, enqueue
|
||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
variants = json.loads(args)
|
variants = json.loads(args)
|
||||||
@@ -228,27 +235,31 @@ def enqueue_multiple_variant_creation(item, args):
|
|||||||
frappe.throw(_("Please do not create more than 500 items at a time"))
|
frappe.throw(_("Please do not create more than 500 items at a time"))
|
||||||
return
|
return
|
||||||
if total_variants < 10:
|
if total_variants < 10:
|
||||||
return create_multiple_variants(item, args)
|
return create_multiple_variants(item, args, use_template_image)
|
||||||
else:
|
else:
|
||||||
frappe.enqueue(
|
frappe.enqueue(
|
||||||
"erpnext.controllers.item_variant.create_multiple_variants",
|
"erpnext.controllers.item_variant.create_multiple_variants",
|
||||||
item=item,
|
item=item,
|
||||||
args=args,
|
args=args,
|
||||||
|
use_template_image=use_template_image,
|
||||||
now=frappe.flags.in_test,
|
now=frappe.flags.in_test,
|
||||||
)
|
)
|
||||||
return "queued"
|
return "queued"
|
||||||
|
|
||||||
|
|
||||||
def create_multiple_variants(item, args):
|
def create_multiple_variants(item, args, use_template_image=False):
|
||||||
count = 0
|
count = 0
|
||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
args = json.loads(args)
|
args = json.loads(args)
|
||||||
|
|
||||||
|
template_item = frappe.get_doc("Item", item)
|
||||||
args_set = generate_keyed_value_combinations(args)
|
args_set = generate_keyed_value_combinations(args)
|
||||||
|
|
||||||
for attribute_values in args_set:
|
for attribute_values in args_set:
|
||||||
if not get_variant(item, args=attribute_values):
|
if not get_variant(item, args=attribute_values):
|
||||||
variant = create_variant(item, attribute_values)
|
variant = create_variant(item, attribute_values)
|
||||||
|
if use_template_image and template_item.image:
|
||||||
|
variant.image = template_item.image
|
||||||
variant.save()
|
variant.save()
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
|
|||||||
@@ -587,6 +587,14 @@ $.extend(erpnext.item, {
|
|||||||
me.multiple_variant_dialog = new frappe.ui.Dialog({
|
me.multiple_variant_dialog = new frappe.ui.Dialog({
|
||||||
title: __("Select Attribute Values"),
|
title: __("Select Attribute Values"),
|
||||||
fields: [
|
fields: [
|
||||||
|
frm.doc.image
|
||||||
|
? {
|
||||||
|
fieldtype: "Check",
|
||||||
|
label: __("Create a variant with the template image."),
|
||||||
|
fieldname: "use_template_image",
|
||||||
|
default: 0,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
{
|
{
|
||||||
fieldtype: "HTML",
|
fieldtype: "HTML",
|
||||||
fieldname: "help",
|
fieldname: "help",
|
||||||
@@ -594,11 +602,14 @@ $.extend(erpnext.item, {
|
|||||||
${__("Select at least one value from each of the attributes.")}
|
${__("Select at least one value from each of the attributes.")}
|
||||||
</label>`,
|
</label>`,
|
||||||
},
|
},
|
||||||
].concat(fields),
|
]
|
||||||
|
.concat(fields)
|
||||||
|
.filter(Boolean),
|
||||||
});
|
});
|
||||||
|
|
||||||
me.multiple_variant_dialog.set_primary_action(__("Create Variants"), () => {
|
me.multiple_variant_dialog.set_primary_action(__("Create Variants"), () => {
|
||||||
let selected_attributes = get_selected_attributes();
|
let selected_attributes = get_selected_attributes();
|
||||||
|
let use_template_image = me.multiple_variant_dialog.get_value("use_template_image");
|
||||||
|
|
||||||
me.multiple_variant_dialog.hide();
|
me.multiple_variant_dialog.hide();
|
||||||
frappe.call({
|
frappe.call({
|
||||||
@@ -606,6 +617,7 @@ $.extend(erpnext.item, {
|
|||||||
args: {
|
args: {
|
||||||
item: frm.doc.name,
|
item: frm.doc.name,
|
||||||
args: selected_attributes,
|
args: selected_attributes,
|
||||||
|
use_template_image: use_template_image,
|
||||||
},
|
},
|
||||||
callback: function (r) {
|
callback: function (r) {
|
||||||
if (r.message === "queued") {
|
if (r.message === "queued") {
|
||||||
@@ -720,6 +732,15 @@ $.extend(erpnext.item, {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (frm.doc.image) {
|
||||||
|
fields.push({
|
||||||
|
fieldtype: "Check",
|
||||||
|
label: __("Create a variant with the template image."),
|
||||||
|
fieldname: "use_template_image",
|
||||||
|
default: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var d = new frappe.ui.Dialog({
|
var d = new frappe.ui.Dialog({
|
||||||
title: __("Create Variant"),
|
title: __("Create Variant"),
|
||||||
fields: fields,
|
fields: fields,
|
||||||
@@ -761,6 +782,7 @@ $.extend(erpnext.item, {
|
|||||||
args: {
|
args: {
|
||||||
item: frm.doc.name,
|
item: frm.doc.name,
|
||||||
args: d.get_values(),
|
args: d.get_values(),
|
||||||
|
use_template_image: args.use_template_image,
|
||||||
},
|
},
|
||||||
callback: function (r) {
|
callback: function (r) {
|
||||||
var doclist = frappe.model.sync(r.message);
|
var doclist = frappe.model.sync(r.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user