date is optional in get_exchange_rate

This commit is contained in:
Nabin Hait
2016-12-09 12:14:47 +05:30
parent 352d95344e
commit ad4f1c7fd1
13 changed files with 49 additions and 43 deletions

View File

@@ -649,7 +649,8 @@ def get_payment_entry(ref_doc, args):
cost_center = frappe.db.get_value("Company", ref_doc.company, "cost_center") cost_center = frappe.db.get_value("Company", ref_doc.company, "cost_center")
exchange_rate = 1 exchange_rate = 1
if args.get("party_account"): if args.get("party_account"):
# Modified to include the posting date for which the exchange rate is required. Assumed to be the posting date in the reference document # Modified to include the posting date for which the exchange rate is required.
# Assumed to be the posting date in the reference document
exchange_rate = get_exchange_rate(ref_doc.posting_date, args.get("party_account"), args.get("party_account_currency"), exchange_rate = get_exchange_rate(ref_doc.posting_date, args.get("party_account"), args.get("party_account_currency"),
ref_doc.company, ref_doc.doctype, ref_doc.name) ref_doc.company, ref_doc.doctype, ref_doc.name)
@@ -683,8 +684,8 @@ def get_payment_entry(ref_doc, args):
bank_account = get_default_bank_cash_account(ref_doc.company, "Bank", account=args.get("bank_account")) bank_account = get_default_bank_cash_account(ref_doc.company, "Bank", account=args.get("bank_account"))
if bank_account: if bank_account:
bank_row.update(bank_account) bank_row.update(bank_account)
# Modified to include the posting date for which the exchange rate is required. Assumed to be the posting date of the # Modified to include the posting date for which the exchange rate is required.
# reference date # Assumed to be the posting date of the reference date
bank_row.exchange_rate = get_exchange_rate(ref_doc.posting_date, bank_account["account"], bank_row.exchange_rate = get_exchange_rate(ref_doc.posting_date, bank_account["account"],
bank_account["account_currency"], ref_doc.company) bank_account["account_currency"], ref_doc.company)
@@ -809,8 +810,9 @@ def get_account_balance_and_party_type(account, date, company, debit=None, credi
"party_type": party_type, "party_type": party_type,
"account_type": account_details.account_type, "account_type": account_details.account_type,
"account_currency": account_details.account_currency or company_currency, "account_currency": account_details.account_currency or company_currency,
# The date used to retreive the exchange rate here is the date passed in as an argument to this function.
# It is assumed to be the date on which the balance is sought # The date used to retreive the exchange rate here is the date passed in
# as an argument to this function. It is assumed to be the date on which the balance is sought
"exchange_rate": get_exchange_rate(date, account, account_details.account_currency, "exchange_rate": get_exchange_rate(date, account, account_details.account_currency,
company, debit=debit, credit=credit, exchange_rate=exchange_rate) company, debit=debit, credit=credit, exchange_rate=exchange_rate)
} }
@@ -849,10 +851,10 @@ def get_exchange_rate(posting_date, account, account_currency=None, company=None
(account_details.root_type == "Liability" and debit)): (account_details.root_type == "Liability" and debit)):
exchange_rate = get_average_exchange_rate(account) exchange_rate = get_average_exchange_rate(account)
# The date used to retreive the exchange rate here is the date passed
# in as an argument to this function.
if not exchange_rate and account_currency and posting_date: if not exchange_rate and account_currency and posting_date:
# The date used to retreive the exchange rate here is the date passed in as an argument to this function. exchange_rate = get_exchange_rate(account_currency, company_currency, posting_date)
exchange_rate = get_exchange_rate(posting_date, account_currency, company_currency)
else: else:
exchange_rate = 1 exchange_rate = 1

View File

@@ -152,12 +152,12 @@ class PaymentEntry(AccountsController):
elif self.payment_type in ("Pay", "Internal Transfer"): elif self.payment_type in ("Pay", "Internal Transfer"):
self.source_exchange_rate = get_average_exchange_rate(self.paid_from) self.source_exchange_rate = get_average_exchange_rate(self.paid_from)
else: else:
self.source_exchange_rate = get_exchange_rate(self.posting_date, self.paid_from_account_currency, self.source_exchange_rate = get_exchange_rate(self.paid_from_account_currency,
self.company_currency) self.company_currency, self.posting_date)
if self.paid_to and not self.target_exchange_rate: if self.paid_to and not self.target_exchange_rate:
self.target_exchange_rate = get_exchange_rate(self.posting_date, self.paid_to_account_currency, self.target_exchange_rate = get_exchange_rate(self.paid_to_account_currency,
self.company_currency) self.company_currency, self.posting_date)
def validate_mandatory(self): def validate_mandatory(self):
for field in ("paid_amount", "received_amount", "source_exchange_rate", "target_exchange_rate"): for field in ("paid_amount", "received_amount", "source_exchange_rate", "target_exchange_rate"):
@@ -518,7 +518,8 @@ def get_orders_to_be_billed(posting_date, party_type, party, party_account_curre
for d in orders: for d in orders:
d["voucher_type"] = voucher_type d["voucher_type"] = voucher_type
# This assumes that the exchange rate required is the one in the SO # This assumes that the exchange rate required is the one in the SO
d["exchange_rate"] = get_exchange_rate(posting_date,party_account_currency, company_currency) d["exchange_rate"] = get_exchange_rate(party_account_currency,
company_currency, posting_date)
order_list.append(d) order_list.append(d)
return order_list return order_list
@@ -593,16 +594,19 @@ def get_reference_details(reference_doctype, reference_name, party_account_curre
exchange_rate = 1 exchange_rate = 1
else: else:
total_amount = ref_doc.grand_total total_amount = ref_doc.grand_total
# Get the exchange rate from the original ref doc or get it based on the posting date of the ref doc
# Get the exchange rate from the original ref doc
# or get it based on the posting date of the ref doc
exchange_rate = ref_doc.get("conversion_rate") or \ exchange_rate = ref_doc.get("conversion_rate") or \
get_exchange_rate(ref_doc.posting_date, party_account_currency, ref_doc.company_currency) get_exchange_rate(party_account_currency, ref_doc.company_currency, ref_doc.posting_date)
outstanding_amount = ref_doc.get("outstanding_amount") \ outstanding_amount = ref_doc.get("outstanding_amount") \
if reference_doctype in ("Sales Invoice", "Purchase Invoice") \ if reference_doctype in ("Sales Invoice", "Purchase Invoice") \
else flt(total_amount) - flt(ref_doc.advance_paid) else flt(total_amount) - flt(ref_doc.advance_paid)
else: else:
# Get the exchange rate based on the posting date of the ref doc # Get the exchange rate based on the posting date of the ref doc
exchange_rate = get_exchange_rate(ref_doc.posting_date, party_account_currency, ref_doc.company_currency) exchange_rate = get_exchange_rate(party_account_currency,
ref_doc.company_currency, ref_doc.posting_date)
return frappe._dict({ return frappe._dict({
"due_date": ref_doc.get("due_date"), "due_date": ref_doc.get("due_date"),

View File

@@ -9,8 +9,6 @@ from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_orde
from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from frappe.utils import nowdate
# test_records = frappe.get_test_records('Payment Request')
test_dependencies = ["Currency Exchange", "Journal Entry", "Contact", "Address"] test_dependencies = ["Currency Exchange", "Journal Entry", "Contact", "Address"]
@@ -53,7 +51,7 @@ class TestPaymentRequest(unittest.TestCase):
self.assertEquals(pr.reference_name, so_inr.name) self.assertEquals(pr.reference_name, so_inr.name)
self.assertEquals(pr.currency, "INR") self.assertEquals(pr.currency, "INR")
conversion_rate = get_exchange_rate(nowdate(), "USD", "INR") conversion_rate = get_exchange_rate("USD", "INR")
si_usd = create_sales_invoice(currency="USD", conversion_rate=conversion_rate) si_usd = create_sales_invoice(currency="USD", conversion_rate=conversion_rate)
pr = make_payment_request(dt="Sales Invoice", dn=si_usd.name, recipient_id="saurabh@erpnext.com") pr = make_payment_request(dt="Sales Invoice", dn=si_usd.name, recipient_id="saurabh@erpnext.com")

View File

@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe, json import frappe, json
from frappe import _
from frappe.utils import nowdate from frappe.utils import nowdate
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from erpnext.stock.get_item_details import get_pos_profile from erpnext.stock.get_item_details import get_pos_profile
@@ -63,8 +62,10 @@ def update_pos_profile_data(doc, pos_profile, company_data):
doc.currency = pos_profile.get('currency') or company_data.default_currency doc.currency = pos_profile.get('currency') or company_data.default_currency
doc.conversion_rate = 1.0 doc.conversion_rate = 1.0
if doc.currency != company_data.default_currency: if doc.currency != company_data.default_currency:
doc.conversion_rate = get_exchange_rate(doc.posting_date, doc.currency, company_data.default_currency) doc.conversion_rate = get_exchange_rate(doc.currency, company_data.default_currency, doc.posting_date)
doc.selling_price_list = pos_profile.get('selling_price_list') or \ doc.selling_price_list = pos_profile.get('selling_price_list') or \
frappe.db.get_value('Selling Settings', None, 'selling_price_list') frappe.db.get_value('Selling Settings', None, 'selling_price_list')
doc.naming_series = pos_profile.get('naming_series') or 'SINV-' doc.naming_series = pos_profile.get('naming_series') or 'SINV-'

View File

@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from erpnext.utils import nowdate
import frappe import frappe
@@ -40,7 +39,7 @@ def get_quote_list(item, qty_list):
#Add a row for each supplier #Add a row for each supplier
for root in set(suppliers): for root in set(suppliers):
supplier_currency = frappe.db.get_value("Supplier",root,"default_currency") supplier_currency = frappe.db.get_value("Supplier",root,"default_currency")
exg = get_exchange_rate(nowdate(),supplier_currency,company_currency) exg = get_exchange_rate(supplier_currency, company_currency)
row = frappe._dict({ row = frappe._dict({
"supplier_name": root "supplier_name": root

View File

@@ -144,8 +144,8 @@ class AccountsController(TransactionBase):
self.plc_conversion_rate = 1.0 self.plc_conversion_rate = 1.0
elif not self.plc_conversion_rate: elif not self.plc_conversion_rate:
self.plc_conversion_rate = get_exchange_rate(transaction_date, self.plc_conversion_rate = get_exchange_rate(self.price_list_currency,
self.price_list_currency, self.company_currency) self.company_currency, transaction_date)
# currency # currency
if not self.currency: if not self.currency:
@@ -154,8 +154,8 @@ class AccountsController(TransactionBase):
elif self.currency == self.company_currency: elif self.currency == self.company_currency:
self.conversion_rate = 1.0 self.conversion_rate = 1.0
elif not self.conversion_rate: elif not self.conversion_rate:
self.conversion_rate = get_exchange_rate(transaction_date, self.currency, self.conversion_rate = get_exchange_rate(self.currency,
self.company_currency) self.company_currency, transaction_date)
def set_missing_item_details(self, for_validate=False): def set_missing_item_details(self, for_validate=False):
"""set missing item values""" """set missing item values"""

View File

@@ -205,7 +205,8 @@ def make_quotation(source_name, target_doc=None):
if company_currency == quotation.currency: if company_currency == quotation.currency:
exchange_rate = 1 exchange_rate = 1
else: else:
exchange_rate = get_exchange_rate(quotation.transaction_date, quotation.currency, company_currency) exchange_rate = get_exchange_rate(quotation.currency, company_currency,
quotation.transaction_date)
quotation.conversion_rate = exchange_rate quotation.conversion_rate = exchange_rate

View File

@@ -12,7 +12,6 @@ from erpnext.exceptions import InvalidCurrency
from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \ from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \
make_supplier_quotation as make_quotation_from_rfq make_supplier_quotation as make_quotation_from_rfq
from frappe.utils.nowdate
def work(): def work():
frappe.set_user(frappe.db.get_global('demo_purchase_user')) frappe.set_user(frappe.db.get_global('demo_purchase_user'))
@@ -57,7 +56,7 @@ def work():
if company_currency == party_account_currency: if company_currency == party_account_currency:
exchange_rate = 1 exchange_rate = 1
else: else:
exchange_rate = get_exchange_rate(nowdate(), party_account_currency, company_currency) exchange_rate = get_exchange_rate(party_account_currency, company_currency)
# make supplier quotations # make supplier quotations
if random.random() < 0.2: if random.random() < 0.2:

View File

@@ -9,7 +9,6 @@ from frappe.utils.make_random import add_random_children, get_random
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from erpnext.accounts.party import get_party_account_currency from erpnext.accounts.party import get_party_account_currency
from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry
from frappe.utils import nowdate
def work(): def work():
frappe.set_user(frappe.db.get_global('demo_sales_user_2')) frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
@@ -89,7 +88,7 @@ def make_quotation():
if company_currency == party_account_currency: if company_currency == party_account_currency:
exchange_rate = 1 exchange_rate = 1
else: else:
exchange_rate = get_exchange_rate(nowdate(), party_account_currency, company_currency) exchange_rate = get_exchange_rate(party_account_currency, company_currency)
qtn = frappe.get_doc({ qtn = frappe.get_doc({
"creation": frappe.flags.current_date, "creation": frappe.flags.current_date,

View File

@@ -3,7 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe.utils import cint, cstr, flt, nowdate from frappe.utils import cint, cstr, flt
from frappe import _ from frappe import _
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
from frappe.model.document import Document from frappe.model.document import Document
@@ -229,7 +229,7 @@ class BOM(Document):
frappe.throw(_("Currency of the price list {0} is not similar with the selected currency {1}").format(self.buying_price_list, self.currency)) frappe.throw(_("Currency of the price list {0} is not similar with the selected currency {1}").format(self.buying_price_list, self.currency))
def set_conversion_rate(self): def set_conversion_rate(self):
self.conversion_rate = get_exchange_rate(nowdate(), self.currency, self.company_currency()) self.conversion_rate = get_exchange_rate(self.currency, self.company_currency())
def validate_materials(self): def validate_materials(self):
""" Validate raw material entries """ """ Validate raw material entries """

View File

@@ -11,10 +11,10 @@ class TestCurrencyExchange(unittest.TestCase):
from erpnext.setup.utils import get_exchange_rate from erpnext.setup.utils import get_exchange_rate
# Exchange rate as on 15th Jan, 2016, should be fetched from Currency Exchange record # Exchange rate as on 15th Jan, 2016, should be fetched from Currency Exchange record
exchange_rate = get_exchange_rate("2016-01-15", "USD", "INR") exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15")
self.assertEqual(exchange_rate, 60.0) self.assertEqual(exchange_rate, 60.0)
# Exchange rate as on 15th Dec, 2015, should be fetched from fixer.io # Exchange rate as on 15th Dec, 2015, should be fetched from fixer.io
exchange_rate = get_exchange_rate("2015-12-15", "USD", "INR") exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15")
self.assertFalse(exchange_rate==60) self.assertFalse(exchange_rate==60)

View File

@@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe import frappe
from frappe import _, throw from frappe import _, throw
from frappe.utils import flt from frappe.utils import flt
from frappe.utils import get_datetime, get_datetime_str from frappe.utils import get_datetime_str, nowdate
def get_company_currency(company): def get_company_currency(company):
currency = frappe.db.get_value("Company", company, "default_currency", cache=True) currency = frappe.db.get_value("Company", company, "default_currency", cache=True)
@@ -65,8 +65,10 @@ def before_tests():
frappe.db.commit() frappe.db.commit()
@frappe.whitelist() @frappe.whitelist()
def get_exchange_rate(transaction_date, from_currency, to_currency): def get_exchange_rate(from_currency, to_currency, transaction_date=None):
if not (transaction_date and from_currency and to_currency): if not transaction_date:
transaction_date = nowdate()
if not (from_currency and to_currency):
# manqala 19/09/2016: Should this be an empty return or should it throw and exception? # manqala 19/09/2016: Should this be an empty return or should it throw and exception?
return return

View File

@@ -482,7 +482,8 @@ def get_price_list_currency_and_exchange_rate(args):
if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \ if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
and price_list_currency != args.price_list_currency): and price_list_currency != args.price_list_currency):
# cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
plc_conversion_rate = get_exchange_rate(args.transaction_date, price_list_currency, args.currency) or plc_conversion_rate plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency,
args.transaction_date) or plc_conversion_rate
return frappe._dict({ return frappe._dict({
"price_list_currency": price_list_currency, "price_list_currency": price_list_currency,