bom追溯方式

This commit is contained in:
陈赓
2025-07-16 09:12:31 +08:00
parent 556b9fdfbf
commit 344b79d76b

View File

@@ -7,7 +7,7 @@ from odoo.tools import float_compare
from datetime import datetime, timedelta from datetime import datetime, timedelta
from odoo.exceptions import UserError from odoo.exceptions import UserError
import re import re
#本地环境问题 不加报错
class ProductCategory(models.Model): class ProductCategory(models.Model):
_inherit = 'product.category' _inherit = 'product.category'
@@ -710,21 +710,30 @@ class SfProductionDemandPlan(models.Model):
self.action_confirm() self.action_confirm()
def action_confirm(self): def action_confirm(self):
self = self.with_context( """
demand_plan_line_id=self.id 确认需求计划行,创建 BOM、触发库存规则并更新状态。
) """
# 将当前需求计划行 ID 写入上下文,便于后续方法使用
self = self.with_context(demand_plan_line_id=self.id)
# 创建物料清单BOM根据供货方式进行不同的处理
self.mrp_bom_create() self.mrp_bom_create()
# 启动库存规则(创建采购、生产等)
self._action_launch_stock_rule() self._action_launch_stock_rule()
# 根据供货方式设置状态字段
if self.supply_method in ('automation', 'manual'): if self.supply_method in ('automation', 'manual'):
self.write({'status': '50'}) self.write({'status': '50'}) # 自动/手工 供货:待排产
self.update_sale_order_state() self.update_sale_order_state()
else: else:
self.write({'status': '60'}) self.write({'status': '60'}) # 外购/外协/客户自供:无需排产
self.update_sale_order_state() self.update_sale_order_state()
def _get_embryo_template_by_supply_method(self): def _get_embryo_template_by_supply_method(self):
""" """
根据供货方式获取对应的胚料模板供mrp_bom_create调用 根据供货方式返回对应的胚料模板 product.template 记录
""" """
supply_map = { supply_map = {
'automation': self.env.ref('sf_dlm.product_embryo_sf_self_machining').sudo(), 'automation': self.env.ref('sf_dlm.product_embryo_sf_self_machining').sudo(),
@@ -738,7 +747,12 @@ def _get_embryo_template_by_supply_method(self):
raise UserError(f"未配置供货方式 {self.supply_method} 对应的胚料模板") raise UserError(f"未配置供货方式 {self.supply_method} 对应的胚料模板")
return template return template
def mrp_bom_create(self): def mrp_bom_create(self):
"""
创建 BOM包含胚料与成品 BOM用于后续生产或采购流程。
"""
# 如果同一计划中已有对应的 BOM 可复用,则直接使用
if self.supply_method in ('automation', 'manual'): if self.supply_method in ('automation', 'manual'):
line_ids = self.demand_plan_id.line_ids.filtered( line_ids = self.demand_plan_id.line_ids.filtered(
lambda p: p.supply_method in ('automation', 'manual') and p.status in ('50', '60')) lambda p: p.supply_method in ('automation', 'manual') and p.status in ('50', '60'))
@@ -752,6 +766,7 @@ def mrp_bom_create(self):
self.bom_id = line_ids[0].bom_id.id self.bom_id = line_ids[0].bom_id.id
return return
# 根据供货方式选择模板和 BOM 类型
bom_type = '' bom_type = ''
if self.supply_method == 'automation': if self.supply_method == 'automation':
bom_type = 'normal' bom_type = 'normal'
@@ -765,12 +780,17 @@ def mrp_bom_create(self):
bom_type = 'normal' bom_type = 'normal'
product_template_id = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_manual_processing').sudo() product_template_id = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_manual_processing').sudo()
# 使用模板复制内容到当前产品
self.product_id.product_tmpl_id.copy_template(product_template_id) self.product_id.product_tmpl_id.copy_template(product_template_id)
# 构造 BOM 编码(包含时间戳)
future_time = datetime.now() + timedelta(hours=8) future_time = datetime.now() + timedelta(hours=8)
code = f"{self.product_id.default_code}-{bom_type}-{future_time.strftime('%Y%m%d%H%M%S')}" code = f"{self.product_id.default_code}-{bom_type}-{future_time.strftime('%Y%m%d%H%M%S')}"
order_id = self.sale_order_id order_id = self.sale_order_id
product = self.product_id product = self.product_id
# 构造胚料产品的参数
item = { item = {
'texture_code': product.materials_id.materials_no, 'texture_code': product.materials_id.materials_no,
'texture_type_code': product.materials_type_id.materials_no, 'texture_type_code': product.materials_type_id.materials_no,
@@ -783,6 +803,8 @@ def mrp_bom_create(self):
'embryo_redundancy_id': self.sale_order_line_id.embryo_redundancy_id, 'embryo_redundancy_id': self.sale_order_line_id.embryo_redundancy_id,
'model_id': self.model_id 'model_id': self.model_id
} }
# 从产品名中提取编号(如 S12345-3
product_name = '' product_name = ''
match = re.search(r'(S\d{5}-\d+)', product.name) match = re.search(r'(S\d{5}-\d+)', product.name)
product_seria = 0 product_seria = 0
@@ -790,7 +812,9 @@ def mrp_bom_create(self):
product_name = match.group(0) product_name = match.group(0)
product_seria = int(product_name.split('-')[-1]) product_seria = int(product_name.split('-')[-1])
# 如果供货方式不是采购,则需要先创建胚料产品
if self.supply_method != 'purchase': if self.supply_method != 'purchase':
# 判断是否为客户自供
if self.sale_order_line_id.embryo_redundancy_id: if self.sale_order_line_id.embryo_redundancy_id:
embryo_template = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_embryo_customer_provided').sudo() embryo_template = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_embryo_customer_provided').sudo()
embryo_key = 'material_customer_provided' embryo_key = 'material_customer_provided'
@@ -798,8 +822,10 @@ def mrp_bom_create(self):
embryo_template = self._get_embryo_template_by_supply_method() embryo_template = self._get_embryo_template_by_supply_method()
embryo_key = self.supply_method embryo_key = self.supply_method
# 获取批次追踪方式
tracking_method = embryo_template.tracking tracking_method = embryo_template.tracking
# 创建胚料产品(无 BOM 产品)
embryo_product = self.env['product.template'].sudo().no_bom_product_create( embryo_product = self.env['product.template'].sudo().no_bom_product_create(
embryo_template.with_context(active_test=False).product_variant_id, embryo_template.with_context(active_test=False).product_variant_id,
item, item,
@@ -812,6 +838,7 @@ def mrp_bom_create(self):
if embryo_product == -3: if embryo_product == -3:
raise UserError('该订单模型的材料型号暂未设置获取方式和供应商,请先配置再进行分配') raise UserError('该订单模型的材料型号暂未设置获取方式和供应商,请先配置再进行分配')
# 设置胚料 BOM 类型
if embryo_key in ('automation', 'manual', 'material_customer_provided'): if embryo_key in ('automation', 'manual', 'material_customer_provided'):
embryo_bom_type = 'normal' embryo_bom_type = 'normal'
elif embryo_key == 'outsourcing': elif embryo_key == 'outsourcing':
@@ -821,6 +848,7 @@ def mrp_bom_create(self):
else: else:
embryo_bom_type = 'normal' embryo_bom_type = 'normal'
# 创建胚料 BOM 及 BOM 行
embryo_bom = self.env['mrp.bom'].with_user(self.env.ref("base.user_admin")).bom_create( embryo_bom = self.env['mrp.bom'].with_user(self.env.ref("base.user_admin")).bom_create(
embryo_product, embryo_bom_type, True, tracking=tracking_method) embryo_product, embryo_bom_type, True, tracking=tracking_method)
@@ -828,18 +856,28 @@ def mrp_bom_create(self):
if not embryo_bom_line: if not embryo_bom_line:
raise UserError('该订单模型的材料型号暂未有原材料,请先配置再进行分配') raise UserError('该订单模型的材料型号暂未有原材料,请先配置再进行分配')
# 创建成品 BOM包含胚料
product_bom = self.env['mrp.bom'].with_user(self.env.ref("base.user_admin")).bom_create( product_bom = self.env['mrp.bom'].with_user(self.env.ref("base.user_admin")).bom_create(
product, bom_type, 'product', code, tracking=tracking_method) product, bom_type, 'product', code, tracking=tracking_method)
product_bom.with_user(self.env.ref("base.user_admin")).bom_create_line_has(embryo_product) product_bom.with_user(self.env.ref("base.user_admin")).bom_create_line_has(embryo_product)
# 赋值 BOM ID
self.bom_id = product_bom.id self.bom_id = product_bom.id
def _action_launch_stock_rule(self): def _action_launch_stock_rule(self):
"""
触发库存规则(如采购、生产),并确认相关拣货单。
"""
procurements = [] procurements = []
group_id = self.sale_order_id.procurement_group_id group_id = self.sale_order_id.procurement_group_id
if not group_id: if not group_id:
# 没有分组则创建
group_id = self.env['procurement.group'].create(self._prepare_procurement_group_vals()) group_id = self.env['procurement.group'].create(self._prepare_procurement_group_vals())
self.sale_order_id.procurement_group_id = group_id self.sale_order_id.procurement_group_id = group_id
else: else:
# 若已有分组但字段有变动则更新
updated_vals = {} updated_vals = {}
if group_id.partner_id != self.sale_order_id.partner_shipping_id: if group_id.partner_id != self.sale_order_id.partner_shipping_id:
updated_vals.update({'partner_id': self.sale_order_id.partner_shipping_id.id}) updated_vals.update({'partner_id': self.sale_order_id.partner_shipping_id.id})
@@ -847,27 +885,42 @@ def _action_launch_stock_rule(self):
updated_vals.update({'move_type': self.sale_order_id.picking_policy}) updated_vals.update({'move_type': self.sale_order_id.picking_policy})
if updated_vals: if updated_vals:
group_id.write(updated_vals) group_id.write(updated_vals)
# 构造 procurement 所需的字段
values = self._prepare_procurement_values(group_id=group_id) values = self._prepare_procurement_values(group_id=group_id)
# 单位换算
line_uom = self.sale_order_line_id.product_uom line_uom = self.sale_order_line_id.product_uom
quant_uom = self.product_id.uom_id quant_uom = self.product_id.uom_id
plan_uom_qty, procurement_uom = line_uom._adjust_uom_quantities(self.plan_uom_qty, quant_uom) plan_uom_qty, procurement_uom = line_uom._adjust_uom_quantities(self.plan_uom_qty, quant_uom)
# 创建 procurement 请求
procurements.append(self.env['procurement.group'].Procurement( procurements.append(self.env['procurement.group'].Procurement(
self.product_id, plan_uom_qty, procurement_uom, self.product_id, plan_uom_qty, procurement_uom,
self.sale_order_id.partner_shipping_id.property_stock_customer, self.sale_order_id.partner_shipping_id.property_stock_customer,
self.product_id.display_name, self.sale_order_id.name, self.sale_order_id.company_id, values)) self.product_id.display_name, self.sale_order_id.name, self.sale_order_id.company_id, values))
# 执行调度
if procurements: if procurements:
procurement_group = self.env['procurement.group'] procurement_group = self.env['procurement.group']
if self.env.context.get('import_file'): if self.env.context.get('import_file'):
procurement_group = procurement_group.with_context(import_file=False) procurement_group = procurement_group.with_context(import_file=False)
procurement_group.run(procurements) procurement_group.run(procurements)
# 确认相关的拣货单
orders = self.mapped('sale_order_id') orders = self.mapped('sale_order_id')
for order in orders: for order in orders:
pickings_to_confirm = order.picking_ids.filtered(lambda p: p.state not in ['cancel', 'done']) pickings_to_confirm = order.picking_ids.filtered(lambda p: p.state not in ['cancel', 'done'])
if pickings_to_confirm: if pickings_to_confirm:
pickings_to_confirm.action_confirm() pickings_to_confirm.action_confirm()
return True return True
def _prepare_procurement_group_vals(self): def _prepare_procurement_group_vals(self):
"""
构造创建 procurement group 所需的字段。
"""
return { return {
'name': self.sale_order_id.name, 'name': self.sale_order_id.name,
'move_type': self.sale_order_id.picking_policy, 'move_type': self.sale_order_id.picking_policy,
@@ -875,11 +928,18 @@ def _prepare_procurement_group_vals(self):
'partner_id': self.sale_order_id.partner_shipping_id.id, 'partner_id': self.sale_order_id.partner_shipping_id.id,
} }
def _prepare_procurement_values(self, group_id=False): def _prepare_procurement_values(self, group_id=False):
"""
构造单个 procurement 请求所需的字段字典。
"""
self.ensure_one() self.ensure_one()
# 交货日期与计划日期
date_deadline = self.sale_order_id.commitment_date or ( date_deadline = self.sale_order_id.commitment_date or (
self.sale_order_id.date_order + timedelta(days=self.sale_order_line_id.customer_lead or 0.0)) self.sale_order_id.date_order + timedelta(days=self.sale_order_line_id.customer_lead or 0.0))
date_planned = date_deadline - timedelta(days=self.sale_order_id.company_id.security_lead) date_planned = date_deadline - timedelta(days=self.sale_order_id.company_id.security_lead)
values = { values = {
'group_id': group_id, 'group_id': group_id,
'sale_line_id': self.sale_order_line_id.id, 'sale_line_id': self.sale_order_line_id.id,
@@ -895,4 +955,6 @@ def _prepare_procurement_values(self, group_id=False):
'sequence': self.sale_order_line_id.sequence, 'sequence': self.sale_order_line_id.sequence,
'demand_plan_line_id': self.id 'demand_plan_line_id': self.id
} }
return values return values