blob: 7a1db6d2653fffd609f430e6011d99aa260ee7c0 [file] [log] [blame]
Anand Doshi099bbbd2015-09-02 11:18:32 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Chillar Anand915b3432021-09-02 16:44:59 +05304
5import copy
6import json
7
Anand Doshi099bbbd2015-09-02 11:18:32 +05308import frappe
9from frappe import _
10from frappe.utils import cstr, flt
Achilles Rasquinha56b2e122018-02-13 14:42:40 +053011
Sabu Siyadf900a782023-10-17 17:05:44 +053012from erpnext.utilities.product import get_item_codes_by_attributes
13
Chillar Anand915b3432021-09-02 16:44:59 +053014
Ankush Menat494bd9e2022-03-28 18:52:46 +053015class ItemVariantExistsError(frappe.ValidationError):
16 pass
17
18
19class InvalidItemAttributeValueError(frappe.ValidationError):
20 pass
21
22
23class ItemTemplateCannotHaveStock(frappe.ValidationError):
24 pass
25
Anand Doshi099bbbd2015-09-02 11:18:32 +053026
27@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +053028def get_variant(template, args=None, variant=None, manufacturer=None, manufacturer_part_no=None):
Sabu Siyadf900a782023-10-17 17:05:44 +053029 """
30 Validates Attributes and their Values, then looks for an exactly
Ankush Menat494bd9e2022-03-28 18:52:46 +053031 matching Item Variant
Anand Doshi099bbbd2015-09-02 11:18:32 +053032
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 :param item: Template Item
34 :param args: A dictionary with "Attribute" as key and "Attribute Value" as value
Anand Doshi099bbbd2015-09-02 11:18:32 +053035 """
Ankush Menat494bd9e2022-03-28 18:52:46 +053036 item_template = frappe.get_doc("Item", template)
Anand Doshi099bbbd2015-09-02 11:18:32 +053037
Ankush Menat494bd9e2022-03-28 18:52:46 +053038 if item_template.variant_based_on == "Manufacturer" and manufacturer:
39 return make_variant_based_on_manufacturer(item_template, manufacturer, manufacturer_part_no)
Anand Doshi099bbbd2015-09-02 11:18:32 +053040
Sabu Siyadf900a782023-10-17 17:05:44 +053041 if isinstance(args, str):
42 args = json.loads(args)
43
44 if not args:
45 frappe.throw(_("Please specify at least one attribute in the Attributes table"))
46
47 return find_variant(template, args, variant)
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +010048
Ankush Menat494bd9e2022-03-28 18:52:46 +053049
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +010050def make_variant_based_on_manufacturer(template, manufacturer, manufacturer_part_no):
Ankush Menat494bd9e2022-03-28 18:52:46 +053051 """Make and return a new variant based on manufacturer and
52 manufacturer part no"""
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +010053 from frappe.model.naming import append_number_if_name_exists
54
Ankush Menat494bd9e2022-03-28 18:52:46 +053055 variant = frappe.new_doc("Item")
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +010056
57 copy_attributes_to_variant(template, variant)
58
Rohit Waghchaured7e6b832024-01-17 20:52:16 +053059 variant_name = f"{template.name} - {manufacturer}"
60 if manufacturer_part_no:
61 variant_name += f" - {manufacturer_part_no}"
62
63 variant.item_code = append_number_if_name_exists("Item", variant_name)
mergify[bot]4aa960b2023-12-19 14:59:33 +053064 variant.flags.ignore_mandatory = True
65 variant.save()
66
Akhil Narang3effaf22024-03-27 11:37:26 +053067 if not frappe.db.exists("Item Manufacturer", {"item_code": variant.name, "manufacturer": manufacturer}):
mergify[bot]4aa960b2023-12-19 14:59:33 +053068 manufacturer_doc = frappe.new_doc("Item Manufacturer")
69 manufacturer_doc.update(
70 {
71 "item_code": variant.name,
72 "manufacturer": manufacturer,
73 "manufacturer_part_no": manufacturer_part_no,
74 }
75 )
76
77 manufacturer_doc.flags.ignore_mandatory = True
78 manufacturer_doc.save(ignore_permissions=True)
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +010079
80 return variant
Anand Doshi099bbbd2015-09-02 11:18:32 +053081
Ankush Menat494bd9e2022-03-28 18:52:46 +053082
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +053083def validate_item_variant_attributes(item, args=None):
Ankush Menat8fe5feb2021-11-04 19:48:32 +053084 if isinstance(item, str):
Ankush Menat494bd9e2022-03-28 18:52:46 +053085 item = frappe.get_doc("Item", item)
Rushabh Mehta95383bb2016-07-15 15:11:46 +053086
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +053087 if not args:
Ankush Menat494bd9e2022-03-28 18:52:46 +053088 args = {d.attribute.lower(): d.attribute_value for d in item.attributes}
Rushabh Mehtaaed79e92016-06-02 17:49:16 +053089
Nabin Haitaaf378e2017-12-13 18:40:52 +053090 attribute_values, numeric_values = get_attribute_values(item)
Rushabh Mehtaaed79e92016-06-02 17:49:16 +053091
Anand Doshi099bbbd2015-09-02 11:18:32 +053092 for attribute, value in args.items():
Rushabh Mehta95383bb2016-07-15 15:11:46 +053093 if not value:
94 continue
95
96 if attribute.lower() in numeric_values:
97 numeric_attribute = numeric_values[attribute.lower()]
Rohit Waghchaure6500ef42016-12-15 18:24:32 +053098 validate_is_incremental(numeric_attribute, attribute, value, item.name)
Anand Doshi099bbbd2015-09-02 11:18:32 +053099
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530100 else:
101 attributes_list = attribute_values.get(attribute.lower(), [])
marination0df7f0f2020-06-01 11:56:33 +0530102 validate_item_attribute_value(attributes_list, attribute, value, item.name, from_variant=True)
Anand Doshi099bbbd2015-09-02 11:18:32 +0530103
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530105def validate_is_incremental(numeric_attribute, attribute, value, item):
106 from_range = numeric_attribute.from_range
107 to_range = numeric_attribute.to_range
108 increment = numeric_attribute.increment
Anand Doshi099bbbd2015-09-02 11:18:32 +0530109
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530110 if increment == 0:
111 # defensive validation to prevent ZeroDivisionError
112 frappe.throw(_("Increment for Attribute {0} cannot be 0").format(attribute))
Anand Doshi099bbbd2015-09-02 11:18:32 +0530113
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530114 is_in_range = from_range <= flt(value) <= to_range
115 precision = max(len(cstr(v).split(".")[-1].rstrip("0")) for v in (value, increment))
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116 # avoid precision error by rounding the remainder
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530117 remainder = flt((flt(value) - from_range) % increment, precision)
Anand Doshi099bbbd2015-09-02 11:18:32 +0530118
Ankush Menat494bd9e2022-03-28 18:52:46 +0530119 is_incremental = remainder == 0 or remainder == increment
Rushabh Mehtaaed79e92016-06-02 17:49:16 +0530120
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530121 if not (is_in_range and is_incremental):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530122 frappe.throw(
123 _(
124 "Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3} for Item {4}"
125 ).format(attribute, from_range, to_range, increment, item),
126 InvalidItemAttributeValueError,
127 title=_("Invalid Attribute"),
128 )
Rohit Waghchaure6500ef42016-12-15 18:24:32 +0530129
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130
Akhil Narang3effaf22024-03-27 11:37:26 +0530131def validate_item_attribute_value(attributes_list, attribute, attribute_value, item, from_variant=True):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530132 allow_rename_attribute_value = frappe.db.get_single_value(
133 "Item Variant Settings", "allow_rename_attribute_value"
134 )
Vishal Dhayagude668ec252018-03-07 15:31:08 +0530135 if allow_rename_attribute_value:
136 pass
137 elif attribute_value not in attributes_list:
marination0df7f0f2020-06-01 11:56:33 +0530138 if from_variant:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530139 frappe.throw(
140 _("{0} is not a valid Value for Attribute {1} of Item {2}.").format(
141 frappe.bold(attribute_value), frappe.bold(attribute), frappe.bold(item)
142 ),
143 InvalidItemAttributeValueError,
144 title=_("Invalid Value"),
145 )
marination0df7f0f2020-06-01 11:56:33 +0530146 else:
marination91dfd002020-06-17 19:05:40 +0530147 msg = _("The value {0} is already assigned to an existing Item {1}.").format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530148 frappe.bold(attribute_value), frappe.bold(item)
149 )
150 msg += "<br>" + _(
151 "To still proceed with editing this Attribute Value, enable {0} in Item Variant Settings."
152 ).format(frappe.bold("Allow Rename Attribute Value"))
marination0df7f0f2020-06-01 11:56:33 +0530153
Ankush Menat494bd9e2022-03-28 18:52:46 +0530154 frappe.throw(msg, InvalidItemAttributeValueError, title=_("Edit Not Allowed"))
155
Anand Doshi099bbbd2015-09-02 11:18:32 +0530156
Nabin Haitaaf378e2017-12-13 18:40:52 +0530157def get_attribute_values(item):
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +0530158 if not frappe.flags.attribute_values:
159 attribute_values = {}
Rushabh Mehta95383bb2016-07-15 15:11:46 +0530160 numeric_values = {}
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +0530161 for t in frappe.get_all("Item Attribute Value", fields=["parent", "attribute_value"]):
Rushabh Mehta95383bb2016-07-15 15:11:46 +0530162 attribute_values.setdefault(t.parent.lower(), []).append(t.attribute_value)
163
Ankush Menat494bd9e2022-03-28 18:52:46 +0530164 for t in frappe.get_all(
165 "Item Variant Attribute",
Nabin Haitaaf378e2017-12-13 18:40:52 +0530166 fields=["attribute", "from_range", "to_range", "increment"],
Ankush Menat494bd9e2022-03-28 18:52:46 +0530167 filters={"numeric_values": 1, "parent": item.variant_of},
168 ):
Nabin Haitaaf378e2017-12-13 18:40:52 +0530169 numeric_values[t.attribute.lower()] = t
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +0530170
171 frappe.flags.attribute_values = attribute_values
Rushabh Mehta95383bb2016-07-15 15:11:46 +0530172 frappe.flags.numeric_values = numeric_values
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +0530173
Rushabh Mehta95383bb2016-07-15 15:11:46 +0530174 return frappe.flags.attribute_values, frappe.flags.numeric_values
Rushabh Mehtab8bdfbc2016-07-15 12:40:47 +0530175
Ankush Menat494bd9e2022-03-28 18:52:46 +0530176
Nabin Hait6b068e12015-12-31 13:20:32 +0530177def find_variant(template, args, variant_item_code=None):
Akhil Narang3effaf22024-03-27 11:37:26 +0530178 possible_variants = [i for i in get_item_codes_by_attributes(args, template) if i != variant_item_code]
Anand Doshi099bbbd2015-09-02 11:18:32 +0530179
180 for variant in possible_variants:
181 variant = frappe.get_doc("Item", variant)
182
183 if len(args.keys()) == len(variant.get("attributes")):
184 # has the same number of attributes and values
185 # assuming no duplication as per the validation in Item
186 match_count = 0
187
188 for attribute, value in args.items():
189 for row in variant.attributes:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530190 if row.attribute == attribute and row.attribute_value == cstr(value):
Anand Doshi099bbbd2015-09-02 11:18:32 +0530191 # this row matches
192 match_count += 1
193 break
194
195 if match_count == len(args.keys()):
196 return variant.name
197
Ankush Menat494bd9e2022-03-28 18:52:46 +0530198
Anand Doshi099bbbd2015-09-02 11:18:32 +0530199@frappe.whitelist()
200def create_variant(item, args):
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530201 if isinstance(args, str):
Anand Doshi099bbbd2015-09-02 11:18:32 +0530202 args = json.loads(args)
203
204 template = frappe.get_doc("Item", item)
205 variant = frappe.new_doc("Item")
Ankush Menat494bd9e2022-03-28 18:52:46 +0530206 variant.variant_based_on = "Item Attribute"
Anand Doshi099bbbd2015-09-02 11:18:32 +0530207 variant_attributes = []
208
209 for d in template.attributes:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530210 variant_attributes.append({"attribute": d.attribute, "attribute_value": args.get(d.attribute)})
Anand Doshi099bbbd2015-09-02 11:18:32 +0530211
212 variant.set("attributes", variant_attributes)
213 copy_attributes_to_variant(template, variant)
Prateeksha Singh89cec182017-05-19 12:35:36 +0530214 make_variant_item_code(template.item_code, template.item_name, variant)
Anand Doshi099bbbd2015-09-02 11:18:32 +0530215
216 return variant
217
Ankush Menat494bd9e2022-03-28 18:52:46 +0530218
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530219@frappe.whitelist()
220def enqueue_multiple_variant_creation(item, args):
221 # There can be innumerable attribute combinations, enqueue
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530222 if isinstance(args, str):
Ameya Shenoy935f4a42018-07-03 10:48:59 +0530223 variants = json.loads(args)
224 total_variants = 1
225 for key in variants:
226 total_variants *= len(variants[key])
227 if total_variants >= 600:
Anurag Mishra841d8522019-07-03 15:15:08 +0530228 frappe.throw(_("Please do not create more than 500 items at a time"))
Ameya Shenoy935f4a42018-07-03 10:48:59 +0530229 return
Rushabh Mehta87053712018-08-16 09:22:33 +0530230 if total_variants < 10:
231 return create_multiple_variants(item, args)
232 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530233 frappe.enqueue(
234 "erpnext.controllers.item_variant.create_multiple_variants",
235 item=item,
236 args=args,
237 now=frappe.flags.in_test,
238 )
239 return "queued"
240
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530241
242def create_multiple_variants(item, args):
Rushabh Mehta87053712018-08-16 09:22:33 +0530243 count = 0
Ankush Menat8fe5feb2021-11-04 19:48:32 +0530244 if isinstance(args, str):
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530245 args = json.loads(args)
246
247 args_set = generate_keyed_value_combinations(args)
248
249 for attribute_values in args_set:
250 if not get_variant(item, args=attribute_values):
251 variant = create_variant(item, attribute_values)
252 variant.save()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530253 count += 1
Rushabh Mehta87053712018-08-16 09:22:33 +0530254
255 return count
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530256
Ankush Menat494bd9e2022-03-28 18:52:46 +0530257
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530258def generate_keyed_value_combinations(args):
259 """
260 From this:
261
Ankush Menat494bd9e2022-03-28 18:52:46 +0530262 args = {"attr1": ["a", "b", "c"], "attr2": ["1", "2"], "attr3": ["A"]}
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530263
264 To this:
265
Ankush Menat494bd9e2022-03-28 18:52:46 +0530266 [
267 {u'attr1': u'a', u'attr2': u'1', u'attr3': u'A'},
268 {u'attr1': u'b', u'attr2': u'1', u'attr3': u'A'},
269 {u'attr1': u'c', u'attr2': u'1', u'attr3': u'A'},
270 {u'attr1': u'a', u'attr2': u'2', u'attr3': u'A'},
271 {u'attr1': u'b', u'attr2': u'2', u'attr3': u'A'},
272 {u'attr1': u'c', u'attr2': u'2', u'attr3': u'A'}
273 ]
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530274
275 """
276 # Return empty list if empty
277 if not args:
278 return []
279
280 # Turn `args` into a list of lists of key-value tuples:
281 # [
282 # [(u'attr2', u'1'), (u'attr2', u'2')],
283 # [(u'attr3', u'A')],
284 # [(u'attr1', u'a'), (u'attr1', u'b'), (u'attr1', u'c')]
285 # ]
286 key_value_lists = [[(key, val) for val in args[key]] for key in args.keys()]
287
288 # Store the first, but as objects
289 # [{u'attr2': u'1'}, {u'attr2': u'2'}]
290 results = key_value_lists.pop(0)
291 results = [{d[0]: d[1]} for d in results]
292
293 # Iterate the remaining
294 # Take the next list to fuse with existing results
295 for l in key_value_lists:
296 new_results = []
297 for res in results:
298 for key_val in l:
299 # create a new clone of object in result
300 obj = copy.deepcopy(res)
301 # to be used with every incoming new value
302 obj[key_val[0]] = key_val[1]
303 # and pushed into new_results
304 new_results.append(obj)
305 results = new_results
306
307 return results
308
Ankush Menat494bd9e2022-03-28 18:52:46 +0530309
Anand Doshi099bbbd2015-09-02 11:18:32 +0530310def copy_attributes_to_variant(item, variant):
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +0100311 # copy non no-copy fields
312
Ankush Menat494bd9e2022-03-28 18:52:46 +0530313 exclude_fields = [
314 "naming_series",
315 "item_code",
316 "item_name",
317 "published_in_website",
318 "opening_stock",
319 "variant_of",
320 "valuation_rate",
321 ]
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +0100322
Ankush Menat494bd9e2022-03-28 18:52:46 +0530323 if item.variant_based_on == "Manufacturer":
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +0100324 # don't copy manufacturer values if based on part no
Ankush Menat494bd9e2022-03-28 18:52:46 +0530325 exclude_fields += ["manufacturer", "manufacturer_part_no"]
Rushabh Mehtaa07c43f2017-03-21 17:48:34 +0100326
Ankush Menat494bd9e2022-03-28 18:52:46 +0530327 allow_fields = [d.field_name for d in frappe.get_all("Variant Field", fields=["field_name"])]
Nabin Hait945f5022017-09-29 15:11:50 +0530328 if "variant_based_on" not in allow_fields:
329 allow_fields.append("variant_based_on")
Anand Doshi099bbbd2015-09-02 11:18:32 +0530330 for field in item.meta.fields:
tundebabzy6015f0f2017-07-04 11:13:02 +0100331 # "Table" is part of `no_value_field` but we shouldn't ignore tables
Rohit Waghchaure0e28fcc2017-08-29 18:15:57 +0530332 if (field.reqd or field.fieldname in allow_fields) and field.fieldname not in exclude_fields:
Anand Doshi099bbbd2015-09-02 11:18:32 +0530333 if variant.get(field.fieldname) != item.get(field.fieldname):
Nabin Hait945f5022017-09-29 15:11:50 +0530334 if field.fieldtype == "Table":
335 variant.set(field.fieldname, [])
336 for d in item.get(field.fieldname):
337 row = copy.deepcopy(d)
338 if row.get("name"):
339 row.name = None
340 variant.append(field.fieldname, row)
341 else:
342 variant.set(field.fieldname, item.get(field.fieldname))
Nabin Haitc3144852017-09-28 18:55:40 +0530343
Rohit Waghchaure0eef3f62018-06-13 13:06:25 +0530344 variant.variant_of = item.name
Anurag Mishra62d58ac2019-05-30 14:04:08 +0530345
Ankush Menat494bd9e2022-03-28 18:52:46 +0530346 if "description" not in allow_fields:
hiousi38de9942018-04-24 08:40:45 +0200347 if not variant.description:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530348 variant.description = ""
Rucha Mahabalc6b548b2019-09-24 19:17:13 +0530349 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530350 if item.variant_based_on == "Item Attribute":
hiousi38de9942018-04-24 08:40:45 +0200351 if variant.attributes:
Anurag Mishra62d58ac2019-05-30 14:04:08 +0530352 attributes_description = item.description + " "
hiousi38de9942018-04-24 08:40:45 +0200353 for d in variant.attributes:
Akhil Narang3effaf22024-03-27 11:37:26 +0530354 attributes_description += (
355 "<div>" + d.attribute + ": " + cstr(d.attribute_value) + "</div>"
356 )
Prateeksha Singh8f43d252017-11-16 18:06:26 +0530357
hiousi38de9942018-04-24 08:40:45 +0200358 if attributes_description not in variant.description:
Rucha Mahabalc6b548b2019-09-24 19:17:13 +0530359 variant.description = attributes_description
Anand Doshi099bbbd2015-09-02 11:18:32 +0530360
Ankush Menat494bd9e2022-03-28 18:52:46 +0530361
Prateeksha Singh89cec182017-05-19 12:35:36 +0530362def make_variant_item_code(template_item_code, template_item_name, variant):
Anand Doshi099bbbd2015-09-02 11:18:32 +0530363 """Uses template's item code and abbreviations to make variant's item code"""
364 if variant.item_code:
365 return
366
367 abbreviations = []
368 for attr in variant.attributes:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530369 item_attribute = frappe.db.sql(
370 """select i.numeric_values, v.abbr
Anand Doshi099bbbd2015-09-02 11:18:32 +0530371 from `tabItem Attribute` i left join `tabItem Attribute Value` v
372 on (i.name=v.parent)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530373 where i.name=%(attribute)s and (v.attribute_value=%(attribute_value)s or i.numeric_values = 1)""",
374 {"attribute": attr.attribute, "attribute_value": attr.attribute_value},
375 as_dict=True,
376 )
Anand Doshi099bbbd2015-09-02 11:18:32 +0530377
378 if not item_attribute:
Faris Ansariab148922019-05-03 13:57:20 +0530379 continue
Rushabh Mehta95383bb2016-07-15 15:11:46 +0530380 # frappe.throw(_('Invalid attribute {0} {1}').format(frappe.bold(attr.attribute),
381 # frappe.bold(attr.attribute_value)), title=_('Invalid Attribute'),
382 # exc=InvalidItemAttributeValueError)
Anand Doshi099bbbd2015-09-02 11:18:32 +0530383
Ankush Menat494bd9e2022-03-28 18:52:46 +0530384 abbr_or_value = (
385 cstr(attr.attribute_value) if item_attribute[0].numeric_values else item_attribute[0].abbr
386 )
Rohit Waghchaure01693412017-03-09 17:02:55 +0530387 abbreviations.append(abbr_or_value)
Anand Doshi099bbbd2015-09-02 11:18:32 +0530388
389 if abbreviations:
Akhil Narang3effaf22024-03-27 11:37:26 +0530390 variant.item_code = "{}-{}".format(template_item_code, "-".join(abbreviations))
391 variant.item_name = "{}-{}".format(template_item_name, "-".join(abbreviations))
Rushabh Mehtad5c64162017-11-14 15:27:28 +0530392
Ankush Menat494bd9e2022-03-28 18:52:46 +0530393
Rushabh Mehtad5c64162017-11-14 15:27:28 +0530394@frappe.whitelist()
395def create_variant_doc_for_quick_entry(template, args):
396 variant_based_on = frappe.db.get_value("Item", template, "variant_based_on")
397 args = json.loads(args)
398 if variant_based_on == "Manufacturer":
399 variant = get_variant(template, **args)
400 else:
401 existing_variant = get_variant(template, args)
402 if existing_variant:
403 return existing_variant
404 else:
405 variant = create_variant(template, args=args)
406 variant.name = variant.item_code
407 validate_item_variant_attributes(variant, args)
408 return variant.as_dict()