优化制造订单和生成工单逻辑

This commit is contained in:
jinling.yang
2024-06-19 17:35:13 +08:00
parent b390712308
commit 67b48814f6
6 changed files with 87 additions and 152 deletions

View File

@@ -9,7 +9,6 @@
<tree>
<field name="sequence_number"/>
<field name="program_name"/>
<field name="cnc_id" string="文件"/>
<field name="functional_tool_type_id"/>
<field name="cutting_tool_name"/>
<field name="cutting_tool_no"/>

View File

@@ -292,7 +292,7 @@ class MrpProduction(models.Model):
# 则根据设备找到工作中心;否则采用前面描述的工作中心分配机制;
# 其他规则限制: 默认只分配给工作中心状态为非故障的工作中心;
def _create_workorder3(self):
def _create_workorder3(self, item):
# 根据product_id对self进行分组
grouped_product_ids = {k: list(g) for k, g in groupby(self, key=lambda x: x.product_id.id)}
# 初始化一个字典来存储每个product_id对应的生产订单名称列表
@@ -328,19 +328,6 @@ class MrpProduction(models.Model):
'state': 'pending',
}]
if production.product_id.categ_id.type == '成品':
if production.product_id.id in product_id_to_production_names:
# # 同一个产品多个制造订单对应一个编程单和模型库
# # 只调用一次fetchCNC并将所有生产订单的名称作为字符串传递
if not production.programming_no:
production_programming = self.search(
[('product_id.id', '=', production.product_id.id), ('origin', '=', production.origin)],
limit=1, order='id asc')
if not production_programming.programming_no:
production.fetchCNC(', '.join(product_id_to_production_names[production.product_id.id]))
else:
production.write({'programming_no': production_programming.programming_no,
'programming_state': '编程中'})
# # 根据加工面板的面数及对应的工序模板生成工单
i = 0
processing_panel_len = len(production.product_id.model_processing_panel.split(','))
@@ -353,10 +340,10 @@ class MrpProduction(models.Model):
for route in product_routing_workcenter:
if route.is_repeat is True:
workorders_values.append(
self.env['mrp.workorder'].json_workorder_str(k, production, route))
if i == processing_panel_len and route.routing_type == '解除装夹':
workorders_values.append(
self.env['mrp.workorder'].json_workorder_str(k, production, route))
self.env['mrp.workorder'].json_workorder_str(k, production, route, item))
# if i == processing_panel_len and route.routing_type == '解除装夹':
# workorders_values.append(
# self.env['mrp.workorder'].json_workorder_str(k, production, route))
# 表面工艺工序
# 获取表面工艺id
if production.product_id.model_process_parameters_ids:
@@ -546,8 +533,8 @@ class MrpProduction(models.Model):
# work.button_finish()
# 创建工单并进行排序
def _create_workorder(self):
self._create_workorder3()
def _create_workorder(self, item):
self._create_workorder3(item)
self._reset_work_order_sequence()
return True

View File

@@ -472,7 +472,7 @@ class ResMrpWorkOrder(models.Model):
raise UserError(_("该工单暂未完成,无法进行工件配送"))
# 拼接工单对象属性值
def json_workorder_str(self, k, production, route):
def json_workorder_str(self, k, production, route, item):
# 计算预计时长duration_expected
if route.routing_type == '切割':
duration_expected = self.env['mrp.routing.workcenter'].sudo().search(
@@ -514,6 +514,10 @@ class ResMrpWorkOrder(models.Model):
'date_planned_finished': datetime.now() + timedelta(days=1),
'duration_expected': duration_expected,
'duration': 0,
'cnc_ids': False if route.routing_type != 'CNC加工' else self.env['sf.cnc.processing']._json_cnc_processing(
k, item),
'cmc_ids': False if route.routing_type != 'CNC加工' else self.env['sf.cmm.program']._json_cmm_program(k,
item),
'workpiece_delivery_ids': False if not route.routing_type == '装夹预调' else self._json_workpiece_delivery_list(
production)
}]
@@ -1094,7 +1098,7 @@ class CNCprocessing(models.Model):
_rec_name = 'program_name'
_order = 'sequence_number,id'
cnc_id = fields.Many2one('ir.attachment')
# cnc_id = fields.Many2one('ir.attachment')
sequence_number = fields.Integer('序号')
program_name = fields.Char('程序名')
functional_tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型')
@@ -1144,24 +1148,26 @@ class CNCprocessing(models.Model):
cnc_workorder.write({'programming_state': '已编程', 'work_state': '已编程'})
return cnc_processing
def _json_cnc_processing(self, obj):
cnc_processing_str = (0, 0, {
'sequence_number': obj['sequence_number'],
'program_name': obj['program_name'],
'cutting_tool_name': obj['cutting_tool_name'],
'cutting_tool_no': obj['cutting_tool_no'],
'processing_type': obj['processing_type'],
'margin_x_y': obj['margin_x_y'],
'margin_z': obj['margin_z'],
'depth_of_processing_z': obj['depth_of_processing_z'],
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
'estimated_processing_time': obj['estimated_processing_time'],
'program_path': obj['program_path'],
'cnc_id': obj['cnc_id'].id,
'remark': obj['remark']
})
return cnc_processing_str
def _json_cnc_processing(self, panel, ret):
cnc_processing = []
for item in ret['programming_list']:
if item['processing_panel'] == panel:
cnc_processing.append(0, 0, {
'sequence_number': obj['sequence_number'],
'program_name': obj['program_name'],
'cutting_tool_name': obj['cutting_tool_name'],
'cutting_tool_no': obj['cutting_tool_no'],
'processing_type': obj['processing_type'],
'margin_x_y': obj['margin_x_y'],
'margin_z': obj['margin_z'],
'depth_of_processing_z': obj['depth_of_processing_z'],
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
'estimated_processing_time': obj['estimated_processing_time'],
'program_path': obj['program_path'],
'remark': obj['remark']
})
return cnc_processing
# 根据程序名和加工面匹配到ftp里对应的Nc程序名,可优化为根据cnc_processing.program_path进行匹配
def get_cnc_processing_file(self, serverdir, cnc_processing, program_path):
@@ -1602,80 +1608,22 @@ class CMMprogram(models.Model):
_name = 'sf.cmm.program'
_description = "CMM程序"
cmm_id = fields.Many2one('ir.attachment')
sequence_number = fields.Integer('序号')
program_name = fields.Char('程序名')
cutting_tool_name = fields.Char('刀具名称')
cutting_tool_no = fields.Char('刀号')
processing_type = fields.Char('加工类型')
margin_x_y = fields.Char('余量_X/Y')
margin_z = fields.Char('余量_Z')
depth_of_processing_z = fields.Char('加工深度(Z)')
cutting_tool_extension_length = fields.Char('刀具伸出长度')
cutting_tool_handle_type = fields.Char('刀柄型号')
estimated_processing_time = fields.Char('预计加工时间')
remark = fields.Text('备注')
workorder_id = fields.Many2one('mrp.workorder', string="工单")
production_id = fields.Many2one('mrp.production', string="制造订单")
program_path = fields.Char('程序文件路径')
program_create_date = fields.Datetime('程序创建日期')
def cmm_program_create(self, ret, program_path, program_path_tmp):
cmm_program = None
for obj in ret['programming_list']:
workorder = self.env['mrp.workorder'].search(
[('production_id.name', '=', ret['production_order_no'].split(',')[0]),
('processing_panel', '=', obj['processing_panel']),
('routing_type', '=', 'CNC加工')])
if obj['program_name'] in program_path:
logging.info('obj:%s' % obj['program_name'])
cmm_program = self.sudo().create({
'workorder_id': workorder.id,
def _json_cmm_program(self, panel, ret):
cmm_program = []
for item in ret['programming_list']:
if item['processing_panel'] == panel:
cmm_program.append(0, 0, {
'sequence_number': obj['sequence_number'],
'program_name': obj['program_name'],
'cutting_tool_name': obj['cutting_tool_name'],
'cutting_tool_no': obj['cutting_tool_no'],
'processing_type': obj['processing_type'],
'margin_x_y': obj['margin_x_y'],
'margin_z': obj['margin_z'],
'depth_of_processing_z': obj['depth_of_processing_z'],
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
'estimated_processing_time': obj['estimated_processing_time'],
'remark': obj['remark'],
'program_path': program_path.replace('/tmp', '')
'program_path': obj['program_path'],
'program_create_date': obj['program_create_date']
})
cmm_program.get_cmm_program_file(program_path_tmp, cmm_program, program_path)
return cmm_program
# 根据程序名和加工面匹配到ftp里对应的cmm程序名
def get_cmm_program_file(self, serverdir, cmm_program, program_path):
logging.info('cmm-serverdir:%s' % serverdir)
for root, dirs, files in os.walk(serverdir):
for f in files:
if f in program_path:
cmm_program_file_path = os.path.join(serverdir, root, f)
self.write_file_cmm(cmm_program_file_path, cmm_program)
# 创建附件(nc文件)
def attachment_create(self, name, data):
attachment = self.env['ir.attachment'].create({
'datas': base64.b64encode(data),
'type': 'binary',
'public': True,
'description': '程序文件',
'name': name
})
return attachment
# 将cmm文件存到attach的datas里
def write_file_cmm(self, cmm_file_path, cnc):
cmm_file_name = cmm_file_path.split('/')
logging.info('cmm_file_name:%s' % cmm_file_name[-1])
if os.path.exists(cmm_file_path):
with open(cmm_file_path, 'rb') as file:
data_bytes = file.read()
attachment = self.attachment_create(cnc.program_name + cmm_file_name[-1], data_bytes)
cnc.write({'cmm_id': attachment.id})
file.close()
else:
return False

View File

@@ -210,7 +210,7 @@ class StockRule(models.Model):
创建工单
'''
# productions._create_workorder()
根据product_id对self进行分组
# 根据product_id对self进行分组
grouped_product_ids = {k: list(g) for k, g in groupby(self, key=lambda x: x.productions.product_id.id)}
# 初始化一个字典来存储每个product_id对应的生产订单名称列表
product_id_to_production_names = {}
@@ -219,20 +219,20 @@ class StockRule(models.Model):
# 为同一个product_id创建一个生产订单名称列表
product_id_to_production_names[product_id] = [production.name for production in productions]
for production_item in productions:
if production_item.product_id.id in product_id_to_production_names:
# # 同一个产品多个制造订单对应一个编程单和模型库
# # 只调用一次fetchCNC并将所有生产订单的名称作为字符串传递
if not production_item.programming_no:
production_programming = self.search(
[('product_id.id', '=', production_item.product_id.id),
('origin', '=', production_item.origin)],
limit=1, order='id asc')
if not production_programming.programming_no:
production_item.fetchCNC(
', '.join(product_id_to_production_names[production_item.product_id.id]))
else:
production_item.write({'programming_no': production_programming.programming_no,
'programming_state': '编程中'})
# if production_item.product_id.id in product_id_to_production_names:
# # 同一个产品多个制造订单对应一个编程单和模型库
# # 只调用一次fetchCNC并将所有生产订单的名称作为字符串传递
if not production_item.programming_no:
production_programming = self.env['mrp.production'].search(
[('product_id.id', '=', production_item.product_id.id),
('origin', '=', production_item.origin)],
limit=1, order='id asc')
if not production_programming.programming_no:
production_item.fetchCNC(
', '.join(product_id_to_production_names[production_item.product_id.id]))
else:
production_item.write({'programming_no': production_programming.programming_no,
'programming_state': '编程中'})
productions.filtered(lambda p: (not p.orderpoint_id and p.move_raw_ids) or \
(

View File

@@ -504,7 +504,6 @@
<tree decoration-success="button_state" decoration-bf="button_state">
<field name="sequence_number"/>
<field name="program_name"/>
<field name="cnc_id" string="文件"/>
<field name="cutting_tool_name"/>
<field name="cutting_tool_no"/>
<field name="processing_type"/>
@@ -527,16 +526,8 @@
<tree>
<field name="sequence_number"/>
<field name="program_name"/>
<field name="cmm_id" string="文件"/>
<field name="cutting_tool_name"/>
<field name="cutting_tool_no"/>
<field name="processing_type"/>
<field name="margin_x_y"/>
<field name="margin_z"/>
<field name="depth_of_processing_z"/>
<field name="cutting_tool_extension_length"/>
<field name="cutting_tool_handle_type"/>
<field name="estimated_processing_time"/>
<field name="program_path"/>
<field name="program_create_date"/>
<field name="remark"/>
</tree>
</field>

View File

@@ -24,6 +24,7 @@ class Sf_Mrs_Connect(http.Controller):
ret = json.loads(datas)
ret = json.loads(ret['result'])
logging.info('下发编程单:%s' % ret)
all_production = None
is_delete_file = False
# 查询状态为进行中且类型为获取CNC加工程序的工单
cnc_production = request.env['mrp.production'].with_user(
@@ -83,26 +84,35 @@ class Sf_Mrs_Connect(http.Controller):
logging.info('cnc_processing111:%s' % cnc_processing)
if cnc_processing:
cnc_processing_arr.append(cnc_processing._json_cnc_processing(cnc_processing))
if (cnc_program and cnc_processing_arr) or (not cnc_program and cnc_processing_arr):
cnc_production.workorder_ids.filtered(lambda g: g.routing_type == '装夹预调').write(
{'processing_drawing': cnc_production.workorder_ids.filtered(
lambda g1: g1.routing_type == 'CNC加工').cnc_worksheet})
if cnc_program and cnc_processing_arr:
cnc_program.write({'programming_state': '已编程', 'work_state': '已编程'})
cnc_program.workorder_ids.filtered(lambda d: d.routing_type == '装夹预调').write(
{'processing_drawing': cnc_production.workorder_ids.filtered(
lambda d1: d1.routing_type == 'CNC加工').cnc_worksheet})
cnc_program.workorder_ids.filtered(lambda b: b.routing_type == 'CNC加工').write(
{'cnc_ids': cnc_processing_arr, 'cnc_worksheet': cnc_production.workorder_ids.filtered(
lambda b1: b1.routing_type == 'CNC加工').cnc_worksheet})
cnc_program |= cnc_production
if not cnc_program and cnc_processing_arr:
cnc_program = cnc_production
cnc_program_ids = [item.id for item in cnc_program]
workpiece_delivery = request.env['sf.workpiece.delivery'].sudo().search(
[('production_id', 'in', cnc_program_ids)])
if workpiece_delivery:
workpiece_delivery.write({'is_cnc_program_down': True})
productions = request.env['mrp.production'].with_user(
request.env.ref("base.user_admin")).search(
[('programming_no', '=', cnc_production.programming_no)])
if productions:
if not productions.workorder_ids:
productions.product_id.model_processing_panel = ret['processing_panel']
productions._create_workorder(ret)
# cnc_workorder = productions.workorder_ids.filtered(lambda g: g.routing_type == '装夹预调')
# cnc_production.workorder_ids.filtered(lambda g: g.routing_type == '装夹预调').write(
# {'processing_drawing': cnc_production.workorder_ids.filtered(
# lambda g1: g1.routing_type == 'CNC加工').cnc_worksheet})
# if cnc_program and cnc_processing_arr:
# cnc_program.write({'programming_state': '已编程', 'work_state': '已编程'})
# cnc_program.workorder_ids.filtered(lambda d: d.routing_type == '装夹预调').write(
# {'processing_drawing': cnc_production.workorder_ids.filtered(
# lambda d1: d1.routing_type == 'CNC加工').cnc_worksheet})
# cnc_program.workorder_ids.filtered(lambda b: b.routing_type == 'CNC加工').write(
# {'cnc_ids': cnc_processing_arr, 'cnc_worksheet': cnc_production.workorder_ids.filtered(
# lambda b1: b1.routing_type == 'CNC加工').cnc_worksheet})
# cnc_program |= cnc_production
# if not cnc_program and cnc_processing_arr:
# cnc_program = cnc_production
# cnc_program_ids = [item.id for item in cnc_program]
# workpiece_delivery = request.env['sf.workpiece.delivery'].sudo().search(
# [('production_id', 'in', cnc_program_ids)])
# if workpiece_delivery:
# workpiece_delivery.write({'is_cnc_program_down': True})
return json.JSONEncoder().encode(res)
else:
res = {'status': 0, 'message': '该制造订单暂未开始'}