[fix] Apply pricing rule on the item based on quantity as per stock uom (#8792)

diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
index b913b6b..71897d4 100644
--- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
@@ -285,9 +285,10 @@
 
 def filter_pricing_rules(args, pricing_rules):
 	# filter for qty
+	stock_qty = args.get('qty') * args.get('conversion_factor', 1)
 	if pricing_rules:
-		pricing_rules = filter(lambda x: (flt(args.get("qty"))>=flt(x.min_qty)
-			and (flt(args.get("qty"))<=x.max_qty if x.max_qty else True)), pricing_rules)
+		pricing_rules = filter(lambda x: (flt(stock_qty)>=flt(x.min_qty)
+			and (flt(stock_qty)<=x.max_qty if x.max_qty else True)), pricing_rules)
 
 		# add variant_of property in pricing rule
 		for p in pricing_rules:
diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
index 814c5c0..31b1d46 100644
--- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
@@ -5,6 +5,9 @@
 from __future__ import unicode_literals
 import unittest
 import frappe
+from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+from erpnext.stock.get_item_details import get_item_details
+from frappe import MandatoryError
 
 class TestPricingRule(unittest.TestCase):
 	def test_pricing_rule_for_discount(self):
@@ -203,3 +206,46 @@
 
 		details = get_item_details(args)
 		self.assertEquals(details.get("discount_percentage"), 17.5)
+
+	def test_pricing_rule_for_stock_qty(self):
+		frappe.db.sql("delete from `tabPricing Rule`")
+
+		test_record = {
+			"doctype": "Pricing Rule",
+			"title": "_Test Pricing Rule",
+			"apply_on": "Item Code",
+			"item_code": "_Test Item",
+			"selling": 1,
+			"price_or_discount": "Discount Percentage",
+			"price": 0,
+			"min_qty": 5,
+			"max_qty": 7,
+			"discount_percentage": 17.5,
+			"company": "_Test Company"
+		}
+		frappe.get_doc(test_record.copy()).insert()
+
+		if not frappe.db.get_value('UOM Conversion Detail',
+			{'parent': '_Test Item', 'uom': 'box'}):
+			item = frappe.get_doc('Item', '_Test Item')
+			item.append('uoms', {
+				'uom': 'Box',
+				'conversion_factor': 5
+			})
+			item.save(ignore_permissions=True)
+
+		# With pricing rule
+		so = make_sales_order(item_code="_Test Item", qty=1, uom="Box", do_not_submit=True)
+		so.items[0].price_list_rate = 100
+		so.submit()
+		so = frappe.get_doc('Sales Order', so.name)
+		self.assertEquals(so.items[0].discount_percentage, 17.5)
+		self.assertEquals(so.items[0].rate, 82.5)
+
+		# Without pricing rule
+		so = make_sales_order(item_code="_Test Item", qty=2, uom="Box", do_not_submit=True)
+		so.items[0].price_list_rate = 100
+		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
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index c18ae0c..0417e5e 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -529,8 +529,8 @@
 			"item_code": args.item or args.item_code or "_Test Item",
 			"warehouse": args.warehouse,
 			"qty": args.qty or 10,
-			"rate": args.rate or 100,
-			"conversion_factor": 1.0,
+			"uom": args.uom or None,
+			"rate": args.rate or 100
 		})
 
 	if not args.do_not_save: