[hotfix] multicurrency patch

This commit is contained in:
Anand Doshi 2015-09-17 17:28:39 +05:30
parent a8de61e24b
commit c439b87ccc

View File

@ -6,84 +6,84 @@ import frappe
def execute(): def execute():
# Reload doctype # Reload doctype
for dt in ("Account", "GL Entry", "Journal Entry", for dt in ("Account", "GL Entry", "Journal Entry",
"Journal Entry Account", "Sales Invoice", "Purchase Invoice", "Customer", "Supplier"): "Journal Entry Account", "Sales Invoice", "Purchase Invoice", "Customer", "Supplier"):
frappe.reload_doctype(dt) frappe.reload_doctype(dt)
company_list = 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: for company in company_list:
# update currency in account and gl entry as per company currency # update currency in account and gl entry as per company currency
frappe.db.sql("""update `tabAccount` set account_currency = %s frappe.db.sql("""update `tabAccount` set account_currency = %s
where ifnull(account_currency, '') = '' and company=%s""", (company.default_currency, company.name)) where ifnull(account_currency, '') = '' and company=%s""", (company.default_currency, company.name))
# update newly introduced field's value in sales / purchase invoice # update newly introduced field's value in sales / purchase invoice
frappe.db.sql(""" frappe.db.sql("""
update update
`tabSales Invoice` `tabSales Invoice`
set set
base_paid_amount=paid_amount, base_paid_amount=paid_amount,
base_write_off_amount=write_off_amount, base_write_off_amount=write_off_amount,
party_account_currency=%s party_account_currency=%s
where company=%s where company=%s
""", (company.default_currency, company.name)) """, (company.default_currency, company.name))
frappe.db.sql(""" frappe.db.sql("""
update update
`tabPurchase Invoice` `tabPurchase Invoice`
set set
base_write_off_amount=write_off_amount, base_write_off_amount=write_off_amount,
party_account_currency=%s party_account_currency=%s
where company=%s where company=%s
""", (company.default_currency, company.name)) """, (company.default_currency, company.name))
# update exchange rate, debit/credit in account currency in Journal Entry # update exchange rate, debit/credit in account currency in Journal Entry
frappe.db.sql(""" frappe.db.sql("""
update `tabJournal Entry Account` jea update `tabJournal Entry Account` jea
set exchange_rate=1, set exchange_rate=1,
debit_in_account_currency=debit, debit_in_account_currency=debit,
credit_in_account_currency=credit, credit_in_account_currency=credit,
account_type=(select account_type from `tabAccount` where name=jea.account) account_type=(select account_type from `tabAccount` where name=jea.account)
""") """)
frappe.db.sql(""" frappe.db.sql("""
update `tabJournal Entry Account` jea, `tabJournal Entry` je update `tabJournal Entry Account` jea, `tabJournal Entry` je
set account_currency=%s set account_currency=%s
where jea.parent = je.name and je.company=%s where jea.parent = je.name and je.company=%s
""", (company.default_currency, company.name)) """, (company.default_currency, company.name))
# update debit/credit in account currency in GL Entry # update debit/credit in account currency in GL Entry
frappe.db.sql(""" frappe.db.sql("""
update update
`tabGL Entry` `tabGL Entry`
set set
debit_in_account_currency=debit, debit_in_account_currency=debit,
credit_in_account_currency=credit, credit_in_account_currency=credit,
account_currency=%s account_currency=%s
where where
company=%s company=%s
""", (company.default_currency, company.name)) """, (company.default_currency, company.name))
# Set party account if default currency of party other than company's default currency # Set party account if default currency of party other than company's default currency
for dt in ("Customer", "Supplier"): for dt in ("Customer", "Supplier"):
parties = frappe.get_all(dt) parties = frappe.get_all(dt)
for p in parties: for p in parties:
party = frappe.get_doc(dt, p.name) party = frappe.get_doc(dt, p.name)
party_accounts = []
for company in company_list: for company in company_list:
# Get party GL Entries # Get party GL Entries
party_gle = frappe.db.get_value("GL Entry", {"party_type": dt, "party": p.name, party_gle = frappe.db.get_value("GL Entry", {"party_type": dt, "party": p.name,
"company": company.name}, ["account", "account_currency", "name"], as_dict=True) "company": company.name}, ["account", "account_currency", "name"], as_dict=True)
# set party account currency # set party account currency
if party_gle: if party_gle:
party.party_account_currency = party_gle.account_currency party.party_account_currency = party_gle.account_currency
elif not party.party_account_currency: elif not party.party_account_currency:
party.party_account_currency = company.default_currency party.party_account_currency = company.default_currency
# Add default receivable /payable account if not exists # Add default receivable /payable account if not exists
# and currency is other than company currency # and currency is other than company currency
party_accounts = []
if party.party_account_currency and party.party_account_currency != company.default_currency: if party.party_account_currency and party.party_account_currency != company.default_currency:
party_account_exists_for_company = False party_account_exists_for_company = False
for d in party.get("accounts"): for d in party.get("accounts"):
@ -96,22 +96,23 @@ def execute():
}) })
party_account_exists_for_company = True party_account_exists_for_company = True
break break
if not party_account_exists_for_company: if not party_account_exists_for_company:
party_account = None party_account = None
if party_gle: if party_gle:
party_account = party_gle.account party_account = party_gle.account
else: else:
default_receivable_account_currency = frappe.db.get_value("Account", default_receivable_account_currency = frappe.db.get_value("Account",
company.default_receivable_account, "account_currency") company.default_receivable_account, "account_currency")
if default_receivable_account_currency != company.default_currency: if default_receivable_account_currency != company.default_currency:
party_account = company.default_receivable_account party_account = company.default_receivable_account
if party_account: if party_account:
party_accounts.append({ party_accounts.append({
"company": company.name, "company": company.name,
"account": party_account "account": party_account
}) })
party.set("accounts", party_accounts) party.set("accounts", party_accounts)
party.flags.ignore_mandatory = True party.flags.ignore_mandatory = True
party.save() party.save()