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