import logging import re from odoo import models, fields, api, _ from urllib.parse import urlencode class SFMessageStockPicking(models.Model): _name = 'stock.picking' _description = "库存调拨" _inherit = ['stock.picking', 'jikimo.message.dispatch'] quality_check_ids = fields.One2many('quality.check', 'picking_id', '质量检测单') @api.model_create_multi def create(self, vals): result = super(SFMessageStockPicking, self).create(vals) try: for obj in result: if obj.location_id.name == '进货' and obj.location_dest_id.name == '刀具房': obj.add_queue('调拨入库') except Exception as e: logging.info('add_queue调拨入库 error:%s' % e) return result @api.depends('move_type', 'immediate_transfer', 'move_ids.state', 'move_ids.picking_id', 'quality_check_ids.quality_state') def _compute_state(self): super(SFMessageStockPicking, self)._compute_state() try: for record in self: if (record.state == 'assigned' and record.picking_type_id.sequence_code == 'PC' and record.product_id.categ_id.type == '坯料'): record.add_queue('坯料发料提醒') if record.picking_type_id.sequence_code == 'SFP' and record.state == 'done': stock_picking_sfp = record.env['stock.picking'].search( [('origin', '=', record.origin), ('state', '!=', 'done'), ('picking_type_id.sequence_code', '=', 'SFP')]) if not stock_picking_sfp: stock_picking_send = self.env["jikimo.message.queue"].sudo().search( [('res_id', '=', record.id)]) if not stock_picking_send: record.add_queue('订单发货提醒') if record.picking_type_id.sequence_code == 'OCOUT' and record.state == 'assigned': mrp_production = self.env['mrp.production'].sudo().search([('name', '=', record.origin)]) production_list = self.env['mrp.production'].sudo().search( [('product_id', '=', mrp_production.product_id.id)]) manufacturing_order_names = production_list.mapped('name') stock_picking_list = self.env['stock.picking'].sudo().search( [('origin', 'in', manufacturing_order_names), ('picking_type_id.sequence_code', '=', 'OCOUT')]) all_ready_or_done = all(picking.state in ['assigned', 'done'] for picking in stock_picking_list) if all_ready_or_done: mrp_production.add_queue('工序外协发料通知') if all(qc.quality_state in ['pass', 'fail'] for qc in record.quality_check_ids): record.add_queue('调拨单质检完成提醒') except Exception as e: logging.info('add_queue_compute_state error:%s' % e) def deal_stock_picking_sfp(self, message_queue_id): # 处理订单发货提醒 content = None stock_picking = self.env['stock.picking'].sudo().search([('id', '=', int(message_queue_id.res_id))]) stock_picking_out = self.env['stock.picking'].sudo().search( [('origin', '=', stock_picking.origin), ('state', '=', 'assigned'), ('picking_type_id.sequence_code', '=', 'OUT')]) if stock_picking_out and len(stock_picking_out) > 0: content = message_queue_id.message_template_id.content url = self.request_url1(stock_picking_out.id) content = content.replace('{{name}}', stock_picking_out.name).replace( '{{sale_order_name}}', stock_picking_out.origin).replace('{{request_url}}', url) logging.info('订单发货提醒: %s' % content) return content 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 stock_picking_line = self.env['stock.picking'].sudo().search( [('id', '=', int(message_queue_id.res_id))]) url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') action_id = self.env.ref('stock.action_picking_tree_ready').id menu_id = self.env.ref('stock.menu_stock_root').id url_with_id = f"{url}/web#view_type=form&action={action_id}&menu_id={menu_id}&id={stock_picking_line.id}" content = content.replace('{{name}}', stock_picking_line.name).replace( '{{request_url}}', url_with_id) contents.append(content) elif message_queue_id.message_template_id.name == '订单发货提醒': content = self.deal_stock_picking_sfp(message_queue_id) if content: contents.append(content) elif message_queue_id.message_template_id.name == '调拨单质检完成提醒': content = message_queue_id.message_template_id.content content = content.replace('{{picking_type_name}}', self.picking_type_id.name).replace( '{{name}}', self.name) contents.append(content) return contents, message_queue_ids def get_special_url(self, id, tmplate_name, special_name, model_id): menu_id = 0 action_id = 0 if tmplate_name == '调拨入库' and special_name == 'transfer_inventory_special_url': menu_id = self.env.ref('stock.menu_stock_root').id action_id = self.env.ref('stock.action_picking_tree_ready').id return super(SFMessageStockPicking, self).get_url(id, menu_id, action_id, model_id) else: return super(SFMessageStockPicking, self).get_special_url(id, tmplate_name, special_name, model_id) def request_url1(self, id): url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') action_id = self.env.ref('stock.action_picking_tree_all').id menu_id = self.env['ir.model.data'].sudo().search([('name', '=', 'module_theme_treehouse')]).id # 查询参数 params = {'id': id, 'menu_id': menu_id, 'action': action_id, 'model': 'stock.picking', 'view_type': 'form'} # 拼接查询参数 query_string = urlencode(params) # 拼接URL full_url = url + "/web#" + query_string return full_url