# Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import api, fields, models from datetime import datetime class HRContract(models.Model): _inherit = 'hr.contract' l10n_ir_other_allowances = fields.Monetary(string='Iran Other Allowances') l10n_ir_responsibility_allowance = fields.Monetary(string='Iran Responsibility Allowance') l10n_ir_years_of_service = fields.Integer( string='Years of Service', compute='_compute_years_of_service', inverse='_inverse_years_of_service', store=True, help='Sum of all completed service years for the employee across confirmed contracts.') _sql_constraints = [ ('check_l10n_ir_years_of_service_positive', 'CHECK(l10n_ir_years_of_service >= 0)', 'Years of Service must be equal to or greater than 0'), ] def _inverse_years_of_service(self): # No action needed — just having this allows field to be editable manually # The manually set value will stay until a recompute is triggered pass @api.depends('employee_id') def _compute_years_of_service(self): for contract in self: total_days = 0 if contract.employee_id: employee_contracts = self.env['hr.contract'].search([ ('employee_id', '=', contract.employee_id.id), ('state', 'in', ['open', 'close']), ('date_start', '!=', False), ('date_end', '!=', False), ]) for c in employee_contracts: duration = (c.date_end - c.date_start).days total_days += max(duration, 0) contract.l10n_ir_years_of_service = total_days // 365