198 lines
8.4 KiB
Python
198 lines
8.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api, _
|
|
from datetime import date
|
|
|
|
|
|
class ReasearcherMemberWizard(models.TransientModel):
|
|
_name = 'researcher.member.wizard'
|
|
|
|
def _default_member(self):
|
|
return self._context.get('active_id')
|
|
|
|
# selector = fields.Selection([
|
|
# ('researcher', 'Researcher'),
|
|
# ('researcher_team', 'Researcher Team'),
|
|
# ], string='Selector' ,default="researcher")
|
|
# researcher_id = fields.Many2one("hr.employee", string="Researcher")
|
|
researcher_team = fields.Many2one("committees.line", string="Researcher Team")
|
|
member_id = fields.Many2one("family.member", string="Member", default=_default_member)
|
|
|
|
def submit_member(self):
|
|
for rec in self:
|
|
rec.member_id.state_a = "complete_info"
|
|
# rec.member.researcher_id = rec.researcher_team.id
|
|
# self.env['visit.location'].create({
|
|
# 'benefit_id': rec._id.id,
|
|
# 'visit_date': date.today(),
|
|
# 'visit_types': 1,
|
|
# 'contact_type': 'email',
|
|
# # 'selector': rec.selector,
|
|
# # 'researcher_id': rec.researcher_id.id,
|
|
# 'researcher_team': rec.researcher_team.id,
|
|
# 'state': 'draft'
|
|
# })
|
|
|
|
|
|
class ReasearcherFamilyWizard(models.TransientModel):
|
|
_name = 'researcher.family.wizard'
|
|
|
|
def _default_benefit(self):
|
|
return self._context.get('active_id')
|
|
|
|
def _default_branch_custom_id(self):
|
|
benefit_id = self._context.get('active_id')
|
|
if benefit_id:
|
|
benefit = self.env['grant.benefit'].browse(benefit_id)
|
|
return benefit.branch_family_id.id
|
|
return False
|
|
|
|
# selector = fields.Selection([
|
|
# ('researcher', 'Researcher'),
|
|
# ('researcher_team', 'Researcher Team'),
|
|
# ], string='Selector' ,default="researcher")
|
|
# researcher_id = fields.Many2one("hr.employee", string="Researcher")
|
|
researcher_team = fields.Many2one("committees.line", string="Researcher Team",
|
|
domain="[('branch_custom_id', '=',branch_custom_id)]")
|
|
benefit_id = fields.Many2one("grant.benefit", string="Benefit", default=_default_benefit)
|
|
branch_custom_id = fields.Many2one("branch.settings", string="Branch",
|
|
domain="[('has_employees', '=', True)]",
|
|
default=_default_branch_custom_id)
|
|
branch_has_employees = fields.Boolean('Has Employees In branch', related="benefit_id.branch_has_employees")
|
|
is_submitted = fields.Boolean(string="Submitted", default=False)
|
|
|
|
def submit_family(self):
|
|
for rec in self:
|
|
rec.benefit_id.with_context(bypass_attachments_requirement=True).write({
|
|
'state': 'complete_info',
|
|
'contact_type': 'sms',
|
|
'researcher_id': rec.researcher_team.id,
|
|
'branch_custom_id': rec.branch_custom_id.id,
|
|
})
|
|
visit_record = self.env['visit.location'].create({
|
|
'benefit_id': rec.benefit_id.id,
|
|
'visit_date': date.today(),
|
|
'visit_types': 1,
|
|
# 'selector': rec.selector,
|
|
# 'researcher_id': rec.researcher_id.id,
|
|
'researcher_team': rec.researcher_team.id,
|
|
'state': 'draft'
|
|
})
|
|
rec.is_submitted = True
|
|
rec._send_assignment_notification(visit_record)
|
|
specialist_name = rec.researcher_team.name or _('Not Specified')
|
|
visit_number = visit_record.name or str(visit_record.id)
|
|
message = _('File has been assigned to specialist "%s" and initial visit created with number "%s"') % (
|
|
specialist_name, visit_number)
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('Assignment Successful ✓'),
|
|
'message': message,
|
|
'type': 'success',
|
|
'sticky': False,
|
|
'next': {
|
|
'type': 'ir.actions.act_window_close'
|
|
}
|
|
}
|
|
}
|
|
|
|
def action_return_to_benefit(self):
|
|
self.ensure_one()
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'grant.benefit',
|
|
'view_mode': 'form',
|
|
'res_id': self.benefit_id.id,
|
|
'target': 'main'
|
|
}
|
|
|
|
def send_visit_date_email(self):
|
|
template = self.env.ref('odex_benefit.visit_date_email', False)
|
|
if not template:
|
|
return
|
|
template.with_context(lang=self.env.user.lang).send_mail(self.id, force_send=True, raise_exception=False)
|
|
|
|
def _send_assignment_notification(self, visit_record):
|
|
if not self.researcher_team or not self.researcher_team.employee_id:
|
|
return
|
|
for employee in self.researcher_team.employee_id:
|
|
user = employee.user_id
|
|
if not user or not user.active:
|
|
continue
|
|
|
|
notification_type = getattr(user, 'notification_type', 'email')
|
|
if notification_type == 'email':
|
|
self._send_email_notification(user, visit_record)
|
|
else:
|
|
self._send_in_system_notification(user, visit_record)
|
|
|
|
def _send_email_notification(self, specialist_user, visit_record):
|
|
try:
|
|
template = self.env.ref('odex_benefit.visit_notification_template', False)
|
|
if not template:
|
|
return
|
|
template.write({
|
|
'email_to': specialist_user.work_email or specialist_user.email,
|
|
'email_cc': self.env.user.company_id.hr_email or self.env.user.company_id.email,
|
|
})
|
|
template.with_context(lang=self.env.user.lang).send_mail(visit_record.id, force_send=True,
|
|
raise_exception=False)
|
|
|
|
except Exception as e:
|
|
self._send_in_system_notification(specialist_user, visit_record)
|
|
|
|
def _send_in_system_notification(self, specialist_user, visit_record):
|
|
try:
|
|
template = self.env.ref('odex_benefit.visit_notification_template', False)
|
|
if template:
|
|
body_html = template._render_field('body_html', [visit_record.id], compute_lang=True)[
|
|
visit_record.id]
|
|
else:
|
|
body_html = """
|
|
<div style="font-family: Arial, sans-serif; padding: 20px;">
|
|
<h3>%s</h3>
|
|
<p>%s %s,</p>
|
|
<p>%s</p>
|
|
<ul>
|
|
<li><strong>%s</strong> %s</li>
|
|
<li><strong>%s</strong> %s</li>
|
|
<li><strong>%s</strong> %s</li>
|
|
</ul>
|
|
<p>%s</p>
|
|
<p>%s<br/>%s</p>
|
|
</div>
|
|
""" % (
|
|
_('New File Assignment'),
|
|
_('Dear'), specialist_user.name,
|
|
_('A new file has been assigned to you:'),
|
|
_('Benefit File:'), self.benefit_id.name or _('N/A'),
|
|
_('Visit Number:'), visit_record.name or visit_record.id,
|
|
_('Visit Date:'), visit_record.visit_date,
|
|
_('Please check your dashboard for more details.'),
|
|
_('Best regards,'), _('System')
|
|
)
|
|
|
|
partners = specialist_user.partner_id
|
|
partners.message_post(
|
|
body=body_html,
|
|
partner_ids=partners.ids,
|
|
subtype_id=self.env.ref('mail.mt_comment').id,
|
|
content_subtype='html',
|
|
email_layout_xmlid=False,
|
|
notify_by_email=False,
|
|
)
|
|
|
|
except Exception as e:
|
|
visit_record.message_post(
|
|
body=_("New file assignment for %s. Visit #%s created.") % (
|
|
specialist_user.name,
|
|
visit_record.name or visit_record.id
|
|
),
|
|
partner_ids=[specialist_user.partner_id.id],
|
|
subtype_id=self.env.ref('mail.mt_note').id,
|
|
notify_by_email=False,
|
|
)
|