From 6015f0f2ec0dc62c28af41885bb204cd4e705696 Mon Sep 17 00:00:00 2001 From: tundebabzy Date: Tue, 4 Jul 2017 11:13:02 +0100 Subject: [PATCH] Inspection required before delivery isn't working for item variants (#9362) * makes `copy_attributes_to_variant` to not ignore "Table" * fixes test cases - `test_auto_material_request` and `test_auto_material_request_for_variant` * adds test case - tables in templates should be copied to variants * [ci] use deprecated trusty build for now --- erpnext/controllers/item_variant.py | 6 +- .../controllers/tests/test_item_variant.py | 58 +++++++++++++++++++ .../doctype/stock_entry/test_stock_entry.py | 15 ++--- 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 erpnext/controllers/tests/test_item_variant.py diff --git a/erpnext/controllers/item_variant.py b/erpnext/controllers/item_variant.py index 334930314b..226b17f7a2 100644 --- a/erpnext/controllers/item_variant.py +++ b/erpnext/controllers/item_variant.py @@ -169,6 +169,7 @@ def create_variant(item, args): return variant + def copy_attributes_to_variant(item, variant): from frappe.model import no_value_fields @@ -181,8 +182,9 @@ def copy_attributes_to_variant(item, variant): exclude_fields += ['manufacturer', 'manufacturer_part_no'] for field in item.meta.fields: - if field.fieldtype not in no_value_fields and (not field.no_copy)\ - and field.fieldname not in exclude_fields: + # "Table" is part of `no_value_field` but we shouldn't ignore tables + if (field.fieldtype == 'Table' or field.fieldtype not in no_value_fields) \ + and (not field.no_copy) and field.fieldname not in exclude_fields: if variant.get(field.fieldname) != item.get(field.fieldname): variant.set(field.fieldname, item.get(field.fieldname)) variant.variant_of = item.name diff --git a/erpnext/controllers/tests/test_item_variant.py b/erpnext/controllers/tests/test_item_variant.py new file mode 100644 index 0000000000..9fc45d234c --- /dev/null +++ b/erpnext/controllers/tests/test_item_variant.py @@ -0,0 +1,58 @@ +from __future__ import unicode_literals + +import frappe +import json +import unittest + +from erpnext.controllers.item_variant import copy_attributes_to_variant, make_variant_item_code + +# python 3 compatibility stuff +try: + unicode = unicode +except NameError: + # Python 3 + basestring = (str, bytes) +else: + # Python 2 + basestring = basestring + + +def create_variant_with_tables(item, args): + if isinstance(args, basestring): + args = json.loads(args) + + template = frappe.get_doc("Item", item) + template.quality_parameters.append({ + "specification": "Moisture", + "value": "< 5%", + }) + variant = frappe.new_doc("Item") + variant.variant_based_on = 'Item Attribute' + variant_attributes = [] + + for d in template.attributes: + variant_attributes.append({ + "attribute": d.attribute, + "attribute_value": args.get(d.attribute) + }) + + variant.set("attributes", variant_attributes) + copy_attributes_to_variant(template, variant) + make_variant_item_code(template.item_code, template.item_name, variant) + + return variant + + +def make_item_variant(): + frappe.delete_doc_if_exists("Item", "_Test Variant Item-S", force=1) + variant = create_variant_with_tables("_Test Variant Item", '{"Test Size": "Small"}') + variant.item_code = "_Test Variant Item-S" + variant.item_name = "_Test Variant Item-S" + variant.save() + return variant + + +class TestItemVariant(unittest.TestCase): + def test_tables_in_template_copied_to_variant(self): + variant = make_item_variant() + self.assertNotEqual(variant.get("quality_parameters"), []) diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py index 8670f73225..fdb65cda53 100644 --- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py @@ -85,12 +85,7 @@ class TestStockEntry(unittest.TestCase): self._test_auto_material_request("_Test Item Warehouse Group Wise Reorder", warehouse="_Test Warehouse Group-C1 - _TC") def _test_auto_material_request(self, item_code, material_request_type="Purchase", warehouse="_Test Warehouse - _TC"): - item = frappe.get_doc("Item", item_code) - - if item.variant_of: - template = frappe.get_doc("Item", item.variant_of) - else: - template = item + variant = frappe.get_doc("Item", item_code) projected_qty, actual_qty = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, ["projected_qty", "actual_qty"]) or [0, 0] @@ -105,10 +100,10 @@ class TestStockEntry(unittest.TestCase): frappe.db.set_value("Stock Settings", None, "auto_indent", 1) # update re-level qty so that it is more than projected_qty - if projected_qty >= template.reorder_levels[0].warehouse_reorder_level: - template.reorder_levels[0].warehouse_reorder_level += projected_qty - template.reorder_levels[0].material_request_type = material_request_type - template.save() + if projected_qty >= variant.reorder_levels[0].warehouse_reorder_level: + variant.reorder_levels[0].warehouse_reorder_level += projected_qty + variant.reorder_levels[0].material_request_type = material_request_type + variant.save() from erpnext.stock.reorder_item import reorder_item mr_list = reorder_item()