From 06c6f7cfd3862955229d95168b77b39c5cd56916 Mon Sep 17 00:00:00 2001 From: Tufan Kaynak <31142607+toofun666@users.noreply.github.com> Date: Mon, 11 Nov 2019 14:41:16 +0300 Subject: [PATCH] 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 --- erpnext/regional/turkey/__init__.py | 0 .../currency_exchange/currency_exchange.py | 11 ++- .../test_currency_exchange.py | 38 +++++--- .../currency_exchange/test_records.json | 96 +++++++++++-------- 4 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 erpnext/regional/turkey/__init__.py diff --git a/erpnext/regional/turkey/__init__.py b/erpnext/regional/turkey/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/erpnext/setup/doctype/currency_exchange/currency_exchange.py b/erpnext/setup/doctype/currency_exchange/currency_exchange.py index 4effb5ab017..60d367a4bbc 100644 --- a/erpnext/setup/doctype/currency_exchange/currency_exchange.py +++ b/erpnext/setup/doctype/currency_exchange/currency_exchange.py @@ -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.")) \ No newline at end of file + throw(_("Currency Exchange must be applicable for Buying or for Selling.")) diff --git a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py index c488b996ffa..857f666b2a6 100644 --- a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py +++ b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py @@ -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) \ No newline at end of file + exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying") + self.assertEqual(flt(exchange_rate, 3), 67.79) diff --git a/erpnext/setup/doctype/currency_exchange/test_records.json b/erpnext/setup/doctype/currency_exchange/test_records.json index 0c9cfbb67c0..152060edfc4 100644 --- a/erpnext/setup/doctype/currency_exchange/test_records.json +++ b/erpnext/setup/doctype/currency_exchange/test_records.json @@ -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" - } -] \ No newline at end of file + "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 + } +]