坯料的采购订单零件图号,零件名称显示

This commit is contained in:
liaodanlong
2025-02-26 09:37:56 +08:00
parent c400553294
commit c4c6386871
3 changed files with 69 additions and 54 deletions

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import re
from collections import defaultdict
from odoo import api, fields, models, _
@@ -109,15 +109,37 @@ class PurchaseOrder(models.Model):
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
part_number = fields.Char('零件图号', related='product_id.part_number', readonly=True)
part_number = fields.Char('零件图号', store=True, compute='_compute_origin_sale_id')
part_name = fields.Char('零件图号', store=True,
compute='_compute_origin_sale_id')
related_product = fields.Many2one('product.product', string='关联产品',
help='经此产品工艺加工成的成品')
# @api.depends('order_id.origin')
# def _compute_related_product(self):
# for record in self:
# if record.product_id.detailed_type:
# production_id = self.env['mrp.production'].search([('name', '=', record.order_id.origin)])
# record.related_product = production_id.product_id if production_id else False
# else:
# record.related_product = False
@api.depends('product_id')
def _compute_related_product(self):
for record in self:
if record.product_id.categ_id.name == '坯料':
product_name = ''
match = re.search(r'(S\d{5}-\d)', record.product_id.name)
# 如果匹配成功,提取结果
if match:
product_name = match.group(0)
sale_order_name = ''
match_sale = re.search(r'(S\d+)', record.product_id.name)
if match_sale:
sale_order_name = match_sale.group(0)
sale_order = self.env['sale.order'].sudo().search(
[('name', '=', sale_order_name)])
if sale_order:
filtered_order_line = sale_order.order_line.filtered(
lambda order_line: product_name in order_line.product_id.name
)
record.part_number = filtered_order_line.product_id.part_number if filtered_order_line else None
record.part_name = filtered_order_line.product_id.part_name if filtered_order_line else None
else:
record.part_number = record.product_id.part_number
record.part_name = record.product_id.part_name
# if record.product_id.detailed_type:
# production_id = self.env['mrp.production'].search([('name', '=', record.order_id.origin)])
# record.related_product = production_id.product_id if production_id else False
# else:
# record.related_product = False