fix: unique gl account for plaid bank accounts

(cherry picked from commit bf6e32a960)
This commit is contained in:
Gursheen Anand 2024-02-25 13:17:41 +05:30 committed by Mergify
parent df7afc3154
commit 65853da505

View File

@ -10,7 +10,6 @@ from frappe.model.document import Document
from frappe.utils import add_months, formatdate, getdate, sbool, today
from plaid.errors import ItemError
from erpnext.accounts.doctype.journal_entry.journal_entry import get_default_bank_cash_account
from erpnext.erpnext_integrations.doctype.plaid_settings.plaid_connector import PlaidConnector
@ -90,9 +89,15 @@ def add_bank_accounts(response, bank, company):
bank = json.loads(bank)
result = []
default_gl_account = get_default_bank_cash_account(company, "Bank")
if not default_gl_account:
frappe.throw(_("Please setup a default bank account for company {0}").format(company))
parent_gl_account = frappe.db.get_all(
"Account", {"company": company, "account_type": "Bank", "is_group": 1, "disabled": 0}
)
if not parent_gl_account:
frappe.throw(
_(
"Please setup and enable a group account with the Account Type - {0} for the company {1}"
).format(frappe.bold("Bank"), company)
)
for account in response["accounts"]:
acc_type = frappe.db.get_value("Bank Account Type", account["type"])
@ -108,11 +113,22 @@ def add_bank_accounts(response, bank, company):
if not existing_bank_account:
try:
gl_account = frappe.get_doc(
{
"doctype": "Account",
"account_name": account["name"] + " - " + response["institution"]["name"],
"parent_account": parent_gl_account[0].name,
"account_type": "Bank",
"company": company,
}
)
gl_account.insert(ignore_if_duplicate=True)
new_account = frappe.get_doc(
{
"doctype": "Bank Account",
"bank": bank["bank_name"],
"account": default_gl_account.account,
"account": gl_account.name,
"account_name": account["name"],
"account_type": account.get("type", ""),
"account_subtype": account.get("subtype", ""),