From 07aaf675beb43278b7368b6d212faa010279ff3f Mon Sep 17 00:00:00 2001 From: karuna Date: Thu, 15 May 2025 16:03:53 +0530 Subject: [PATCH] ATS ATTACHMENTS --- .../models/hr_applicant.py | 4 +- .../post_onboarding_attachment_wizard.py | 111 ++++++++++++------ .../post_onboarding_attachment_wizard.xml | 52 +++++--- 3 files changed, 115 insertions(+), 52 deletions(-) diff --git a/addons_extensions/hr_recruitment_extended/models/hr_applicant.py b/addons_extensions/hr_recruitment_extended/models/hr_applicant.py index 865aa5fa6..9315e073e 100644 --- a/addons_extensions/hr_recruitment_extended/models/hr_applicant.py +++ b/addons_extensions/hr_recruitment_extended/models/hr_applicant.py @@ -254,7 +254,7 @@ class HRApplicant(models.Model): 'view_mode': 'form', 'view_type': 'form', 'target': 'new', - 'context': {'default_attachment_ids': []} + 'context': {'default_req_attachment_ids': []} } @@ -266,7 +266,7 @@ class HRApplicant(models.Model): 'view_mode': 'form', 'view_type': 'form', 'target': 'new', - 'context': {'default_attachment_ids': [],'default_is_pre_onboarding_attachment_request': True} + 'context': {'default_req_attachment_ids': [],'default_is_pre_onboarding_attachment_request': True} } def _track_template(self, changes): res = super(HRApplicant, self)._track_template(changes) diff --git a/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.py b/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.py index 282abb514..2f89f911d 100644 --- a/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.py +++ b/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.py @@ -1,14 +1,20 @@ from odoo import models, fields, api +from odoo.exceptions import UserError + class PostOnboardingAttachmentWizard(models.TransientModel): _name = 'post.onboarding.attachment.wizard' _description = 'Post Onboarding Attachment Wizard' - attachment_ids = fields.Many2many( + req_attachment_ids = fields.Many2many( 'recruitment.attachments', string='Attachments to Request' ) + attachment_ids = fields.Many2many('ir.attachment') is_pre_onboarding_attachment_request = fields.Boolean(default=False) + template_id = fields.Many2one('mail.template', string='Email Template',compute='_compute_template_id') + submit_date = fields.Date(string='Submission Date', required=True, default=fields.Date.today()) + send_email_from_odoo = fields.Boolean(string="Send Email From Odoo", default=False) email_from = fields.Char('Email From') email_to = fields.Char('Email To') email_cc = fields.Text('Email CC') @@ -18,56 +24,89 @@ class PostOnboardingAttachmentWizard(models.TransientModel): prefetch=True, translate=True, sanitize='email_outgoing', ) + @api.depends('is_pre_onboarding_attachment_request') + def _compute_template_id(self): + for rec in self: + if rec.is_pre_onboarding_attachment_request: + rec.template_id = self.env.ref('hr_recruitment_extended.email_template_request_documents', + raise_if_not_found=False) + else: + rec.template_id = self.env.ref('hr_recruitment_extended.email_template_post_onboarding_form', + raise_if_not_found=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') + applicant = self.env['hr.applicant'].browse(record_id) + + if record_id: + record = self.env[self.template_id.model].sudo().browse(record_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 + @api.model def default_get(self, fields_list): """Pre-fill attachments with is_default=True""" defaults = super(PostOnboardingAttachmentWizard, self).default_get(fields_list) default_attachments = self.env['recruitment.attachments'].search([('is_default', '=', True)]) if default_attachments: - defaults['attachment_ids'] = [(6, 0, default_attachments.ids)] + defaults['req_attachment_ids'] = [(6, 0, default_attachments.ids)] return defaults def action_confirm(self): - self.ensure_one() - context = self.env.context - active_id = context.get('active_id') - applicant = self.env['hr.applicant'].browse(active_id) + for rec in self: + self.ensure_one() + context = self.env.context + active_id = context.get('active_id') + applicant = self.env['hr.applicant'].browse(active_id) - applicant.recruitment_attachments = [(4, attachment.id) for attachment in self.attachment_ids] + 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') - if self.is_pre_onboarding_attachment_request: - template = self.env.ref('hr_recruitment_extended.email_template_request_documents', raise_if_not_found=False) - else: - template = self.env.ref('hr_recruitment_extended.email_template_post_onboarding_form', raise_if_not_found=False) + # 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, + 'attachment_ids': [(6, 0, rec.attachment_ids.ids)], + + } + # Use 'with_context' to override the email template fields dynamically + template.sudo().with_context(default_body_html=rec.email_body, default_subject=rec.email_subject, + **email_context).send_mail(applicant.id, email_values=email_values, + force_send=True) - personal_docs = self.attachment_ids.filtered(lambda a: a.attachment_type == 'personal').mapped('name') - education_docs = self.attachment_ids.filtered(lambda a: a.attachment_type == 'education').mapped('name') - previous_employer_docs = self.attachment_ids.filtered( - lambda a: a.attachment_type == 'previous_employer').mapped('name') - other_docs = self.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_to': applicant.email_from, - 'auto_delete': True, - } - - template.with_context(**email_context).send_mail( - applicant.id, force_send=True, email_values=email_values - ) - - if self.is_pre_onboarding_attachment_request: + 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' diff --git a/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.xml b/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.xml index ca37c5e15..dc7be774d 100644 --- a/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.xml +++ b/addons_extensions/hr_recruitment_extended/wizards/post_onboarding_attachment_wizard.xml @@ -1,19 +1,43 @@ - post.onboarding.attachment.wizard.form - post.onboarding.attachment.wizard - -
- - - -
-
-
-
-
+ post.onboarding.attachment.wizard.form + post.onboarding.attachment.wizard + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
\ No newline at end of file