diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index c89e24dc214..cd793635029 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -327,7 +327,7 @@ class JournalEntry(AccountsController): d.exchange_rate = 1 elif not d.exchange_rate or d.exchange_rate == 1 or \ (d.reference_type in ("Sales Invoice", "Purchase Invoice") and d.reference_name and d.posting_date): - # Modified to include the posting date for which to retreive the exchange rate + # Modified to include the posting date for which to retreive the exchange rate d.exchange_rate = get_exchange_rate(self.posting_date, d.account, d.account_currency, self.company, d.reference_type, d.reference_name, d.debit, d.credit, d.exchange_rate) @@ -649,7 +649,8 @@ def get_payment_entry(ref_doc, args): cost_center = frappe.db.get_value("Company", ref_doc.company, "cost_center") exchange_rate = 1 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"), 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")) if 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 - # reference date + # Modified to include the posting date for which the exchange rate is required. + # 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_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, "account_type": account_details.account_type, "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, 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)): 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: - # 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(posting_date, account_currency, company_currency) - + exchange_rate = get_exchange_rate(account_currency, company_currency, posting_date) else: exchange_rate = 1 diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index ef6eec97a89..3e10b515498 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -152,12 +152,12 @@ class PaymentEntry(AccountsController): elif self.payment_type in ("Pay", "Internal Transfer"): self.source_exchange_rate = get_average_exchange_rate(self.paid_from) else: - self.source_exchange_rate = get_exchange_rate(self.posting_date, self.paid_from_account_currency, - self.company_currency) + self.source_exchange_rate = get_exchange_rate(self.paid_from_account_currency, + self.company_currency, self.posting_date) 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.company_currency) + self.target_exchange_rate = get_exchange_rate(self.paid_to_account_currency, + self.company_currency, self.posting_date) def validate_mandatory(self): for field in ("paid_amount", "received_amount", "source_exchange_rate", "target_exchange_rate"): @@ -517,8 +517,9 @@ def get_orders_to_be_billed(posting_date, party_type, party, party_account_curre order_list = [] for d in orders: d["voucher_type"] = voucher_type - # 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) + # This assumes that the exchange rate required is the one in the SO + d["exchange_rate"] = get_exchange_rate(party_account_currency, + company_currency, posting_date) order_list.append(d) return order_list @@ -593,16 +594,19 @@ def get_reference_details(reference_doctype, reference_name, party_account_curre exchange_rate = 1 else: 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 \ - 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") \ if reference_doctype in ("Sales Invoice", "Purchase Invoice") \ else flt(total_amount) - flt(ref_doc.advance_paid) else: - # 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) + # Get the exchange rate based on the posting date of the ref doc + exchange_rate = get_exchange_rate(party_account_currency, + ref_doc.company_currency, ref_doc.posting_date) return frappe._dict({ "due_date": ref_doc.get("due_date"), diff --git a/erpnext/accounts/doctype/payment_request/test_payment_request.py b/erpnext/accounts/doctype/payment_request/test_payment_request.py index d695bf6cc8c..bf3e24fe7af 100644 --- a/erpnext/accounts/doctype/payment_request/test_payment_request.py +++ b/erpnext/accounts/doctype/payment_request/test_payment_request.py @@ -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.sales_invoice.test_sales_invoice import create_sales_invoice 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"] @@ -53,7 +51,7 @@ class TestPaymentRequest(unittest.TestCase): self.assertEquals(pr.reference_name, so_inr.name) 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) pr = make_payment_request(dt="Sales Invoice", dn=si_usd.name, recipient_id="saurabh@erpnext.com") diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py index eeb556082e9..9fb11f2b7c2 100644 --- a/erpnext/accounts/doctype/sales_invoice/pos.py +++ b/erpnext/accounts/doctype/sales_invoice/pos.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import frappe, json -from frappe import _ from frappe.utils import nowdate from erpnext.setup.utils import get_exchange_rate 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.conversion_rate = 1.0 + 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 \ frappe.db.get_value('Selling Settings', None, 'selling_price_list') doc.naming_series = pos_profile.get('naming_series') or 'SINV-' diff --git a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py index 2b024ef3855..03f7a340792 100644 --- a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py +++ b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals from erpnext.setup.utils import get_exchange_rate -from erpnext.utils import nowdate import frappe @@ -40,7 +39,7 @@ def get_quote_list(item, qty_list): #Add a row for each supplier for root in set(suppliers): 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({ "supplier_name": root diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index b998bbd9bad..554529c6f6c 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -144,8 +144,8 @@ class AccountsController(TransactionBase): self.plc_conversion_rate = 1.0 elif not self.plc_conversion_rate: - self.plc_conversion_rate = get_exchange_rate(transaction_date, - self.price_list_currency, self.company_currency) + self.plc_conversion_rate = get_exchange_rate(self.price_list_currency, + self.company_currency, transaction_date) # currency if not self.currency: @@ -154,8 +154,8 @@ class AccountsController(TransactionBase): elif self.currency == self.company_currency: self.conversion_rate = 1.0 elif not self.conversion_rate: - self.conversion_rate = get_exchange_rate(transaction_date, self.currency, - self.company_currency) + self.conversion_rate = get_exchange_rate(self.currency, + self.company_currency, transaction_date) def set_missing_item_details(self, for_validate=False): """set missing item values""" diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 38164cca5ae..6ee90036080 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -205,7 +205,8 @@ def make_quotation(source_name, target_doc=None): if company_currency == quotation.currency: exchange_rate = 1 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 diff --git a/erpnext/demo/user/purchase.py b/erpnext/demo/user/purchase.py index c7ec62b6f4f..ab8ec772574 100644 --- a/erpnext/demo/user/purchase.py +++ b/erpnext/demo/user/purchase.py @@ -12,7 +12,6 @@ from erpnext.exceptions import InvalidCurrency from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \ make_supplier_quotation as make_quotation_from_rfq -from frappe.utils.nowdate def work(): frappe.set_user(frappe.db.get_global('demo_purchase_user')) @@ -57,7 +56,7 @@ def work(): if company_currency == party_account_currency: exchange_rate = 1 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 if random.random() < 0.2: diff --git a/erpnext/demo/user/sales.py b/erpnext/demo/user/sales.py index b9e6535e43f..10df14334fc 100644 --- a/erpnext/demo/user/sales.py +++ b/erpnext/demo/user/sales.py @@ -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.accounts.party import get_party_account_currency from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry -from frappe.utils import nowdate def work(): frappe.set_user(frappe.db.get_global('demo_sales_user_2')) @@ -89,7 +88,7 @@ def make_quotation(): if company_currency == party_account_currency: exchange_rate = 1 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({ "creation": frappe.flags.current_date, diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index 783df20d97e..2a27922c7f4 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import frappe -from frappe.utils import cint, cstr, flt, nowdate +from frappe.utils import cint, cstr, flt from frappe import _ from erpnext.setup.utils import get_exchange_rate 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)) 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): """ Validate raw material entries """ diff --git a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py index 69ca1c2a10d..181f07257b3 100644 --- a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py +++ b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py @@ -11,10 +11,10 @@ class TestCurrencyExchange(unittest.TestCase): from erpnext.setup.utils import get_exchange_rate # 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) # 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) \ No newline at end of file diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 9a8ae071136..0c214e4c2a8 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe from frappe import _, throw 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): currency = frappe.db.get_value("Company", company, "default_currency", cache=True) @@ -65,8 +65,10 @@ def before_tests(): frappe.db.commit() @frappe.whitelist() -def get_exchange_rate(transaction_date, from_currency, to_currency): - if not (transaction_date and from_currency and to_currency): +def get_exchange_rate(from_currency, to_currency, transaction_date=None): + 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? return diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index c0fe3a62314..16ea58e47ab 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -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 \ and price_list_currency != args.price_list_currency): # 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({ "price_list_currency": price_list_currency,