odoo18/addons_extensions/recruitment_employee_bridge/models/hr_candidate.py

95 lines
3.4 KiB
Python

from odoo import _, fields, models
from odoo.exceptions import UserError
class HrCandidate(models.Model):
_inherit = "hr.candidate"
bridge_ids = fields.One2many("recruitment.employee.bridge", "candidate_id", string="Onboarding Records")
bridge_count = fields.Integer(compute="_compute_bridge_count")
bridge_employee_id = fields.Many2one(
"hr.employee",
compute="_compute_bridge_employee",
string="Onboarding Employee",
)
bridge_employee_name = fields.Char(related="bridge_employee_id.name", string="Employee")
def _compute_bridge_count(self):
data = self.env["recruitment.employee.bridge"].read_group(
[("candidate_id", "in", self.ids), ("active", "=", True)],
["candidate_id"],
["candidate_id"],
)
counts = {item["candidate_id"][0]: item["candidate_id_count"] for item in data}
for candidate in self:
candidate.bridge_count = counts.get(candidate.id, 0)
def _compute_bridge_employee(self):
bridges = self.env["recruitment.employee.bridge"].search(
[("candidate_id", "in", self.ids), ("active", "=", True)],
order="id desc",
)
bridge_by_candidate = {}
for bridge in bridges:
bridge_by_candidate.setdefault(bridge.candidate_id.id, bridge)
for candidate in self:
candidate.bridge_employee_id = bridge_by_candidate.get(candidate.id).employee_id if bridge_by_candidate.get(candidate.id) else False
def _get_bridge_employee_create_vals(self):
self.ensure_one()
vals = dict(self._get_employee_create_vals())
vals.pop("candidate_id", None)
return vals
def create_employee_from_candidate(self):
self.ensure_one()
self._check_interviewer_access()
bridge = self.env["recruitment.employee.bridge"].search(
[("candidate_id", "=", self.id), ("active", "=", True)],
limit=1,
order="id desc",
)
if bridge:
return {
"name": _("Onboarding"),
"type": "ir.actions.act_window",
"res_model": "recruitment.employee.bridge",
"view_mode": "form",
"res_id": bridge.id,
}
bridge = self.env["recruitment.employee.bridge"].create({
"candidate_id": self.id,
"employee_name": self.partner_name,
"work_email": self.email_from,
"work_phone": self.partner_phone,
"company_id": self.company_id.id or self.env.company.id,
"source": "recruitment",
"state": "draft",
})
return {
"name": _("Onboarding"),
"type": "ir.actions.act_window",
"res_model": "recruitment.employee.bridge",
"view_mode": "form",
"res_id": bridge.id,
}
def action_open_bridge_employee(self):
self.ensure_one()
bridge = self.env["recruitment.employee.bridge"].search(
[("candidate_id", "=", self.id), ("active", "=", True)],
limit=1,
order="id desc",
)
if not bridge:
raise UserError(_("No bridge found."))
return {
"name": _("Onboarding"),
"type": "ir.actions.act_window",
"res_model": "recruitment.employee.bridge",
"view_mode": "form",
"res_id": bridge.id,
}