Corrections properly distinguish between transaction_date and posting_date as the basis for determining translation date that is passed to get_exchange_rate

This commit is contained in:
Chude Osiegbu
2016-09-19 03:53:37 +01:00
parent 375a3c003d
commit 256ec170d0
3 changed files with 22 additions and 20 deletions

View File

@@ -119,6 +119,13 @@ class AccountsController(TransactionBase):
validate_due_date(self.posting_date, self.due_date, "Supplier", self.supplier, self.company) validate_due_date(self.posting_date, self.due_date, "Supplier", self.supplier, self.company)
def set_price_list_currency(self, buying_or_selling): def set_price_list_currency(self, buying_or_selling):
# manqala 19/09/2016: for PO and SO the translation date for exchange rate conversions is transaction_date. For everything else it is posting_date
if self.meta.get_field("posting_date"):
translation_date = self.posting_date
else:
translation_date = self.transaction_date
# end manqala
if self.meta.get_field("currency"): if self.meta.get_field("currency"):
# price list part # price list part
fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \ fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \
@@ -133,7 +140,7 @@ class AccountsController(TransactionBase):
elif not self.plc_conversion_rate: elif not self.plc_conversion_rate:
# manqala 19/09/16: using the transaction date on the PO or SO as the basis for getting the exchange rate # manqala 19/09/16: using the transaction date on the PO or SO as the basis for getting the exchange rate
self.plc_conversion_rate = get_exchange_rate( self.plc_conversion_rate = get_exchange_rate(
self.transaction_date, self.price_list_currency, self.company_currency) translation_date, self.price_list_currency, self.company_currency)
# currency # currency
if not self.currency: if not self.currency:
@@ -142,9 +149,10 @@ 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:
# cksgb 19/09/2016: added transaction date to arguments for get_exchange_rate # manqala 19/09/2016: added transaction date to arguments for get_exchange_rate
self.conversion_rate = get_exchange_rate(self.transaction_date, self.currency, self.conversion_rate = get_exchange_rate(translation_date, self.currency,
self.company_currency) self.company_currency)
# end manqala
def set_missing_item_details(self): def set_missing_item_details(self):
"""set missing item values""" """set missing item values"""

View File

@@ -417,15 +417,9 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
}, },
currency: function() { currency: function() {
/* cksgb 19/09/2016: We need to detect if this transaction is a PO/SO. /* manqala 19/09/2016: let the translation date be whichever of the transaction_date or posting_date is available */
If so, use transaction_date. translation_date = this.frm.doc.transaction_date || this.frm.doc.posting_date;
If not, use posting_date as we assume that any other document type is an accounting document. */ /* end manqala */
if (this.frm.doc.doctype == "Purchase Order" || this.frm.doc.doctype == "Sales Order"){
lookup_date = this.frm.doc.transaction_date;
}else{
lookup_date = this.frm.doc.posting_date;
}
var me = this; var me = this;
this.set_dynamic_labels(); this.set_dynamic_labels();
@@ -434,7 +428,7 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
// Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
if(this.frm.doc.currency && this.frm.doc.currency !== company_currency if(this.frm.doc.currency && this.frm.doc.currency !== company_currency
&& !this.frm.doc.ignore_pricing_rule) { && !this.frm.doc.ignore_pricing_rule) {
this.get_exchange_rate(lookup_date, this.frm.doc.currency, company_currency, this.get_exchange_rate(translation_date, this.frm.doc.currency, company_currency,
function(exchange_rate) { function(exchange_rate) {
me.frm.set_value("conversion_rate", exchange_rate); me.frm.set_value("conversion_rate", exchange_rate);
}); });
@@ -462,11 +456,11 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
} }
}, },
get_exchange_rate: function(posting_date, from_currency, to_currency, callback) { get_exchange_rate: function(translation_date, from_currency, to_currency, callback) {
return frappe.call({ return frappe.call({
method: "erpnext.setup.utils.get_exchange_rate", method: "erpnext.setup.utils.get_exchange_rate",
args: { args: {
posting_date: posting_date, translation_date: translation_date,
from_currency: from_currency, from_currency: from_currency,
to_currency: to_currency to_currency: to_currency
}, },

View File

@@ -65,16 +65,16 @@ def before_tests():
frappe.db.commit() frappe.db.commit()
@frappe.whitelist() @frappe.whitelist()
def get_exchange_rate(posting_date, from_currency, to_currency): def get_exchange_rate(translation_date, from_currency, to_currency):
if not (posting_date and from_currency and to_currency): if not (translation_date and from_currency and to_currency):
# cksgb 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
if from_currency == to_currency: if from_currency == to_currency:
return 1 return 1
# cksgb 19/09/2016: get all entries in Currency Exchange with from_currency and to_currency. Order by date desc. Top one is the required exchange rate # cksgb 19/09/2016: get all entries in Currency Exchange with from_currency and to_currency. Order by date desc. Top one is the required exchange rate
entries = frappe.get_all("Currency Exchange", fields = ["*"], filters=[["date", "<=", get_datetime_str(posting_date)], ["from_currency", "=", from_currency], ["to_currency", "=", to_currency]], order_by="date desc") entries = frappe.get_all("Currency Exchange", fields = ["*"], filters=[["date", "<=", get_datetime_str(translation_date)], ["from_currency", "=", from_currency], ["to_currency", "=", to_currency]], order_by="date desc")
if entries: if entries:
return flt(entries[0].exchange_rate) return flt(entries[0].exchange_rate)
@@ -96,5 +96,5 @@ def get_exchange_rate(posting_date, from_currency, to_currency):
return flt(value) return flt(value)
except: except:
frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}").format(from_currency, to_currency, posting_date)) frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}").format(from_currency, to_currency, translation_date))
return 0.0 return 0.0