From fbf27b5031768d27d8b7376ad9e6a4dcf5d3af4f Mon Sep 17 00:00:00 2001 From: Chude Osiegbu Date: Wed, 7 Sep 2016 03:06:08 +0100 Subject: [PATCH] Further corrections to Currency Exchange table --- .../doctype/journal_entry/journal_entry.js | 1 + .../doctype/journal_entry/journal_entry.py | 24 ++++++++++++------- .../doctype/payment_entry/payment_entry.js | 2 ++ .../doctype/payment_entry/payment_entry.py | 17 +++++++------ .../payment_request/test_payment_request.py | 3 ++- erpnext/accounts/doctype/sales_invoice/pos.py | 2 +- .../quoted_item_comparison.py | 3 ++- .../crm/doctype/opportunity/opportunity.py | 2 +- erpnext/demo/user/purchase.py | 3 ++- erpnext/demo/user/sales.py | 3 ++- erpnext/public/js/controllers/transaction.js | 2 +- 11 files changed, 40 insertions(+), 22 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js index 13f2f61971..57f6e25f01 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.js +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js @@ -421,6 +421,7 @@ $.extend(erpnext.journal_entry, { frappe.call({ method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_exchange_rate", args: { + posting_date: frm.doc.posting_date, account: row.account, account_currency: row.account_currency, company: frm.doc.company, diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 4e00c32808..2a6d276b03 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -310,8 +310,9 @@ class JournalEntry(AccountsController): if d.account_currency == self.company_currency: 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): - d.exchange_rate = get_exchange_rate(d.account, d.account_currency, self.company, + (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 + 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) if not d.exchange_rate: @@ -631,7 +632,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"): - exchange_rate = get_exchange_rate(args.get("party_account"), args.get("party_account_currency"), + # 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) je = frappe.new_doc("Journal Entry") @@ -664,7 +666,9 @@ 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) - bank_row.exchange_rate = get_exchange_rate(bank_account["account"], + # 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) bank_row.cost_center = cost_center @@ -787,7 +791,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, - "exchange_rate": get_exchange_rate(account, account_details.account_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 + "exchange_rate": get_exchange_rate(date, account, account_details.account_currency, company, debit=debit, credit=credit, exchange_rate=exchange_rate) } @@ -797,8 +803,9 @@ def get_account_balance_and_party_type(account, date, company, debit=None, credi return grid_values +# Added posting_date as one of the parameters of get_exchange_rate @frappe.whitelist() -def get_exchange_rate(account, account_currency=None, company=None, +def get_exchange_rate(posting_date, account, account_currency=None, company=None, reference_type=None, reference_name=None, debit=None, credit=None, exchange_rate=None): from erpnext.setup.utils import get_exchange_rate account_details = frappe.db.get_value("Account", account, @@ -824,8 +831,9 @@ def get_exchange_rate(account, account_currency=None, company=None, (account_details.root_type == "Liability" and debit)): exchange_rate = get_average_exchange_rate(account) - if not exchange_rate and account_currency: - exchange_rate = get_exchange_rate(account_currency, company_currency) + 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) else: exchange_rate = 1 diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js index 219fbf2e73..be12d577f1 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.js +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js @@ -350,6 +350,7 @@ frappe.ui.form.on('Payment Entry', { frappe.call({ method: "erpnext.setup.utils.get_exchange_rate", args: { + posting_date: frm.doc.posting_date, from_currency: from_currency, to_currency: to_currency }, @@ -442,6 +443,7 @@ frappe.ui.form.on('Payment Entry', { method: 'erpnext.accounts.doctype.payment_entry.payment_entry.get_outstanding_reference_documents', args: { args: { + "posting_date": frm.doc.posting_date, "company": frm.doc.company, "party_type": frm.doc.party_type, "payment_type": frm.doc.payment_type, diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 179a321d19..314e814021 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -145,11 +145,11 @@ 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.paid_from_account_currency, + self.source_exchange_rate = get_exchange_rate(self.posting_date, self.paid_from_account_currency, self.company_currency) if self.paid_to and not self.target_exchange_rate: - self.target_exchange_rate = get_exchange_rate(self.paid_to_account_currency, + self.target_exchange_rate = get_exchange_rate(self.posting_date, self.paid_to_account_currency, self.company_currency) def validate_mandatory(self): @@ -475,12 +475,12 @@ def get_outstanding_reference_documents(args): d["exchange_rate"] = frappe.db.get_value(d.voucher_type, d.voucher_no, "conversion_rate") # Get all SO / PO which are not fully billed or aginst which full advance not paid - orders_to_be_billed = get_orders_to_be_billed(args.get("party_type"), args.get("party"), + orders_to_be_billed = get_orders_to_be_billed(args.get("posting_date"),args.get("party_type"), args.get("party"), party_account_currency, company_currency) return negative_outstanding_invoices + outstanding_invoices + orders_to_be_billed -def get_orders_to_be_billed(party_type, party, party_account_currency, company_currency): +def get_orders_to_be_billed(posting_date, party_type, party, party_account_currency, company_currency): voucher_type = 'Sales Order' if party_type == "Customer" else 'Purchase Order' ref_field = "base_grand_total" if party_account_currency == company_currency else "grand_total" @@ -510,7 +510,8 @@ def get_orders_to_be_billed(party_type, party, party_account_currency, company_c order_list = [] for d in orders: d["voucher_type"] = voucher_type - d["exchange_rate"] = get_exchange_rate(party_account_currency, company_currency) + # 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) order_list.append(d) return order_list @@ -585,14 +586,16 @@ 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 exchange_rate = ref_doc.get("conversion_rate") or \ - get_exchange_rate(party_account_currency, ref_doc.company_currency) + get_exchange_rate(ref_doc.posting_date, party_account_currency, ref_doc.company_currency) 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: - exchange_rate = get_exchange_rate(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(ref_doc.posting_date, party_account_currency, ref_doc.company_currency) 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 73c412f247..d695bf6cc8 100644 --- a/erpnext/accounts/doctype/payment_request/test_payment_request.py +++ b/erpnext/accounts/doctype/payment_request/test_payment_request.py @@ -9,6 +9,7 @@ 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"] @@ -52,7 +53,7 @@ class TestPaymentRequest(unittest.TestCase): self.assertEquals(pr.reference_name, so_inr.name) self.assertEquals(pr.currency, "INR") - conversion_rate = get_exchange_rate("USD", "INR") + conversion_rate = get_exchange_rate(nowdate(), "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 5336b5452c..4ac8cec8cf 100644 --- a/erpnext/accounts/doctype/sales_invoice/pos.py +++ b/erpnext/accounts/doctype/sales_invoice/pos.py @@ -60,7 +60,7 @@ 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.currency, company_data.default_currency) + doc.conversion_rate = get_exchange_rate(doc.posting_date, doc.currency, company_data.default_currency) 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 1793fc3140..2b024ef385 100644 --- a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py +++ b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from erpnext.setup.utils import get_exchange_rate +from erpnext.utils import nowdate import frappe @@ -39,7 +40,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(supplier_currency,company_currency) + exg = get_exchange_rate(nowdate(),supplier_currency,company_currency) row = frappe._dict({ "supplier_name": root diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index a2a1be2302..bc85f4dbd8 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -202,7 +202,7 @@ def make_quotation(source_name, target_doc=None): if company_currency == quotation.currency: exchange_rate = 1 else: - exchange_rate = get_exchange_rate(quotation.currency, company_currency) + exchange_rate = get_exchange_rate(quotation.transaction_date, quotation.currency, company_currency) quotation.conversion_rate = exchange_rate diff --git a/erpnext/demo/user/purchase.py b/erpnext/demo/user/purchase.py index ab8ec77257..c7ec62b6f4 100644 --- a/erpnext/demo/user/purchase.py +++ b/erpnext/demo/user/purchase.py @@ -12,6 +12,7 @@ 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')) @@ -56,7 +57,7 @@ def work(): if company_currency == party_account_currency: exchange_rate = 1 else: - exchange_rate = get_exchange_rate(party_account_currency, company_currency) + exchange_rate = get_exchange_rate(nowdate(), 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 10df14334f..b9e6535e43 100644 --- a/erpnext/demo/user/sales.py +++ b/erpnext/demo/user/sales.py @@ -9,6 +9,7 @@ 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')) @@ -88,7 +89,7 @@ def make_quotation(): if company_currency == party_account_currency: exchange_rate = 1 else: - exchange_rate = get_exchange_rate(party_account_currency, company_currency) + exchange_rate = get_exchange_rate(nowdate(), party_account_currency, company_currency) qtn = frappe.get_doc({ "creation": frappe.flags.current_date, diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 6a0d0f8b7f..78776a3833 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -473,7 +473,7 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ var company_currency = this.get_company_currency(); // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) { - this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency, + this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency, function(exchange_rate) { me.frm.set_value("plc_conversion_rate", exchange_rate); });