From b3ccc4bfb953b90dc8301a6af953c1a2cd66d4b6 Mon Sep 17 00:00:00 2001 From: maharshivpatel <39730881+maharshivpatel@users.noreply.github.com> Date: Tue, 31 May 2022 19:48:30 +0530 Subject: [PATCH] fix: Auto Insert Item Price If Missing when discount & blank UOM (#31168) * fix: Auto Insert Item Price If Missing when discount and blank UOM fixes wrong item price insert when discount is used and adds uom=stock_uom instead of blank as price is converted to stock uom * unit tests added for item with discount I have added test for auto_insert_price where discount is used. * unit test issue fixed fixed make_sales_order as some of the test that depended on it were failing due to passing of incorrect parameters. Co-authored-by: Ankush Menat --- .../doctype/sales_order/test_sales_order.py | 24 ++++++++++++++++++- erpnext/stock/get_item_details.py | 8 +++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py index acae37f547..96308f0bee 100644 --- a/erpnext/selling/doctype/sales_order/test_sales_order.py +++ b/erpnext/selling/doctype/sales_order/test_sales_order.py @@ -783,6 +783,7 @@ class TestSalesOrder(FrappeTestCase): def test_auto_insert_price(self): make_item("_Test Item for Auto Price List", {"is_stock_item": 0}) + make_item("_Test Item for Auto Price List with Discount Percentage", {"is_stock_item": 0}) frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 1) item_price = frappe.db.get_value( @@ -804,6 +805,25 @@ class TestSalesOrder(FrappeTestCase): 100, ) + make_sales_order( + item_code="_Test Item for Auto Price List with Discount Percentage", + selling_price_list="_Test Price List", + price_list_rate=200, + discount_percentage=20, + ) + + self.assertEqual( + frappe.db.get_value( + "Item Price", + { + "price_list": "_Test Price List", + "item_code": "_Test Item for Auto Price List with Discount Percentage", + }, + "price_list_rate", + ), + 200, + ) + # do not update price list frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0) @@ -1659,7 +1679,9 @@ def make_sales_order(**args): "warehouse": args.warehouse, "qty": args.qty or 10, "uom": args.uom or None, - "rate": args.rate or 100, + "price_list_rate": args.price_list_rate or None, + "discount_percentage": args.discount_percentage or None, + "rate": args.rate or (None if args.price_list_rate else 100), "against_blanket_order": args.against_blanket_order, }, ) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index c6241f8df6..c8d9f5404f 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -353,6 +353,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "has_batch_no": item.has_batch_no, "batch_no": args.get("batch_no"), "uom": args.uom, + "stock_uom": item.stock_uom, "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "", "qty": flt(args.qty) or 1.0, "stock_qty": flt(args.qty) or 1.0, @@ -365,7 +366,7 @@ def get_basic_details(args, item, overwrite_warehouse=True): "net_rate": 0.0, "net_amount": 0.0, "discount_percentage": 0.0, - "discount_amount": 0.0, + "discount_amount": flt(args.discount_amount) or 0.0, "supplier": get_default_supplier(args, item_defaults, item_group_defaults, brand_defaults), "update_stock": args.get("update_stock") if args.get("doctype") in ["Sales Invoice", "Purchase Invoice"] @@ -823,7 +824,9 @@ def insert_item_price(args): ): if frappe.has_permission("Item Price", "write"): price_list_rate = ( - args.rate / args.get("conversion_factor") if args.get("conversion_factor") else args.rate + (args.rate + args.discount_amount) / args.get("conversion_factor") + if args.get("conversion_factor") + else (args.rate + args.discount_amount) ) item_price = frappe.db.get_value( @@ -849,6 +852,7 @@ def insert_item_price(args): "item_code": args.item_code, "currency": args.currency, "price_list_rate": price_list_rate, + "uom": args.stock_uom, } ) item_price.insert()