2015-08-27 07:25:24 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
|
|
|
|
|
|
|
def execute():
|
|
|
|
# Reload doctype
|
|
|
|
for dt in ("Account", "GL Entry", "Journal Entry",
|
2015-09-03 04:58:08 +00:00
|
|
|
"Journal Entry Account", "Sales Invoice", "Purchase Invoice", "Customer", "Supplier"):
|
2015-08-27 07:25:24 +00:00
|
|
|
frappe.reload_doctype(dt)
|
|
|
|
|
|
|
|
for company in frappe.get_all("Company", fields=["name", "default_currency", "default_receivable_account"]):
|
|
|
|
|
|
|
|
# update currency in account and gl entry as per company currency
|
2015-08-28 13:56:28 +00:00
|
|
|
frappe.db.sql("""update `tabAccount` set account_currency = %s
|
|
|
|
where ifnull(account_currency, '') = '' and company=%s""", (company.default_currency, company.name))
|
2015-08-27 07:25:24 +00:00
|
|
|
|
|
|
|
# update newly introduced field's value in sales / purchase invoice
|
|
|
|
frappe.db.sql("""
|
|
|
|
update
|
|
|
|
`tabSales Invoice`
|
|
|
|
set
|
|
|
|
base_paid_amount=paid_amount,
|
|
|
|
base_write_off_amount=write_off_amount,
|
|
|
|
party_account_currency=%s
|
|
|
|
where company=%s
|
|
|
|
""", (company.default_currency, company.name))
|
|
|
|
|
|
|
|
frappe.db.sql("""
|
|
|
|
update
|
|
|
|
`tabPurchase Invoice`
|
|
|
|
set
|
|
|
|
base_write_off_amount=write_off_amount,
|
|
|
|
party_account_currency=%s
|
|
|
|
where company=%s
|
|
|
|
""", (company.default_currency, company.name))
|
|
|
|
|
|
|
|
# update exchange rate, debit/credit in account currency in Journal Entry
|
|
|
|
frappe.db.sql("""update `tabJournal Entry` set exchange_rate=1""")
|
|
|
|
|
|
|
|
frappe.db.sql("""
|
|
|
|
update
|
|
|
|
`tabJournal Entry Account` jea, `tabJournal Entry` je
|
|
|
|
set
|
|
|
|
debit_in_account_currency=debit,
|
|
|
|
credit_in_account_currency=credit,
|
2015-08-28 13:56:28 +00:00
|
|
|
account_currency=%s
|
2015-08-27 07:25:24 +00:00
|
|
|
where
|
|
|
|
jea.parent = je.name
|
|
|
|
and je.company=%s
|
|
|
|
""", (company.default_currency, company.name))
|
|
|
|
|
|
|
|
# update debit/credit in account currency in GL Entry
|
|
|
|
frappe.db.sql("""
|
|
|
|
update
|
|
|
|
`tabGL Entry`
|
|
|
|
set
|
|
|
|
debit_in_account_currency=debit,
|
|
|
|
credit_in_account_currency=credit,
|
2015-08-28 13:56:28 +00:00
|
|
|
account_currency=%s
|
2015-08-27 07:25:24 +00:00
|
|
|
where
|
|
|
|
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"):
|
2015-09-03 13:48:00 +00:00
|
|
|
parties = frappe.get_all(dt)
|
2015-08-27 07:25:24 +00:00
|
|
|
for p in parties:
|
2015-09-03 04:58:08 +00:00
|
|
|
# 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)
|
|
|
|
|
2015-09-03 13:48:00 +00:00
|
|
|
# set party account currency
|
|
|
|
if party_gle or not party.party_account_currency:
|
|
|
|
party.party_account_currency = company.default_currency
|
2015-09-03 04:58:08 +00:00
|
|
|
|
|
|
|
# Add default receivable /payable account if not exists
|
|
|
|
# and currency is other than company currency
|
2015-09-03 13:48:00 +00:00
|
|
|
if party.party_account_currency and party.party_account_currency != company.default_currency:
|
2015-09-03 04:58:08 +00:00
|
|
|
party_account_exists = False
|
|
|
|
for d in party.get("accounts"):
|
|
|
|
if d.company == company.name:
|
|
|
|
party_account_exists = True
|
|
|
|
break
|
2015-08-27 07:25:24 +00:00
|
|
|
|
2015-09-03 04:58:08 +00:00
|
|
|
if not party_account_exists:
|
|
|
|
party_account = party_gle.account if party_gle else company.default_receivable_account
|
|
|
|
if party_account:
|
|
|
|
party.append("accounts", {
|
|
|
|
"company": company.name,
|
|
|
|
"account": party_account
|
|
|
|
})
|
2015-08-27 07:25:24 +00:00
|
|
|
|
2015-09-03 04:58:08 +00:00
|
|
|
party.flags.ignore_mandatory = True
|
2015-08-27 07:25:24 +00:00
|
|
|
party.save()
|