from odoo import fields, api, models class HRJob(models.Model): _inherit = 'hr.job' require_no_of_recruitment = fields.Integer(string='Target', copy=False, help='Number of new employees you expect to recruit.', default=1, compute="_compute_no_of_recruitment") hr_job_recruitments = fields.One2many('hr.job.recruitment', 'job_id', string='Recruitments') @api.depends('hr_job_recruitments') def _compute_no_of_recruitment(self): for record in self: # Sum the no_of_recruitment from the related hr.job.recruitment records record.require_no_of_recruitment = sum(rec.no_of_recruitment for rec in record.hr_job_recruitments) def write(self, vals): if 'date_to' in vals: vals.pop('date_to') res = super().write(vals) return res def _compute_new_application_count(self): self.env.cr.execute( """ WITH job_stage AS ( SELECT DISTINCT ON (j.id) j.id AS hr_job_recruitment, j.job_id AS job_id, s.id AS stage_id, s.sequence AS sequence FROM hr_job_recruitment j LEFT JOIN hr_job_recruitment_hr_recruitment_stage_rel rel ON rel.hr_job_recruitment_id = j.id JOIN hr_recruitment_stage s ON s.id = rel.hr_recruitment_stage_id WHERE j.job_id IN %s ORDER BY j.id, s.sequence ASC ) SELECT js.job_id, COALESCE(SUM(CASE WHEN a.id IS NOT NULL THEN 1 ELSE 0 END), 0) AS new_applicant FROM job_stage js LEFT JOIN hr_applicant a ON a.hr_job_recruitment = js.hr_job_recruitment AND a.recruitment_stage_id = js.stage_id AND a.active IS TRUE AND (a.company_id IN %s OR a.company_id IS NULL) GROUP BY js.job_id; """, [tuple(self.ids), tuple(self.env.companies.ids)] ) new_applicant_count = dict(self.env.cr.fetchall()) for job in self: job.new_application_count = new_applicant_count.get(job.id, 0)