Files
test/sf_tool_management/models/mrp_workorder.py
2025-01-13 13:51:07 +08:00

229 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import requests
import logging
from datetime import datetime
from odoo import fields, models, api
from odoo.exceptions import ValidationError
from odoo.addons.sf_base.commons.common import Common
class CNCprocessing(models.Model):
_inherit = 'sf.cnc.processing'
_description = 'CNC加工用刀检测'
# ==========MES装刀指令接口==========
# def register_cnc_processing(self, knife_plan):
# config = self.env['res.config.settings'].get_values()
# # token = sf_sync_config['token'Ba F2CF5DCC-1A00-4234-9E95-65603F70CC8A]
# headers = {'Authorization': config['center_control_Authorization']}
# crea_url = config['center_control_url'] + "/AutoDeviceApi/ToolLoadInstruct"
# val = {
# 'DeviceId': knife_plan.machine_table_name,
# 'RfidCode': knife_plan.sf_functional_tool_assembly_id.rfid.zfill(10),
# 'ToolId': int(knife_plan.cam_cutter_spacing_code[1:])
# }
# r = requests.post(crea_url, json=val, headers=headers)
# ret = r.json()
# logging.info('register_cnc_processing:%s' % ret)
# if ret['Succeed']:
# return "MES装刀指令发送成功"
# else:
# raise ValidationError("MES装刀指令发送失败")
def cnc_tool_checkout_1(self, cnc_processing_ids):
"""
根据传入的工单信息查询是否有需要的功能刀具如果没有则生成CAM工单程序用刀计划
"""
logging.info('开始进行工单cnc程序用刀校验')
logging.info(f'cnc_processing_ids:{cnc_processing_ids}')
if not cnc_processing_ids:
return False
cam_id = self.env['sf.cam.work.order.program.knife.plan']
production_ids = [] # 制造订单集
datas = {'缺刀': {}, '无效刀': {}} # 缺刀/无效刀集
for cnc_processing in cnc_processing_ids:
# ======创建字典: {'缺刀': {'制造订单1': {'加工面1': [], ...}, ...}, '无效刀': {'制造订单1': {'加工面1': [], ...}, ...}}======
production_name = cnc_processing.workorder_id.production_id.name # 制造订单
processing_panel = cnc_processing.workorder_id.processing_panel # 加工面
if production_name not in list(datas['缺刀'].keys()):
datas['缺刀'].update({production_name: {processing_panel: []}})
datas['无效刀'].update({production_name: {processing_panel: []}})
production_ids.append(cnc_processing.workorder_id.production_id)
else:
if processing_panel not in list(datas['缺刀'].get(production_name).keys()):
datas['缺刀'].get(production_name).update({processing_panel: []})
datas['无效刀'].get(production_name).update({processing_panel: []})
# ======================================
if cnc_processing.cutting_tool_name:
tool_name = cnc_processing.cutting_tool_name
# 检验CNC用刀是否是功能刀具清单中的刀具
tool_inventory_id = self.env['sf.tool.inventory'].sudo().search([('name', '=', tool_name)])
if not tool_inventory_id:
if cnc_processing.cutting_tool_name not in datas['无效刀'][production_name][processing_panel]:
datas['无效刀'][production_name][processing_panel].append(cnc_processing.cutting_tool_name)
cnc_processing.tool_state = '2'
logging.info(f'"无效刀"[{production_name}{processing_panel}{cnc_processing.cutting_tool_name}]')
# 跳过本次循环
continue
# 校验CNC用刀在系统是否存在
functional_tools = self.env['sf.functional.cutting.tool.entity'].sudo().search(
[('tool_name_id', '=', tool_inventory_id.id), ('functional_tool_status', '=', '正常')])
# 判断线边、机内是否有满足条件的刀
if not functional_tools.filtered(lambda p: p.current_location in ('线边刀库', '机内刀库')):
if cnc_processing.cutting_tool_name not in datas['缺刀'][production_name][processing_panel]:
datas['缺刀'][production_name][processing_panel].append(cnc_processing.cutting_tool_name)
cnc_processing.tool_state = '1'
logging.info(f'"缺刀"[{production_name}{processing_panel}{cnc_processing.cutting_tool_name}]')
# 判断是否有满足条件的刀
if not functional_tools:
# 创建CAM申请装刀记录
cam_id.create_cam_work_plan(cnc_processing)
logging.info('成功调用CAM工单程序用刀计划创建方法')
logging.info(f'datas:{datas}')
for production_id in production_ids:
logging.info(f'production_id: {production_id}')
if production_id:
data1 = datas['无效刀'].get(production_id.name) # data1: {'加工面1': [], ...}
data2 = datas['缺刀'].get(production_id.name) # data2: {'加工面1': [], ...}
# tool_state_remark1 = ''
tool_state_remark2 = ''
# 对无效刀信息进行处理
for key in data1:
if data1.get(key):
# if tool_state_remark1 != '':
# tool_state_remark1 = f'{tool_state_remark1}\n{key}无效刀:{data1.get(key)}'
# else:
# tool_state_remark1 = f'{key}无效刀:{data1.get(key)}'
# 无效刀处理逻辑
# 1、创建制造订单无效刀检测结果记录
if not production_id.detection_result_ids.filtered(
lambda a: (a.processing_panel == key and a.detailed_reason == '无效功能刀具'
and a.handle_result == '待处理' and a.routing_type == 'CNC加工'
and a.rework_reason == 'programming' and a.test_results == '返工')):
logging.info('创建制造订单无效刀检测结果记录!')
production_id.detection_result_ids.create({
'production_id': production_id.id,
'processing_panel': key,
'routing_type': 'CNC加工',
'rework_reason': 'programming', # 原因:编程(programming)
'detailed_reason': '无效功能刀具',
'test_results': '返工',
'handle_result': '待处理'
})
# 修改当前面装夹预调工单的 is_rework 为 True
# work_ids = production_id.workorder_ids.filtered(
# lambda a: a.routing_type == '装夹预调' and a.processing_panel == key and not a.is_rework)
# work_ids.write({'is_rework': True})
# 对缺刀信息进行处理
for key in data2:
if data2.get(key):
if tool_state_remark2 != '':
tool_state_remark2 = f'{tool_state_remark2}\n{key}缺刀:{data2.get(key)}'
else:
tool_state_remark2 = f'{key}缺刀:{data2.get(key)}'
# 将备注信息存入制造订单功能刀具状态的备注字段
logging.info('修改制造订单功能刀具状态的备注字段')
production_id.write({
'tool_state_remark': tool_state_remark2,
# 'tool_state_remark2': tool_state_remark1
})
logging.info('工单cnc程序用刀校验已完成')
class MrpWorkCenter(models.Model):
_inherit = 'mrp.workcenter'
def action_tool_order(self):
action = self.env.ref('sf_tool_management.sf_functional_tool_assembly_view_act')
result = action.read()[0]
return result
class MrpProduction(models.Model):
_inherit = 'mrp.production'
def production_cnc_tool_checkout(self):
logging.info('开始进行工单cnc程序用刀校验')
invalid_tool = [] # 无效刀
invalid_tool_processing_panel = [] # 无效刀加工面
missing_tool_1 = [] # 缺刀(机内、线边)
missing_tool_2 = [] # 缺刀(库存)
for item in self:
workorder_ids = item.workorder_ids.filtered(lambda a: a.state not in ['rework', 'cancel'])
for workorder_id in workorder_ids:
if workorder_id.cnc_ids:
for cnc_id in workorder_id.cnc_ids:
tool_name = cnc_id.cutting_tool_name
# 查询功能刀具在清单中是否存在
tool_inventory_id = self.env['sf.tool.inventory'].sudo().search([('name', '=', tool_name)])
if not tool_inventory_id:
invalid_tool.append(tool_name)
invalid_tool_processing_panel.append(workorder_id.processing_panel)
continue
# 查询功能刀具是否存在库存
functional_tools = self.env['sf.functional.cutting.tool.entity'].sudo().search(
[('tool_name_id', '=', tool_inventory_id.id), ('functional_tool_status', '=', '正常')])
if not functional_tools.filtered(lambda p: p.current_location in ('线边刀库', '机内刀库')):
missing_tool_1.append(tool_name) # 判断为 ('线边刀库', '机内刀库') 缺刀
if not functional_tools:
missing_tool_2.append(tool_name) # 判断为 库存缺刀
break
# 修改cnc程序的刀具状态
workorder_ids = self.env['mrp.workorder'].sudo().search([('production_id', 'in', self.ids)])
if invalid_tool:
logging.info(f'无效刀:{invalid_tool}')
# 修改cnc程序的刀具状态为 ‘无效刀’
cnc_ids = self.env['sf.cnc.processing'].sudo().search(
[('workorder_id', 'in', workorder_ids.ids), ('cutting_tool_name', 'in', invalid_tool)])
if cnc_ids:
for cnc_id in cnc_ids:
cnc_id.tool_state = '2'
# cnc_ids.write({'tool_state': '2'})
# 创建制造订单无效刀检测结果记录
for production_id in self:
for processing_panel in list(set(invalid_tool_processing_panel)):
if not production_id.detection_result_ids.filtered(
lambda a: (a.processing_panel == processing_panel and a.detailed_reason == '无效功能刀具'
and a.handle_result == '待处理' and a.routing_type == 'CNC加工'
and a.rework_reason == 'programming' and a.test_results == '返工')):
production_id.detection_result_ids.create({
'production_id': production_id.id,
'processing_panel': processing_panel,
'routing_type': 'CNC加工',
'rework_reason': 'programming', # 原因:编程(programming)
'detailed_reason': '无效功能刀具',
'test_results': '返工',
'handle_result': '待处理'
})
# 自动调用重新获取编程的方法
logging.info('cnc用刀校验到无效刀自动调用重新编程方法update_programming_state()')
self[0].update_programming_state(trigger_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
self[0].write({'is_rework': False})
# 修改制造订单 编程状态变为“编程中”
self.write({'programming_state': '编程中', 'work_state': '编程中'})
self[0].workorder_ids.filtered(
lambda a: a.name == '装夹预调' and a.state not in ['rework', 'done', 'cancel'])._compute_state()
return True
if missing_tool_1:
logging.info(f'线边、机内缺刀:{missing_tool_1}')
# 修改 修改cnc程序的刀具状态 为 ‘缺刀’
cnc_ids = self.env['sf.cnc.processing'].sudo().search(
[('workorder_id', 'in', workorder_ids.ids), ('cutting_tool_name', 'in', missing_tool_1)])
if cnc_ids:
for cnc_id in cnc_ids:
cnc_id.tool_state = '1'
# cnc_ids.write({'tool_state': '1'})
if missing_tool_2 and invalid_tool == []:
logging.info(f'库存缺刀:{missing_tool_2}')
# 调用CAM工单程序用刀计划创建方法
cnc_ids = self.env['sf.cnc.processing'].sudo().search(
[('workorder_id', 'in', workorder_ids.filtered(lambda a: a.production_id == self[0]).ids),
('cutting_tool_name', 'in', missing_tool_2)])
if cnc_ids:
logging.info('调用CAM工单程序用刀计划创建方法')
self.env['sf.cam.work.order.program.knife.plan'].sudo().create_cam_work_plan(cnc_ids)
if not invalid_tool and not missing_tool_1:
self.sudo().write({'tool_state': '0'})
logging.info('校验cnc用刀正常!')
logging.info('工单cnc程序用刀校验完成')