diff --git a/sf_manufacturing/models/mrp_production.py b/sf_manufacturing/models/mrp_production.py index f11a32ac..4fc7fde0 100644 --- a/sf_manufacturing/models/mrp_production.py +++ b/sf_manufacturing/models/mrp_production.py @@ -8,6 +8,9 @@ import re import requests from itertools import groupby from collections import defaultdict, namedtuple + +from loguru import _logger + from odoo import api, fields, models, SUPERUSER_ID, _ from odoo.exceptions import UserError, ValidationError from odoo.addons.sf_base.commons.common import Common @@ -18,6 +21,7 @@ class MrpProduction(models.Model): _inherit = 'mrp.production' _description = "制造订单" _order = 'create_date desc' + sale_order_id = fields.Many2one('sale.order',string='销售订单', compute='_compute_sale_order_id', store=True) deadline_of_delivery = fields.Date('订单交期', tracking=True, compute='_compute_deadline_of_delivery') # tray_ids = fields.One2many('sf.tray', 'production_id', string="托盘") maintenance_count = fields.Integer(compute='_compute_maintenance_count', string="Number of maintenance requests") @@ -34,6 +38,28 @@ class MrpProduction(models.Model): tool_state_remark = fields.Text(string='功能刀具状态备注(缺刀)', compute='_compute_tool_state_remark', store=True) tool_state_remark2 = fields.Text(string='功能刀具状态备注(无效刀)', readonly=True) + @api.depends('procurement_group_id.mrp_production_ids.move_dest_ids.group_id.sale_id') + def _compute_sale_order_id(self): + for production in self: + # 初始化 sale_order_id 为 False + sale_order_id = False + # 使用正则表达式查找产品名称中的 'S' 开头的字母数字字符串 + match = re.search(r'S\d+', production.product_id.with_context(lang='zh_CN').name) # 从字符串开始匹配 + + if match: + result = match.group(0) + try: + # 查找与匹配的字符串相符的销售订单 + sale_order = self.env['sale.order'].search( + [('name', '=', result)], limit=1, order='id asc' + ) + if sale_order: + production.sale_order_id = sale_order.id + else: + _logger.warning("No sale order found for production %s with product %s (name match: %s)", + production.id, production.product_id.name, result) + except Exception as e: + _logger.error("Error while fetching sale order for production %s: %s", production.id, str(e)) @api.depends('procurement_group_id.mrp_production_ids.move_dest_ids.group_id.sale_id') def _compute_deadline_of_delivery(self): for production in self: @@ -1435,6 +1461,26 @@ class MrpProduction(models.Model): for production in self: production.production_type = '自动化产线加工' if not production.product_id.is_manual_processing else '人工线下加工' + @api.depends('procurement_group_id.mrp_production_ids.move_dest_ids.group_id.sale_id') + def _compute_sale_order_count(self): + for production in self: + if production.sale_order_id: + production.sale_order_count = 1 + else: + production.sale_order_count = 0 + def action_view_sale_orders(self): + if self.sale_order_id: + action = { + 'res_model': 'sale.order', + 'type': 'ir.actions.act_window', + } + action.update({ + 'view_mode': 'form', + 'res_id': self.sale_order_id.id, + }) + return action + + @api.model_create_multi def create(self, vals_list): """ diff --git a/sf_manufacturing/views/mrp_production_addional_change.xml b/sf_manufacturing/views/mrp_production_addional_change.xml index fd0a5ead..043990ef 100644 --- a/sf_manufacturing/views/mrp_production_addional_change.xml +++ b/sf_manufacturing/views/mrp_production_addional_change.xml @@ -43,6 +43,7 @@ + @@ -123,6 +124,7 @@ + @@ -584,6 +586,7 @@ +