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