|
| 1 | +# Copyright 2025 - TODAY, Cristiano Mafra Junior <[email protected]> |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo.exceptions import ValidationError |
| 5 | + |
| 6 | +from odoo.addons.payroll.tests.common import TestPayslipBase |
| 7 | + |
| 8 | + |
| 9 | +class TestPayrollContractAdvantages(TestPayslipBase): |
| 10 | + def setUp(self): |
| 11 | + super().setUp() |
| 12 | + self.AdvantageTemplate = self.env["hr.contract.advantage.template"] |
| 13 | + self.Advantage = self.env["hr.contract.advantage"] |
| 14 | + |
| 15 | + def _create_template( |
| 16 | + self, |
| 17 | + *, |
| 18 | + lower=0.0, |
| 19 | + upper=100.0, |
| 20 | + default=10.0, |
| 21 | + code="BEN", |
| 22 | + name="Benefit", |
| 23 | + ): |
| 24 | + return self.AdvantageTemplate.create( |
| 25 | + { |
| 26 | + "name": name, |
| 27 | + "code": code, |
| 28 | + "lower_bound": lower, |
| 29 | + "upper_bound": upper, |
| 30 | + "default_value": default, |
| 31 | + } |
| 32 | + ) |
| 33 | + |
| 34 | + def test_onchange_advantage_template_sets_default_amount(self): |
| 35 | + """Onchange should set amount from template default value.""" |
| 36 | + template = self._create_template(default=123.45) |
| 37 | + advantage = self.Advantage.new( |
| 38 | + { |
| 39 | + "contract_id": self.richard_contract.id, |
| 40 | + "advantage_template_id": template.id, |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + advantage._onchange_advantage_template_id() |
| 45 | + |
| 46 | + self.assertEqual(advantage.amount, template.default_value) |
| 47 | + |
| 48 | + def test_constraint_allows_amount_inside_bounds(self): |
| 49 | + """Constraint should allow amount inside bounds.""" |
| 50 | + template = self._create_template(lower=0.0, upper=100.0) |
| 51 | + |
| 52 | + advantage = self.Advantage.create( |
| 53 | + { |
| 54 | + "contract_id": self.richard_contract.id, |
| 55 | + "advantage_template_id": template.id, |
| 56 | + "amount": 50.0, |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + self.assertEqual(advantage.amount, 50.0) |
| 61 | + |
| 62 | + def test_constraint_raises_if_above_upper_bound(self): |
| 63 | + """Constraint should raise when amount is above upper bound.""" |
| 64 | + template = self._create_template(lower=0.0, upper=100.0) |
| 65 | + |
| 66 | + with self.assertRaises(ValidationError): |
| 67 | + self.Advantage.create( |
| 68 | + { |
| 69 | + "contract_id": self.richard_contract.id, |
| 70 | + "advantage_template_id": template.id, |
| 71 | + "amount": 150.0, |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + def test_constraint_raises_if_below_lower_bound(self): |
| 76 | + """Constraint should raise when amount is below lower bound.""" |
| 77 | + template = self._create_template(lower=10.0, upper=100.0) |
| 78 | + |
| 79 | + with self.assertRaises(ValidationError): |
| 80 | + self.Advantage.create( |
| 81 | + { |
| 82 | + "contract_id": self.richard_contract.id, |
| 83 | + "advantage_template_id": template.id, |
| 84 | + "amount": 5.0, |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + def test_get_current_contract_dict_contains_advantages(self): |
| 89 | + """get_current_contract_dict should expose advantages by code.""" |
| 90 | + template = self._create_template( |
| 91 | + lower=0.0, |
| 92 | + upper=100.0, |
| 93 | + default=25.0, |
| 94 | + code="FUEL", |
| 95 | + name="Fuel Allowance", |
| 96 | + ) |
| 97 | + |
| 98 | + self.Advantage.create( |
| 99 | + { |
| 100 | + "contract_id": self.richard_contract.id, |
| 101 | + "advantage_template_id": template.id, |
| 102 | + "amount": 30.0, |
| 103 | + } |
| 104 | + ) |
| 105 | + |
| 106 | + self.apply_contract_cron() |
| 107 | + |
| 108 | + payslip = self.Payslip.create({"employee_id": self.richard_emp.id}) |
| 109 | + payslip.onchange_employee() |
| 110 | + contracts = payslip._get_employee_contracts() |
| 111 | + |
| 112 | + res = payslip.get_current_contract_dict(self.richard_contract, contracts) |
| 113 | + advantages = res.get("advantages") |
| 114 | + |
| 115 | + self.assertIsNotNone(advantages) |
| 116 | + self.assertEqual(advantages.FUEL, 30.0) |
0 commit comments