Merge pull request #8 from expsa/web_tree_and_ps_report

fix11
This commit is contained in:
esam-sermah 2025-09-21 19:23:00 +03:00 committed by GitHub
commit fb996bf313
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 106 additions and 100 deletions

View File

@ -1,12 +1,8 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _ from odoo import models, fields, api, _
from datetime import datetime from datetime import datetime
class BaseAutomation(models.Model): class BaseAutomation(models.Model):
_inherit = 'base.automation' _inherit = 'base.automation'
send_notify = fields.Boolean(string='Send Notify', help="Send Notifications Within The System") send_notify = fields.Boolean(string='Send Notify', help="Send Notifications Within The System")
notify_title = fields.Char(string='Notification Title', related='model_id.name') notify_title = fields.Char(string='Notification Title', related='model_id.name')
notify_note = fields.Char(string='Notification Note') notify_note = fields.Char(string='Notification Note')
@ -44,13 +40,11 @@ class BaseAutomation(models.Model):
help="Send Notification To The Financial Manager Only") help="Send Notification To The Financial Manager Only")
cyber_security_id = fields.Boolean(string='Cyber Security', cyber_security_id = fields.Boolean(string='Cyber Security',
help="Send Notification To The Cyber Security Only") help="Send Notification To The Cyber Security Only")
def check_user_access(self, user_id, record, mode='read'): def check_user_access(self, user_id, record, mode='read'):
try: try:
return record.with_user(user_id).has_access(mode) return record.with_user(user_id).has_access(mode)
except Exception: except Exception:
return False return False
def access_users(self, groups, record): def access_users(self, groups, record):
users = [] users = []
for group in groups: for group in groups:
@ -60,26 +54,36 @@ class BaseAutomation(models.Model):
if self.hr_notifys: if self.hr_notifys:
if hasattr(record, 'employee_id') and record.employee_id: if hasattr(record, 'employee_id') and record.employee_id:
hr_mail = record.employee_id.sudo().company_id.hr_email hr_mail = record.employee_id.sudo().company_id.hr_email
if self.direct_manager_notify and user.id == record.employee_id.sudo().parent_id.user_id.id: if self.direct_manager_notify:
users.append(user.partner_id.email) if user.id == record.employee_id.sudo().parent_id.user_id.id:
if self.department_manager_notify and user.id == record.employee_id.sudo().coach_id.user_id.id: users.append(user.partner_id.email)
users.append(user.partner_id.email) if self.department_manager_notify:
if self.employee_notify and user.id == record.employee_id.sudo().user_id.id: if user.id == record.employee_id.sudo().coach_id.user_id.id:
users.append(user.partner_id.email) users.append(user.partner_id.email)
if self.ceo_notify and user.id == record.employee_id.sudo().company_id.general_supervisor_id.user_id.id: if self.employee_notify:
users.append(user.partner_id.email) if user.id == record.employee_id.sudo().user_id.id:
if self.hr_manager_notify and user.id == record.employee_id.sudo().company_id.hr_manager_id.user_id.id: users.append(user.partner_id.email)
users.append(user.partner_id.email) if self.ceo_notify:
if self.services_manager_id and user.id == record.employee_id.sudo().company_id.services_manager_id.user_id.id: if user.id == record.employee_id.sudo().company_id.general_supervisor_id.user_id.id:
users.append(user.partner_id.email) users.append(user.partner_id.email)
if self.it_manager_id and user.id == record.employee_id.sudo().company_id.it_manager_id.user_id.id: if self.hr_manager_notify:
users.append(user.partner_id.email) if user.id == record.employee_id.sudo().company_id.hr_manager_id.user_id.id:
if self.admin_manager_id and user.id == record.employee_id.sudo().company_id.admin_manager_id.user_id.id: users.append(user.partner_id.email)
users.append(user.partner_id.email) if self.services_manager_id:
if self.financial_manager_id and user.id == record.employee_id.sudo().company_id.financial_manager_id.user_id.id: if user.id == record.employee_id.sudo().company_id.services_manager_id.user_id.id:
users.append(user.partner_id.email) users.append(user.partner_id.email)
if self.cyber_security_id and user.id == record.employee_id.sudo().company_id.cyber_security_id.user_id.id: if self.it_manager_id:
users.append(user.partner_id.email) if user.id == record.employee_id.sudo().company_id.it_manager_id.user_id.id:
users.append(user.partner_id.email)
if self.admin_manager_id:
if user.id == record.employee_id.sudo().company_id.admin_manager_id.user_id.id:
users.append(user.partner_id.email)
if self.financial_manager_id:
if user.id == record.employee_id.sudo().company_id.financial_manager_id.user_id.id:
users.append(user.partner_id.email)
if self.cyber_security_id:
if user.id == record.employee_id.sudo().company_id.cyber_security_id.user_id.id:
users.append(user.partner_id.email)
if self.hr_email_notify and hr_mail not in users: if self.hr_email_notify and hr_mail not in users:
users.append(hr_mail) users.append(hr_mail)
else: else:
@ -87,45 +91,55 @@ class BaseAutomation(models.Model):
else: else:
users.append(user.partner_id.email) users.append(user.partner_id.email)
return ",".join(users) return ",".join(users)
def access_users_ids(self, groups, record): def access_users_ids(self, groups, record):
processed_users = set() processed_users = set()
for group in groups: for group in groups:
for user in group.users: for user in group.users:
if user.id not in processed_users and self.check_user_access(user_id=user.id, record=record, mode='read'): if user.id not in processed_users and self.check_user_access(user_id=user.id, record=record,mode='read'):
if self.hr_notifys: if self.hr_notifys:
if hasattr(record, 'employee_id') and record.employee_id: if hasattr(record, 'employee_id') and record.employee_id:
if self.direct_manager_notify and user.id == record.employee_id.sudo().parent_id.user_id.id: if self.direct_manager_notify:
processed_users.add(user.id) if user.id == record.employee_id.sudo().parent_id.user_id.id:
if self.department_manager_notify and user.id == record.employee_id.sudo().coach_id.user_id.id: processed_users.add(user.id)
processed_users.add(user.id) if self.department_manager_notify:
if self.employee_notify and user.id == record.employee_id.sudo().user_id.id: if user.id == record.employee_id.sudo().coach_id.user_id.id:
processed_users.add(user.id) processed_users.add(user.id)
if self.ceo_notify and user.id == record.employee_id.sudo().company_id.general_supervisor_id.user_id.id: if self.employee_notify:
processed_users.add(user.id) if user.id == record.employee_id.sudo().user_id.id:
if self.hr_manager_notify and user.id == record.employee_id.sudo().company_id.hr_manager_id.user_id.id: processed_users.add(user.id)
processed_users.add(user.id) if self.ceo_notify:
if self.services_manager_id and user.id == record.employee_id.sudo().company_id.services_manager_id.user_id.id: if user.id == record.employee_id.sudo().company_id.general_supervisor_id.user_id.id:
processed_users.add(user.id) processed_users.add(user.id)
if self.it_manager_id and user.id == record.employee_id.sudo().company_id.it_manager_id.user_id.id: if self.hr_manager_notify:
processed_users.add(user.id) if user.id == record.employee_id.sudo().company_id.hr_manager_id.user_id.id:
if self.admin_manager_id and user.id == record.employee_id.sudo().company_id.admin_manager_id.user_id.id: processed_users.add(user.id)
processed_users.add(user.id) if self.services_manager_id:
if self.financial_manager_id and user.id == record.employee_id.sudo().company_id.financial_manager_id.user_id.id: if user.id == record.employee_id.sudo().company_id.services_manager_id.user_id.id:
processed_users.add(user.id) processed_users.add(user.id)
if self.cyber_security_id and user.id == record.employee_id.sudo().company_id.cyber_security_id.user_id.id: if self.it_manager_id:
processed_users.add(user.id) if user.id == record.employee_id.sudo().company_id.it_manager_id.user_id.id:
processed_users.add(user.id)
if self.admin_manager_id:
if user.id == record.employee_id.sudo().company_id.admin_manager_id.user_id.id:
processed_users.add(user.id)
if self.financial_manager_id:
if user.id == record.employee_id.sudo().company_id.financial_manager_id.user_id.id:
processed_users.add(user.id)
if self.cyber_security_id:
if user.id == record.employee_id.sudo().company_id.cyber_security_id.user_id.id:
processed_users.add(user.id)
else: else:
processed_users.add(user.id) processed_users.add(user.id)
else: else:
processed_users.add(user.id) processed_users.add(user.id)
return list(processed_users) return list(processed_users)
def get_notify_message(self, record): def get_notify_message(self, record):
user_ids = self.access_users_ids(self.notify_to_groups_ids, record) user_ids = self.access_users_ids(self.notify_to_groups_ids, record)
today = datetime.today() today = datetime.today()
for user in user_ids: for user in user_ids:
summary = (self.name or (self.template_id.name if hasattr(self, 'template_id') and self.template_id else 'Notification')) summary = (self.name or
(self.template_id.name if hasattr(self, 'template_id') and self.template_id else None) or
'Notification')
data = { data = {
'res_id': record.id, 'res_id': record.id,
'res_model_id': self.env['ir.model'].search([('model', '=', record._name)]).id, 'res_model_id': self.env['ir.model'].search([('model', '=', record._name)]).id,
@ -135,47 +149,46 @@ class BaseAutomation(models.Model):
'date_deadline': today 'date_deadline': today
} }
self.env['mail.activity'].create(data) self.env['mail.activity'].create(data)
if self.hr_notifys and not user_ids and self.notify_to_groups_ids: if self.hr_notifys:
if hasattr(record, 'employee_id') and record.employee_id: if not user_ids and self.notify_to_groups_ids:
hr_manager_user = record.employee_id.sudo().company_id.hr_manager_id.user_id.id if hasattr(record, 'employee_id') and record.employee_id:
summary = (self.name or (self.template_id.name if hasattr(self, 'template_id') and self.template_id else 'Notification')) hr_manager_user = record.employee_id.sudo().company_id.hr_manager_id.user_id.id
data = { summary = (self.name or
'res_id': record.id, (self.template_id.name if hasattr(self, 'template_id') and self.template_id else None) or
'res_model_id': self.env['ir.model'].search([('model', '=', record._name)]).id, 'Notification')
'user_id': hr_manager_user, data = {
'summary': _(summary), 'res_id': record.id,
'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id, 'res_model_id': self.env['ir.model'].search([('model', '=', record._name)]).id,
'date_deadline': today 'user_id': hr_manager_user,
} 'summary': _(summary),
self.env['mail.activity'].create(data) 'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id,
'date_deadline': today
}
self.env['mail.activity'].create(data)
def get_mail_to(self, record): def get_mail_to(self, record):
users = self.access_users(self.notify_to_groups_ids, record) users = self.access_users(self.notify_to_groups_ids, record)
if self.hr_notifys and not users and self.notify_to_groups_ids: if self.hr_notifys:
if hasattr(record, 'employee_id') and record.employee_id: if not users and self.notify_to_groups_ids:
hr_manager_mail = record.employee_id.sudo().company_id.hr_manager_id.user_id.partner_id.email if hasattr(record, 'employee_id') and record.employee_id:
users = hr_manager_mail hr_manager_mail = record.employee_id.sudo().company_id.hr_manager_id.user_id.partner_id.email
users = hr_manager_mail
return users return users
def get_mail_cc(self, record): def get_mail_cc(self, record):
users = self.access_users(self.notify_cc_groups_ids, record) users = self.access_users(self.notify_cc_groups_ids, record)
if self.hr_notifys and not users and self.notify_cc_groups_ids: if self.hr_notifys:
if hasattr(record, 'employee_id') and record.employee_id: if not users and self.notify_cc_groups_ids:
hr_mail = record.employee_id.sudo().company_id.hr_email if hasattr(record, 'employee_id') and record.employee_id:
users = hr_mail hr_mail = record.employee_id.sudo().company_id.hr_email
users = hr_mail
return users return users
def _process(self, records, domain_post=None): def _process(self, records, domain_post=None):
result = super()._process(records, domain_post) result = super()._process(records, domain_post)
if self.send_notify: if self.send_notify:
for record in records: for record in records:
self.get_notify_message(record) self.get_notify_message(record)
return result return result
class ServerActions(models.Model): class ServerActions(models.Model):
_inherit = 'ir.actions.server' _inherit = 'ir.actions.server'
@api.model @api.model
def _run_action_email(self, eval_context=None): def _run_action_email(self, eval_context=None):
if self._context.get('__action_done'): if self._context.get('__action_done'):
@ -183,9 +196,9 @@ class ServerActions(models.Model):
automation = list(automations.keys())[0] automation = list(automations.keys())[0]
record = automations[automation] record = automations[automation]
action = automation.action_server_id action = automation.action_server_id
old_email_to = action.template_id.email_to if action.template_id else ''
old_email_cc = action.template_id.email_cc if action.template_id else ''
if action.template_id: if action.template_id:
old_email_to = action.template_id.email_to
old_email_cc = action.template_id.email_cc
template_values = { template_values = {
'email_to': automation.get_mail_to(record), 'email_to': automation.get_mail_to(record),
'email_cc': automation.get_mail_cc(record), 'email_cc': automation.get_mail_cc(record),
@ -206,35 +219,29 @@ class ServerActions(models.Model):
automation.get_notify_message(record) automation.get_notify_message(record)
return False return False
return super()._run_action_email(eval_context=eval_context) return super()._run_action_email(eval_context=eval_context)
class MailActivity(models.Model): class MailActivity(models.Model):
_inherit = 'mail.activity' _inherit = 'mail.activity'
def action_notify(self): def action_notify(self):
if not self: if not self:
return return
original_context = self.env.context original_context = self.env.context
body_template = self.env.ref('mail.message_activity_assigned') body_template = self.env.ref('mail.message_activity_assigned')
for activity in self: for activity in self:
if activity.user_id.lang: if activity.user_id.lang:
self = self.with_context(lang=activity.user_id.lang) self = self.with_context(lang=activity.user_id.lang)
body_template = body_template.with_context(lang=activity.user_id.lang) body_template = body_template.with_context(lang=activity.user_id.lang)
activity = activity.with_context(lang=activity.user_id.lang) activity = activity.with_context(lang=activity.user_id.lang)
model_description = self.env['ir.model']._get(activity.res_model).display_name model_description = self.env['ir.model']._get(activity.res_model).display_name
template_values = { body = body_template._render(
'activity': activity, {
'model_description': model_description, 'activity': activity,
'access_link': self.env['mail.thread']._notify_get_action_link( 'model_description': model_description,
'view', model=activity.res_model, res_id=activity.res_id), 'access_link': self.env['mail.thread']._notify_get_action_link('view', model=activity.res_model,
} res_id=activity.res_id),
try: },
body = body_template.render(template_values, engine='ir.qweb', minimal_qcontext=True) engine='ir.qweb',
except Exception: minimal_qcontext=True
body = _('Activity assigned: %(summary)s', summary=activity.summary or activity.activity_type_id.name) )
record = self.env[activity.res_model].browse(activity.res_id) record = self.env[activity.res_model].browse(activity.res_id)
if activity.user_id: if activity.user_id:
record.message_post( record.message_post(
@ -246,17 +253,16 @@ class MailActivity(models.Model):
email_layout_xmlid='system_notification.mail_notification_odex', email_layout_xmlid='system_notification.mail_notification_odex',
message_type='notification' message_type='notification'
) )
self.with_context(original_context) body_template = body_template.with_context(original_context)
self = self.with_context(original_context)
class BaseGroupAutomation(models.Model): class BaseGroupAutomation(models.Model):
_name = 'automation.group' _name = 'automation.group'
_rec_name = 'model_id' _rec_name = 'model_id'
model_id = fields.Many2one('ir.model', string="Model", ondelete='cascade', required=True) model_id = fields.Many2one('ir.model', string="Model", ondelete='cascade', required=True)
atuomation_ids = fields.Many2many(comodel_name='base.automation', relation='automation_state_groups_rel', atuomation_ids = fields.Many2many(comodel_name='base.automation', relation='automation_state_groups_rel',
string='Group States ') string='Group States ')
def unlink(self): def unlink(self):
for record in self.atuomation_ids: for record in self.atuomation_ids:
record.unlink() record.unlink()
return super().unlink() result = super().unlink()
return result