Merge pull request #9811 from rohitwaghchaure/pricing_rule_issue_for_discount

[Fix] Manual discount is not applying in the transaction if discount amount is zero in the pricing rule
diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
index 71897d4..a701024 100644
--- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
@@ -185,7 +185,7 @@
 				"discount_percentage": 0.0
 			})
 		else:
-			item_details.discount_percentage = pricing_rule.discount_percentage
+			item_details.discount_percentage = pricing_rule.discount_percentage or args.discount_percentage
 	elif args.get('pricing_rule'):
 		item_details = remove_pricing_rule_for_item(args.get("pricing_rule"), item_details)
 
diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
index 31b1d46..82a3d65 100644
--- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
@@ -6,6 +6,7 @@
 import unittest
 import frappe
 from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
 from erpnext.stock.get_item_details import get_item_details
 from frappe import MandatoryError
 
@@ -248,4 +249,51 @@
 		so.submit()
 		so = frappe.get_doc('Sales Order', so.name)
 		self.assertEquals(so.items[0].discount_percentage, 0)
-		self.assertEquals(so.items[0].rate, 100)
\ No newline at end of file
+		self.assertEquals(so.items[0].rate, 100)
+
+	def test_pricing_rule_with_margin_and_discount(self):
+		make_pricing_rule(selling=1, margin_type="Percentage", margin_rate_or_amount=10)
+		si = create_sales_invoice(do_not_save=True)
+		si.items[0].price_list_rate = 1000
+		si.insert(ignore_permissions=True)
+
+		item = si.items[0]
+		self.assertEquals(item.rate, 1100)
+		self.assertEquals(item.margin_rate_or_amount, 10)
+
+		# With discount
+		item.discount_percentage = 10
+		si.save()
+		item = si.items[0]
+		self.assertEquals(item.rate, 990)
+		self.assertEquals(item.discount_percentage, 10)
+		frappe.db.sql("delete from `tabPricing Rule`")
+
+def make_pricing_rule(**args):
+	args = frappe._dict(args)
+
+	doc = frappe.get_doc({
+		"doctype": "Pricing Rule",
+		"title": args.title or "_Test Pricing Rule",
+		"company": args.company or "_Test Company",
+		"apply_on": args.apply_on or "Item Code",
+		"item_code": args.item_code or "_Test Item",
+		"applicable_for": args.applicable_for,
+		"selling": args.selling or 0,
+		"buying": args.buying or 0,
+		"min_qty": args.min_qty or 0.0,
+		"max_qty": args.max_qty or 0.0,
+		"price_or_discount": args.price_or_discount or "Discount Percentage",
+		"discount_percentage": args.discount_percentage or 0.0,
+		"price": args.price or 0.0,
+		"margin_type": args.margin_type,
+		"margin_rate_or_amount": args.margin_rate_or_amount or 0.0
+	}).insert(ignore_permissions=True)
+
+	apply_on = doc.apply_on.replace(' ', '_').lower()
+	if args.get(apply_on) and apply_on != "item_code":
+		doc.db_set(apply_on, args.get(apply_on))
+
+	applicable_for = doc.applicable_for.replace(' ', '_').lower()
+	if args.get(applicable_for):
+		doc.db_set(applicable_for, args.get(applicable_for))
\ No newline at end of file
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 29f38bd..9ed1de2 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -859,6 +859,7 @@
 					"pricing_rule": d.pricing_rule,
 					"warehouse": d.warehouse,
 					"serial_no": d.serial_no,
+					"discount_percentage": d.discount_percentage || 0.0,
 					"conversion_factor": d.conversion_factor || 1.0
 				});