fix: format currency/float as per number format in work history (#34545)

Co-authored-by: Rucha Mahabal <ruchamahabal2@gmail.com>
This commit is contained in:
RJPvT
2023-04-04 07:38:55 +02:00
committed by GitHub
parent 50de045247
commit 892c480408
2 changed files with 45 additions and 9 deletions

View File

@@ -4,6 +4,7 @@
import unittest
import frappe
from frappe.tests.utils import change_settings
from frappe.utils import add_days, getdate
from erpnext.hr.doctype.employee.test_employee import make_employee
@@ -99,6 +100,16 @@ class TestEmployeeTransfer(unittest.TestCase):
self.assertEqual(data.from_date, dt[0])
self.assertEqual(data.to_date, None)
@change_settings("System Settings", {"number_format": "#.###,##"})
def test_data_formatting_in_history(self):
from erpnext.hr.utils import get_formatted_value
value = get_formatted_value("12.500,00", "Float")
self.assertEqual(value, 12500.0)
value = get_formatted_value("12.500,00", "Currency")
self.assertEqual(value, 12500.0)
def create_company():
if not frappe.db.exists("Company", "Test Company"):

View File

@@ -13,6 +13,7 @@ from frappe.utils import (
formatdate,
get_datetime,
get_link_to_form,
get_number_format_info,
getdate,
nowdate,
today,
@@ -185,15 +186,11 @@ def update_employee_work_history(employee, details, date=None, cancel=False):
field = frappe.get_meta("Employee").get_field(item.fieldname)
if not field:
continue
fieldtype = field.fieldtype
new_data = item.new if not cancel else item.current
if fieldtype == "Date" and new_data:
new_data = getdate(new_data)
elif fieldtype == "Datetime" and new_data:
new_data = get_datetime(new_data)
elif fieldtype in ["Currency", "Float"] and new_data:
new_data = flt(new_data)
setattr(employee, item.fieldname, new_data)
new_value = item.new if not cancel else item.current
new_value = get_formatted_value(new_value, field.fieldtype)
setattr(employee, item.fieldname, new_value)
if item.fieldname in ["department", "designation", "branch"]:
internal_work_history[item.fieldname] = item.new
@@ -207,6 +204,34 @@ def update_employee_work_history(employee, details, date=None, cancel=False):
return employee
def get_formatted_value(value, fieldtype):
"""
Since the fields in Internal Work History table are `Data` fields
format them as per relevant field types
"""
if not value:
return
if fieldtype == "Date":
value = getdate(value)
elif fieldtype == "Datetime":
value = get_datetime(value)
elif fieldtype in ["Currency", "Float"]:
# in case of currency/float, the value might be in user's prefered number format
# instead of machine readable format. Convert it into a machine readable format
number_format = frappe.db.get_default("number_format") or "#,###.##"
decimal_str, comma_str, _number_format_precision = get_number_format_info(number_format)
if comma_str == "." and decimal_str == ",":
value = value.replace(",", "#$")
value = value.replace(".", ",")
value = value.replace("#$", ".")
value = flt(value)
return value
def delete_employee_work_history(details, employee, date):
filters = {}
for d in details: