diff --git a/erpnext/patches/v6_0/multi_currency.py b/erpnext/patches/v6_0/multi_currency.py index 2b53134c05..979e03e2f2 100644 --- a/erpnext/patches/v6_0/multi_currency.py +++ b/erpnext/patches/v6_0/multi_currency.py @@ -9,8 +9,9 @@ def execute(): for dt in ("Account", "GL Entry", "Journal Entry", "Journal Entry Account", "Sales Invoice", "Purchase Invoice", "Customer", "Supplier"): frappe.reload_doctype(dt) - - for company in frappe.get_all("Company", fields=["name", "default_currency", "default_receivable_account"]): + + company_list = frappe.get_all("Company", fields=["name", "default_currency", "default_receivable_account"]) + for company in company_list: # update currency in account and gl entry as per company currency frappe.db.sql("""update `tabAccount` set account_currency = %s @@ -63,30 +64,40 @@ def execute(): company=%s """, (company.default_currency, company.name)) - # Set party account if default currency of party other than company's default currency - for dt in ("Customer", "Supplier"): - parties = frappe.get_all(dt) - for p in parties: + # Set party account if default currency of party other than company's default currency + for dt in ("Customer", "Supplier"): + parties = frappe.get_all(dt) + for p in parties: + party = frappe.get_doc(dt, p.name) + + for company in company_list: # Get party GL Entries party_gle = frappe.db.get_value("GL Entry", {"party_type": dt, "party": p.name, - "company": company.name}, ["account", "account_currency"], as_dict=True) - - party = frappe.get_doc(dt, p.name) - + "company": company.name}, ["account", "account_currency", "name"], as_dict=True) + # set party account currency - if party_gle or not party.party_account_currency: + if party_gle: + party.party_account_currency = party_gle.account_currency + elif not party.party_account_currency: party.party_account_currency = company.default_currency # Add default receivable /payable account if not exists # and currency is other than company currency + party_accounts = [] if party.party_account_currency and party.party_account_currency != company.default_currency: - party_account_exists = False + party_account_exists_for_company = False for d in party.get("accounts"): if d.company == company.name: - party_account_exists = True - break - - if not party_account_exists: + account_currency = frappe.db.get_value("Account", d.account, "account_currency") + if account_currency == party.party_account_currency: + party_accounts.append({ + "company": d.company, + "account": d.account + }) + party_account_exists_for_company = True + break + + if not party_account_exists_for_company: party_account = None if party_gle: party_account = party_gle.account @@ -95,12 +106,12 @@ def execute(): company.default_receivable_account, "account_currency") if default_receivable_account_currency != company.default_currency: party_account = company.default_receivable_account - + if party_account: - party.append("accounts", { + party_accounts.append({ "company": company.name, "account": party_account }) - - party.flags.ignore_mandatory = True - party.save() \ No newline at end of file + party.set("accounts", party_accounts) + party.flags.ignore_mandatory = True + party.save() \ No newline at end of file