fix(test): Fixed item discount amount calculation and test cases related to pricing rule
This commit is contained in:
parent
ee887f4db7
commit
688758a56f
@ -11,11 +11,17 @@ from erpnext.stock.get_item_details import get_item_details
|
|||||||
from frappe import MandatoryError
|
from frappe import MandatoryError
|
||||||
|
|
||||||
class TestPricingRule(unittest.TestCase):
|
class TestPricingRule(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
frappe.db.sql("delete from `tabPricing Rule`")
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
frappe.db.sql("delete from `tabPricing Rule`")
|
||||||
|
|
||||||
def test_pricing_rule_for_discount(self):
|
def test_pricing_rule_for_discount(self):
|
||||||
from erpnext.stock.get_item_details import get_item_details
|
from erpnext.stock.get_item_details import get_item_details
|
||||||
from frappe import MandatoryError
|
from frappe import MandatoryError
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
test_record = {
|
test_record = {
|
||||||
"doctype": "Pricing Rule",
|
"doctype": "Pricing Rule",
|
||||||
@ -89,14 +95,10 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
details = get_item_details(args)
|
details = get_item_details(args)
|
||||||
self.assertEqual(details.get("discount_percentage"), 15)
|
self.assertEqual(details.get("discount_percentage"), 15)
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
def test_pricing_rule_for_margin(self):
|
def test_pricing_rule_for_margin(self):
|
||||||
from erpnext.stock.get_item_details import get_item_details
|
from erpnext.stock.get_item_details import get_item_details
|
||||||
from frappe import MandatoryError
|
from frappe import MandatoryError
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
test_record = {
|
test_record = {
|
||||||
"doctype": "Pricing Rule",
|
"doctype": "Pricing Rule",
|
||||||
"title": "_Test Pricing Rule",
|
"title": "_Test Pricing Rule",
|
||||||
@ -111,14 +113,14 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
"company": "_Test Company"
|
"company": "_Test Company"
|
||||||
}
|
}
|
||||||
frappe.get_doc(test_record.copy()).insert()
|
frappe.get_doc(test_record.copy()).insert()
|
||||||
|
|
||||||
item_price = frappe.get_doc({
|
item_price = frappe.get_doc({
|
||||||
"doctype": "Item Price",
|
"doctype": "Item Price",
|
||||||
"price_list": "_Test Price List 2",
|
"price_list": "_Test Price List 2",
|
||||||
"item_code": "_Test FG Item 2",
|
"item_code": "_Test FG Item 2",
|
||||||
"price_list_rate": 100
|
"price_list_rate": 100
|
||||||
})
|
})
|
||||||
|
|
||||||
item_price.insert(ignore_permissions=True)
|
item_price.insert(ignore_permissions=True)
|
||||||
|
|
||||||
args = frappe._dict({
|
args = frappe._dict({
|
||||||
@ -138,14 +140,10 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
self.assertEqual(details.get("margin_type"), "Percentage")
|
self.assertEqual(details.get("margin_type"), "Percentage")
|
||||||
self.assertEqual(details.get("margin_rate_or_amount"), 10)
|
self.assertEqual(details.get("margin_rate_or_amount"), 10)
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
def test_pricing_rule_for_variants(self):
|
def test_pricing_rule_for_variants(self):
|
||||||
from erpnext.stock.get_item_details import get_item_details
|
from erpnext.stock.get_item_details import get_item_details
|
||||||
from frappe import MandatoryError
|
from frappe import MandatoryError
|
||||||
|
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
if not frappe.db.exists("Item", "Test Variant PRT"):
|
if not frappe.db.exists("Item", "Test Variant PRT"):
|
||||||
frappe.get_doc({
|
frappe.get_doc({
|
||||||
"doctype": "Item",
|
"doctype": "Item",
|
||||||
@ -213,8 +211,6 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
self.assertEqual(details.get("discount_percentage"), 17.5)
|
self.assertEqual(details.get("discount_percentage"), 17.5)
|
||||||
|
|
||||||
def test_pricing_rule_for_stock_qty(self):
|
def test_pricing_rule_for_stock_qty(self):
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
|
||||||
|
|
||||||
test_record = {
|
test_record = {
|
||||||
"doctype": "Pricing Rule",
|
"doctype": "Pricing Rule",
|
||||||
"title": "_Test Pricing Rule",
|
"title": "_Test Pricing Rule",
|
||||||
@ -257,25 +253,19 @@ class TestPricingRule(unittest.TestCase):
|
|||||||
self.assertEqual(so.items[0].rate, 100)
|
self.assertEqual(so.items[0].rate, 100)
|
||||||
|
|
||||||
def test_pricing_rule_with_margin_and_discount(self):
|
def test_pricing_rule_with_margin_and_discount(self):
|
||||||
frappe.delete_doc_if_exists('Pricing Rule', '_Test Pricing Rule')
|
make_pricing_rule(selling=1, margin_type="Percentage", margin_rate_or_amount=10, discount_percentage=10)
|
||||||
make_pricing_rule(selling=1, margin_type="Percentage", margin_rate_or_amount=10)
|
|
||||||
si = create_sales_invoice(do_not_save=True)
|
si = create_sales_invoice(do_not_save=True)
|
||||||
si.items[0].price_list_rate = 1000
|
si.items[0].price_list_rate = 1000
|
||||||
si.payment_schedule = []
|
si.payment_schedule = []
|
||||||
si.insert(ignore_permissions=True)
|
si.insert(ignore_permissions=True)
|
||||||
|
|
||||||
item = si.items[0]
|
item = si.items[0]
|
||||||
self.assertEqual(item.rate, 1100)
|
|
||||||
self.assertEqual(item.margin_rate_or_amount, 10)
|
self.assertEqual(item.margin_rate_or_amount, 10)
|
||||||
|
self.assertEqual(item.rate_with_margin, 1100)
|
||||||
# With discount
|
|
||||||
item.discount_percentage = 10
|
|
||||||
si.payment_schedule = []
|
|
||||||
si.save()
|
|
||||||
item = si.items[0]
|
|
||||||
self.assertEqual(item.rate, 990)
|
|
||||||
self.assertEqual(item.discount_percentage, 10)
|
self.assertEqual(item.discount_percentage, 10)
|
||||||
frappe.db.sql("delete from `tabPricing Rule`")
|
self.assertEqual(item.discount_amount, 110)
|
||||||
|
self.assertEqual(item.rate, 990)
|
||||||
|
|
||||||
|
|
||||||
def make_pricing_rule(**args):
|
def make_pricing_rule(**args):
|
||||||
args = frappe._dict(args)
|
args = frappe._dict(args)
|
||||||
|
@ -70,7 +70,7 @@ class calculate_taxes_and_totals(object):
|
|||||||
if item.rate_with_margin > 0 else item.rate
|
if item.rate_with_margin > 0 else item.rate
|
||||||
|
|
||||||
item.net_rate = item.rate
|
item.net_rate = item.rate
|
||||||
item.discount_amount = item.price_list_rate - item.rate
|
item.discount_amount = item.rate_with_margin - item.rate
|
||||||
item.amount = flt(item.rate * item.qty, item.precision("amount"))
|
item.amount = flt(item.rate * item.qty, item.precision("amount"))
|
||||||
item.net_amount = item.amount
|
item.net_amount = item.amount
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user