Get average exchange rate in case of bank transfer

This commit is contained in:
Nabin Hait 2015-09-08 12:18:45 +05:30
parent 3607737b5e
commit b2739cbb79
2 changed files with 33 additions and 10 deletions

View File

@ -316,7 +316,8 @@ frappe.ui.form.on("Journal Entry Account", {
args: { args: {
account: d.account, account: d.account,
date: frm.doc.posting_date, 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) { callback: function(r) {
if(r.message) { if(r.message) {

View File

@ -276,12 +276,10 @@ class JournalEntry(AccountsController):
if len(alternate_currency) > 1: if len(alternate_currency) > 1:
frappe.throw(_("Only one alternate currency can be used in a single Journal Entry")) frappe.throw(_("Only one alternate currency can be used in a single Journal Entry"))
if not self.exchange_rate: self.set_exchange_rate()
self.exchange_rate = get_exchange_rate(alternate_currency[0], self.company_currency)
if not self.exchange_rate: if not self.exchange_rate:
frappe.throw(_("Exchange Rate is mandatory in multi-currency Journal Entry")) frappe.throw(_("Exchange Rate is mandatory in multi-currency Journal Entry"))
else: else:
self.exchange_rate = 1.0 self.exchange_rate = 1.0
@ -291,6 +289,17 @@ class JournalEntry(AccountsController):
d.debit = flt(flt(d.debit_in_account_currency)*exchange_rate, d.precision("debit")) 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")) 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): def create_remarks(self):
r = [] r = []
@ -723,7 +732,7 @@ def get_party_account_and_balance(company, party_type, party):
} }
@frappe.whitelist() @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.""" """Returns dict of account balance and party type to be set in Journal Entry on selection of account."""
if not frappe.has_permission("Account"): if not frappe.has_permission("Account"):
frappe.msgprint(_("No Permission"), raise_exception=1) frappe.msgprint(_("No Permission"), raise_exception=1)
@ -740,7 +749,10 @@ def get_account_balance_and_party_type(account, date, company):
exchange_rate = None exchange_rate = None
if account_details.account_currency != company_currency: 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 = { grid_values = {
"balance": get_balance_on(account, date), "balance": get_balance_on(account, date),
@ -748,3 +760,13 @@ def get_account_balance_and_party_type(account, date, company):
"account_currency": account_details.account_currency or company_currency, "account_currency": account_details.account_currency or company_currency,
} }
return grid_values, exchange_rate 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