import logging import re from odoo import models, fields, api, _ from urllib.parse import urlencode class SFMessageMrpProduction(models.Model): _name = 'mrp.production' _description = "制造订单" _inherit = ['mrp.production', 'jikimo.message.dispatch'] @api.depends( 'move_raw_ids.state', 'move_raw_ids.quantity_done', 'move_finished_ids.state', 'workorder_ids.state', 'product_qty', 'qty_producing') def _compute_state(self): super(SFMessageMrpProduction, self)._compute_state() for record in self: if record.state in ['scrap', 'done']: # 查询制造订单下的所有未完成的生产订单 mrp_production = record.env['mrp.production'].search( [('origin', '=', record.origin), ('state', 'not in', ['scrap', 'done'])]) if not mrp_production: mrp_production_queue = self.env["jikimo.message.queue"].search([('res_id', '=', record.id)]) if not mrp_production_queue: record.add_queue('生产完工入库提醒') # 获取发送消息内容 def _get_message(self, message_queue_ids): contents = [] 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_production = self.env['mrp.production'].search([('id', '=', int(message_queue_id.res_id))]) if mrp_production and len(mrp_production) > 0: stock_picking_sfp = self.env['stock.picking'].search( [('origin', '=', mrp_production.origin), ('picking_type_id.sequence_code', '=', 'SFP'), ('state', '=', 'assigned')], limit=1) if stock_picking_sfp: url = self.request_url(stock_picking_sfp.id) content = content.replace('{{name}}', stock_picking_sfp.name).replace( '{{sale_order_name}}', mrp_production.origin).replace('{{request_url}}', url) contents.append(content) logging.info('生产完工入库提醒: %s' % contents) return contents def request_url(self, id): url = self.env['ir.config_parameter'].get_param('web.base.url') action_id = self.env.ref('stock.action_picking_tree_all').id menu_id = self.env['ir.model.data'].search([('name', '=', 'module_theme_treehouse')]).id # 查询参数 params = {'id': id, 'menu_id': menu_id, 'action': action_id, 'model': 'mrp.production', 'view_type': 'form'} # 拼接查询参数 query_string = urlencode(params) # 拼接URL full_url = url + "/web#" + query_string return full_url