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 |
| 8 | import json |
| 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 | |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 59 | attribute_values, numeric_values = get_attribute_values() |
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): |
| 95 | if attribute_value not in attributes_list: |
| 96 | frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values for Item {2}").format( |
| 97 | attribute_value, attribute, item), InvalidItemAttributeValueError, title=_('Invalid Attribute')) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 98 | |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 99 | def get_attribute_values(): |
| 100 | if not frappe.flags.attribute_values: |
| 101 | attribute_values = {} |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 102 | numeric_values = {} |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 103 | 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] | 104 | attribute_values.setdefault(t.parent.lower(), []).append(t.attribute_value) |
| 105 | |
| 106 | for t in frappe.get_all('Item Attribute', |
| 107 | fields=["name", "from_range", "to_range", "increment"], filters={'numeric_values': 1}): |
| 108 | numeric_values[t.name.lower()] = t |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 109 | |
| 110 | frappe.flags.attribute_values = attribute_values |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 111 | frappe.flags.numeric_values = numeric_values |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 112 | |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 113 | return frappe.flags.attribute_values, frappe.flags.numeric_values |
Rushabh Mehta | b8bdfbc | 2016-07-15 12:40:47 +0530 | [diff] [blame] | 114 | |
Nabin Hait | 6b068e1 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 115 | def find_variant(template, args, variant_item_code=None): |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 116 | conditions = ["""(iv_attribute.attribute="{0}" and iv_attribute.attribute_value="{1}")"""\ |
| 117 | .format(frappe.db.escape(key), frappe.db.escape(cstr(value))) for key, value in args.items()] |
| 118 | |
| 119 | conditions = " or ".join(conditions) |
| 120 | |
| 121 | # use approximate match and shortlist possible variant matches |
| 122 | # it is approximate because we are matching using OR condition |
| 123 | # and it need not be exact match at this stage |
| 124 | # this uses a simpler query instead of using multiple exists conditions |
| 125 | possible_variants = frappe.db.sql_list("""select name from `tabItem` item |
| 126 | where variant_of=%s and exists ( |
| 127 | select name from `tabItem Variant Attribute` iv_attribute |
| 128 | where iv_attribute.parent=item.name |
Nabin Hait | 6626e32 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 129 | and ({conditions}) and parent != %s |
Nabin Hait | 6b068e1 | 2015-12-31 13:20:32 +0530 | [diff] [blame] | 130 | )""".format(conditions=conditions), (template, cstr(variant_item_code))) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 131 | |
| 132 | for variant in possible_variants: |
| 133 | variant = frappe.get_doc("Item", variant) |
| 134 | |
| 135 | if len(args.keys()) == len(variant.get("attributes")): |
| 136 | # has the same number of attributes and values |
| 137 | # assuming no duplication as per the validation in Item |
| 138 | match_count = 0 |
| 139 | |
| 140 | for attribute, value in args.items(): |
| 141 | for row in variant.attributes: |
| 142 | if row.attribute==attribute and row.attribute_value== cstr(value): |
| 143 | # this row matches |
| 144 | match_count += 1 |
| 145 | break |
| 146 | |
| 147 | if match_count == len(args.keys()): |
| 148 | return variant.name |
| 149 | |
| 150 | @frappe.whitelist() |
| 151 | def create_variant(item, args): |
| 152 | if isinstance(args, basestring): |
| 153 | args = json.loads(args) |
| 154 | |
| 155 | template = frappe.get_doc("Item", item) |
| 156 | variant = frappe.new_doc("Item") |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 157 | variant.variant_based_on = 'Item Attribute' |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 158 | variant_attributes = [] |
| 159 | |
| 160 | for d in template.attributes: |
| 161 | variant_attributes.append({ |
| 162 | "attribute": d.attribute, |
| 163 | "attribute_value": args.get(d.attribute) |
| 164 | }) |
| 165 | |
| 166 | variant.set("attributes", variant_attributes) |
| 167 | copy_attributes_to_variant(template, variant) |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 168 | make_variant_item_code(template.item_code, template.item_name, variant) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 169 | |
| 170 | return variant |
| 171 | |
| 172 | def copy_attributes_to_variant(item, variant): |
| 173 | from frappe.model import no_value_fields |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 174 | |
| 175 | # copy non no-copy fields |
| 176 | |
| 177 | exclude_fields = ["item_code", "item_name", "show_in_website"] |
| 178 | |
| 179 | if item.variant_based_on=='Manufacturer': |
| 180 | # don't copy manufacturer values if based on part no |
| 181 | exclude_fields += ['manufacturer', 'manufacturer_part_no'] |
| 182 | |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 183 | for field in item.meta.fields: |
| 184 | if field.fieldtype not in no_value_fields and (not field.no_copy)\ |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 185 | and field.fieldname not in exclude_fields: |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 186 | if variant.get(field.fieldname) != item.get(field.fieldname): |
| 187 | variant.set(field.fieldname, item.get(field.fieldname)) |
| 188 | variant.variant_of = item.name |
| 189 | variant.has_variants = 0 |
Rushabh Mehta | a07c43f | 2017-03-21 17:48:34 +0100 | [diff] [blame] | 190 | |
| 191 | if item.variant_based_on=='Item Attribute': |
| 192 | if variant.attributes: |
| 193 | variant.description += "\n" |
| 194 | for d in variant.attributes: |
| 195 | variant.description += "<p>" + d.attribute + ": " + cstr(d.attribute_value) + "</p>" |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 196 | |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 197 | def make_variant_item_code(template_item_code, template_item_name, variant): |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 198 | """Uses template's item code and abbreviations to make variant's item code""" |
| 199 | if variant.item_code: |
| 200 | return |
| 201 | |
| 202 | abbreviations = [] |
| 203 | for attr in variant.attributes: |
| 204 | item_attribute = frappe.db.sql("""select i.numeric_values, v.abbr |
| 205 | from `tabItem Attribute` i left join `tabItem Attribute Value` v |
| 206 | on (i.name=v.parent) |
Rohit Waghchaure | 0169341 | 2017-03-09 17:02:55 +0530 | [diff] [blame] | 207 | 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] | 208 | "attribute": attr.attribute, |
| 209 | "attribute_value": attr.attribute_value |
| 210 | }, as_dict=True) |
| 211 | |
| 212 | if not item_attribute: |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 213 | return |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 214 | # frappe.throw(_('Invalid attribute {0} {1}').format(frappe.bold(attr.attribute), |
| 215 | # frappe.bold(attr.attribute_value)), title=_('Invalid Attribute'), |
| 216 | # exc=InvalidItemAttributeValueError) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 217 | |
Rohit Waghchaure | 0169341 | 2017-03-09 17:02:55 +0530 | [diff] [blame] | 218 | abbr_or_value = cstr(attr.attribute_value) if item_attribute[0].numeric_values else item_attribute[0].abbr |
| 219 | abbreviations.append(abbr_or_value) |
Anand Doshi | 099bbbd | 2015-09-02 11:18:32 +0530 | [diff] [blame] | 220 | |
| 221 | if abbreviations: |
Rushabh Mehta | 95383bb | 2016-07-15 15:11:46 +0530 | [diff] [blame] | 222 | variant.item_code = "{0}-{1}".format(template_item_code, "-".join(abbreviations)) |
Prateeksha Singh | 89cec18 | 2017-05-19 12:35:36 +0530 | [diff] [blame] | 223 | variant.item_name = "{0}-{1}".format(template_item_name, "-".join(abbreviations)) |