Merge pull request #3939 from neilLasrado/variant
Fixes in Item Variants
This commit is contained in:
commit
b968772e97
@ -513,12 +513,10 @@ def validate_item_variant_attributes(item, args):
|
|||||||
filters={"parent": ["in", args.keys()]}):
|
filters={"parent": ["in", args.keys()]}):
|
||||||
(attribute_values.setdefault(t.parent, [])).append(t.attribute_value)
|
(attribute_values.setdefault(t.parent, [])).append(t.attribute_value)
|
||||||
|
|
||||||
numeric_attributes = frappe._dict((t.name, t) for t in frappe.get_list("Item Attribute", filters={"numeric_values":1,
|
numeric_attributes = frappe._dict((t.attribute, t) for t in \
|
||||||
"name": ["in", args.keys()]}, fields=["name", "from_range", "to_range", "increment"]))
|
frappe.db.sql("""select attribute, from_range, to_range, increment from `tabItem Variant Attribute`
|
||||||
|
where parent = %s and numeric_values=1""", (item), as_dict=1))
|
||||||
template_item = frappe.get_doc("Item", item)
|
|
||||||
template_item_attributes = frappe._dict((d.attribute, d) for d in template_item.attributes)
|
|
||||||
|
|
||||||
for attribute, value in args.items():
|
for attribute, value in args.items():
|
||||||
|
|
||||||
if attribute in numeric_attributes:
|
if attribute in numeric_attributes:
|
||||||
@ -531,10 +529,17 @@ def validate_item_variant_attributes(item, args):
|
|||||||
if increment == 0:
|
if increment == 0:
|
||||||
# defensive validation to prevent ZeroDivisionError
|
# defensive validation to prevent ZeroDivisionError
|
||||||
frappe.throw(_("Increment for Attribute {0} cannot be 0").format(attribute))
|
frappe.throw(_("Increment for Attribute {0} cannot be 0").format(attribute))
|
||||||
|
|
||||||
|
is_in_range = from_range <= flt(value) <= to_range
|
||||||
|
precision = len(cstr(increment).split(".")[-1].rstrip("0"))
|
||||||
|
#avoid precision error by rounding the remainder
|
||||||
|
remainder = flt((flt(value) - from_range) % increment, precision)
|
||||||
|
|
||||||
|
is_incremental = remainder==0 or remainder==0 or remainder==increment
|
||||||
|
|
||||||
if not ( (from_range <= flt(value) <= to_range) and (flt(value) - from_range) % increment == 0 ):
|
if not (is_in_range and is_incremental):
|
||||||
frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3}").format(attribute, from_range, to_range, increment), InvalidItemAttributeValueError)
|
frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3}")\
|
||||||
|
.format(attribute, from_range, to_range, increment), InvalidItemAttributeValueError)
|
||||||
|
|
||||||
elif value not in attribute_values[attribute]:
|
elif value not in attribute_values[attribute]:
|
||||||
frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values").format(
|
frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values").format(
|
||||||
|
@ -6,9 +6,8 @@ import unittest
|
|||||||
import frappe
|
import frappe
|
||||||
|
|
||||||
from frappe.test_runner import make_test_records
|
from frappe.test_runner import make_test_records
|
||||||
from erpnext.stock.doctype.item.item import (WarehouseNotSet, ItemTemplateCannotHaveStock, create_variant,
|
from erpnext.stock.doctype.item.item import (WarehouseNotSet, create_variant,
|
||||||
ItemVariantExistsError, InvalidItemAttributeValueError)
|
ItemVariantExistsError, InvalidItemAttributeValueError)
|
||||||
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
|
||||||
|
|
||||||
test_ignore = ["BOM"]
|
test_ignore = ["BOM"]
|
||||||
test_dependencies = ["Warehouse"]
|
test_dependencies = ["Warehouse"]
|
||||||
@ -129,8 +128,16 @@ class TestItem(unittest.TestCase):
|
|||||||
# make template item
|
# make template item
|
||||||
make_item("_Test Numeric Template Item", {
|
make_item("_Test Numeric Template Item", {
|
||||||
"attributes": [
|
"attributes": [
|
||||||
{"attribute": "Test Size"},
|
{
|
||||||
{"attribute": "Test Item Length"}
|
"attribute": "Test Size"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"attribute": "Test Item Length",
|
||||||
|
"numeric_values": 1,
|
||||||
|
"from_range": 0.0,
|
||||||
|
"to_range": 100.0,
|
||||||
|
"increment": 0.5
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"default_warehouse": "_Test Warehouse - _TC"
|
"default_warehouse": "_Test Warehouse - _TC"
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user