Files
test/sf_message/models/sf_message_sale.py
2024-10-08 17:50:59 +08:00

135 lines
7.4 KiB
Python

# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, api, _
class SFMessageSale(models.Model):
_name = 'sale.order'
_inherit = ['sale.order', 'jikimo.message.dispatch']
def create(self, vals_list):
res = super(SFMessageSale, self).create(vals_list)
if res:
try:
logging.info('add_queue res:%s' % res)
res.add_queue('待接单')
except Exception as e:
logging.info('add_queue error:%s' % e)
return res
# 确认接单
def action_confirm(self):
res = super(SFMessageSale, self).action_confirm()
if res is True:
try:
self.add_queue('确认接单')
picking_ids = self.mrp_production_ids
purchase_order_id = []
if picking_ids:
for picking_id in picking_ids:
purchase_order_ids = (
picking_id.procurement_group_id.stock_move_ids.created_purchase_line_id.order_id |
picking_id.procurement_group_id.stock_move_ids.move_orig_ids.purchase_line_id.order_id).ids
purchase_order_id.extend(purchase_order_ids)
if purchase_order_id:
purchase_order_list = self.env['purchase.order'].search([('id', 'in', purchase_order_id)])
for purchase_order_info in purchase_order_list:
purchase_order_info.add_queue('坯料采购提醒')
except Exception as e:
logging.info('add_queue error:%s' % e)
return res
# 继承并重写jikimo.message.dispatch的_get_message()
def _get_message(self, message_queue_ids):
contents = []
url = self.env['ir.config_parameter'].get_param('web.base.url')
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)
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))])
product = sale_order_line[0].product_id.name if len(sale_order_line) == 1 else '%s...' % \
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)
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_warning = self.sudo().search(
[('delivery_warning', '=', 'warning'), ('id', '=', int(item.res_id))])
if sale_order_warning:
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)
return contents
# # 销售订单逾期预警
def _overdue_warning_func(self):
today = fields.Date.today()
deadline_check = today + timedelta(days=2)
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:
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':
item.delivery_warning = 'warning'
sale_order_warning = self.sudo().search([('delivery_warning', '=', 'warning')])
if sale_order_warning:
sale_order_warning.add_queue('销售订单逾期预警')
# 销售订单已逾期 订单交期(交货日期)-当前日期时间≤0(未交货完成的销售订单)
def _overdue_func(self):
today = fields.Date.today()
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):
item.delivery_warning = 'overdue'
elif len(production.filtered(
lambda p: p.state in ['done', 'scrap'])) == item.mrp_production_count:
if delta <= timedelta(days=0):
item.delivery_warning = 'overdue'
sale_order_overdue = self.sudo().search([('delivery_warning', '=', 'overdue')])
if sale_order_overdue:
sale_order_overdue.add_queue('销售订单已逾期')