Compare commits

..

2 Commits

Author SHA1 Message Date
陈赓
344b79d76b bom追溯方式 2025-07-16 09:12:31 +08:00
陈赓
556b9fdfbf 修改bom追溯方式 2025-07-15 16:48:19 +08:00
11 changed files with 286 additions and 186 deletions

View File

@@ -42,7 +42,6 @@ class MrsMaterialModel(models.Model):
materials_num = fields.Char("编码号")
name = fields.Char('型号名')
need_h = fields.Boolean("热处理", default="false")
need_m = fields.Boolean("是否磁吸", default="false")
mf_materia_post = fields.Char("热处理后密度")
density = fields.Float("密度(kg/m³)")
materials_id = fields.Many2one('sf.production.materials', "材料名")

View File

@@ -263,7 +263,6 @@
<field name="materials_no" readonly="1" force_save="1"/>
<field name="gain_way" required="0"/>
<field name="density" readonly="1" required="1" class="custom_required"/>
<field name="need_m" default="false" readonly="1"/>
</group>
<group>
<field name="rough_machining" required="1"/>
@@ -307,7 +306,6 @@
<field name="tensile_strength"/>
<field name="hardness" optional="show"/>
<field name="need_h"/>
<field name="need_m"/>
<field name="apply" widget="many2many_tags" optional="show"/>
<field name="density" optional="show"/>
<field name="rough_machining" optional="hide"/>
@@ -354,7 +352,6 @@
<field name="materials_no"/>
<field name="name"/>
<field name="need_h"/>
<field name="need_m"/>
<field name="mf_materia_post"/>
<field name="density"/>
<field name='materials_id' default="default" invisible="1"/>

View File

@@ -10,7 +10,7 @@
""",
'category': 'sf',
'website': 'https://www.sf.jikimo.com',
'depends': ['sf_plan','jikimo_printing'],
'depends': ['sf_plan'], #'jikimo_printing',
'data': [
'security/ir.model.access.csv',
'data/stock_route_group.xml',

View File

@@ -7,7 +7,29 @@ from odoo.tools import float_compare
from datetime import datetime, timedelta
from odoo.exceptions import UserError
import re
#本地环境问题 不加报错
class ProductCategory(models.Model):
_inherit = 'product.category'
negative_inventory_allowed = fields.Boolean(string="允许负库存", default=False)
class StockPicking(models.Model):
_inherit = 'stock.picking'
whether_show_quality_check = fields.Boolean(string="是否显示质检")
class ProductTemplate(models.Model):
_inherit = 'product.template'
blank_type = fields.Selection([
('圆料', '圆料'),
('方料', '方料'),
], string='坯料分类')
blank_precision = fields.Selection([
('精坯', '精坯'),
('粗坯', '粗坯'),
], string='坯料类型')
class SfProductionDemandPlan(models.Model):
_name = 'sf.production.demand.plan'
@@ -688,19 +710,49 @@ class SfProductionDemandPlan(models.Model):
self.action_confirm()
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._action_launch_stock_rule()
# 根据供货方式设置状态字段
if self.supply_method in ('automation', 'manual'):
self.write({'status': '50'})
self.write({'status': '50'}) # 自动/手工 供货:待排产
self.update_sale_order_state()
else:
self.write({'status': '60'})
self.write({'status': '60'}) # 外购/外协/客户自供:无需排产
self.update_sale_order_state()
def _get_embryo_template_by_supply_method(self):
"""
根据供货方式返回对应的胚料模板 product.template 记录。
"""
supply_map = {
'automation': self.env.ref('sf_dlm.product_embryo_sf_self_machining').sudo(),
'outsourcing': self.env.ref('sf_dlm.product_embryo_sf_outsource').sudo(),
'purchase': self.env.ref('sf_dlm.product_embryo_sf_purchase').sudo(),
'manual': self.env.ref('jikimo_sale_multiple_supply_methods.product_template_manual_processing').sudo(),
'material_customer_provided': self.env.ref('jikimo_sale_multiple_supply_methods.product_template_embryo_customer_provided').sudo(),
}
template = supply_map.get(self.supply_method)
if not template:
raise UserError(f"未配置供货方式 {self.supply_method} 对应的胚料模板")
return template
def mrp_bom_create(self):
"""
创建 BOM包含胚料与成品 BOM用于后续生产或采购流程。
"""
# 如果同一计划中已有对应的 BOM 可复用,则直接使用
if self.supply_method in ('automation', 'manual'):
line_ids = self.demand_plan_id.line_ids.filtered(
lambda p: p.supply_method in ('automation', 'manual') and p.status in ('50', '60'))
@@ -713,32 +765,32 @@ class SfProductionDemandPlan(models.Model):
if line_ids:
self.bom_id = line_ids[0].bom_id.id
return
# 根据供货方式选择模板和 BOM 类型
bom_type = ''
# 根据供货方式修改成品模板
if self.supply_method == 'automation':
bom_type = 'normal'
product_template_id = self.env.ref('sf_dlm.product_template_sf').sudo().product_tmpl_id
elif self.supply_method == 'outsourcing':
bom_type = 'subcontract'
product_template_id = self.env.ref(
'jikimo_sale_multiple_supply_methods.product_template_outsourcing').sudo()
product_template_id = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_outsourcing').sudo()
elif self.supply_method == 'purchase':
product_template_id = self.env.ref(
'jikimo_sale_multiple_supply_methods.product_template_purchase').sudo()
product_template_id = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_purchase').sudo()
elif self.supply_method == 'manual':
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)
# 构造 BOM 编码(包含时间戳)
future_time = datetime.now() + timedelta(hours=8)
# 生成BOM单据编码
code = f"{self.product_id.default_code}-{bom_type}-{future_time.strftime('%Y%m%d%H%M%S')}"
order_id = self.sale_order_id
product = self.product_id
# 拼接方法需要的item结构成品的模型数据信息就是坯料的数据信息
# 构造胚料产品的参数
item = {
'texture_code': product.materials_id.materials_no,
'texture_type_code': product.materials_type_id.materials_no,
@@ -751,110 +803,81 @@ class SfProductionDemandPlan(models.Model):
'embryo_redundancy_id': self.sale_order_line_id.embryo_redundancy_id,
'model_id': self.model_id
}
# 从产品名中提取编号(如 S12345-3
product_name = ''
match = re.search(r'(S\d{5}-\d+)', product.name)
product_seria = 0
# 如果匹配成功,提取结果
if match:
product_name = match.group(0)
# 获取成品名结尾-n的n
product_seria = int(product_name.split('-')[-1])
# 成品供货方式为采购则不生成bom
# 如果供货方式不是采购,则需要先创建胚料产品
if self.supply_method != 'purchase':
# 当成品上带有客供料选项时,生成坯料时选择“客供料”路线
# 判断是否为客户自供
if self.sale_order_line_id.embryo_redundancy_id:
# 将成品模板的内容复制到成品上
customer_provided_embryo = self.env.ref(
'jikimo_sale_multiple_supply_methods.product_template_embryo_customer_provided').sudo()
# 创建坯料客供料的批量不需要创建bom
material_customer_provided_embryo = self.env['product.template'].sudo().no_bom_product_create(
customer_provided_embryo.with_context(active_test=False).product_variant_id,
item,
order_id, 'material_customer_provided', product_seria, product)
# 成品配置bom
product_bom_material_customer_provided = self.env['mrp.bom'].with_user(
self.env.ref("base.user_admin")).bom_create(
product, bom_type, 'product', code)
product_bom_material_customer_provided.with_user(
self.env.ref("base.user_admin")).bom_create_line_has(
material_customer_provided_embryo)
self.bom_id = product_bom_material_customer_provided.id
elif self.product_id.materials_type_id.gain_way == '自加工':
self_machining_id = self.env.ref('sf_dlm.product_embryo_sf_self_machining').sudo()
# 创建坯料
self_machining_embryo = self.env['product.template'].sudo().no_bom_product_create(
self_machining_id,
item,
order_id, 'self_machining', product_seria, product)
# 创建坯料的bom
self_machining_bom = self.env['mrp.bom'].with_user(
self.env.ref("base.user_admin")).bom_create(
self_machining_embryo, 'normal', False)
# 创建坯料里bom的组件
self_machining_bom_line = self_machining_bom.with_user(
self.env.ref("base.user_admin")).bom_create_line(
self_machining_embryo)
if not self_machining_bom_line:
raise UserError('该订单模型的材料型号暂未有原材料,请先配置再进行分配')
# 产品配置bom
product_bom_self_machining = self.env['mrp.bom'].with_user(
self.env.ref("base.user_admin")).bom_create(
product, bom_type, 'product', code)
product_bom_self_machining.with_user(self.env.ref("base.user_admin")).bom_create_line_has(
self_machining_embryo)
self.bom_id = product_bom_self_machining.id
elif self.product_id.materials_type_id.gain_way == '外协':
outsource_id = self.env.ref('sf_dlm.product_embryo_sf_outsource').sudo()
# 创建坯料
outsource_embryo = self.env['product.template'].sudo().no_bom_product_create(outsource_id,
item,
order_id,
'subcontract',
product_seria,
product)
if outsource_embryo == -3:
raise UserError('该订单模型的材料型号暂未设置获取方式和供应商,请先配置再进行分配')
# 创建坯料的bom
outsource_bom = self.env['mrp.bom'].with_user(self.env.ref("base.user_admin")).bom_create(
outsource_embryo,
'subcontract', True)
# 创建坯料的bom的组件
outsource_bom_line = outsource_bom.with_user(
self.env.ref("base.user_admin")).bom_create_line(outsource_embryo)
if not outsource_bom_line:
raise UserError('该订单模型的材料型号暂未有原材料,请先配置再进行分配')
# 产品配置bom
product_bom_outsource = self.env['mrp.bom'].with_user(
self.env.ref("base.user_admin")).bom_create(product, bom_type, 'product', code)
product_bom_outsource.with_user(self.env.ref("base.user_admin")).bom_create_line_has(
outsource_embryo)
self.bom_id = product_bom_outsource.id
elif self.product_id.materials_type_id.gain_way == '采购':
purchase_id = self.env.ref('sf_dlm.product_embryo_sf_purchase').sudo()
purchase_embryo = self.env['product.template'].sudo().no_bom_product_create(purchase_id,
item,
order_id,
'purchase',
product_seria,
product)
if purchase_embryo and purchase_embryo == -3:
raise UserError('该订单模型的材料型号暂未设置获取方式和供应商,请先配置再进行分配')
else:
# 产品配置bom
product_bom_purchase = self.env['mrp.bom'].with_user(
self.env.ref("base.user_admin")).bom_create(product, bom_type, 'product', code)
product_bom_purchase.with_user(self.env.ref("base.user_admin")).bom_create_line_has(
purchase_embryo)
self.bom_id = product_bom_purchase.id
embryo_template = self.env.ref('jikimo_sale_multiple_supply_methods.product_template_embryo_customer_provided').sudo()
embryo_key = 'material_customer_provided'
else:
embryo_template = self._get_embryo_template_by_supply_method()
embryo_key = self.supply_method
# 获取批次追踪方式
tracking_method = embryo_template.tracking
# 创建胚料产品(无 BOM 产品)
embryo_product = self.env['product.template'].sudo().no_bom_product_create(
embryo_template.with_context(active_test=False).product_variant_id,
item,
order_id,
embryo_key,
product_seria,
product
)
if embryo_product == -3:
raise UserError('该订单模型的材料型号暂未设置获取方式和供应商,请先配置再进行分配')
# 设置胚料 BOM 类型
if embryo_key in ('automation', 'manual', 'material_customer_provided'):
embryo_bom_type = 'normal'
elif embryo_key == 'outsourcing':
embryo_bom_type = 'subcontract'
elif embryo_key == 'purchase':
embryo_bom_type = 'purchase'
else:
embryo_bom_type = 'normal'
# 创建胚料 BOM 及 BOM 行
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_bom_line = embryo_bom.with_user(self.env.ref("base.user_admin")).bom_create_line(embryo_product)
if not embryo_bom_line:
raise UserError('该订单模型的材料型号暂未有原材料,请先配置再进行分配')
# 创建成品 BOM包含胚料
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.with_user(self.env.ref("base.user_admin")).bom_create_line_has(embryo_product)
# 赋值 BOM ID
self.bom_id = product_bom.id
def _action_launch_stock_rule(self):
"""
触发库存规则(如采购、生产),并确认相关拣货单。
"""
procurements = []
group_id = self.sale_order_id.procurement_group_id
if not group_id:
# 没有分组则创建
group_id = self.env['procurement.group'].create(self._prepare_procurement_group_vals())
self.sale_order_id.procurement_group_id = group_id
else:
# 若已有分组但字段有变动则更新
updated_vals = {}
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})
@@ -862,27 +885,42 @@ class SfProductionDemandPlan(models.Model):
updated_vals.update({'move_type': self.sale_order_id.picking_policy})
if updated_vals:
group_id.write(updated_vals)
# 构造 procurement 所需的字段
values = self._prepare_procurement_values(group_id=group_id)
# 单位换算
line_uom = self.sale_order_line_id.product_uom
quant_uom = self.product_id.uom_id
plan_uom_qty, procurement_uom = line_uom._adjust_uom_quantities(self.plan_uom_qty, quant_uom)
# 创建 procurement 请求
procurements.append(self.env['procurement.group'].Procurement(
self.product_id, plan_uom_qty, procurement_uom,
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))
# 执行调度
if procurements:
procurement_group = self.env['procurement.group']
if self.env.context.get('import_file'):
procurement_group = procurement_group.with_context(import_file=False)
procurement_group.run(procurements)
# 确认相关的拣货单
orders = self.mapped('sale_order_id')
for order in orders:
pickings_to_confirm = order.picking_ids.filtered(lambda p: p.state not in ['cancel', 'done'])
if pickings_to_confirm:
pickings_to_confirm.action_confirm()
return True
def _prepare_procurement_group_vals(self):
"""
构造创建 procurement group 所需的字段。
"""
return {
'name': self.sale_order_id.name,
'move_type': self.sale_order_id.picking_policy,
@@ -890,11 +928,18 @@ class SfProductionDemandPlan(models.Model):
'partner_id': self.sale_order_id.partner_shipping_id.id,
}
def _prepare_procurement_values(self, group_id=False):
"""
构造单个 procurement 请求所需的字段字典。
"""
self.ensure_one()
# 交货日期与计划日期
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)
values = {
'group_id': group_id,
'sale_line_id': self.sale_order_line_id.id,
@@ -910,4 +955,6 @@ class SfProductionDemandPlan(models.Model):
'sequence': self.sale_order_line_id.sequence,
'demand_plan_line_id': self.id
}
return values

View File

@@ -1 +1,2 @@
from . import controllers
from . import sync_controller

View File

@@ -0,0 +1,22 @@
# study/jikimo_sf/sf_mrs_connect/controllers/sync_controller.py
from odoo import http
from odoo.http import request
class FixtureSyncController(http.Controller):
@http.route('/api/fixture_model/sync_from_mrs', type='json', auth='none', csrf=False)
def sync_model(self, **kw):
code = kw.get('code')
if not code:
return {'status':'fail','msg':'code missing'}
request.env['sf.fixture.model'].sudo().sync_from_mrs(code)
return {'status':'success'}
@http.route('/api/fixture_param/sync_from_mrs', type='json', auth='none', csrf=False)
def sync_param(self, **kw):
code = kw.get('code')
if not code:
return {'status':'fail','msg':'code missing'}
request.env['sf.fixture.materials.basic.parameters']\
.sudo().sync_from_mrs(code)
return {'status':'success'}

View File

@@ -1,4 +1,4 @@
from . import ftp_operate
from . import res_config_setting
from . import sync_common
from . import order_price
from . import order_price

View File

@@ -0,0 +1,14 @@
# study/jikimo_sf/sf_mrs_connect/models/common.py
import time, hashlib
class Common:
@staticmethod
def get_headers(token, secret_key):
ts = str(int(time.time()))
sign = hashlib.sha256(f"{token}{secret_key}{ts}".encode()).hexdigest()
return {
"token": token,
"sign": sign,
"timestamp": ts,
"Content-Type": "application/json",
}

View File

@@ -0,0 +1,30 @@
# study/jikimo_sf/sf_mrs_connect/models/redis_utils.py
import redis, json, logging
_logger = logging.getLogger(__name__)
class RedisClient:
def __init__(self, host='localhost', port=6379, db=0):
try:
self.client = redis.Redis(host=host, port=port, db=db, decode_responses=True)
except Exception as e:
_logger.error(f"Redis init error: {e}")
self.client = None
def get_json(self, key):
if not self.client:
return None
try:
data = self.client.get(key)
return json.loads(data) if data else None
except Exception as e:
_logger.error(f"Redis GET error [{key}]: {e}")
return None
def set_json(self, key, value, ex=3600):
if not self.client:
return
try:
self.client.set(key, json.dumps(value, ensure_ascii=False), ex=ex)
except Exception as e:
_logger.error(f"Redis SET error [{key}]: {e}")

View File

@@ -5,9 +5,11 @@ import base64
import traceback
import requests
from odoo import models
from odoo import models,api,fields
from odoo.exceptions import ValidationError
from odoo.addons.sf_base.commons.common import Common
from .redis_utils import RedisClient
from .common import Common
from odoo.addons.jikimo_sf.sf_base.commons.common import Common
_logger = logging.getLogger(__name__)
@@ -1546,7 +1548,7 @@ class SyncMulti_Mounting_Type(models.Model):
# raise ValidationError("夹具型号认证未通过")
# # 定时同步所有夹具型号列表
# def sync_all_fixture_model(self):
# def sync_all_fixture_model(self):
# config = self.env['res.config.settings'].get_values()
# headers = Common.get_headers(self, config['token'], config['sf_secret_key'])
# strUrl = config['sf_url'] + self.url
@@ -1610,6 +1612,10 @@ class SyncMulti_Mounting_Type(models.Model):
# self._write_or_create(all_list.get('adapter_board_yesterday_list'), '转接板(锁板)夹具')
# if all_list.get('scroll_chuck_all_list'):
# self._write_or_create(all_list.get('scroll_chuck_yesterday_list'), '三爪卡盘')
# if all_list.get('air_tray_all_list'):
# self._write_or_create(all_list.get('air_tray_all_list'),'气吹托盘')
# if all_list.get('magnet_tray_all_list'):
# self._write_or_create(all_list.get('magnet_tray_all_list'),'磁吸托盘')
# else:
# raise ValidationError("夹具型号基本参数认证未通过")
@@ -1634,10 +1640,14 @@ class SyncMulti_Mounting_Type(models.Model):
# self._write_or_create(all_list.get('jaw_vice_all_list'), '虎钳夹具')
# if all_list.get('magnet_fixture_all_list'):
# self._write_or_create(all_list.get('magnet_fixture_all_list'), '磁吸夹具')
# if all_list.get('adapter_board_all_list'):
# if all_list.get('adapter_board_all_list'):
# self._write_or_create(all_list.get('adapter_board_all_list'), '转接板(锁板)夹具')
# if all_list.get('scroll_chuck_all_list'):
# self._write_or_create(all_list.get('scroll_chuck_all_list'), '三爪卡盘')
# if all_list.get('air_tray_all_list'):
# self._write_or_create(all_list.get('air_tray_all_list'),'气吹托盘')
# if all_list.get('magnet_tray_all_list'):
# self._write_or_create(all_list.get('magnet_tray_all_list'),'磁吸托盘')
# else:
# raise ValidationError("夹具型号基本参数认证未通过")
@@ -3237,56 +3247,6 @@ class EmbryoRedundancySync(models.Model):
class SyncfixtureMaterialsBasicParameters(models.Model):
_inherit = 'sf.fixture.materials.basic.parameters'
_description = 'Redis 优先同步夹具基本参数'
def sync_all_fixture_materials_basic_parameters(self):
rc = RedisClient()
key = 'mrs:fixture_param_all_list'
all_list = rc.get_json(key)
# ✅ Redis 无数据时,请求 MRS 端刷新 Redis 缓存
if not all_list:
config = self.env['res.config.settings'].get_values()
headers = Common.get_headers(config['token'], config['sf_secret_key'])
refresh_url = config['mrs_url'] + '/api/refresh_redis/fixture_param'
try:
res = requests.post(refresh_url, headers=headers, json={}, timeout=10)
res.raise_for_status()
except Exception as e:
raise ValidationError(f"Redis 无数据MRS 缓存刷新失败: {str(e)}")
# 再次尝试从 Redis 获取
all_list = rc.get_json(key)
if not all_list:
raise ValidationError("Redis 刷新后仍无数据,无法同步夹具基本参数")
# ✅ 同步函数作为局部函数定义在方法体内部
def _sync_list(param_list, material_name):
for item in param_list or []:
if not item or not item.get('code'):
continue
record = self.search([('code', '=', item['code'])], limit=1)
vals = self._get_basic_parameters_list(item, material_name)
if record:
record.write(vals)
else:
self.create(vals)
# ✅ 各类夹具参数同步调用
_sync_list(all_list.get('zero_chuck_all_list'), '零点卡盘')
_sync_list(all_list.get('zero_tray_all_list'), '零点托盘')
_sync_list(all_list.get('pneumatic_fixture_all_list'), '气动夹具')
_sync_list(all_list.get('jaw_vice_all_list'), '虎钳夹具')
_sync_list(all_list.get('magnet_fixture_all_list'), '磁吸夹具')
_sync_list(all_list.get('adapter_board_all_list'), '转接板(锁板)夹具')
_sync_list(all_list.get('scroll_chuck_all_list'), '三爪卡盘')
_sync_list(all_list.get('air_tray_all_list'), '气吹托盘')
_sync_list(all_list.get('magnet_tray_all_list'), '磁吸托盘')
class SyncFixtureModel(models.Model):
_inherit = 'sf.fixture.model'
_description = 'Redis 优先同步夹具型号'
@@ -3295,25 +3255,9 @@ class SyncFixtureModel(models.Model):
rc = RedisClient()
key = 'mrs:fixture_model_all_list'
all_list = rc.get_json(key)
# ✅ Redis 没数据时,请求 MRS 接口刷新
if not all_list:
config = self.env['res.config.settings'].get_values()
headers = Common.get_headers(config['token'], config['sf_secret_key'])
refresh_url = config['mrs_url'] + '/api/refresh_redis/fixture_model'
raise ValidationError(f"Redis 中未找到 key={key}")
try:
res = requests.post(refresh_url, headers=headers, json={}, timeout=10)
res.raise_for_status()
except Exception as e:
raise ValidationError(f"Redis 无数据MRS 刷新失败: {str(e)}")
# 再次尝试获取 Redis
all_list = rc.get_json(key)
if not all_list:
raise ValidationError("刷新后仍无 Redis 数据,无法同步夹具型号")
# ✅ 开始同步夹具型号
for item in all_list:
if not item or not item.get('code'):
continue
@@ -3335,4 +3279,55 @@ class SyncFixtureModel(models.Model):
if record:
record.write(vals)
else:
self.create(vals)
self.create(vals)
class SyncfixtureMaterialsBasicParameters(models.Model):
_inherit = 'sf.fixture.materials.basic.parameters'
_description = 'Redis 优先同步夹具基本参数'
def sync_all_fixture_materials_basic_parameters(self):
rc = RedisClient()
key = 'mrs:fixture_param_all_list'
all_list = rc.get_json(key)
if not all_list:
raise ValidationError(f"Redis 中未找到 key={key}")
def _sync_list(param_list, material_name):
for item in param_list or []:
if not item or not item.get('code'):
continue
record = self.search([('code', '=', item['code'])], limit=1)
vals = self._get_basic_parameters_list(item, material_name)
if record:
record.write(vals)
else:
self.create(vals)
_sync_list(all_list.get('zero_chuck_all_list'), '零点卡盘')
_sync_list(all_list.get('zero_tray_all_list'), '零点托盘')
_sync_list(all_list.get('pneumatic_fixture_all_list'), '气动夹具')
_sync_list(all_list.get('jaw_vice_all_list'), '虎钳夹具')
_sync_list(all_list.get('magnet_fixture_all_list'), '磁吸夹具')
_sync_list(all_list.get('adapter_board_all_list'), '转接板(锁板)夹具')
_sync_list(all_list.get('scroll_chuck_all_list'), '三爪卡盘')
_sync_list(all_list.get('air_tray_all_list'), '气吹托盘')
_sync_list(all_list.get('magnet_tray_all_list'), '磁吸托盘')
def _get_basic_parameters_list(self, item, material_name):
"""
统一结构化 item 数据,供写入模型字段使用(你应当根据 material_name 自定义字段映射)
"""
return {
'name': item.get('name'),
'code': item.get('code'),
'length': item.get('length'),
'width': item.get('width'),
'height': item.get('height'),
'diameter': item.get('diameter'),
'weight': item.get('weight'),
'fixture_model_id': self.env['sf.fixture.model'].search([('code', '=', item.get('fixture_model_code'))], limit=1).id,
'materials_model_id': self.env['sf.materials.model'].search([('code', '=', item.get('material_code'))], limit=1).id,
'active': item.get('active', True),
# 你可以根据 material_name 判断类型并补充字段
}

View File

@@ -45759,11 +45759,6 @@ msgstr ""
msgid "热处理"
msgstr ""
#. module: sf_base
#: model:ir.model.fields,field_description:sf_base.field_sf_materials_model__need_m
msgid "是否磁吸"
msgstr ""
#. module: sf_base
#: model:ir.model.fields,field_description:sf_base.field_sf_materials_model__mf_materia_post
msgid "热处理后密度"