From 10fcf5af5fb5a51a05b9f63c544e47feda0ae3c4 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 25 Jan 2024 14:05:42 +0530 Subject: [PATCH] refactor: prevent '{debit/credit}_to' account mismatch (cherry picked from commit 6f2fae1b6133286ab882e421e69f6a87ecb97d4a) --- erpnext/controllers/accounts_controller.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 2b13ca14c6..03c3cd2f9d 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -202,6 +202,7 @@ class AccountsController(TransactionBase): self.validate_party() self.validate_currency() self.validate_party_account_currency() + self.validate_return_against_account() if self.doctype in ["Purchase Invoice", "Sales Invoice"]: if invalid_advances := [ @@ -350,6 +351,20 @@ class AccountsController(TransactionBase): for bundle in bundles: frappe.delete_doc("Serial and Batch Bundle", bundle.name) + def validate_return_against_account(self): + if ( + self.doctype in ["Sales Invoice", "Purchase Invoice"] and self.is_return and self.return_against + ): + cr_dr_account_field = "debit_to" if self.doctype == "Sales Invoice" else "credit_to" + cr_dr_account_label = "Debit To" if self.doctype == "Sales Invoice" else "Credit To" + cr_dr_account = self.get(cr_dr_account_field) + if frappe.get_value(self.doctype, self.return_against, cr_dr_account_field) != cr_dr_account: + frappe.throw( + _("'{0}' account: '{1}' should match the Return Against Invoice").format( + frappe.bold(cr_dr_account_label), frappe.bold(cr_dr_account) + ) + ) + def validate_deferred_income_expense_account(self): field_map = { "Sales Invoice": "deferred_revenue_account",