fix: Create common function for discount accounting
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index c1cc092..fdaa7f0 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -446,6 +446,7 @@
 
 		self.make_supplier_gl_entry(gl_entries)
 		self.make_item_gl_entries(gl_entries)
+		self.make_discount_gl_entries(gl_entries)
 
 		if self.check_asset_cwip_enabled():
 			self.get_asset_gl_entry(gl_entries)
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 15e7951..02d8470 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -959,40 +959,6 @@
 			erpnext.is_perpetual_inventory_enabled(self.company):
 			gl_entries += super(SalesInvoice, self).get_gl_entries()
 
-	def make_discount_gl_entries(self, gl_entries):
-		enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting'))
-
-		if enable_discount_accounting:
-			for item in self.get("items"):
-				if item.get('discount_amount') and item.get('discount_account'):
-					account_currency = get_account_currency(item.discount_account)
-					gl_entries.append(
-						self.get_gl_dict({
-							"account": item.discount_account,
-							"against": self.customer,
-							"debit": flt(item.discount_amount),
-							"debit_in_account_currency": flt(item.discount_amount),
-							"cost_center": self.cost_center,
-							"project": self.project
-						}, account_currency, item=self)
-					)
-
-					income_account = (item.income_account
-						if (not item.enable_deferred_revenue or self.is_return) 
-						else item.deferred_revenue_account)
-
-					account_currency = get_account_currency(income_account)
-					gl_entries.append(
-						self.get_gl_dict({
-							"account": income_account,
-							"against": self.customer,
-							"credit": flt(item.discount_amount),
-							"credit_in_account_currency": flt(item.discount_amount),
-							"cost_center": item.cost_center,
-							"project": item.project or self.project
-						}, account_currency, item=item)
-					)
-
 	def make_loyalty_point_redemption_gle(self, gl_entries):
 		if cint(self.redeem_loyalty_points):
 			gl_entries.append(
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 1c086e9..dac7f0b 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -741,6 +741,51 @@
 							tax_map[tax.account_head] -= allocated_amount
 							allocated_tax_map[tax.account_head] -= allocated_amount
 
+	def make_discount_gl_entries(self, gl_entries):
+		enable_discount_accounting = cint(frappe.db.get_single_value('Accounts Settings', 'enable_discount_accounting'))
+
+		if enable_discount_accounting:
+			for item in self.get("items"):
+				if item.get('discount_amount') and item.get('discount_account'):
+					if self.doctype == "Purchase Invoice":
+						dr_or_cr = "credit"
+						rev_dr_cr = "debit"
+						supplier_or_customer = self.supplier
+						income_or_expense_account = (item.expense_account
+							if (not item.enable_deferred_expense or self.is_return) 
+							else item.deferred_expense_account)
+					else:
+						dr_or_cr = "debit"
+						rev_dr_cr = "credit"
+						supplier_or_customer = self.customer
+						income_or_expense_account = (item.income_account
+							if (not item.enable_deferred_revenue or self.is_return) 
+							else item.deferred_revenue_account)
+
+					account_currency = get_account_currency(item.discount_account)
+					gl_entries.append(
+						self.get_gl_dict({
+							"account": item.discount_account,
+							"against": supplier_or_customer,
+							dr_or_cr: flt(item.discount_amount),
+							dr_or_cr + "_in_account_currency": flt(item.discount_amount),
+							"cost_center": self.cost_center,
+							"project": self.project
+						}, account_currency, item=self)
+					)
+
+					account_currency = get_account_currency(income_or_expense_account)
+					gl_entries.append(
+						self.get_gl_dict({
+							"account": income_or_expense_account,
+							"against": supplier_or_customer,
+							rev_dr_cr: flt(item.discount_amount),
+							rev_dr_cr + "_in_account_currency": flt(item.discount_amount),
+							"cost_center": item.cost_center,
+							"project": item.project or self.project
+						}, account_currency, item=item)
+					)
+
 	def allocate_advance_taxes(self, gl_entries):
 		tax_map = self.get_tax_map()
 		for pe in self.get("advances"):