odoo18/addons_extensions/employee_jod/models/emp_jod.py

139 lines
5.7 KiB
Python

from odoo import fields, api, models, _
from odoo.exceptions import UserError
class HRApplicant(models.Model):
_inherit = 'hr.applicant'
joining_form_link = fields.Char()
class HREmployee(models.Model):
_inherit = 'hr.employee'
applicant_id = fields.Many2one("hr.applicant")
def send_jod_form_to_employee(self):
for rec in self:
if not rec.applicant_id:
application = self.env['hr.applicant'].sudo().search([('employee_id', '=', rec.id)], limit=1)
if not application:
candidate = self.env['hr.candidate'].sudo().create({
'partner_name': rec.name,
'email_from': rec.work_email,
'partner_phone': rec.work_phone,
'employee_id': rec.id,
})
application = self.env['hr.applicant'].sudo().create({
'candidate_id': candidate.id,
'hr_job_recruitment': self.env.ref('employee_jod.employee_jod_internal_job_recruitment_id').id,
'recruitment_stage_id': self.env.ref('employee_jod.hired_stage8').id,
})
rec.applicant_id = application.id
rec.sudo().applicant_id.send_post_onboarding_form = True
return rec.sudo().applicant_id.send_post_onboarding_form_to_candidate()
class PostOnboardingAttachmentWizard(models.TransientModel):
_inherit = 'post.onboarding.attachment.wizard'
send_mail = fields.Boolean(default=False)
@api.onchange('template_id')
def _onchange_template_id(self):
""" Update the email body and recipients based on the selected template. """
if self.template_id:
record_id = self.env.context.get('active_id')
model = self.env.context.get('active_model')
if model == 'hr.applicant':
applicant = self.env['hr.applicant'].browse(record_id)
else:
if model == 'hr.employee':
applicant = self.env['hr.employee'].browse(record_id).applicant_id
if applicant:
record = self.env[self.template_id.model].sudo().browse(applicant.id)
if not record.exists():
raise UserError("The record does not exist or is not accessible.")
# Fetch email template
email_template = self.env['mail.template'].sudo().browse(self.template_id.id)
if not email_template:
raise UserError("Email template not found.")
self.email_from = self.env.company.email
self.email_to = applicant.email_from
self.email_body = email_template.body_html # Assign the rendered email bodyc
self.email_subject = email_template.subject
def action_confirm(self):
for rec in self:
self.ensure_one()
context = self.env.context
active_id = context.get('active_id')
model = context.get('active_model')
if model == 'hr.applicant':
applicant = self.env['hr.applicant'].browse(active_id)
else:
if model == 'hr.employee':
applicant = self.env['hr.employee'].browse(active_id).applicant_id
applicant.recruitment_attachments = [(4, attachment.id) for attachment in rec.req_attachment_ids]
template = rec.template_id
personal_docs = rec.req_attachment_ids.filtered(lambda a: a.attachment_type == 'personal').mapped('name')
education_docs = rec.req_attachment_ids.filtered(lambda a: a.attachment_type == 'education').mapped('name')
previous_employer_docs = rec.req_attachment_ids.filtered(
lambda a: a.attachment_type == 'previous_employer').mapped('name')
other_docs = rec.req_attachment_ids.filtered(lambda a: a.attachment_type == 'others').mapped('name')
# Prepare context for the template
email_context = {
'personal_docs': personal_docs,
'education_docs': education_docs,
'previous_employer_docs': previous_employer_docs,
'other_docs': other_docs,
}
email_values = {
'email_from': rec.email_from,
'email_to': rec.email_to,
'email_cc': rec.email_cc,
'subject': rec.email_subject,
'attachment_ids': [(6, 0, rec.attachment_ids.ids)],
}
# Use 'with_context' to override the email template fields dynamically
if rec.send_mail:
template.sudo().with_context(default_body_html=rec.email_body,
**email_context).send_mail(applicant.id, email_values=email_values,
force_send=True)
base_url = self.get_base_url()
if rec.is_pre_onboarding_attachment_request:
applicant.doc_requests_form_status = 'email_sent_to_candidate'
else:
applicant.post_onboarding_form_status = 'email_sent_to_candidate'
applicant.joining_form_link = '%s/FTPROTECH/JoiningForm/%s'%(base_url,applicant.id)
return {'type': 'ir.actions.act_window_close'}
def get_base_url(self):
""" Return rooturl for a specific record.
By default, it returns the ir.config.parameter of base_url
but it can be overridden by model.
:return: the base url for this record
:rtype: str
"""
if len(self) > 1:
raise ValueError("Expected singleton or no record: %s" % self)
return self.env['ir.config_parameter'].sudo().get_param('web.base.url')