76 lines
3.7 KiB
Python
76 lines
3.7 KiB
Python
from odoo import models, fields, api, _
|
||
import logging, json
|
||
import requests
|
||
from odoo.addons.sf_base.commons.common import Common
|
||
from urllib.parse import urlencode
|
||
|
||
_logger = logging.getLogger(__name__)
|
||
|
||
class SFMessageWork(models.Model):
|
||
_name = 'mrp.workorder'
|
||
_inherit = ['mrp.workorder', 'jikimo.message.dispatch']
|
||
|
||
@api.depends('production_availability', 'blocked_by_workorder_ids.state')
|
||
def _compute_state(self):
|
||
super(SFMessageWork, self)._compute_state()
|
||
for workorder in self:
|
||
if workorder.state == 'ready' and workorder.routing_type == '装夹预调':
|
||
jikimo_message_queue = self.env['jikimo.message.queue'].sudo().search(
|
||
[('res_id', '=', workorder.id), ("message_status", "=", "pending")])
|
||
if not jikimo_message_queue:
|
||
workorder.add_queue('工单已下发通知')
|
||
|
||
def _get_message(self, message_queue_ids):
|
||
contents = []
|
||
product_id = []
|
||
for message_queue_id in message_queue_ids:
|
||
if message_queue_id.message_template_id.name == '工单已下发通知':
|
||
content = message_queue_id.message_template_id.content
|
||
mrp_workorder_line = self.env['mrp.workorder'].search([('id', '=', int(message_queue_id.res_id))])
|
||
mrp_workorder_list = self.env['mrp.workorder'].search(
|
||
[('product_id', '=', mrp_workorder_line.product_id.id), ('state', '=', 'ready'),
|
||
('routing_type', '=', '装夹预调')])
|
||
if len(mrp_workorder_list) > 0 and mrp_workorder_line.product_id.id not in product_id:
|
||
url = self.request_url()
|
||
content = content.replace('{{product_id}}', mrp_workorder_line.product_id.name).replace(
|
||
'{{number}}', str(len(mrp_workorder_list))).replace(
|
||
'{{request_url}}', url)
|
||
product_id.append(mrp_workorder_line.product_id.id)
|
||
contents.append(content)
|
||
return contents
|
||
|
||
@api.depends('cnc_ids.tool_state')
|
||
def _compute_tool_state(self):
|
||
# 将self的id与tool_state进行保存
|
||
tool_state_dict = {record.id: record.tool_state for record in self}
|
||
res = super(SFMessageWork, self)._compute_tool_state()
|
||
data = {'name': []}
|
||
for record in self:
|
||
if tool_state_dict[record.id] != '2' and record.tool_state == '2':
|
||
data['name'].append(record.production_id.programming_no)
|
||
|
||
if data['name']:
|
||
# 请求cloud接口,发送微信消息推送
|
||
configsettings = self.env['res.config.settings'].get_values()
|
||
config_header = Common.get_headers(self, configsettings['token'], configsettings['sf_secret_key'])
|
||
url = '/api/message/invalid_tool_state'
|
||
config_url = configsettings['sf_url'] + url
|
||
data['token'] = configsettings['token']
|
||
ret = requests.post(config_url, json=data, headers=config_header)
|
||
ret = ret.json()
|
||
_logger.info('无效用刀异常消息推送接口:%s' % ret)
|
||
return res
|
||
|
||
def request_url(self):
|
||
url = self.env['ir.config_parameter'].get_param('web.base.url')
|
||
action_id = self.env.ref('sf_manufacturing.mrp_workorder_action_tablet').id
|
||
menu_id = self.env['ir.model.data'].search([('name', '=', 'module_stock_dropshipping')]).id
|
||
# 查询参数
|
||
params = {'menu_id': menu_id, 'action': action_id, 'model': 'mrp.workorder',
|
||
'view_type': 'list', 'active_id': 1}
|
||
# 拼接查询参数
|
||
query_string = urlencode(params)
|
||
# 拼接URL
|
||
full_url = url + "web#" + query_string
|
||
return full_url
|