fix: correct calculation for discount amount when margin is set (#25179)

diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index e329b32..5f73c55 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -113,10 +113,10 @@
 					item.rate_with_margin, item.base_rate_with_margin = self.calculate_margin(item)
 					if flt(item.rate_with_margin) > 0:
 						item.rate = flt(item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
-						if not item.discount_amount:
-							item.discount_amount = item.rate_with_margin - item.rate
-						elif not item.discount_percentage:
+						if item.discount_amount and not item.discount_percentage:
 							item.rate -= item.discount_amount
+						else:
+							item.discount_amount = item.rate_with_margin - item.rate
 					elif flt(item.price_list_rate) > 0:
 						item.discount_amount = item.price_list_rate - item.rate
 				elif flt(item.price_list_rate) > 0 and not item.discount_amount:
@@ -808,4 +808,4 @@
 	def set_amounts_in_company_currency(self):
 		for d in self.doc.get(self.tax_field):
 			d.amount = flt(d.amount, d.precision("amount"))
-			d.base_amount = flt(d.amount * flt(d.exchange_rate), d.precision("base_amount"))
\ No newline at end of file
+			d.base_amount = flt(d.amount * flt(d.exchange_rate), d.precision("base_amount"))