Files
gzth/yuthon_will_task/models/yuthon_will_task.py
T
2025-12-18 11:11:00 +08:00

324 lines
17 KiB
Python

from odoo import api, fields, models, modules
import datetime
from datetime import datetime, timedelta, time
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
class YuthonWillTask(models.Model):
_name = 'yuthon.will.task'
_description = '日程'
_rec_name = 'name'
# _inherit = ['mail.thread.main.attachment', 'mail.activity.mixin']
request_hour_from = fields.Selection([
('0', '0:00'), ('0.25', '0:15'), ('0.5', '0:30'), ('0.75', '0:45'),
('1', '01:00'), ('1.25', '01:15'), ('1.5', '01:30'), ('1.75', '01:45'),
('2', '02:00'), ('2.25', '02:15'), ('2.5', '02:30'), ('2.75', '02:45'),
('3', '03:00'), ('3.25', '03:15'), ('3.5', '03:30'), ('3.75', '03:45'),
('4', '04:00'), ('4.25', '04:15'), ('4.5', '04:30'), ('4.75', '04:45'),
('5', '05:00'), ('5.25', '05:15'), ('5.5', '05:30'), ('5.75', '05:45'),
('6', '06:00'), ('6.25', '06:15'), ('6.5', '06:30'), ('6.75', '06:45'),
('7', '07:00'), ('7.25', '07:15'), ('7.5', '07:30'), ('7.75', '07:45'),
('8', '08:00'), ('8.25', '08:15'), ('8.5', '08:30'), ('8.75', '08:45'),
('9', '09:00'), ('9.25', '09:15'), ('9.5', '09:30'), ('9.75', '09:45'),
('10', '10:00'), ('10.25', '10:15'), ('10.5', '10:30'), ('10.75', '10:45'),
('11', '11:00'), ('11.25', '11:15'), ('11.5', '11:30'), ('11.75', '11:45'),
('12', '12:00'), ('12.25', '12:15'), ('12.5', '12:30'), ('12.75', '12:45'),
('13', '13:00'), ('13.25', '13:15'), ('13.5', '13:30'), ('13.75', '13:45'),
('14', '14:00'), ('14.25', '14:15'), ('14.5', '14:30'), ('14.75', '14:45'),
('15', '15:00'), ('15.25', '15:15'), ('15.5', '15:30'), ('15.75', '15:45'),
('16', '16:00'), ('16.25', '16:15'), ('16.5', '16:30'), ('16.75', '16:45'),
('17', '17:00'), ('17.25', '17:15'), ('17.5', '17:30'), ('17.75', '17:45'),
('18', '18:00'), ('18.25', '18:15'), ('18.5', '18:30'), ('18.75', '18:45'),
('19', '19:00'), ('19.25', '19:15'), ('19.5', '19:30'), ('19.75', '19:45'),
('20', '20:00'), ('20.25', '20:15'), ('20.5', '20:30'), ('20.75', '20:45'),
('21', '21:00'), ('21.25', '21:15'), ('21.5', '21:30'), ('21.75', '21:45'),
('22', '22:00'), ('22.25', '22:15'), ('22.5', '22:30'), ('22.75', '22:45'),
('23', '23:00'), ('23.25', '23:15'), ('23.5', '23:30'), ('23.75', '23:45')], string='开始时间', default='9')
request_hour_to = fields.Selection([
('0', '0:00'), ('0.25', '0:15'), ('0.5', '12:30'), ('0.75', '0:45'),
('1', '01:00'), ('1.25', '01:15'), ('1.5', '01:30'), ('1.75', '01:45'),
('2', '02:00'), ('2.25', '02:15'), ('2.5', '02:30'), ('2.75', '02:45'),
('3', '03:00'), ('3.25', '03:15'), ('3.5', '03:30'), ('3.75', '03:45'),
('4', '04:00'), ('4.25', '04:15'), ('4.5', '04:30'), ('4.75', '04:45'),
('5', '05:00'), ('5.25', '05:15'), ('5.5', '05:30'), ('5.75', '05:45'),
('6', '06:00'), ('6.25', '06:15'), ('6.5', '06:30'), ('6.75', '06:45'),
('7', '07:00'), ('7.25', '07:15'), ('7.5', '07:30'), ('7.75', '07:45'),
('8', '08:00'), ('8.25', '08:15'), ('8.5', '08:30'), ('8.75', '08:45'),
('9', '09:00'), ('9.25', '09:15'), ('9.5', '09:30'), ('9.75', '09:45'),
('10', '10:00'), ('10.25', '10:15'), ('10.5', '10:30'), ('10.75', '10:45'),
('11', '11:00'), ('11.25', '11:15'), ('11.5', '11:30'), ('11.75', '11:45'),
('12', '12:00'), ('12.25', '12:15'), ('12.5', '12:30'), ('12.75', '12:45'),
('13', '13:00'), ('13.25', '13:15'), ('13.5', '13:30'), ('13.75', '13:45'),
('14', '14:00'), ('14.25', '14:15'), ('14.5', '14:30'), ('14.75', '14:45'),
('15', '15:00'), ('15.25', '15:15'), ('15.5', '15:30'), ('15.75', '15:45'),
('16', '16:00'), ('16.25', '16:15'), ('16.5', '16:30'), ('16.75', '16:45'),
('17', '17:00'), ('17.25', '17:15'), ('17.5', '17:30'), ('17.75', '17:45'),
('18', '18:00'), ('18.25', '18:15'), ('18.5', '18:30'), ('18.75', '18:45'),
('19', '19:00'), ('19.25', '19:15'), ('19.5', '19:30'), ('19.75', '19:45'),
('20', '20:00'), ('20.25', '20:15'), ('20.5', '20:30'), ('20.75', '20:45'),
('21', '21:00'), ('21.25', '21:15'), ('21.5', '21:30'), ('21.75', '21:45'),
('22', '22:00'), ('22.25', '22:15'), ('22.5', '22:30'), ('22.75', '22:45'),
('23', '23:00'), ('23.25', '23:15'), ('23.5', '23:30'), ('23.75', '23:45')], string='结束时间', default='18')
priority = fields.Selection([('important', '重要不紧急'),
('urgent', '紧急不重要'), ('very_urgent', '重要且紧急')], string='优先级')
code = fields.Char(string="任务编号", default=lambda self: self.env['ir.sequence'].next_by_code('task_code'))
user_id = fields.Many2one('res.users', string="创建人", default=lambda self: self.env.user)
company_id = fields.Many2one('res.company', string="公司", default=lambda self: self.env.company)
name = fields.Text(string='任务内容')
remarks = fields.Text(string='备注')
creation_time = fields.Date(string='创建时间', default=fields.Date.today())
employee_ids = fields.Many2many('hr.employee', 'will_task_employee_rel', 'custom_id', 'employee_id',
string='人员', store=True, index=True)
employee_ids2 = fields.Many2many('hr.employee', 'will_task_employee_rel2', 'custom_id', 'employee_id',
string='司机', store=True, index=True)
user_role_ids = fields.Many2many('soong.user.role', string='角色', store=True, index=True)
task_time_ids = fields.Many2many('yuthon.task.time', string='提醒')
document_ids = fields.Many2many('ir.attachment', string="附件")
address = fields.Char(string='地点')
start_date = fields.Date(string='开始日期', default=fields.Date.today)
task_time = fields.Char(string='任务时间', compute='_compute_task_time')
is_this_company = fields.Boolean(string='本公司', default=True)
task_state = fields.Selection([('will', '待办'), ('done', '已办'), ('cancel', '取消')], default='will', string='状态', store=True)
users_ids = fields.Many2many(
'res.users',
relation='yuthon_will_task_users_rel', #
column1='task_id',
column2='user_id',
string='用户',
store=True,
index=True,
default=lambda self: self.env.user
)
self_users_ids = fields.Many2many(
'res.users',
relation='yuthon_will_task_current_users_rel',
column1='task_id',
column2='user_id',
string="当前经办人",
store=True, default=lambda self: [(6, 0, [self.env.user.id])]
)
@api.depends('start_date', 'request_hour_from', 'request_hour_to')
def _compute_task_time(self):
for record in self:
date_str = ""
if record.start_date:
date_str = record.start_date.strftime("%Y/%m/%d")
hour_mapping = {
'0': '0:00', '0.5': '0:30',
'1': '01:00', '1.5': '01:30',
'2': '02:00', '2.5': '02:30',
'3': '03:00', '3.5': '03:30',
'4': '04:00', '4.5': '04:30',
'5': '05:00', '5.5': '05:30',
'6': '06:00', '6.5': '06:30',
'7': '07:00', '7.5': '07:30',
'8': '08:00', '8.5': '08:30',
'9': '09:00', '9.5': '09:30',
'10': '10:00', '10.5': '10:30',
'11': '11:00', '11.5': '11:30',
'12': '12:00', '12.5': '12:30',
'13': '13:00', '13.5': '13:30',
'14': '14:00', '14.5': '14:30',
'15': '15:00', '15.5': '15:30',
'16': '16:00', '16.5': '16:30',
'17': '17:00', '17.5': '17:30',
'18': '18:00', '18.5': '18:30',
'19': '19:00', '19.5': '19:30',
'20': '20:00', '20.5': '20:30',
'21': '21:00', '21.5': '21:30',
'22': '22:00', '22.5': '22:30',
'23': '23:00', '23.5': '23:30'
}
from_time = hour_mapping.get(record.request_hour_from, "")
to_time = hour_mapping.get(record.request_hour_to, "")
if date_str and from_time and to_time:
record.task_time = f"{date_str} {from_time}"
else:
record.task_time = ""
@api.onchange('employee_ids', 'user_id')
def _onchange_employee_ids(self):
user_ids = []
if self.user_id:
user_ids.append(self.user_id.id)
for employee in self.employee_ids:
if employee.user_id:
user_ids.append(employee.user_id.id)
for employee2 in self.employee_ids2:
if employee2.user_id:
user_ids.append(employee2.user_id.id)
roles = self.employee_ids.mapped('user_role_id')
self.user_role_ids = roles
self.users_ids = [(6, 0, user_ids)]
def read_number(self):
return self.search_count([('task_state', '=', 'will'), ('users_ids', 'in', self.env.user.id)])
def will_task_time(self):
# 任务状态判断
for task in self.search([('task_state', '=', 'will')]):
# 计算开始时间(将日期和时间选择转换为datetime对象)
hour_from = float(task.request_hour_from)
start_hour = int(hour_from)
start_minute = int((hour_from - start_hour) * 60)
start_datetime = datetime.combine(fields.Date.from_string(task.start_date), time(start_hour, start_minute))
if task.request_hour_to:
hour_to = float(task.request_hour_to)
end_hour = int(hour_to)
end_minute = int((hour_to - end_hour) * 60)
end_datetime = start_datetime.replace(hour=end_hour, minute=end_minute)
else:
end_datetime = start_datetime + timedelta(hours=1)
now = datetime.now() + timedelta(hours=8)
if now > end_datetime:
task.write({'task_state': 'done'})
continue
_logger.info(f"日程消息提醒定时任务判断\n当前时间:{now} --- 任务开始时间:{start_datetime}")
if now <= start_datetime:
if task.task_time_ids:
for task_time in task.task_time_ids:
duration = self._compute_duration(task_time)
trigger_time = now + duration
_logger.info(f"判断可否发送消息\n任务天:-{trigger_time.day}时:-{trigger_time.hour}分:-{trigger_time.minute}\n当前天:-{start_datetime.day}时:-{start_datetime.hour}分:-{start_datetime.minute}")
if (trigger_time.day == start_datetime.day and trigger_time.hour == start_datetime.hour and trigger_time.minute == start_datetime.minute):
task.add_comment()
def _compute_duration(self, task_time):
if task_time.task_unit == 'min':
return timedelta(minutes=task_time.number)
elif task_time.task_unit == 'hour':
return timedelta(hours=task_time.number)
elif task_time.task_unit == 'day':
return timedelta(days=task_time.number)
else:
return timedelta.max
def add_comment(self):
self.ensure_one()
employee_list = []
for task in self.env['yuthon.dates.task'].search([('task_state', '=', 'to_do')]):
now = datetime.now()
if task.reminder_time == now:
task.task_state = 'done'
for task_time in task.employee_ids:
employee_list.append(task_time.employee_id)
for employee_id in employee_list:
if not employee_id.wecom_userid:
raise UserError("请同步{}的企业微信".format(employee_id.name))
url = f'https://oa.thtzjt.com/webh5#/journey?id={self.id}'
data_name = "事项名称: " + self.name + '\n'
data_user_ids = '参与人: ' + ', '.join(self.employee_ids.mapped('name')) + '\n'
data_employee_id = ('司机:' + ', '.join(self.employee_ids2.mapped('name')) + '\n') if self.employee_ids2 else ''
data_nota = ('备注:' + self.remarks) if self.remarks else ('') + '\n'
data = data_name + data_user_ids + data_nota + data_employee_id
self.sudo().env["wecom.apps"].sync_send_message_textcard(category='oa', user_id=employee_id.wecom_userid, title="日程事项提醒", description=data,
url=url, btntxt="详细信息")
def button_add_comment(self):
self.add_comment()
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'type': 'success',
'message': '已发送提醒消息',
'sticky': False,
}
}
def button_add_comment_cancel(self):
self.write({'task_state': 'cancel'})
employee_list = [i.id for i in self.employee_ids] + [i.id for i in self.employee_ids2]
for employee_id in self.env['hr.employee'].search([('id', 'in', employee_list)]):
if not employee_id.wecom_userid:
raise UserError("请同步{}的企业微信".format(employee_id.name))
menu_id = self.env['ir.ui.menu'].search([('name', '=', '我的日程')])
url = f'https://oa.thtzjt.com/web#id={self.id}&cids=2&action={menu_id.action.id}&model=yuthon.will.task&view_type=list&menu_id={menu_id.id}'
self.sudo().env["wecom.apps"].sync_send_message_textcard(category='oa', user_id=employee_id.wecom_userid,
title="日程事项提醒", description="事项名称: " + self.name + '\t' + '该事项已取消',
url=url, btntxt="详细信息")
for record in self:
partner_ids = record.employee_ids.user_id.partner_id.ids
record.with_context(lang=record.env.lang)._message_auto_subscribe_notify(partner_ids, 'yuthon_will_task.mail_action_yuthon_will_task_add_comment')
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'type': 'success',
'message': '已发送取消提醒消息',
'sticky': False,
}
}
@api.model
def create(self, vals):
"""创建日程时,同步创建提醒任务"""
will_task = super(YuthonWillTask, self).create(vals)
will_task._sync_dates_task()
return will_task
def write(self, vals):
"""修改日程时,先删除旧提醒任务,再创建新的"""
res = super(YuthonWillTask, self).write(vals)
key_fields = ['start_date', 'request_hour_from', 'task_time_ids', 'employee_ids', 'employee_ids2']
if any(field in vals for field in key_fields):
self._sync_dates_task()
return res
def _sync_dates_task(self):
reminder_employees = self.employee_ids | self.employee_ids2
if not reminder_employees:
return
old_tasks = self.env['yuthon.dates.task'].search([('yuthon_will_task_id', '=', self.id)])
if old_tasks:
old_tasks.unlink()
start_hour = float(self.request_hour_from)
hours = int(start_hour)
minutes = int((start_hour - hours) * 60)
task_start_datetime = datetime.combine(self.start_date,time(hour=hours, minute=minutes))
for task_time in self.task_time_ids:
reminder_time_str = task_time.name
reminder_hours = 0.0
import re
match = re.match(r'(\d+)(分钟|小时|天)', reminder_time_str.strip())
num = int(match.group(1))
unit = match.group(2)
if unit == '小时':
reminder_hours = num
elif unit == '分钟':
reminder_hours = num / 60.0
elif unit == '天':
reminder_hours = num * 24.0
reminder_datetime = task_start_datetime - timedelta(hours=reminder_hours)
existing_dates_task = self.env['yuthon.dates.task'].search([
('yuthon_will_task_id', '=', self.id),
('reminder_time', '=', reminder_datetime)
])
task_vals = {
'reminder_time': reminder_datetime - timedelta(hours=8),
'employee_ids': [(6, 0, reminder_employees.ids)],
'task_state': 'to_do',
}
if existing_dates_task:
existing_dates_task.write(task_vals)
else:
task_vals['yuthon_will_task_id'] = self.id
self.env['yuthon.dates.task'].create(task_vals)
def schedule_reminders(self):
return {
'type': 'ir.actions.act_window',
'name': '提醒任务详情',
'view_mode': 'tree',
'res_model': 'yuthon.dates.task',
'domain': [('yuthon_will_task_id', '=', self.id)],
}