fix: Currency Exchange for_selling and for_buying on the same day (#19339)

* fix: test data of Currency Exchange to incluse buying and selling

test data of Currency Exchange to incluse buying and selling

* fix: Currency Exchange Test corrected to include selling and buying exchange_rate

Currency Exchange Test corrected to include selling and buying exchange_rate

* fix: Currency Exchange for_selling and for_buying fields test and functionality restored


In this fix:
* You can now add a separate exchange_rate in date for_selling and for_buying. You could not before because the unique field name was only calculated to allow a single name for a date
* tests did not account for for_selling and for_buying fields and thier uniqueness

* Update test_currency_exchange.py

* Update test_records.json

* fix: update test_records.json

* Update test_records.json

* The basic package for turkey is defined. It is empty now but applications specific to Turkey will be created under that package.

* fix: update code with scapes vs.tabs

updated the code regarding spaces issue

* Update currency_exchange.py
This commit is contained in:
Tufan Kaynak 2019-11-11 14:41:16 +03:00 committed by Nabin Hait
parent d3ed499854
commit 06c6f7cfd3
4 changed files with 85 additions and 60 deletions

View File

View File

@ -11,10 +11,15 @@ from frappe.utils import get_datetime_str, formatdate, nowdate, cint
class CurrencyExchange(Document):
def autoname(self):
purpose = ""
if not self.date:
self.date = nowdate()
self.name = '{0}-{1}-{2}'.format(formatdate(get_datetime_str(self.date), "yyyy-MM-dd"),
self.from_currency, self.to_currency)
if cint(self.for_buying)==0 and cint(self.for_selling)==1:
purpose = "Selling"
if cint(self.for_buying)==1 and cint(self.for_selling)==0:
purpose = "Buying"
self.name = '{0}-{1}-{2}{3}'.format(formatdate(get_datetime_str(self.date), "yyyy-MM-dd"),
self.from_currency, self.to_currency, ("-" + purpose) if purpose else "")
def validate(self):
self.validate_value("exchange_rate", ">", 0)
@ -23,4 +28,4 @@ class CurrencyExchange(Document):
throw(_("From Currency and To Currency cannot be same"))
if not cint(self.for_buying) and not cint(self.for_selling):
throw(_("Currency Exchange must be applicable for Buying or for Selling."))
throw(_("Currency Exchange must be applicable for Buying or for Selling."))

View File

@ -4,15 +4,21 @@ from __future__ import unicode_literals
import frappe, unittest
from frappe.utils import flt
from erpnext.setup.utils import get_exchange_rate
from frappe.utils import cint
test_records = frappe.get_test_records('Currency Exchange')
def save_new_records(test_records):
for record in test_records:
purpose = str("")
if cint(record.get("for_buying"))==0 and cint(record.get("for_selling"))==1:
purpose = "Selling"
if cint(record.get("for_buying"))==1 and cint(record.get("for_selling"))==0:
purpose = "Buying"
kwargs = dict(
doctype=record.get("doctype"),
docname=record.get("date") + '-' + record.get("from_currency") + '-' + record.get("to_currency"),
docname=record.get("date") + '-' + record.get("from_currency") + '-' + record.get("to_currency") + '-' + purpose,
fieldname="exchange_rate",
value=record.get("exchange_rate"),
)
@ -25,6 +31,8 @@ def save_new_records(test_records):
curr_exchange.from_currency = record["from_currency"]
curr_exchange.to_currency = record["to_currency"]
curr_exchange.exchange_rate = record["exchange_rate"]
curr_exchange.for_buying = record["for_buying"]
curr_exchange.for_selling = record["for_selling"]
curr_exchange.insert()
@ -44,18 +52,18 @@ class TestCurrencyExchange(unittest.TestCase):
frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
# Start with allow_stale is True
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-01")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-01", "for_buying")
self.assertEqual(flt(exchange_rate, 3), 60.0)
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
self.assertEqual(exchange_rate, 65.1)
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_selling")
self.assertEqual(exchange_rate, 62.9)
# Exchange rate as on 15th Dec, 2015, should be fetched from fixer.io
self.clear_cache()
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15")
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15", "for_selling")
self.assertFalse(exchange_rate == 60)
self.assertEqual(flt(exchange_rate, 3), 66.894)
@ -64,35 +72,35 @@ class TestCurrencyExchange(unittest.TestCase):
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
frappe.db.set_value("Accounts Settings", None, "stale_days", 1)
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-01")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-01", "for_buying")
self.assertEqual(exchange_rate, 60.0)
# Will fetch from fixer.io
self.clear_cache()
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
self.assertEqual(flt(exchange_rate, 3), 67.79)
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_selling")
self.assertEqual(exchange_rate, 62.9)
# Exchange rate as on 15th Dec, 2015, should be fetched from fixer.io
self.clear_cache()
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15")
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15", "for_buying")
self.assertEqual(flt(exchange_rate, 3), 66.894)
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-10")
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-10", "for_selling")
self.assertEqual(exchange_rate, 65.1)
# NGN is not available on fixer.io so these should return 0
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-09")
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-09", "for_selling")
self.assertEqual(exchange_rate, 0)
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-11")
exchange_rate = get_exchange_rate("INR", "NGN", "2016-01-11", "for_selling")
self.assertEqual(exchange_rate, 0)
def test_exchange_rate_strict_switched(self):
# Start with allow_stale is True
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15")
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
self.assertEqual(exchange_rate, 65.1)
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
@ -100,5 +108,5 @@ class TestCurrencyExchange(unittest.TestCase):
# Will fetch from fixer.io
self.clear_cache()
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15")
self.assertEqual(flt(exchange_rate, 3), 67.79)
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
self.assertEqual(flt(exchange_rate, 3), 67.79)

View File

@ -1,44 +1,56 @@
[
{
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 60.0,
"from_currency": "USD",
"to_currency": "INR"
},
{
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 0.773,
"from_currency": "USD",
"to_currency": "EUR"
},
{
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 0.0167,
"from_currency": "INR",
"to_currency": "USD"
},
{
"doctype": "Currency Exchange",
"date": "2016-01-10",
"exchange_rate": 65.1,
"from_currency": "USD",
"to_currency": "INR"
},
{
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 60.0,
"from_currency": "USD",
"to_currency": "INR",
"for_buying": 1,
"for_selling": 0
},
{
"doctype": "Currency Exchange",
"date": "2016-01-30",
"exchange_rate": 62.9,
"from_currency": "USD",
"to_currency": "INR"
},
{
"doctype": "Currency Exchange",
"date": "2016-01-10",
"exchange_rate": 65.1,
"from_currency": "INR",
"to_currency": "NGN"
}
]
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 0.773,
"from_currency": "USD",
"to_currency": "EUR",
"for_buying": 0,
"for_selling": 1
},
{
"doctype": "Currency Exchange",
"date": "2016-01-01",
"exchange_rate": 0.0167,
"from_currency": "INR",
"to_currency": "USD",
"for_buying": 1,
"for_selling": 0
},
{
"doctype": "Currency Exchange",
"date": "2016-01-10",
"exchange_rate": 65.1,
"from_currency": "USD",
"to_currency": "INR",
"for_buying": 1,
"for_selling": 0
},
{
"doctype": "Currency Exchange",
"date": "2016-01-30",
"exchange_rate": 62.9,
"from_currency": "USD",
"to_currency": "INR",
"for_buying": 1,
"for_selling": 1
},
{
"doctype": "Currency Exchange",
"date": "2016-01-10",
"exchange_rate": 65.1,
"from_currency": "INR",
"to_currency": "NGN",
"for_buying": 1,
"for_selling": 1
}
]