From 8e2bfc6bcbb65be8b3ee9e220ed49ae962f57e01 Mon Sep 17 00:00:00 2001 From: Bhavan23 Date: Thu, 20 Mar 2025 19:19:47 +0530 Subject: [PATCH] fix(accounting): update outstanding amount based on update_outstanding_for_self fix(accounting): against voucher has been already paid show proper message and update update_outstanding_for_self as 1 (cherry picked from commit 222f1834f1c4264bdea2a64ecc69a87ef7124106) # Conflicts: # erpnext/controllers/accounts_controller.py --- erpnext/controllers/accounts_controller.py | 46 ++++++++++++++++++++++ erpnext/controllers/taxes_and_totals.py | 3 ++ 2 files changed, 49 insertions(+) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 0ba33999c5c..a146628cd25 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -153,6 +153,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() @@ -178,6 +220,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() if self.meta.get_field("currency"): self.calculate_taxes_and_totals() @@ -209,6 +252,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" @@ -223,6 +267,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 313ed5c0415..ced62b71cf9 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -795,9 +795,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"])