From 34d2822483d782e391071fdcfa77a22ca1077a7e Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 19 Jan 2016 15:45:49 +0530 Subject: [PATCH] Fixes in get_item_details and test cases --- .../doctype/pricing_rule/pricing_rule.py | 30 +++++++++++++++---- .../doctype/pricing_rule/test_pricing_rule.py | 13 ++++---- erpnext/controllers/accounts_controller.py | 4 +-- .../doctype/sales_order/test_sales_order.py | 6 ++-- erpnext/stock/doctype/item/test_item.py | 3 +- erpnext/stock/get_item_details.py | 17 ++++++----- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index 494e70c02b5..fcc1f921734 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -97,7 +97,10 @@ def apply_pricing_rule(args): args = json.loads(args) args = frappe._dict(args) - + + if not args.transaction_type: + set_transaction_type(args) + # list of dictionaries out = [] @@ -134,13 +137,17 @@ def get_pricing_rule_for_item(args): if not args.item_group: frappe.throw(_("Item Group not mentioned in item master for item {0}").format(args.item_code)) - if args.customer and not (args.customer_group and args.territory): - customer = frappe.db.get_value("Customer", args.customer, ["customer_group", "territory"]) - if customer: - args.customer_group, args.territory = customer + if args.transaction_type=="selling": + if args.customer and not (args.customer_group and args.territory): + customer = frappe.db.get_value("Customer", args.customer, ["customer_group", "territory"]) + if customer: + args.customer_group, args.territory = customer + + args.supplier = args.supplier_type = None elif args.supplier and not args.supplier_type: args.supplier_type = frappe.db.get_value("Supplier", args.supplier, "supplier_type") + args.customer = args.customer_group = args.territory = None pricing_rules = get_pricing_rules(args) pricing_rule = filter_pricing_rules(args, pricing_rules) @@ -212,7 +219,7 @@ def get_pricing_rules(args): and {transaction_type} = 1 {conditions} order by priority desc, name desc""".format( item_group_condition=item_group_condition, - transaction_type= "selling" if (args.customer or args.lead) else "buying", + transaction_type= args.transaction_type, conditions=conditions), values, as_dict=1) def filter_pricing_rules(args, pricing_rules): @@ -270,3 +277,14 @@ def apply_internal_priority(pricing_rules, field_set, args): if filtered_rules: break return filtered_rules or pricing_rules + +def set_transaction_type(args): + if args.doctype in ("Opportunity", "Quotation", "Sales Order", "Delivery Note", "Sales Invoice"): + args.transaction_type = "selling" + elif args.doctype in ("Material Request", "Supplier Quotation", "Purchase Order", + "Purchase Receipt", "Purchase Invoice"): + args.transaction_type = "buying" + elif args.customer: + args.transaction_type = "selling" + else: + args.transaction_type = "buying" \ No newline at end of file diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py index 6edfaffbe42..bdd743125a2 100644 --- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py @@ -31,21 +31,22 @@ class TestPricingRule(unittest.TestCase): "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", - "parenttype": "Sales Order", + "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", - "doctype": "Sales Order Item", "name": None }) details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 10) - + prule = frappe.get_doc(test_record.copy()) prule.applicable_for = "Customer" + prule.title = "_Test Pricing Rule for Customer" self.assertRaises(MandatoryError, prule.insert) + prule.customer = "_Test Customer" prule.discount_percentage = 20 prule.insert() @@ -55,16 +56,18 @@ class TestPricingRule(unittest.TestCase): prule = frappe.get_doc(test_record.copy()) prule.apply_on = "Item Group" prule.item_group = "All Item Groups" + prule.title = "_Test Pricing Rule for Item Group" prule.discount_percentage = 15 prule.insert() - - args.customer = None + + args.customer = "_Test Customer 1" details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 10) prule = frappe.get_doc(test_record.copy()) prule.applicable_for = "Campaign" prule.campaign = "_Test Campaign" + prule.title = "_Test Pricing Rule for Campaign" prule.discount_percentage = 5 prule.priority = 8 prule.insert() diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index c3da6b294ca..5c84112df44 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -152,8 +152,8 @@ class AccountsController(TransactionBase): args = parent_dict.copy() args.update(item.as_dict()) - args["doctype"] = parent_dict.get("doctype") - args["name"] = parent_dict.get("name") + args["doctype"] = self.doctype + args["name"] = self.name if not args.get("transaction_date"): args["transaction_date"] = args.get("posting_date") diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py index 3501f525d86..c6ea618eb05 100644 --- a/erpnext/selling/doctype/sales_order/test_sales_order.py +++ b/erpnext/selling/doctype/sales_order/test_sales_order.py @@ -334,15 +334,15 @@ class TestSalesOrder(unittest.TestCase): existing_ordered_qty = bin[0].ordered_qty if bin else 0.0 existing_reserved_qty = bin[0].reserved_qty if bin else 0.0 - bin = frappe.get_all("Bin", filters={"item_code": dn_item.item_code, "warehouse": "_Test Warehouse - _TC"}, - fields=["reserved_qty"]) + bin = frappe.get_all("Bin", filters={"item_code": dn_item.item_code, + "warehouse": "_Test Warehouse - _TC"}, fields=["reserved_qty"]) existing_reserved_qty_for_dn_item = bin[0].reserved_qty if bin else 0.0 #create so, po and partial dn so = make_sales_order(item_list=so_items, do_not_submit=True) so.submit() - + po = make_purchase_order_for_drop_shipment(so.name, '_Test Supplier') po.submit() diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 7538260c6e9..e5392d451b0 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -93,13 +93,14 @@ class TestItem(unittest.TestCase): "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", + "customer": "_Test Customer" }) for key, value in to_check.iteritems(): self.assertEquals(value, details.get(key)) def test_make_item_variant(self): - frappe.delete_doc_if_exists("Item", "_Test Variant Item-L") + frappe.delete_doc_if_exists("Item", "_Test Variant Item-L", force=1) variant = create_variant("_Test Variant Item", {"Test Size": "Large"}) variant.save() diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 38193373497..cc80d1ee441 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -6,7 +6,7 @@ import frappe from frappe import _, throw from frappe.utils import flt, cint, add_days, cstr import json -from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item +from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item, set_transaction_type from erpnext.setup.utils import get_exchange_rate from frappe.model.meta import get_field_precision @@ -84,6 +84,8 @@ def process_args(args): args.item_code = get_item_code(barcode=args.barcode) elif not args.item_code and args.serial_no: args.item_code = get_item_code(serial_no=args.serial_no) + + set_transaction_type(args) return args @@ -107,7 +109,7 @@ def validate_item_details(args, item): from erpnext.stock.doctype.item.item import validate_end_of_life validate_end_of_life(item.name, item.end_of_life, item.disabled) - if args.customer or args.doctype=="Opportunity": + if args.transaction_type=="selling": # validate if sales item or service item if args.get("order_type") == "Maintenance": if item.is_service_item != 1: @@ -119,7 +121,7 @@ def validate_item_details(args, item): if cint(item.has_variants): throw(_("Item {0} is a template, please select one of its variants").format(item.name)) - elif args.supplier and args.doctype != "Material Request": + elif args.transaction_type=="buying" and args.doctype != "Material Request": # validate if purchase item or subcontracted item if item.is_purchase_item != 1: throw(_("Item {0} must be a Purchase Item").format(item.name)) @@ -219,7 +221,7 @@ def get_price_list_rate(args, item_doc, out): out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ / flt(args.conversion_rate) - if not out.price_list_rate and args.supplier: + if not out.price_list_rate and args.transaction_type=="buying": from erpnext.stock.doctype.item.item import get_last_purchase_details out.update(get_last_purchase_details(item_doc.name, args.name, args.conversion_rate)) @@ -251,7 +253,7 @@ def get_price_list_rate_for(price_list, item_code): def validate_price_list(args): if args.get("price_list"): if not frappe.db.get_value("Price List", - {"name": args.price_list, "selling" if (args.customer or args.lead) else "buying": 1, "enabled": 1}): + {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): throw(_("Price List {0} is disabled").format(args.price_list)) else: throw(_("Price List not selected")) @@ -283,10 +285,11 @@ def validate_conversion_rate(args, meta): frappe._dict({"fields": args}))) def get_party_item_code(args, item_doc, out): - if args.customer: + if args.transaction_type=="selling" and args.customer: customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer}) out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None - else: + + if args.transaction_type=="buying" and args.supplier: item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier}) out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None