优化销售订单逾期和预警消息推送

This commit is contained in:
jinling.yang
2024-10-09 16:42:53 +08:00
parent a00f4af923
commit 2639a6ac6b
4 changed files with 105 additions and 67 deletions

View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import logging
from datetime import datetime, timedelta
from odoo import models, fields, api, _
@@ -42,17 +43,20 @@ class SFMessageSale(models.Model):
# 继承并重写jikimo.message.dispatch的_get_message()
def _get_message(self, message_queue_ids):
contents = []
bussiness_node = None
url = self.env['ir.config_parameter'].get_param('web.base.url')
current_time_strf = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
current_time = self.env['sf.sync.common'].sudo().get_add_time(current_time_strf)
current_time_datetime = datetime.strptime(current_time, '%Y-%m-%d %H:%M:%S')
time_range = timedelta(minutes=2)
i = 0
for item in message_queue_ids:
# 待接单的处理
if item.message_template_id.bussiness_node_id.name == '待接单':
content = super(SFMessageSale, self)._get_message(item)
action_id = self.env.ref('sale.action_quotations_with_onboarding').id
url = f"{url}/web#id={item.res_id}&view_type=form&action={action_id}"
content = content[0].replace('{{url}}', url)
url_with_id = f"{url}/web#id={item.res_id}&view_type=form&action={action_id}"
content = content[0].replace('{{url}}', url_with_id)
contents.append(content)
# 确认接单的处理
elif item.message_template_id.bussiness_node_id.name == '确认接单':
content = super(SFMessageSale, self)._get_message(item)
sale_order_line = self.env['sale.order.line'].search([('order_id', '=', int(item.res_id))])
@@ -60,75 +64,90 @@ class SFMessageSale(models.Model):
sale_order_line[
0].product_id.name
action_id = self.env.ref('sf_plan.sf_production_plan_action1').id
url = f"{url}/web#view_type=list&action={action_id}"
content = content[0].replace('{{product_id}}', product).replace('{{url}}', url)
url_with_id = f"{url}/web#view_type=list&action={action_id}"
content = content[0].replace('{{product_id}}', product).replace('{{url}}', url_with_id)
contents.append(content)
elif item.message_template_id.bussiness_node_id.name == '销售订单逾期预警':
current_time = datetime.now()
time_range = timedelta(minutes=2)
elif item.message_template_id.bussiness_node_id.name in ['销售订单逾期预警', '销售订单已逾期']:
bussiness_node = item.message_template_id.bussiness_node_id.name
for reminder_time in item.message_template_id.reminder_time_ids:
content = item.message_template_id.content
target_time = current_time.replace(hour=reminder_time, minute=0, second=0, microsecond=0)
if target_time - time_range <= current_time <= target_time + time_range:
print("当前时间在12点02分以内")
sale_order_warning = self.sudo().search(
[('delivery_warning', '=', 'warning'), ('id', '=', int(item.res_id))])
if sale_order_warning:
target_time = datetime.combine(current_time_datetime.date(), datetime.min.time()).replace(
hour=reminder_time.time_point,
minute=0,
second=0,
microsecond=0
)
logging.info(current_time)
logging.info(target_time)
if target_time - time_range <= current_time_datetime <= target_time + time_range:
search_condition = [
('delivery_warning', '=', 'warning')] if bussiness_node == '销售订单逾期预警' else [
('delivery_warning', '=', 'overdue')]
record = self.sudo().search(search_condition + [('id', '=', int(item.res_id))])
if record:
i += 1
if i >= 1:
content = content.replace('{{sale_warning_num}}', i).replace('{{url}}', url)
contents.append(content)
elif item.message_template_id.bussiness_node_id.name == '销售订单已逾期':
current_time = datetime.now()
time_range = timedelta(minutes=2)
for reminder_time in item.message_template_id.reminder_time_ids:
content = item.message_template_id.content
target_time = current_time.replace(hour=reminder_time, minute=0, second=0, microsecond=0)
if target_time - time_range <= current_time <= target_time + time_range:
print("当前时间在12点02分以内")
sale_order_overdue = self.sudo().search(
[('delivery_warning', '=', 'overdue'), ('id', '=', int(item.res_id))])
if sale_order_overdue:
i += 1
if i >= 1:
content = content.replace('{{sale_overdue_num}}', i).replace('{{url}}', url)
contents.append(content)
if i >= 1:
action_id = self.env.ref('sale.action_orders').id
url_with_id = f"{url}/web#view_type=list&action={action_id}"
content_template = content.replace('{{url}}', url_with_id)
if bussiness_node == '销售订单逾期预警':
content = content_template.replace('{{sale_warning_num}}', str(i))
elif bussiness_node == '销售订单已逾期':
content = content_template.replace('{{sale_overdue_num}}', str(i))
contents.append(content)
return contents
# # 销售订单逾期预警
def _overdue_warning_func(self):
today = fields.Date.today()
deadline_check = today + timedelta(days=2)
deadline_check = today + timedelta(days=1)
sale_order = self.sudo().search([('state', 'in', ['sale']), ('deadline_of_delivery', '!=', False)])
for item in sale_order:
production = self.env['mrp.production'].search([('origin', '=', item.name)])
if len(production.filtered(
lambda p: p.state not in ['done', 'scrap'])) != item.mrp_production_count:
if deadline_check < item.deadline_of_delivery:
lambda p: p.state not in ['done', 'scrap', 'cancel'])) != item.mrp_production_count:
if deadline_check == item.deadline_of_delivery:
item.delivery_warning = 'warning'
elif len(production.filtered(
lambda p: p.state in ['done', 'scrap'])) == item.mrp_production_count:
if deadline_check < item.deadline_of_delivery and item.delivery_status == 'pending':
lambda p: p.state in ['done', 'scrap', 'cancel'])) == item.mrp_production_count:
if deadline_check == item.deadline_of_delivery and item.delivery_status in ['pending', 'partial']:
item.delivery_warning = 'warning'
sale_order_warning = self.sudo().search([('delivery_warning', '=', 'warning')])
if sale_order_warning:
sale_order_warning.add_queue('销售订单逾期预警')
for warning in sale_order_warning:
message_template = self.env["jikimo.message.template"].search([
("model", "=", self._name),
("bussiness_node_id", "=", self.env.ref('sf_message.bussiness_sale_order_overdue_warning').id)
])
sale_order_has = self.env['jikimo.message.queue'].search(
[('res_id', '=', warning.id), ('message_status', '=', 'pending'),
('message_template_id', '=', message_template.id)])
if not sale_order_has:
sale_order_warning.add_queue('销售订单逾期预警')
# 销售订单已逾期 订单交期(交货日期)-当前日期时间≤0(未交货完成的销售订单)
def _overdue_func(self):
today = fields.Date.today()
today = datetime.today().date()
sale_order = self.sudo().search([('state', 'in', ['sale']), ('deadline_of_delivery', '!=', False)])
for item in sale_order:
delta = item.deadline_of_delivery - today
production = self.env['mrp.production'].search([('origin', '=', item.name)])
if len(production.filtered(
lambda p: p.state not in ['done', 'scrap'])) != item.mrp_production_count:
if delta <= timedelta(days=0):
lambda p: p.state not in ['done', 'scrap', 'cancel'])) != item.mrp_production_count:
if today == item.deadline_of_delivery:
item.delivery_warning = 'overdue'
elif len(production.filtered(
lambda p: p.state in ['done', 'scrap'])) == item.mrp_production_count:
if delta <= timedelta(days=0):
lambda p: p.state in ['done', 'scrap', 'cancel'])) == item.mrp_production_count:
if today == item.deadline_of_delivery and item.delivery_status in ['pending', 'partial']:
item.delivery_warning = 'overdue'
sale_order_overdue = self.sudo().search([('delivery_warning', '=', 'overdue')])
if sale_order_overdue:
sale_order_overdue.add_queue('销售订单已逾期')
for overdue in sale_order_overdue:
message_template = self.env["jikimo.message.template"].search([
("model", "=", self._name),
("bussiness_node_id", "=", self.env.ref('sf_message.bussiness_sale_order_overdue').id)
])
sale_order_has = self.env['jikimo.message.queue'].search(
[('res_id', '=', overdue.id), ('message_status', '=', 'pending'),
('message_template_id', '=', message_template.id)])
if not sale_order_has:
sale_order_overdue.add_queue('销售订单已逾期')