odoo18/addons_extensions/recruitment_employee_bridge/hooks.py

104 lines
4.4 KiB
Python

from odoo import api, SUPERUSER_ID
def post_init_hook(env):
"""Backfill bridge records for already hired recruitment records."""
env = api.Environment(env.cr, SUPERUSER_ID, {})
Bridge = env["recruitment.employee.bridge"].sudo().with_context(active_test=False)
Applicant = env["hr.applicant"].sudo().with_context(active_test=False)
Candidate = env["hr.candidate"].sudo().with_context(active_test=False)
def _bridge_vals(applicant=False, candidate=False, employee=False):
company = (
(employee and employee.company_id)
or (applicant and applicant.company_id)
or (candidate and candidate.company_id)
or env.company
)
return {
"applicant_id": applicant.id if applicant else False,
"candidate_id": candidate.id if candidate else False,
"employee_id": employee.id if employee else False,
"company_id": company.id,
"source": "recruitment",
"state": "draft",
}
def _sync_employee_applicant(employee, applicant):
if employee and applicant and "applicant_id" in employee._fields and not employee.applicant_id:
employee.write({"applicant_id": applicant.id})
def _get_or_create_bridge(applicant=False, candidate=False, employee=False):
bridge = Bridge
if employee:
bridge = Bridge.search([("employee_id", "=", employee.id)], limit=1, order="id desc")
if not bridge and applicant:
bridge = Bridge.search([("applicant_id", "=", applicant.id)], limit=1, order="id desc")
if not bridge and candidate:
bridge = Bridge.search([("candidate_id", "=", candidate.id)], limit=1, order="id desc")
vals = _bridge_vals(applicant, candidate, employee)
if bridge:
write_vals = {}
for field_name, value in vals.items():
if value and not bridge[field_name]:
write_vals[field_name] = value
if write_vals:
bridge.write(write_vals)
return bridge
return Bridge.create(vals)
applicants = Applicant.search([("employee_id", "!=", False)], order="id")
for applicant in applicants:
candidate = applicant.candidate_id
employee = applicant.employee_id
if not candidate or not employee:
continue
bridge = _get_or_create_bridge(applicant=applicant, candidate=candidate, employee=employee)
if candidate.employee_id != employee:
candidate.write({"employee_id": employee.id})
_sync_employee_applicant(employee, bridge.applicant_id)
candidates = Candidate.search([("employee_id", "!=", False)], order="id")
for candidate in candidates:
employee = candidate.employee_id
applicant = candidate.applicant_ids[:1]
bridge = _get_or_create_bridge(applicant=applicant, candidate=candidate, employee=employee)
_sync_employee_applicant(employee, bridge.applicant_id)
env.cr.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'offer_letter'
AND column_name IN ('bridge_id', 'candidate_id', 'main_candidate_id', 'salary')
""")
offer_columns = {row[0] for row in env.cr.fetchall()}
if "bridge_id" in offer_columns and "candidate_id" in offer_columns:
env.cr.execute("""
UPDATE offer_letter offer
SET bridge_id = bridge.id
FROM recruitment_employee_bridge bridge
WHERE offer.bridge_id IS NULL
AND offer.candidate_id IS NOT NULL
AND bridge.applicant_id = offer.candidate_id
""")
if "bridge_id" in offer_columns and "main_candidate_id" in offer_columns:
env.cr.execute("""
UPDATE offer_letter offer
SET bridge_id = bridge.id
FROM recruitment_employee_bridge bridge
WHERE offer.bridge_id IS NULL
AND offer.main_candidate_id IS NOT NULL
AND bridge.candidate_id = offer.main_candidate_id
""")
if {"bridge_id", "salary"}.issubset(offer_columns):
env.cr.execute("""
UPDATE recruitment_employee_bridge bridge
SET finalized_ctc = offer.salary
FROM offer_letter offer
WHERE offer.bridge_id = bridge.id
AND COALESCE(bridge.finalized_ctc, 0) = 0
AND COALESCE(offer.salary, 0) != 0
""")