Files
test/sf_tool_management/models/mrp_workorder.py

142 lines
7.9 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 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(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、创建制造订单无效刀检测结果记录
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程序用刀校验已完成')
@api.model_create_multi
def create(self, vals):
obj = super(CNCprocessing, self).create(vals)
# 调用CAM工单程序用刀计划创建方法
self.cnc_tool_checkout(obj)
return obj
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