Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | from frappe import _ |
| 7 | from frappe.utils import cstr, flt |
Nabin Hait | 945f502 | 2017-09-29 15:11:50 +0530 | [diff] [blame] | 8 | import json, copy |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 9 | |
| 10 | class ItemVariantExistsError(frappe.ValidationError): pass |
| 11 | class InvalidItemAttributeValueError(frappe.ValidationError): pass |
| 12 | class ItemTemplateCannotHaveStock(frappe.ValidationError): pass |
| 13 | |
| 14 | @frappe.whitelist() |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 15 | def get_variant(template, args=None, variant=None, manufacturer=None, |
| 16 | manufacturer_part_no=None): |
| 17 | """Validates Attributes and their Values, then looks for an exactly |
| 18 | matching Item Variant |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 19 | |
| 20 | :param item: Template Item |
| 21 | :param args: A dictionary with "Attribute" as key and "Attribute Value" as value |
| 22 | """ |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 23 | item_template = frappe.get_doc('Item', template) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 24 | |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 25 | if item_template.variant_based_on=='Manufacturer' and manufacturer: |
| 26 | return make_variant_based_on_manufacturer(item_template, manufacturer, |
| 27 | manufacturer_part_no) |
| 28 | else: |
| 29 | if isinstance(args, basestring): |
| 30 | args = json.loads(args) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 31 | |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 32 | if not args: |
| 33 | frappe.throw(_("Please specify at least one attribute in the Attributes table")) |
| 34 | return find_variant(template, args, variant) |
| 35 | |
| 36 | def make_variant_based_on_manufacturer(template, manufacturer, manufacturer_part_no): |
| 37 | '''Make and return a new variant based on manufacturer and |
| 38 | manufacturer part no''' |
| 39 | from frappe.model.naming import append_number_if_name_exists |
| 40 | |
| 41 | variant = frappe.new_doc('Item') |
| 42 | |
| 43 | copy_attributes_to_variant(template, variant) |
| 44 | |
Nabin Hait | b6a8920 | 2017-04-25 17:27:53 +0530 | [diff] [blame] | 45 | variant.manufacturer = manufacturer |
| 46 | variant.manufacturer_part_no = manufacturer_part_no |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 47 | |
| 48 | variant.item_code = append_number_if_name_exists('Item', template.name) |
| 49 | |
| 50 | return variant |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 52 | def validate_item_variant_attributes(item, args=None): |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 53 | if isinstance(item, basestring): |
| 54 | item = frappe.get_doc('Item', item) |
| 55 | |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 56 | if not args: |
Rushabh Mehta | 20122ae | 2016-07-15 12:42:41 +0530 | [diff] [blame] | 57 | args = {d.attribute.lower():d.attribute_value for d in item.attributes} |
Rushabh Mehta | aed79e9 | 2016-06-02 17:49:16 +0530 | [diff] [blame] | 58 | |
Nabin Hait | aaf378e | 2017-12-13 18:40:52 +0530 | [diff] [blame] | 59 | attribute_values, numeric_values = get_attribute_values(item) |
Rushabh Mehta | aed79e9 | 2016-06-02 17:49:16 +0530 | [diff] [blame] | 60 | |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 61 | for attribute, value in args.items(): |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 62 | if not value: |
| 63 | continue |
| 64 | |
| 65 | if attribute.lower() in numeric_values: |
| 66 | numeric_attribute = numeric_values[attribute.lower()] |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 67 | validate_is_incremental(numeric_attribute, attribute, value, item.name) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 68 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 69 | else: |
| 70 | attributes_list = attribute_values.get(attribute.lower(), []) |
| 71 | validate_item_attribute_value(attributes_list, attribute, value, item.name) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 72 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 73 | def validate_is_incremental(numeric_attribute, attribute, value, item): |
| 74 | from_range = numeric_attribute.from_range |
| 75 | to_range = numeric_attribute.to_range |
| 76 | increment = numeric_attribute.increment |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 77 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 78 | if increment == 0: |
| 79 | # defensive validation to prevent ZeroDivisionError |
| 80 | frappe.throw(_("Increment for Attribute {0} cannot be 0").format(attribute)) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 81 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 82 | is_in_range = from_range <= flt(value) <= to_range |
| 83 | precision = max(len(cstr(v).split(".")[-1].rstrip("0")) for v in (value, increment)) |
| 84 | #avoid precision error by rounding the remainder |
| 85 | remainder = flt((flt(value) - from_range) % increment, precision) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 86 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 87 | is_incremental = remainder==0 or remainder==increment |
Rushabh Mehta | aed79e9 | 2016-06-02 17:49:16 +0530 | [diff] [blame] | 88 | |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 89 | if not (is_in_range and is_incremental): |
| 90 | frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3} for Item {4}")\ |
| 91 | .format(attribute, from_range, to_range, increment, item), |
| 92 | InvalidItemAttributeValueError, title=_('Invalid Attribute')) |
| 93 | |
| 94 | def validate_item_attribute_value(attributes_list, attribute, attribute_value, item): |
Vishal Dhayagude | 668ec25 | 2018-03-07 15:31:08 +0530 | [diff] [blame] | 95 | allow_rename_attribute_value = frappe.db.get_single_value('Item Variant Settings', 'allow_rename_attribute_value') |
| 96 | if allow_rename_attribute_value: |
| 97 | pass |
| 98 | elif attribute_value not in attributes_list: |
Rohit Waghchaure | 6500ef4 | 2016-12-15 18:24:32 +0530 | [diff] [blame] | 99 | frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values for Item {2}").format( |
| 100 | attribute_value, attribute, item), InvalidItemAttributeValueError, title=_('Invalid Attribute')) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 101 | |
Nabin Hait | aaf378e | 2017-12-13 18:40:52 +0530 | [diff] [blame] | 102 | def get_attribute_values(item): |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 103 | if not frappe.flags.attribute_values: |
| 104 | attribute_values = {} |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 105 | numeric_values = {} |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 106 | for t in frappe.get_all("Item Attribute Value", fields=["parent", "attribute_value"]): |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 107 | attribute_values.setdefault(t.parent.lower(), []).append(t.attribute_value) |
| 108 | |
Nabin Hait | aaf378e | 2017-12-13 18:40:52 +0530 | [diff] [blame] | 109 | for t in frappe.get_all('Item Variant Attribute', |
| 110 | fields=["attribute", "from_range", "to_range", "increment"], |
| 111 | filters={'numeric_values': 1, 'parent': item.variant_of}): |
| 112 | numeric_values[t.attribute.lower()] = t |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 113 | |
| 114 | frappe.flags.attribute_values = attribute_values |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 115 | frappe.flags.numeric_values = numeric_values |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 117 | return frappe.flags.attribute_values, frappe.flags.numeric_values |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 6b068e1 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 119 | def find_variant(template, args, variant_item_code=None): |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 120 | conditions = ["""(iv_attribute.attribute="{0}" and iv_attribute.attribute_value="{1}")"""\ |
| 121 | .format(frappe.db.escape(key), frappe.db.escape(cstr(value))) for key, value in args.items()] |
| 122 | |
| 123 | conditions = " or ".join(conditions) |
| 124 | |
| 125 | # use approximate match and shortlist possible variant matches |
| 126 | # it is approximate because we are matching using OR condition |
| 127 | # and it need not be exact match at this stage |
| 128 | # this uses a simpler query instead of using multiple exists conditions |
| 129 | possible_variants = frappe.db.sql_list("""select name from `tabItem` item |
| 130 | where variant_of=%s and exists ( |
| 131 | select name from `tabItem Variant Attribute` iv_attribute |
| 132 | where iv_attribute.parent=item.name |
Nabin Hait | 6626e32 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 133 | and ({conditions}) and parent != %s |
Nabin Hait | 6b068e1 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 134 | )""".format(conditions=conditions), (template, cstr(variant_item_code))) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 135 | |
| 136 | for variant in possible_variants: |
| 137 | variant = frappe.get_doc("Item", variant) |
| 138 | |
| 139 | if len(args.keys()) == len(variant.get("attributes")): |
| 140 | # has the same number of attributes and values |
| 141 | # assuming no duplication as per the validation in Item |
| 142 | match_count = 0 |
| 143 | |
| 144 | for attribute, value in args.items(): |
| 145 | for row in variant.attributes: |
| 146 | if row.attribute==attribute and row.attribute_value== cstr(value): |
| 147 | # this row matches |
| 148 | match_count += 1 |
| 149 | break |
| 150 | |
| 151 | if match_count == len(args.keys()): |
| 152 | return variant.name |
| 153 | |
| 154 | @frappe.whitelist() |
| 155 | def create_variant(item, args): |
| 156 | if isinstance(args, basestring): |
| 157 | args = json.loads(args) |
| 158 | |
| 159 | template = frappe.get_doc("Item", item) |
| 160 | variant = frappe.new_doc("Item") |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 161 | variant.variant_based_on = 'Item Attribute' |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 162 | variant_attributes = [] |
| 163 | |
| 164 | for d in template.attributes: |
| 165 | variant_attributes.append({ |
| 166 | "attribute": d.attribute, |
| 167 | "attribute_value": args.get(d.attribute) |
| 168 | }) |
| 169 | |
| 170 | variant.set("attributes", variant_attributes) |
| 171 | copy_attributes_to_variant(template, variant) |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 172 | make_variant_item_code(template.item_code, template.item_name, variant) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 173 | |
| 174 | return variant |
| 175 | |
Prateeksha Singh | 8f43d25 | 2017-11-16 18:06:26 +0530 | [diff] [blame] | 176 | @frappe.whitelist() |
| 177 | def enqueue_multiple_variant_creation(item, args): |
| 178 | # There can be innumerable attribute combinations, enqueue |
| 179 | frappe.enqueue("erpnext.controllers.item_variant.create_multiple_variants", |
| 180 | item=item, args=args, now=frappe.flags.in_test); |
| 181 | |
| 182 | def create_multiple_variants(item, args): |
| 183 | if isinstance(args, basestring): |
| 184 | args = json.loads(args) |
| 185 | |
| 186 | args_set = generate_keyed_value_combinations(args) |
| 187 | |
| 188 | for attribute_values in args_set: |
| 189 | if not get_variant(item, args=attribute_values): |
| 190 | variant = create_variant(item, attribute_values) |
| 191 | variant.save() |
| 192 | |
| 193 | def generate_keyed_value_combinations(args): |
| 194 | """ |
| 195 | From this: |
| 196 | |
| 197 | args = {"attr1": ["a", "b", "c"], "attr2": ["1", "2"], "attr3": ["A"]} |
| 198 | |
| 199 | To this: |
| 200 | |
| 201 | [ |
| 202 | {u'attr1': u'a', u'attr2': u'1', u'attr3': u'A'}, |
| 203 | {u'attr1': u'b', u'attr2': u'1', u'attr3': u'A'}, |
| 204 | {u'attr1': u'c', u'attr2': u'1', u'attr3': u'A'}, |
| 205 | {u'attr1': u'a', u'attr2': u'2', u'attr3': u'A'}, |
| 206 | {u'attr1': u'b', u'attr2': u'2', u'attr3': u'A'}, |
| 207 | {u'attr1': u'c', u'attr2': u'2', u'attr3': u'A'} |
| 208 | ] |
| 209 | |
| 210 | """ |
| 211 | # Return empty list if empty |
| 212 | if not args: |
| 213 | return [] |
| 214 | |
| 215 | # Turn `args` into a list of lists of key-value tuples: |
| 216 | # [ |
| 217 | # [(u'attr2', u'1'), (u'attr2', u'2')], |
| 218 | # [(u'attr3', u'A')], |
| 219 | # [(u'attr1', u'a'), (u'attr1', u'b'), (u'attr1', u'c')] |
| 220 | # ] |
| 221 | key_value_lists = [[(key, val) for val in args[key]] for key in args.keys()] |
| 222 | |
| 223 | # Store the first, but as objects |
| 224 | # [{u'attr2': u'1'}, {u'attr2': u'2'}] |
| 225 | results = key_value_lists.pop(0) |
| 226 | results = [{d[0]: d[1]} for d in results] |
| 227 | |
| 228 | # Iterate the remaining |
| 229 | # Take the next list to fuse with existing results |
| 230 | for l in key_value_lists: |
| 231 | new_results = [] |
| 232 | for res in results: |
| 233 | for key_val in l: |
| 234 | # create a new clone of object in result |
| 235 | obj = copy.deepcopy(res) |
| 236 | # to be used with every incoming new value |
| 237 | obj[key_val[0]] = key_val[1] |
| 238 | # and pushed into new_results |
| 239 | new_results.append(obj) |
| 240 | results = new_results |
| 241 | |
| 242 | return results |
| 243 | |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 244 | def copy_attributes_to_variant(item, variant): |
| 245 | from frappe.model import no_value_fields |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 246 | |
| 247 | # copy non no-copy fields |
| 248 | |
Nabin Hait | c314485 | 2017-09-28 18:55:40 +0530 | [diff] [blame] | 249 | exclude_fields = ["naming_series", "item_code", "item_name", "show_in_website", |
Nabin Hait | 945f502 | 2017-09-29 15:11:50 +0530 | [diff] [blame] | 250 | "show_variant_in_website", "opening_stock", "variant_of", "valuation_rate"] |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 251 | |
| 252 | if item.variant_based_on=='Manufacturer': |
| 253 | # don't copy manufacturer values if based on part no |
| 254 | exclude_fields += ['manufacturer', 'manufacturer_part_no'] |
| 255 | |
Rohit Waghchaure | 0e28fcc | 2017-08-29 18:15:57 +0530 | [diff] [blame] | 256 | allow_fields = [d.field_name for d in frappe.get_all("Variant Field", fields = ['field_name'])] |
Nabin Hait | 945f502 | 2017-09-29 15:11:50 +0530 | [diff] [blame] | 257 | if "variant_based_on" not in allow_fields: |
| 258 | allow_fields.append("variant_based_on") |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 259 | for field in item.meta.fields: |
tundebabzy | 6015f0f | 2017-07-04 11:13:02 +0100 | [diff] [blame] | 260 | # "Table" is part of `no_value_field` but we shouldn't ignore tables |
Rohit Waghchaure | 0e28fcc | 2017-08-29 18:15:57 +0530 | [diff] [blame] | 261 | if (field.reqd or field.fieldname in allow_fields) and field.fieldname not in exclude_fields: |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 262 | if variant.get(field.fieldname) != item.get(field.fieldname): |
Nabin Hait | 945f502 | 2017-09-29 15:11:50 +0530 | [diff] [blame] | 263 | if field.fieldtype == "Table": |
| 264 | variant.set(field.fieldname, []) |
| 265 | for d in item.get(field.fieldname): |
| 266 | row = copy.deepcopy(d) |
| 267 | if row.get("name"): |
| 268 | row.name = None |
| 269 | variant.append(field.fieldname, row) |
| 270 | else: |
| 271 | variant.set(field.fieldname, item.get(field.fieldname)) |
Nabin Hait | c314485 | 2017-09-28 18:55:40 +0530 | [diff] [blame] | 272 | |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 273 | variant.variant_of = item.name |
| 274 | variant.has_variants = 0 |
Makarand Bauskar | ce436b7 | 2017-08-02 18:16:53 +0530 | [diff] [blame] | 275 | if not variant.description: |
Nabin Hait | 82c9352 | 2017-10-25 11:46:20 +0530 | [diff] [blame] | 276 | variant.description = "" |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 277 | |
| 278 | if item.variant_based_on=='Item Attribute': |
| 279 | if variant.attributes: |
Nabin Hait | 82c9352 | 2017-10-25 11:46:20 +0530 | [diff] [blame] | 280 | attributes_description = "" |
| 281 | for d in variant.attributes: |
| 282 | attributes_description += "<div>" + d.attribute + ": " + cstr(d.attribute_value) + "</div>" |
Prateeksha Singh | 8f43d25 | 2017-11-16 18:06:26 +0530 | [diff] [blame] | 283 | |
Nabin Hait | 82c9352 | 2017-10-25 11:46:20 +0530 | [diff] [blame] | 284 | if attributes_description not in variant.description: |
| 285 | variant.description += attributes_description |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 286 | |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 287 | def make_variant_item_code(template_item_code, template_item_name, variant): |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 288 | """Uses template's item code and abbreviations to make variant's item code""" |
| 289 | if variant.item_code: |
| 290 | return |
| 291 | |
| 292 | abbreviations = [] |
| 293 | for attr in variant.attributes: |
| 294 | item_attribute = frappe.db.sql("""select i.numeric_values, v.abbr |
| 295 | from `tabItem Attribute` i left join `tabItem Attribute Value` v |
| 296 | on (i.name=v.parent) |
Rohit Waghchaure | 0169341 | 2017-03-09 17:02:55 +0530 | [diff] [blame] | 297 | where i.name=%(attribute)s and (v.attribute_value=%(attribute_value)s or i.numeric_values = 1)""", { |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 298 | "attribute": attr.attribute, |
| 299 | "attribute_value": attr.attribute_value |
| 300 | }, as_dict=True) |
| 301 | |
| 302 | if not item_attribute: |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 303 | return |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 304 | # frappe.throw(_('Invalid attribute {0} {1}').format(frappe.bold(attr.attribute), |
| 305 | # frappe.bold(attr.attribute_value)), title=_('Invalid Attribute'), |
| 306 | # exc=InvalidItemAttributeValueError) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 307 | |
Rohit Waghchaure | 0169341 | 2017-03-09 17:02:55 +0530 | [diff] [blame] | 308 | abbr_or_value = cstr(attr.attribute_value) if item_attribute[0].numeric_values else item_attribute[0].abbr |
| 309 | abbreviations.append(abbr_or_value) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 310 | |
| 311 | if abbreviations: |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 312 | variant.item_code = "{0}-{1}".format(template_item_code, "-".join(abbreviations)) |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 313 | variant.item_name = "{0}-{1}".format(template_item_name, "-".join(abbreviations)) |
Rushabh Mehta | d5c6416 | 2017-11-14 15:27:28 +0530 | [diff] [blame] | 314 | |
| 315 | @frappe.whitelist() |
| 316 | def create_variant_doc_for_quick_entry(template, args): |
| 317 | variant_based_on = frappe.db.get_value("Item", template, "variant_based_on") |
| 318 | args = json.loads(args) |
| 319 | if variant_based_on == "Manufacturer": |
| 320 | variant = get_variant(template, **args) |
| 321 | else: |
| 322 | existing_variant = get_variant(template, args) |
| 323 | if existing_variant: |
| 324 | return existing_variant |
| 325 | else: |
| 326 | variant = create_variant(template, args=args) |
| 327 | variant.name = variant.item_code |
| 328 | validate_item_variant_attributes(variant, args) |
| 329 | return variant.as_dict() |
| 330 | |