Merge branch 'develop' into mergify/bp/develop/pr-30395
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 808b766..6c38a7e 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -945,12 +945,55 @@
 		)
 		pos.append("payments", {"mode_of_payment": "Cash", "account": "Cash - TCP1", "amount": 60})
 
-		pos.change_amount = 5.0
+		pos.write_off_outstanding_amount_automatically = 1
 		pos.insert()
 		pos.submit()
 
 		self.assertEqual(pos.grand_total, 100.0)
-		self.assertEqual(pos.write_off_amount, -5)
+		self.assertEqual(pos.write_off_amount, 0)
+
+	def test_auto_write_off_amount(self):
+		make_pos_profile(
+			company="_Test Company with perpetual inventory",
+			income_account="Sales - TCP1",
+			expense_account="Cost of Goods Sold - TCP1",
+			warehouse="Stores - TCP1",
+			cost_center="Main - TCP1",
+			write_off_account="_Test Write Off - TCP1",
+		)
+
+		make_purchase_receipt(
+			company="_Test Company with perpetual inventory",
+			item_code="_Test FG Item",
+			warehouse="Stores - TCP1",
+			cost_center="Main - TCP1",
+		)
+
+		pos = create_sales_invoice(
+			company="_Test Company with perpetual inventory",
+			debit_to="Debtors - TCP1",
+			item_code="_Test FG Item",
+			warehouse="Stores - TCP1",
+			income_account="Sales - TCP1",
+			expense_account="Cost of Goods Sold - TCP1",
+			cost_center="Main - TCP1",
+			do_not_save=True,
+		)
+
+		pos.is_pos = 1
+		pos.update_stock = 1
+
+		pos.append(
+			"payments", {"mode_of_payment": "Bank Draft", "account": "_Test Bank - TCP1", "amount": 50}
+		)
+		pos.append("payments", {"mode_of_payment": "Cash", "account": "Cash - TCP1", "amount": 40})
+
+		pos.write_off_outstanding_amount_automatically = 1
+		pos.insert()
+		pos.submit()
+
+		self.assertEqual(pos.grand_total, 100.0)
+		self.assertEqual(pos.write_off_amount, 10)
 
 	def test_pos_with_no_gl_entry_for_change_amount(self):
 		frappe.db.set_value("Accounts Settings", None, "post_change_gl_entries", 0)
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index ce58d5b..8183b6e 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -679,7 +679,11 @@
 				)
 
 			if self.doc.docstatus == 0:
+				if self.doc.get("write_off_outstanding_amount_automatically"):
+					self.doc.write_off_amount = 0
+
 				self.calculate_outstanding_amount()
+				self.calculate_write_off_amount()
 
 	def is_internal_invoice(self):
 		"""
@@ -731,7 +735,6 @@
 			change_amount = 0
 
 			if self.doc.doctype == "Sales Invoice" and not self.doc.get("is_return"):
-				self.calculate_write_off_amount()
 				self.calculate_change_amount()
 				change_amount = (
 					self.doc.change_amount
@@ -791,28 +794,26 @@
 			and not self.doc.is_return
 			and any(d.type == "Cash" for d in self.doc.payments)
 		):
-
 			self.doc.change_amount = flt(
-				self.doc.paid_amount - grand_total + self.doc.write_off_amount,
-				self.doc.precision("change_amount"),
+				self.doc.paid_amount - grand_total, self.doc.precision("change_amount")
 			)
 
 			self.doc.base_change_amount = flt(
-				self.doc.base_paid_amount - base_grand_total + self.doc.base_write_off_amount,
-				self.doc.precision("base_change_amount"),
+				self.doc.base_paid_amount - base_grand_total, self.doc.precision("base_change_amount")
 			)
 
 	def calculate_write_off_amount(self):
-		if flt(self.doc.change_amount) > 0:
+		if self.doc.get("write_off_outstanding_amount_automatically"):
 			self.doc.write_off_amount = flt(
-				self.doc.grand_total - self.doc.paid_amount + self.doc.change_amount,
-				self.doc.precision("write_off_amount"),
+				self.doc.outstanding_amount, self.doc.precision("write_off_amount")
 			)
 			self.doc.base_write_off_amount = flt(
 				self.doc.write_off_amount * self.doc.conversion_rate,
 				self.doc.precision("base_write_off_amount"),
 			)
 
+			self.calculate_outstanding_amount()
+
 	def calculate_margin(self, item):
 		rate_with_margin = 0.0
 		base_rate_with_margin = 0.0
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index 9fec43b..047ec81 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -689,7 +689,12 @@
 		}));
 		this.frm.doc.total_advance = flt(total_allocated_amount, precision("total_advance"));
 
+		if (this.frm.doc.write_off_outstanding_amount_automatically) {
+			this.frm.doc.write_off_amount = 0;
+		}
+
 		this.calculate_outstanding_amount(update_paid_amount);
+		this.calculate_write_off_amount();
 	}
 
 	is_internal_invoice() {
@@ -825,26 +830,23 @@
 				var grand_total = this.frm.doc.rounded_total || this.frm.doc.grand_total;
 				var base_grand_total = this.frm.doc.base_rounded_total || this.frm.doc.base_grand_total;
 
-				this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - grand_total +
-					this.frm.doc.write_off_amount, precision("change_amount"));
+				this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - grand_total,
+					precision("change_amount"));
 
 				this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount -
-					base_grand_total + this.frm.doc.base_write_off_amount,
-					precision("base_change_amount"));
+					base_grand_total, precision("base_change_amount"));
 			}
 		}
 	}
 
-	calculate_write_off_amount(){
-		if(this.frm.doc.paid_amount > this.frm.doc.grand_total){
-			this.frm.doc.write_off_amount = flt(this.frm.doc.grand_total - this.frm.doc.paid_amount
-				+ this.frm.doc.change_amount, precision("write_off_amount"));
-
+	calculate_write_off_amount() {
+		if(this.frm.doc.write_off_outstanding_amount_automatically) {
+			this.frm.doc.write_off_amount = flt(this.frm.doc.outstanding_amount, precision("write_off_amount"));
 			this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
 				precision("base_write_off_amount"));
-		}else{
-			this.frm.doc.paid_amount = 0.0;
+
+			this.calculate_outstanding_amount(false);
 		}
-		this.calculate_outstanding_amount(false);
+
 	}
 };