From 78ad3f6cdc8689cc6c297eb8ca4d1adcedc17b6a Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Wed, 29 May 2024 11:26:10 +0530 Subject: [PATCH] refactor: validation to force accounts to be on same currency (cherry picked from commit 0f0b4d88bcd5e298154748a58c20589c5aa9f531) --- erpnext/buying/doctype/supplier/supplier.py | 1 + erpnext/selling/doctype/customer/customer.py | 1 + erpnext/utilities/transaction_base.py | 57 ++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py index 0df17fdd4b9..ff5385dd961 100644 --- a/erpnext/buying/doctype/supplier/supplier.py +++ b/erpnext/buying/doctype/supplier/supplier.py @@ -138,6 +138,7 @@ class Supplier(TransactionBase): validate_party_accounts(self) self.validate_internal_supplier() self.add_role_for_user() + self.validate_currency_for_receivable_payable_and_advance_account() @frappe.whitelist() def get_supplier_group_details(self): diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index f5185c2ff5b..8ce67cc659a 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -144,6 +144,7 @@ class Customer(TransactionBase): self.validate_default_bank_account() self.validate_internal_customer() self.add_role_for_user() + self.validate_currency_for_receivable_payable_and_advance_account() # set loyalty program tier if frappe.db.exists("Customer", self.name): diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py index 3b7812f96c2..9559c9170cf 100644 --- a/erpnext/utilities/transaction_base.py +++ b/erpnext/utilities/transaction_base.py @@ -168,6 +168,63 @@ class TransactionBase(StatusUpdater): if len(child_table_values) > 1: self.set(default_field, None) + def validate_currency_for_receivable_payable_and_advance_account(self): + if self.doctype in ["Customer", "Supplier"]: + account_type = "Receivable" if self.doctype == "Customer" else "Payable" + for x in self.accounts: + company_default_currency = frappe.get_cached_value("Company", x.company, "default_currency") + receivable_payable_account_currency = None + advance_account_currency = None + + if x.account: + receivable_payable_account_currency = frappe.get_cached_value( + "Account", x.account, "account_currency" + ) + + if x.advance_account: + advance_account_currency = frappe.get_cached_value( + "Account", x.advance_account, "account_currency" + ) + if receivable_payable_account_currency and ( + receivable_payable_account_currency != self.default_currency + and receivable_payable_account_currency != company_default_currency + ): + frappe.throw( + _( + "{0} Account must be in either customer billing currency: {1} or Company default currency: {2}" + ).format( + account_type, + frappe.bold(self.default_currency), + frappe.bold(company_default_currency), + ) + ) + + if advance_account_currency and ( + advance_account_currency != self.default_currency + and advance_account_currency != company_default_currency + ): + frappe.throw( + _( + "Advance Account must be in either customer billing currency: {0} or Company default currency: {1}" + ).format(frappe.bold(self.default_currency), frappe.bold(company_default_currency)) + ) + + if ( + receivable_payable_account_currency + and advance_account_currency + and receivable_payable_account_currency != advance_account_currency + ): + frappe.throw( + _( + "Both {0} Account: {1} and Advance Account: {2} must be of same currency for company: {3}" + ).format( + account_type, + frappe.bold(x.account), + frappe.bold(x.advance_account), + frappe.bold(x.company), + ) + ) + def delete_events(ref_type, ref_name): events = (