From b2739cbb79967b72178cc2bfb5f24512b467183b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 8 Sep 2015 12:18:45 +0530 Subject: [PATCH] Get average exchange rate in case of bank transfer --- .../doctype/journal_entry/journal_entry.js | 3 +- .../doctype/journal_entry/journal_entry.py | 40 ++++++++++++++----- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js index 44acab5237..34892c54e7 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.js +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js @@ -316,7 +316,8 @@ frappe.ui.form.on("Journal Entry Account", { args: { account: d.account, date: frm.doc.posting_date, - company: frm.doc.company + company: frm.doc.company, + credited: flt(d.credit_in_account_currency) > 0 ? true : false }, callback: function(r) { if(r.message) { diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 4640b103d7..06fe1a3a19 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -276,12 +276,10 @@ class JournalEntry(AccountsController): if len(alternate_currency) > 1: frappe.throw(_("Only one alternate currency can be used in a single Journal Entry")) - if not self.exchange_rate: - self.exchange_rate = get_exchange_rate(alternate_currency[0], self.company_currency) - + self.set_exchange_rate() + if not self.exchange_rate: frappe.throw(_("Exchange Rate is mandatory in multi-currency Journal Entry")) - else: self.exchange_rate = 1.0 @@ -290,7 +288,18 @@ class JournalEntry(AccountsController): d.debit = flt(flt(d.debit_in_account_currency)*exchange_rate, d.precision("debit")) d.credit = flt(flt(d.credit_in_account_currency)*exchange_rate, d.precision("credit")) - + + def set_exchange_rate(self): + for d in self.get("accounts"): + if d.account_currency != self.company_currency: + account_type = frappe.db.get_value("Account", d.account, "account_type") + if account_type == "Bank" and flt(d.credit_in_account_currency) > 0: + self.exchange_rate = get_average_exchange_rate(d.account) + break + if not self.exchange_rate: + self.exchange_rate = get_exchange_rate(d.account_currency, self.company_currency) + + def create_remarks(self): r = [] @@ -723,7 +732,7 @@ def get_party_account_and_balance(company, party_type, party): } @frappe.whitelist() -def get_account_balance_and_party_type(account, date, company): +def get_account_balance_and_party_type(account, date, company, credited=False): """Returns dict of account balance and party type to be set in Journal Entry on selection of account.""" if not frappe.has_permission("Account"): frappe.msgprint(_("No Permission"), raise_exception=1) @@ -740,11 +749,24 @@ def get_account_balance_and_party_type(account, date, company): exchange_rate = None if account_details.account_currency != company_currency: - exchange_rate = get_exchange_rate(account_details.account_currency, company_currency) - + if account_details.account_type == "Bank" and credited: + exchange_rate = get_average_exchange_rate(account) + else: + exchange_rate = get_exchange_rate(account_details.account_currency, company_currency) + grid_values = { "balance": get_balance_on(account, date), "party_type": party_type, "account_currency": account_details.account_currency or company_currency, } - return grid_values, exchange_rate \ No newline at end of file + return grid_values, exchange_rate + +def get_average_exchange_rate(account): + exchange_rate = 0 + bank_balance_in_account_currency = get_balance_on(account) + if bank_balance_in_account_currency: + bank_balance_in_company_currency = get_balance_on(account, in_account_currency=False) + exchange_rate = bank_balance_in_company_currency / bank_balance_in_account_currency + + return exchange_rate + \ No newline at end of file