diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index bcaf8f897f9..d8581f58a4f 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -165,6 +165,48 @@ class AccountsController(TransactionBase):
raise_exception=1,
)
+ def validate_against_voucher_outstanding(self):
+ from frappe.model.meta import get_meta
+
+ if not get_meta(self.doctype).has_field("outstanding_amount"):
+ return
+
+ if self.get("is_return") and self.return_against and not self.get("is_pos"):
+ against_voucher_outstanding = frappe.get_value(
+ self.doctype, self.return_against, "outstanding_amount"
+ )
+ document_type = "Credit Note" if self.doctype == "Sales Invoice" else "Debit Note"
+
+ msg = ""
+ if self.get("update_outstanding_for_self"):
+ msg = (
+ "We can see {0} is made against {1}. If you want {1}'s outstanding to be updated, "
+ "uncheck '{2}' checkbox.
Or"
+ ).format(
+ frappe.bold(document_type),
+ get_link_to_form(self.doctype, self.get("return_against")),
+ frappe.bold(_("Update Outstanding for Self")),
+ )
+
+ elif not self.update_outstanding_for_self and (
+ abs(flt(self.rounded_total) or flt(self.grand_total)) > flt(against_voucher_outstanding)
+ ):
+ self.update_outstanding_for_self = 1
+ msg = (
+ "The outstanding amount {} in {} is lesser than {}. Updating the outstanding to this invoice.
And"
+ ).format(
+ against_voucher_outstanding,
+ get_link_to_form(self.doctype, self.get("return_against")),
+ flt(abs(self.outstanding_amount)),
+ )
+
+ if msg:
+ msg += " you can use {} tool to reconcile against {} later.".format(
+ get_link_to_form("Payment Reconciliation"),
+ get_link_to_form(self.doctype, self.get("return_against")),
+ )
+ frappe.msgprint(_(msg))
+
def validate(self):
if not self.get("is_return") and not self.get("is_debit_note"):
self.validate_qty_is_not_zero()
@@ -193,6 +235,7 @@ class AccountsController(TransactionBase):
self.disable_tax_included_prices_for_internal_transfer()
self.set_incoming_rate()
self.init_internal_values()
+ self.validate_against_voucher_outstanding()
# Need to set taxes based on taxes_and_charges template
# before calculating taxes and totals
@@ -228,6 +271,7 @@ class AccountsController(TransactionBase):
)
)
+<<<<<<< HEAD
if self.get("is_return") and self.get("return_against") and not self.get("is_pos"):
if self.get("update_outstanding_for_self"):
document_type = "Credit Note" if self.doctype == "Sales Invoice" else "Debit Note"
@@ -242,6 +286,8 @@ class AccountsController(TransactionBase):
)
)
+=======
+>>>>>>> 222f1834f1 (fix(accounting): update outstanding amount based on update_outstanding_for_self)
pos_check_field = "is_pos" if self.doctype == "Sales Invoice" else "is_paid"
if cint(self.allocate_advances_automatically) and not cint(self.get(pos_check_field)):
self.set_advances()
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 091b08cad0f..955c9261031 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -819,9 +819,12 @@ class calculate_taxes_and_totals:
if (
self.doc.is_return
and self.doc.return_against
+ and not self.doc.update_outstanding_for_self
and not self.doc.get("is_pos")
or self.is_internal_invoice()
):
+ # Do not calculate the outstanding amount for a return invoice if 'update_outstanding_for_self' is not enabled.
+ self.doc.outstanding_amount = 0
return
self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])