diff --git a/hr_employee_relative/models/hr_employee_relative.py b/hr_employee_relative/models/hr_employee_relative.py
index 21ed4c63e73..f7258bfde55 100644
--- a/hr_employee_relative/models/hr_employee_relative.py
+++ b/hr_employee_relative/models/hr_employee_relative.py
@@ -23,7 +23,9 @@ class HrEmployeeRelative(models.Model):
selection=[("male", "Male"), ("female", "Female"), ("other", "Other")],
)
date_of_birth = fields.Date()
- age = fields.Float(compute="_compute_age")
+ age_year = fields.Integer(string="Age (Years)", compute="_compute_age")
+ age_month = fields.Integer(string="Age (Months)")
+ age_day = fields.Integer(string="Age (Days)")
job = fields.Char()
phone_number = fields.Char()
@@ -33,8 +35,15 @@ class HrEmployeeRelative(models.Model):
@api.depends("date_of_birth")
def _compute_age(self):
for record in self:
- age = relativedelta(datetime.now(), record.date_of_birth)
- record.age = age.years + (age.months / 12)
+ if record.date_of_birth:
+ age = relativedelta(datetime.now(), record.date_of_birth)
+ record.age_year = age.years
+ record.age_month = age.months
+ record.age_day = age.days
+ else:
+ record.age_year = 0
+ record.age_month = 0
+ record.age_day = 0
@api.onchange("partner_id")
def _onchange_partner_id(self):
diff --git a/hr_employee_relative/tests/test_hr_employee_relatives.py b/hr_employee_relative/tests/test_hr_employee_relatives.py
index 3d5d02fc34f..9f6ab78923e 100644
--- a/hr_employee_relative/tests/test_hr_employee_relatives.py
+++ b/hr_employee_relative/tests/test_hr_employee_relatives.py
@@ -9,13 +9,16 @@
class TestHrEmployeeRelatives(common.TransactionCase):
- def setUp(self):
- super().setUp()
- self.Employee = self.env["hr.employee"]
- self.EmployeeRelative = self.env["hr.employee.relative"]
- self.relation_sibling = self.env.ref("hr_employee_relative.relation_sibling")
+ @classmethod
+ def setUpClass(cls):
+ super().setUpClass()
+ cls.Employee = cls.env["hr.employee"]
+ cls.EmployeeRelative = cls.env["hr.employee.relative"]
+ cls.relation_sibling = cls.env.ref("hr_employee_relative.relation_sibling")
+ cls.partner_1 = cls.env.ref("base.res_partner_1")
+ cls.partner_2 = cls.env.ref("base.res_partner_2")
- def test_age_calculation(self):
+ def create_relative(self, dob):
employee = self.Employee.create(
{
"name": "Employee",
@@ -25,19 +28,37 @@ def test_age_calculation(self):
0,
{
"relation_id": self.relation_sibling.id,
- "partner_id": self.env.ref("base.res_partner_1").id,
+ "partner_id": self.partner_1.id,
"name": "Relative",
- "date_of_birth": datetime.now() + relativedelta(years=-42),
+ "date_of_birth": dob,
},
)
],
}
)
- relative = self.EmployeeRelative.browse(employee.relative_ids[0].id)
- self.assertEqual(int(relative.age), 42)
+ return self.EmployeeRelative.browse(employee.relative_ids[0].id)
+
+ def test_age_calculation_with_dob(self):
+ dob = datetime.now() + relativedelta(years=-42, months=-3, days=-15)
+ relative = self.create_relative(dob)
+
+ expected_age = relativedelta(datetime.now(), dob)
+
+ self.assertEqual(relative.age_year, expected_age.years)
+ self.assertEqual(relative.age_month, expected_age.months)
+ self.assertEqual(relative.age_day, expected_age.days)
self.assertEqual(relative.name, "Relative")
- # onchange partner
+
+ # Test onchange partner
with Form(relative) as f:
- f.partner_id = self.env.ref("base.res_partner_2")
+ f.partner_id = self.partner_2
f.relation_id = self.relation_sibling
self.assertEqual(relative.name, relative.partner_id.display_name)
+
+ def test_age_calculation_without_dob(self):
+ relative = self.create_relative(False)
+
+ self.assertEqual(relative.age_year, 0)
+ self.assertEqual(relative.age_month, 0)
+ self.assertEqual(relative.age_day, 0)
+ self.assertEqual(relative.name, "Relative")
diff --git a/hr_employee_relative/views/hr_employee_relative.xml b/hr_employee_relative/views/hr_employee_relative.xml
index b1ddd11673e..c009c79450a 100644
--- a/hr_employee_relative/views/hr_employee_relative.xml
+++ b/hr_employee_relative/views/hr_employee_relative.xml
@@ -14,7 +14,9 @@
-
+
+
+