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