fix: pricing rule not working on item groups (#20547)

Co-authored-by: Chinmay Pai <chinmaydpai@gmail.com>
diff --git a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
index 332a900..9c1fef6 100644
--- a/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
@@ -9,6 +9,8 @@
 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
+from erpnext.stock.doctype.item.test_item import make_item
+from erpnext.healthcare.doctype.lab_test_template.lab_test_template import make_item_price
 
 class TestPricingRule(unittest.TestCase):
 	def setUp(self):
@@ -145,6 +147,52 @@
 		self.assertEquals(details.get("margin_type"), "Percentage")
 		self.assertEquals(details.get("margin_rate_or_amount"), 10)
 
+	def test_mixed_conditions_for_item_group(self):
+		for item in ["Mixed Cond Item 1", "Mixed Cond Item 2"]:
+			make_item(item, {"item_group": "Products"})
+			make_item_price(item, "_Test Price List", 100)
+
+		test_record = {
+			"doctype": "Pricing Rule",
+			"title": "_Test Pricing Rule for Item Group",
+			"apply_on": "Item Group",
+			"item_groups": [
+				{
+					"item_group": "Products",
+				},
+				{
+					"item_group": "Seed",
+				},
+			],
+			"selling": 1,
+			"mixed_conditions": 1,
+			"currency": "USD",
+			"rate_or_discount": "Discount Percentage",
+			"discount_percentage": 10,
+			"applicable_for": "Customer Group",
+			"customer_group": "All Customer Groups",
+			"company": "_Test Company"
+		}
+		frappe.get_doc(test_record.copy()).insert()
+
+		args = frappe._dict({
+			"item_code": "Mixed Cond Item 1",
+			"item_group": "Products",
+			"company": "_Test Company",
+			"price_list": "_Test Price List",
+			"currency": "_Test Currency",
+			"doctype": "Sales Order",
+			"conversion_rate": 1,
+			"price_list_currency": "_Test Currency",
+			"plc_conversion_rate": 1,
+			"order_type": "Sales",
+			"customer": "_Test Customer",
+			"customer_group": "_Test Customer Group",
+			"name": None
+		})
+		details = get_item_details(args)
+		self.assertEquals(details.get("discount_percentage"), 10)
+
 	def test_pricing_rule_for_variants(self):
 		from erpnext.stock.get_item_details import get_item_details
 		from frappe import MandatoryError
diff --git a/erpnext/accounts/doctype/pricing_rule/utils.py b/erpnext/accounts/doctype/pricing_rule/utils.py
index fe68fdb..e475563 100644
--- a/erpnext/accounts/doctype/pricing_rule/utils.py
+++ b/erpnext/accounts/doctype/pricing_rule/utils.py
@@ -489,7 +489,7 @@
 
 	for d in pr_doc.get(pricing_rule_apply_on):
 		if apply_on == 'item_group':
-			get_child_item_groups(d.get(apply_on))
+			apply_on_data.extend(get_child_item_groups(d.get(apply_on)))
 		else:
 			apply_on_data.append(d.get(apply_on))
 
diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py
index 9f25882..4377840 100644
--- a/erpnext/setup/doctype/item_group/item_group.py
+++ b/erpnext/setup/doctype/item_group/item_group.py
@@ -174,15 +174,11 @@
 			and show_in_website = 1""", {"lft": item_group.lft, "rgt": item_group.rgt})
 
 def get_child_item_groups(item_group_name):
-	child_item_groups = frappe.cache().hget("child_item_groups", item_group_name)
+	item_group = frappe.get_cached_value("Item Group",
+		item_group_name, ["lft", "rgt"], as_dict=1)
 
-	if not child_item_groups:
-		item_group = frappe.get_cached_doc("Item Group", item_group_name)
-
-		child_item_groups = [d.name for d in frappe.get_all('Item Group',
-			filters= {'lft': ('>=', item_group.lft),'rgt': ('>=', item_group.rgt)})]
-
-		frappe.cache().hset("child_item_groups", item_group_name, child_item_groups)
+	child_item_groups = [d.name for d in frappe.get_all('Item Group',
+		filters= {'lft': ('>=', item_group.lft),'rgt': ('<=', item_group.rgt)})]
 
 	return child_item_groups or {}