From 66b35ec9fb21723d237ce04c1976aa2caed58871 Mon Sep 17 00:00:00 2001 From: "Nihantra C. Patel" <141945075+Nihantra-Patel@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:59:41 +0530 Subject: [PATCH] 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 --- erpnext/controllers/item_variant.py | 21 ++++++++++++++++----- erpnext/stock/doctype/item/item.js | 24 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/erpnext/controllers/item_variant.py b/erpnext/controllers/item_variant.py index 7a1db6d2653..cc6870f892a 100644 --- a/erpnext/controllers/item_variant.py +++ b/erpnext/controllers/item_variant.py @@ -41,7 +41,8 @@ def get_variant(template, args=None, variant=None, manufacturer=None, manufactur if isinstance(args, str): 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")) return find_variant(template, args, variant) @@ -197,7 +198,8 @@ def find_variant(template, args, variant_item_code=None): @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): args = json.loads(args) @@ -211,13 +213,18 @@ def create_variant(item, args): variant.set("attributes", variant_attributes) 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) return variant @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 if isinstance(args, str): 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")) return if total_variants < 10: - return create_multiple_variants(item, args) + return create_multiple_variants(item, args, use_template_image) else: frappe.enqueue( "erpnext.controllers.item_variant.create_multiple_variants", item=item, args=args, + use_template_image=use_template_image, now=frappe.flags.in_test, ) return "queued" -def create_multiple_variants(item, args): +def create_multiple_variants(item, args, use_template_image=False): count = 0 if isinstance(args, str): args = json.loads(args) + template_item = frappe.get_doc("Item", item) args_set = generate_keyed_value_combinations(args) for attribute_values in args_set: if not get_variant(item, args=attribute_values): variant = create_variant(item, attribute_values) + if use_template_image and template_item.image: + variant.image = template_item.image variant.save() count += 1 diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index d92a998a471..81eeb914af0 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -587,6 +587,14 @@ $.extend(erpnext.item, { me.multiple_variant_dialog = new frappe.ui.Dialog({ title: __("Select Attribute Values"), fields: [ + frm.doc.image + ? { + fieldtype: "Check", + label: __("Create a variant with the template image."), + fieldname: "use_template_image", + default: 0, + } + : null, { fieldtype: "HTML", fieldname: "help", @@ -594,11 +602,14 @@ $.extend(erpnext.item, { ${__("Select at least one value from each of the attributes.")} `, }, - ].concat(fields), + ] + .concat(fields) + .filter(Boolean), }); me.multiple_variant_dialog.set_primary_action(__("Create Variants"), () => { let selected_attributes = get_selected_attributes(); + let use_template_image = me.multiple_variant_dialog.get_value("use_template_image"); me.multiple_variant_dialog.hide(); frappe.call({ @@ -606,6 +617,7 @@ $.extend(erpnext.item, { args: { item: frm.doc.name, args: selected_attributes, + use_template_image: use_template_image, }, callback: function (r) { 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({ title: __("Create Variant"), fields: fields, @@ -761,6 +782,7 @@ $.extend(erpnext.item, { args: { item: frm.doc.name, args: d.get_values(), + use_template_image: args.use_template_image, }, callback: function (r) { var doclist = frappe.model.sync(r.message);