Files
test/sf_tool_management/models/base.py

1720 lines
94 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.

# -*- coding: utf-8 -*-
import re
import logging
from datetime import timedelta
from odoo import SUPERUSER_ID
from odoo import fields, models, api
from odoo.exceptions import ValidationError
class MachineTableToolChangingApply(models.Model):
_name = 'sf.machine.table.tool.changing.apply'
_description = '机床换刀申请'
_order = 'cutter_spacing_code_id'
name = fields.Char('名称', related='maintenance_equipment_id.name', store=True)
# 设备信息
maintenance_equipment_id = fields.Many2one('maintenance.equipment', string='CNC机床', readonly=True,
domain=[('category_id.equipment_type', '=', '机床')])
production_line_id = fields.Many2one('sf.production.line', string='生产线', readonly=True)
machine_table_type_id = fields.Many2one('maintenance.equipment.category', string='机床类型', readonly=True,
compute='_compute_machine_table_type_id')
machine_tool_code = fields.Char(string='机台号', related='maintenance_equipment_id.name')
cutter_spacing_code_id = fields.Many2one('maintenance.equipment.tool', string='刀位号', readonly=True,
required=True, domain="[('equipment_id', '=', maintenance_equipment_id)]")
# 功能刀具信息
code = fields.Char('编码', related='functional_tool_name_id.code')
tool_groups_id = fields.Many2one('sf.tool.groups', '刀具组', related='functional_tool_name_id.tool_groups_id')
functional_tool_name = fields.Char(string='刀具名称', related='functional_tool_name_id.name', store=True)
barcode_id = fields.Many2one('stock.lot', string='功能刀具序列号', store=True,
domain=[('product_id.name', '=', '功能刀具')],
related='functional_tool_name_id.barcode_id')
rfid = fields.Char('Rfid', related='functional_tool_name_id.rfid')
functional_tool_name_id = fields.Many2one('sf.functional.tool.assembly', domain=[('assemble_status', '=', '1')],
string='功能刀具名称')
functional_tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型', store=True,
related='functional_tool_name_id.after_assembly_functional_tool_type_id')
tool_position_interface_type = fields.Selection(
[('BT刀柄式', 'BT刀柄式'), ('SK刀柄式', 'SK刀柄式'), ('HSK刀柄式', 'HSK刀柄式'),
('CAT刀柄式', 'CAT刀柄式'), ('ISO刀盘式', 'ISO刀盘式'), ('DIN刀盘式', 'DIN刀盘式'),
('直装固定式', '直装固定式')], string='刀位接口型号')
diameter = fields.Float(string='刀具直径(mm)')
knife_tip_r_angle = fields.Float(string='刀尖R角(mm)')
max_lifetime_value = fields.Integer(string='最大寿命值(min)')
alarm_value = fields.Integer(string='报警值(min)')
used_value = fields.Integer(string='已使用值(min)')
whether_standard_knife = fields.Boolean(string='是否标准刀', default=True)
extension_length = fields.Float(string='伸出长(mm)')
effective_length = fields.Float(string='有效长(mm)')
functional_tool_status = fields.Selection([('正常', '正常'), ('报警', '报警')], string='功能刀具状态', store=True,
default='正常', compute='_compute_functional_tool_status')
assembly_order_code = fields.Char(string='组装单编码', readonly=True)
applicant = fields.Char(string='申请人', readonly=True)
reason_for_applying = fields.Char(string='申请原因', readonly=True)
remark = fields.Char(string='备注说明', readonly=False)
status = fields.Selection([('0', '未操作'), ('1', '已申请换刀'), ('2', '已转移'), ('3', '已组装')],
string='操作状态', default='0')
sf_functional_tool_assembly_id = fields.Many2one('sf.functional.tool.assembly', '功能刀具组装单', readonly=True)
active = fields.Boolean(string='已归档', default=True)
@api.depends('alarm_value', 'used_value')
def _compute_functional_tool_status(self):
for record in self:
if record.alarm_value < record.used_value:
record.sudo().functional_tool_status = '报警'
else:
record.sudo().functional_tool_status = '正常'
@api.depends('maintenance_equipment_id')
def _compute_machine_table_type_id(self):
for record in self:
if record:
record.sudo().production_line_id = record.maintenance_equipment_id.production_line_id.id
record.sudo().machine_table_type_id = record.maintenance_equipment_id.category_id.id
record.sudo().machine_tool_code = record.maintenance_equipment_id.code
else:
record.sudo().production_line_id = None
record.sudo().machine_table_type_id = None
record.sudo().machine_tool_code = None
@api.constrains("cutter_spacing_code_id")
def _check_cutter_spacing_code_id(self):
for obj in self:
records = self.env['sf.machine.table.tool.changing.apply'].search([
('maintenance_equipment_id', '=', obj.maintenance_equipment_id.id),
('cutter_spacing_code_id', '=', obj.cutter_spacing_code_id.id)])
if len(records) > 1:
raise ValidationError('该刀位号已存在,请重新选择!!!')
# @api.constrains('functional_tool_status')
# def automation_apply_for_tool_change(self):
# """
# 自动申请换刀
# :return:
# """
# # 更新数据到机台换刀申请界面
# if self.functional_tool_status == '报警' and not self.sf_functional_tool_assembly_id:
# machine_table_tool_changing_apply = self.env['sf.machine.table.tool.changing.apply'].search(
# [('maintenance_equipment_id', '=', self.maintenance_equipment_id.id),
# ('cutter_spacing_code_id', '=', self.cutter_spacing_code_id.id)
# ])
#
# # 创建功能刀具预警记录
# self.env['sf.functional.tool.warning'].create_tool_warning_record({'tool_changing_apply_id': self})
#
# # 新建组装任务
# sf_functional_tool_assembly = self.env['sf.functional.tool.assembly'].create({
# 'functional_tool_name': self.functional_tool_name,
# 'functional_tool_type_id': self.functional_tool_type_id.id,
# 'functional_tool_diameter': self.diameter,
# 'knife_tip_r_angle': self.knife_tip_r_angle,
# 'coarse_middle_thin': '3',
# 'new_former': '0',
# 'functional_tool_length': self.extension_length,
# 'effective_length': self.effective_length,
# 'loading_task_source': '1',
# 'use_tool_time': fields.Datetime.now() + timedelta(hours=4),
# 'production_line_name_id': self.production_line_id.id,
# 'machine_tool_name_id': self.maintenance_equipment_id.id,
# 'applicant': '系统自动',
# 'apply_time': fields.Datetime.now(),
# 'cutter_spacing_code_id': self.cutter_spacing_code_id.id,
# 'whether_standard_knife': self.whether_standard_knife,
# 'reason_for_applying': '机台报警自动换刀',
# 'sf_machine_table_tool_changing_apply_id': self.id
# })
#
# machine_table_tool_changing_apply.write(
# {'status': '1',
# 'sf_functional_tool_assembly_id': sf_functional_tool_assembly.id})
def revocation_1(self):
"""
换刀申请撤回按键
:return:
"""
# 撤回功能刀具组装创建的任务
self.env['sf.functional.tool.assembly'].search(
[('id', '=', self.sf_functional_tool_assembly_id.id),
('loading_task_source', '=', '1')]).unlink()
# 撤回数据更新
self.env['sf.machine.table.tool.changing.apply'].search(
[('maintenance_equipment_id', '=', self.maintenance_equipment_id.id),
('cutter_spacing_code_id', '=', self.cutter_spacing_code_id.id)]).write(
{'status': '0',
'sf_functional_tool_assembly_id': None
})
def revocation_2(self):
"""
转移撤回按键
:return:
"""
self.env['sf.machine.table.tool.changing.apply'].search(
[('name', '=', self.name.id)]).write({'status': '0'})
def create_tool_change_application(self):
"""
根据已有机床刀位创建机台换刀申请记录
"""
maintenance_equipment_ids = self.env['maintenance.equipment'].sudo().search(
[('product_template_ids', '!=', False)])
tool_changing_apply = self.env['sf.machine.table.tool.changing.apply']
if maintenance_equipment_ids:
for maintenance_equipment_id in maintenance_equipment_ids:
if maintenance_equipment_id.product_template_ids:
for product_template_id in maintenance_equipment_id.product_template_ids:
tool_changing_apply.sudo().create({
'maintenance_equipment_id': product_template_id.equipment_id.id,
'cutter_spacing_code_id': product_template_id.id
})
class CAMWorkOrderProgramKnifePlan(models.Model):
_name = 'sf.cam.work.order.program.knife.plan'
_inherit = ['mail.thread']
_description = 'CAM工单程序用刀计划'
name = fields.Char('工单任务编号')
programming_no = fields.Char('编程单号')
cam_procedure_code = fields.Char('程序名')
filename = fields.Char('文件')
cam_cutter_spacing_code = fields.Char('刀号')
tool_position_interface_type = fields.Selection(
[('BT刀柄式', 'BT刀柄式'), ('SK刀柄式', 'SK刀柄式'), ('HSK刀柄式', 'HSK刀柄式'),
('CAT刀柄式', 'CAT刀柄式'), ('ISO刀盘式', 'ISO刀盘式'), ('DIN刀盘式', 'DIN刀盘式'),
('直装固定式', '直装固定式')], string='刀位接口型号')
production_line_id = fields.Many2one('sf.production.line', string='生产线', group_expand='_read_group_names')
machine_table_name_id = fields.Many2one('maintenance.equipment', string='机床名称',
domain="[('production_line_id', '=', production_line_id)]")
machine_table_name = fields.Char(string='机台号', readonly=True, related='machine_table_name_id.name')
cutter_spacing_code_id = fields.Many2one('maintenance.equipment.tool', string='刀位号',
domain="[('equipment_id', '=', machine_table_name_id)]")
whether_standard_knife = fields.Boolean(string='是否标准刀', default=True)
need_knife_time = fields.Datetime(string='用刀时间', readonly=False)
applicant = fields.Char(string='申请人', readonly=True)
applicant_time = fields.Datetime(string='申请时间', readonly=True)
reason_for_applying = fields.Char(string='申请原因', readonly=False)
barcode_id = fields.Many2one('stock.lot', string='功能刀具序列号',
domain=[('product_id.name', '=', '功能刀具')])
functional_tool_name = fields.Char(string='功能刀具名称', readonly=True)
functional_tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型',
compute='_compute_tool_number', store=True)
tool_groups_id = fields.Many2one('sf.tool.groups', '刀具组', compute='_compute_tool_number', store=True)
diameter = fields.Float(string='刀具直径(mm)', compute='_compute_tool_number', store=True)
tool_included_angle = fields.Float(string='刀尖R角(mm)', compute='_compute_tool_number', store=True)
tool_loading_length = fields.Float(string='总长度(mm)', compute='_compute_tool_number', store=True)
extension_length = fields.Float(string='伸出长(mm)')
effective_length = fields.Float(string='有效长(mm)')
new_former = fields.Selection([('0', ''), ('1', '')], string='新/旧', readonly=False, default='0')
coarse_middle_thin = fields.Selection([("1", ""), ('2', ''), ('3', '')], default='3',
string='粗/中/精', readonly=False)
L_D = fields.Float(string='L/D值', readonly=False)
clearance_length = fields.Float(string='避空长(mm)', compute='_compute_tool_number', store=True)
required_cutting_time = fields.Integer(string='需切削时长', readonly=False)
process_type = fields.Char('加工类型')
margin_x_y = fields.Float('余量_X/Y')
margin_z = fields.Float('余量_Z')
finish_depth = fields.Float('加工深度')
shank_model = fields.Char('刀柄型号')
estimated_processing_time = fields.Char('预计加工时间')
plan_execute_status = fields.Selection([('0', '待下发'), ('1', '执行中'), ('2', '已完成')],
string='计划执行状态', default='0', readonly=False, tracking=True)
sf_functional_tool_assembly_id = fields.Many2one('sf.functional.tool.assembly', '功能刀具组装', readonly=True)
active = fields.Boolean(string='已归档', default=True, groups='base.user_root')
@api.depends('functional_tool_name')
def _compute_tool_number(self):
for item in self:
inventory = self.env['sf.tool.inventory'].sudo().search([('name', '=', item.functional_tool_name)])
if inventory:
item.functional_tool_type_id = inventory.functional_cutting_tool_model_id.id
item.tool_groups_id = inventory.tool_groups_id.id
item.diameter = int(inventory.diameter)
item.tool_included_angle = inventory.angle
item.tool_loading_length = inventory.tool_length
item.clearance_length = inventory.blade_length
else:
item.functional_tool_type_id = False
item.tool_groups_id = False
item.diameter = 0
item.tool_included_angle = 0
item.tool_loading_length = 0
item.clearance_length = 0
@api.model
def _read_group_names(self, categories, domain, order):
names = categories._search([], order=order, access_rights_uid=SUPERUSER_ID)
return categories.browse(names)
def apply_for_tooling(self):
"""
申请装刀
:return:
"""
record = self.env['sf.functional.tool.assembly'].create({
'functional_tool_name': self.functional_tool_name,
'functional_tool_type_id': self.functional_tool_type_id.id,
'tool_groups_id': self.tool_groups_id.id,
'functional_tool_diameter': self.diameter,
'knife_tip_r_angle': self.tool_included_angle,
'tool_loading_length': self.tool_loading_length,
'functional_tool_length': self.extension_length,
'effective_length': self.effective_length,
'whether_standard_knife': self.whether_standard_knife,
'coarse_middle_thin': self.coarse_middle_thin,
'new_former': self.new_former,
'production_line_name_id': self.production_line_id.id,
'machine_tool_name_id': self.machine_table_name_id.id,
'cutter_spacing_code_id': self.env['maintenance.equipment.tool'].sudo().search(
[('code', '=', self.cam_cutter_spacing_code), ('equipment_id', '=', self.machine_table_name_id.id)]).id,
'loading_task_source': '0',
'applicant': self.env.user.name,
'use_tool_time': fields.Datetime.now() + timedelta(
hours=4) if not self.need_knife_time else self.need_knife_time,
'reason_for_applying': '工单用刀',
'sf_cam_work_order_program_knife_plan_id': self.id
})
self.sf_functional_tool_assembly_id = record.id
# 将计划执行状态改为执行中
self.env['sf.cam.work.order.program.knife.plan'].search(
[('name', '=', self.name), ('functional_tool_name', '=', self.functional_tool_name)]).write(
{'plan_execute_status': '1',
'applicant': self.env.user.name})
def revocation(self):
"""
撤回装刀申请
:return:
"""
self.env['sf.functional.tool.assembly'].sudo().search(
[('assembly_order_code', '=', self.sf_functional_tool_assembly_id.assembly_order_code),
('loading_task_source', '=', '0')]).unlink()
# 将计划执行状态改为待执行,同时清除申请人、功能刀具组装字段数据
self.env['sf.cam.work.order.program.knife.plan'].search(
[('name', '=', self.name), ('functional_tool_name', '=', self.functional_tool_name)]).write(
{'plan_execute_status': '0',
'applicant': None,
'sf_functional_tool_assembly_id': None})
def create_cam_work_plan(self, cnc_ids):
"""
根据传入的工单信息查询是否有需要的功能刀具如果没有则生成CAM工单程序用刀计划
"""
# 获取编程单号
programming_no = cnc_ids[0].workorder_id.production_id.programming_no
logging.info(f'编程单号:{programming_no}')
for cnc_processing in cnc_ids:
tool_name = cnc_processing.cutting_tool_name
cam_id = self.env['sf.cam.work.order.program.knife.plan'].sudo().search(
[('programming_no', '=', programming_no),
('functional_tool_name', '=', tool_name)])
if cam_id:
logging.info(f'编程单号{programming_no}功能刀具名称{tool_name}已存在CAM装刀计划{cam_id}')
else:
knife_plan = self.env['sf.cam.work.order.program.knife.plan'].sudo().create({
'name': cnc_processing.workorder_id.production_id.name,
'programming_no': programming_no,
'cam_procedure_code': cnc_processing.program_name,
'filename': cnc_processing.cnc_id.name,
'functional_tool_name': tool_name,
'cam_cutter_spacing_code': cnc_processing.cutting_tool_no,
'process_type': cnc_processing.processing_type,
'margin_x_y': float(cnc_processing.margin_x_y),
'margin_z': float(cnc_processing.margin_z),
'finish_depth': float(cnc_processing.depth_of_processing_z),
'extension_length': float(cnc_processing.cutting_tool_extension_length),
'shank_model': cnc_processing.cutting_tool_handle_type,
'estimated_processing_time': cnc_processing.estimated_processing_time,
})
logging.info(f'创建CAM工单程序用刀计划{knife_plan}')
# 创建装刀请求
knife_plan.apply_for_tooling()
logging.info('CAM工单程序用刀计划创建已完成')
def unlink_cam_plan(self, production):
for item in production:
cam_plan_ids = self.env['sf.cam.work.order.program.knife.plan'].search([('name', '=', item.name)])
for cam_plan_id in cam_plan_ids:
assembly_id = cam_plan_id.sf_functional_tool_assembly_id
if assembly_id.assemble_status in ('0', '待组装') and not assembly_id.start_preset_bool:
logging.info('%s删除成功!!!' % assembly_id)
assembly_id.sudo().unlink()
logging.info('unlink_cam_plan成功')
cam_plan_ids.sudo().unlink()
class FunctionalToolAssembly(models.Model):
_name = 'sf.functional.tool.assembly'
_inherit = ['mail.thread', 'barcodes.barcode_events_mixin']
_description = '功能刀具组装'
_order = 'tool_loading_time desc, use_tool_time asc'
def on_barcode_scanned(self, barcode):
"""
智能工厂组装单处扫码校验刀具物料
"""
if 'O-CMD' in barcode:
return ''
for record in self:
tool_assembly_id = self.env['sf.functional.tool.assembly'].browse(self.ids)
lot_ids = self.env['stock.lot'].sudo().search([('rfid', '=', barcode)])
if lot_ids:
for lot_id in lot_ids:
if lot_id.tool_material_status != '可用':
raise ValidationError(f'Rfid为【{barcode}】的刀柄已被占用,请重新扫描!!')
if lot_id.product_id == record.handle_product_id:
record.handle_code_id = lot_id.id
tool_assembly_id.handle_code_id = lot_id.id
else:
raise ValidationError('刀柄选择错误,请重新确认!!!')
else:
location = self.env['sf.shelf.location'].sudo().search([('barcode', '=', barcode)])
if location:
if location == record.integral_freight_barcode_id:
tool_assembly_id.integral_verify = True
record.integral_verify = True
elif location == record.blade_freight_barcode_id:
tool_assembly_id.blade_verify = True
record.blade_verify = True
elif location == record.bar_freight_barcode_id:
tool_assembly_id.bar_verify = True
record.bar_verify = True
elif location == record.pad_freight_barcode_id:
tool_assembly_id.pad_verify = True
record.pad_verify = True
elif location == record.chuck_freight_barcode_id:
tool_assembly_id.chuck_verify = True
record.chuck_verify = True
else:
raise ValidationError('刀具选择错误,请重新确认!')
else:
raise ValidationError(f'扫描为【{barcode}】的货位不存在,请重新扫描!')
@api.depends('functional_tool_name')
def _compute_name(self):
for obj in self:
obj.name = obj.assembly_order_code
rfid = fields.Char('Rfid', readonly=True)
tool_groups_id = fields.Many2one('sf.tool.groups', '刀具组', store=True, compute='_compute_inventory_num')
name = fields.Char(string='名称', readonly=True, compute='_compute_name')
assembly_order_code = fields.Char(string='组装单编码', readonly=True)
functional_tool_name_id = fields.Many2one('product.product', string='功能刀具', readonly=True)
functional_tool_name = fields.Char(string='功能刀具名称', readonly=True, required=True)
tool_inventory_id = fields.Many2one('sf.tool.inventory', string='功能刀具清单', store=True,
compute='_compute_functional_tool_name')
@api.depends('functional_tool_name')
def _compute_functional_tool_name(self):
for item in self:
if item.functional_tool_name:
inventory = self.env['sf.tool.inventory'].sudo().search([('name', '=', item.functional_tool_name)])
if inventory:
item.tool_inventory_id = inventory.id
item.after_assembly_functional_tool_name = item.functional_tool_name # 组装后名称
item.functional_tool_type_id = item.tool_inventory_id.functional_cutting_tool_model_id.id
item.tool_groups_id = item.tool_inventory_id.tool_groups_id.id # 刀具组
item.after_assembly_functional_tool_type_id = item.tool_inventory_id.functional_cutting_tool_model_id.id
item.after_assembly_functional_tool_diameter = item.tool_inventory_id.diameter # 直径
item.after_assembly_knife_tip_r_angle = item.tool_inventory_id.angle # R角
item.after_assembly_tool_loading_length = item.tool_inventory_id.tool_length # 总长度
item.after_assembly_functional_tool_length = item.tool_inventory_id.extension # 伸出长度
item.after_assembly_max_lifetime_value = item.tool_inventory_id.life_span # 最大寿命
@api.depends('tool_inventory_id', 'tool_inventory_id.functional_cutting_tool_model_id', 'tool_inventory_id.angle',
'tool_inventory_id.tool_groups_id', 'tool_inventory_id.diameter', 'tool_inventory_id.tool_length',
'tool_inventory_id.extension', 'tool_inventory_id.life_span')
def _compute_inventory_num(self):
for item in self:
if item.assemble_status != '01':
return True
if item.tool_inventory_id:
item.functional_tool_type_id = item.tool_inventory_id.functional_cutting_tool_model_id.id
item.tool_groups_id = item.tool_inventory_id.tool_groups_id.id # 刀具组
item.after_assembly_functional_tool_type_id = item.tool_inventory_id.functional_cutting_tool_model_id.id
item.after_assembly_functional_tool_diameter = item.tool_inventory_id.diameter # 直径
item.after_assembly_knife_tip_r_angle = item.tool_inventory_id.angle # R角
item.after_assembly_tool_loading_length = item.tool_inventory_id.tool_length # 总长度
item.after_assembly_max_lifetime_value = item.tool_inventory_id.life_span # 最大寿命
functional_tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型',
group_expand='_read_group_functional_tool_type_ids', store=True,
compute='_compute_inventory_num')
functional_tool_diameter = fields.Float(string='功能刀具直径(mm)', readonly=True)
knife_tip_r_angle = fields.Float(string='刀尖R角(mm)', readonly=True)
coarse_middle_thin = fields.Selection([("1", ""), ('2', ''), ('3', '')], string='粗/中/精', readonly=True)
new_former = fields.Selection([('0', ''), ('1', '')], string='新/旧', readonly=True)
tool_loading_length = fields.Float(string='总长度(mm)', readonly=True)
functional_tool_length = fields.Float(string='伸出长(mm)', readonly=True)
effective_length = fields.Float(string='有效长(mm)', readonly=True)
loading_task_source = fields.Selection(
[('0', 'CAM装刀'), ('1', '机台换刀'), ('2', '按库存组装'), ('3', '寿命到期组装')],
string='装刀任务来源', readonly=True)
use_tool_time = fields.Datetime(string='用刀时间', readonly=True)
production_line_name_id = fields.Many2one('sf.production.line', string='申请产线', readonly=True)
machine_tool_name_id = fields.Many2one('maintenance.equipment', string='申请机台', readonly=True)
machine_tool_code = fields.Char(string='机台号', readonly=True)
applicant = fields.Char(string='申请人', readonly=True)
apply_time = fields.Datetime(string='申请时间', default=fields.Datetime.now(), readonly=True)
assemble_status = fields.Selection([('0', '待组装'), ('01', '组装中'), ('1', '已组装'), ('3', '已取消')],
string='组装状态', default='0', tracking=True, readonly=True)
cutter_spacing_code_id = fields.Many2one('maintenance.equipment.tool', string='刀位号', readonly=True)
whether_standard_knife = fields.Boolean(string='是否标准刀', default=True, readonly=True)
reason_for_applying = fields.Char(string='申请原因', readonly=True)
max_lifetime_value = fields.Integer(string='最大寿命值(min)', readonly=True)
alarm_value = fields.Integer(string='报警值(min)', readonly=True)
used_value = fields.Integer(string='已使用值(min)', readonly=True)
image = fields.Binary('图片', readonly=False)
@api.model
def _read_group_functional_tool_type_ids(self, categories, domain, order):
"""读取分组自定义以便在看板视图中显示所有的类别,即使它们为空"""
functional_tool_type_ids = categories._search([], order=order, access_rights_uid=SUPERUSER_ID)
return categories.browse(functional_tool_type_ids)
# 刀具物料信息
# ==============整体式刀具型号=============
integral_freight_barcode_id = fields.Many2one('sf.shelf.location', string='整体式刀具货位', readonly=True,
domain="[('product_id.cutting_tool_material_id.name', '=', '整体式刀具'),('product_num', '>', 0)]")
integral_lot_id = fields.Many2one('stock.lot', string='整体式刀具批次', readonly=True)
integral_product_id = fields.Many2one('product.product', string='整体式刀具名称',
compute='_compute_integral_product_id', store=True)
cutting_tool_integral_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='整体式刀具型号',
related='integral_product_id.cutting_tool_model_id')
integral_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='整体式刀具规格',
related='integral_product_id.specification_id')
sf_tool_brand_id_1 = fields.Many2one('sf.machine.brand', string='整体式刀具品牌',
related='integral_product_id.brand_id')
integral_verify = fields.Boolean('整体刀校验', default=False)
@api.depends('integral_lot_id')
def _compute_integral_product_id(self):
for item in self:
if item.integral_lot_id:
item.integral_product_id = item.integral_lot_id.product_id.id
else:
item.integral_product_id = False
# =================刀片型号=============
blade_freight_barcode_id = fields.Many2one('sf.shelf.location', string='刀片货位', readonly=True,
domain="[('product_id.cutting_tool_material_id.name', '=', '刀片'),('product_num', '>', 0)]")
blade_lot_id = fields.Many2one('stock.lot', string='刀片批次', readonly=True)
blade_product_id = fields.Many2one('product.product', string='刀片名称', compute='_compute_blade_product_id',
store=True)
cutting_tool_blade_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀片型号',
related='blade_product_id.cutting_tool_model_id')
blade_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='刀片规格',
related='blade_product_id.specification_id')
sf_tool_brand_id_2 = fields.Many2one('sf.machine.brand', '刀片品牌', related='blade_product_id.brand_id')
blade_verify = fields.Boolean('刀片校验', default=False)
@api.depends('blade_lot_id')
def _compute_blade_product_id(self):
for item in self:
if item.blade_lot_id:
item.blade_product_id = item.blade_lot_id.product_id.id
else:
item.blade_product_id = False
# ==============刀杆型号================
bar_freight_barcode_id = fields.Many2one('sf.shelf.location', string='刀杆货位', readonly=True,
domain="[('product_id.cutting_tool_material_id.name', '=', '刀杆'),('product_num', '>', 0)]")
bar_lot_id = fields.Many2one('stock.lot', string='刀杆批次', readonly=True)
bar_product_id = fields.Many2one('product.product', string='刀杆名称', compute='_compute_bar_product_id',
store=True)
cutting_tool_cutterbar_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀杆型号',
related='bar_product_id.cutting_tool_model_id')
bar_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='刀杆规格',
related='bar_product_id.specification_id')
sf_tool_brand_id_3 = fields.Many2one('sf.machine.brand', '刀杆品牌', related='bar_product_id.brand_id')
bar_verify = fields.Boolean('刀杆校验', default=False)
@api.depends('bar_lot_id')
def _compute_bar_product_id(self):
for item in self:
if item.bar_lot_id:
item.bar_product_id = item.bar_lot_id.product_id.id
else:
item.bar_product_id = False
# =============刀盘型号================
pad_freight_barcode_id = fields.Many2one('sf.shelf.location', string='刀盘货位', readonly=True,
domain="[('product_id.cutting_tool_material_id.name', '=', '刀盘'),('product_num', '>', 0)]")
pad_lot_id = fields.Many2one('stock.lot', string='刀盘批次', readonly=True)
pad_product_id = fields.Many2one('product.product', string='刀盘名称', compute='_compute_pad_product_id',
store=True)
cutting_tool_cutterpad_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀盘型号',
related='pad_product_id.cutting_tool_model_id')
pad_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='刀盘规格',
related='pad_product_id.specification_id')
sf_tool_brand_id_4 = fields.Many2one('sf.machine.brand', '刀盘品牌', related='pad_product_id.brand_id')
pad_verify = fields.Boolean('刀盘校验', default=False)
@api.depends('pad_lot_id')
def _compute_pad_product_id(self):
for item in self:
if item.pad_lot_id:
item.pad_product_id = item.pad_lot_id.product_id.id
else:
item.pad_product_id = False
# ==============刀柄型号==============
handle_code_id = fields.Many2one('stock.lot', '刀柄序列号', readonly=True,
domain="[('product_id', '=', handle_product_id)]")
handle_freight_rfid = fields.Char('刀柄Rfid', compute='_compute_handle_rfid', store=True)
handle_product_id = fields.Many2one('product.product', string='刀柄名称', readonly=True)
cutting_tool_cutterhandle_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀柄型号',
related='handle_product_id.cutting_tool_model_id')
handle_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='刀柄规格',
related='handle_product_id.specification_id')
sf_tool_brand_id_5 = fields.Many2one('sf.machine.brand', '刀柄品牌', related='handle_product_id.brand_id')
handle_verify = fields.Boolean('刀柄校验', default=False)
@api.depends('handle_code_id')
def _compute_handle_rfid(self):
for item in self:
if item.handle_code_id:
item.handle_freight_rfid = item.handle_code_id.rfid
item.rfid = item.handle_freight_rfid
else:
item.handle_freight_rfid = False
item.rfid = False
# ==============夹头型号==============
chuck_freight_barcode_id = fields.Many2one('sf.shelf.location', string='夹头货位', readonly=True,
domain="[('product_id.cutting_tool_material_id.name', '=', '夹头'),('product_num', '>', 0)]")
chuck_lot_id = fields.Many2one('stock.lot', string='夹头批次', readonly=True)
chuck_product_id = fields.Many2one('product.product', string='夹头名称', compute='_compute_chuck_product_id',
store=True)
cutting_tool_cutterhead_model_id = fields.Many2one('sf.cutting_tool.standard.library', string='夹头型号',
related='chuck_product_id.cutting_tool_model_id')
chuck_specification_id = fields.Many2one('sf.tool.materials.basic.parameters', string='夹头规格',
related='chuck_product_id.specification_id')
sf_tool_brand_id_6 = fields.Many2one('sf.machine.brand', '夹头品牌', related='chuck_product_id.brand_id')
chuck_verify = fields.Boolean('夹头校验', default=False)
@api.depends('chuck_lot_id')
def _compute_chuck_product_id(self):
for item in self:
if item.chuck_lot_id:
item.chuck_product_id = item.chuck_lot_id.product_id.id
else:
item.chuck_product_id = False
# ==================待删除字段==================
integral_freight_barcode = fields.Char('整体式刀具货位')
blade_freight_barcode = fields.Char('刀片货位')
bar_freight_barcode = fields.Char('刀杆货位')
pad_freight_barcode = fields.Char('刀盘货位')
chuck_freight_barcode = fields.Char('夹头货位')
blade_name = fields.Char('')
integral_name = fields.Char('')
blade_code_id = fields.Many2one('stock.lot', '刀片序列号')
integral_code_id = fields.Many2one('stock.lot', '整体式刀具序列号')
bar_code_id = fields.Many2one('stock.lot', '刀杆序列号')
bar_name = fields.Char('')
pad_code_id = fields.Many2one('stock.lot', '刀盘序列号')
pad_name = fields.Char('')
handle_name = fields.Char('')
chuck_code_id = fields.Many2one('stock.lot', '夹头序列号')
chuck_name = fields.Char('')
# ====================暂时无用字段=========================
after_assembly_used_value = fields.Integer(string='组装后已使用值(min)', readonly=True)
# ==============================================
# 组装功能刀具参数信息
start_preset_bool = fields.Boolean('开始预调', default=False)
barcode_id = fields.Many2one('stock.lot', string='功能刀具序列号', readonly=True)
after_assembly_functional_tool_name = fields.Char(string='组装后功能刀具名称', store=True,
compute='_compute_functional_tool_name')
after_assembly_functional_tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', store=True,
string='组装后功能刀具类型',
compute='_compute_inventory_num')
after_assembly_functional_tool_diameter = fields.Float(string='组装后功能刀具直径(mm)', digits=(10, 3), store=True,
compute='_compute_inventory_num')
after_assembly_knife_tip_r_angle = fields.Float(string='组装后刀尖R角(mm)', digits=(10, 3), store=True,
compute='_compute_inventory_num')
after_assembly_new_former = fields.Selection([('0', ''), ('1', '')], string='组装后新/旧', default='0',
store=True, compute='_compute_rota_tive')
cut_time = fields.Integer(string='已切削时间(min)', readonly=True)
cut_length = fields.Float(string='已切削长度(mm)', readonly=True)
cut_number = fields.Integer(string='已切削次数', readonly=True)
after_assembly_whether_standard_knife = fields.Boolean(string='组装后是否标准刀', default=True, readonly=True)
after_assembly_coarse_middle_thin = fields.Selection([("1", ""), ('2', ''), ('3', '')], store=True,
string='组装后粗/中/精', default='3',
compute='_compute_rota_tive')
after_assembly_max_lifetime_value = fields.Integer(string='组装后最大寿命值(min)', store=True,
compute='_compute_inventory_num')
after_assembly_alarm_value = fields.Integer(string='组装后报警值(min)')
after_assembly_tool_loading_length = fields.Float(string='组装后总长度(mm)', digits=(10, 3), store=True,
compute='_compute_inventory_num')
after_assembly_handle_length = fields.Float(string='组装后刀柄长度(mm)', digits=(10, 3))
after_assembly_functional_tool_length = fields.Float(string='组装后伸出长(mm)', digits=(10, 3), store=True,
compute='_compute_length')
after_assembly_effective_length = fields.Float(string='组装后有效长(mm)', readonly=True)
L_D_number = fields.Float(string='L/D值(mm)', readonly=True)
hiding_length = fields.Float(string='避空长(mm)', readonly=True)
# functional_tool_cutting_type = fields.Char(string='功能刀具切削类型', readonly=False)
tool_loading_person = fields.Char(string='装刀人', readonly=True)
tool_loading_time = fields.Datetime(string='装刀时间', readonly=True)
remark = fields.Char(string='备注说明', readonly=True)
check_box_1 = fields.Boolean(string='复选框', default=False, readonly=False)
sf_machine_table_tool_changing_apply_id = fields.Many2one('sf.machine.table.tool.changing.apply', '机床换刀申请',
readonly=True)
sf_cam_work_order_program_knife_plan_id = fields.Many2one('sf.cam.work.order.program.knife.plan',
'CAM工单程序用刀计划', readonly=True)
active = fields.Boolean(string='已归档', default=True)
code = fields.Char('功能刀具编码', compute='_compute_code')
@api.depends('after_assembly_tool_loading_length', 'after_assembly_handle_length')
def _compute_length(self):
for item in self:
if item.after_assembly_tool_loading_length > item.after_assembly_handle_length:
item.after_assembly_functional_tool_length = (
item.after_assembly_tool_loading_length - item.after_assembly_handle_length)
@api.depends('integral_freight_barcode_id', 'blade_freight_barcode_id')
def _compute_rota_tive(self):
for item in self:
rota_tive_boolean = False
if item.integral_freight_barcode_id:
# 整体刀
if item.integral_freight_barcode_id.rotative_Boolean:
rota_tive_boolean = True
elif item.blade_freight_barcode_id:
# 刀片
if item.blade_freight_barcode_id.rotative_Boolean:
rota_tive_boolean = True
if rota_tive_boolean:
item.after_assembly_coarse_middle_thin = '1'
item.after_assembly_new_former = '1'
else:
item.after_assembly_coarse_middle_thin = '3'
item.after_assembly_new_former = '0'
@api.onchange('handle_product_id')
def _onchange_after_assembly_handle_length(self):
for item in self:
if item:
if item.handle_product_id:
item.after_assembly_handle_length = item.handle_product_id.cutting_tool_shank_length
@api.depends('after_assembly_functional_tool_type_id', 'cutting_tool_cutterhandle_model_id',
'after_assembly_functional_tool_diameter', 'after_assembly_tool_loading_length',
'after_assembly_knife_tip_r_angle', 'after_assembly_functional_tool_length',
'after_assembly_handle_length')
def _compute_code(self):
for obj in self:
if obj.cutting_tool_cutterhandle_model_id:
code = obj.cutting_tool_cutterhandle_model_id.code.split('-', 1)[0]
str_1 = '%s-GNDJ-%s-D%sL%sR%sB%sH%s' % (
code, obj.after_assembly_functional_tool_type_id.code, obj.after_assembly_functional_tool_diameter,
obj.after_assembly_tool_loading_length, obj.after_assembly_knife_tip_r_angle,
round(obj.after_assembly_functional_tool_length, 3), obj.after_assembly_handle_length)
obj.code = '%s-%s' % (str_1, self._get_code_1(str_1))
else:
obj.code = ''
def _get_code_1(self, str_2):
functional_tool_assembly = self.env['sf.functional.cutting.tool.entity'].sudo().search(
[('code', 'like', str_2)],
limit=1,
order="id desc"
)
if not functional_tool_assembly:
num = "%03d" % 1
else:
m = int(functional_tool_assembly.code[-3:]) + 1
num = "%03d" % m
return num
def action_open_reference1(self):
self.ensure_one()
return {
'res_model': self._name,
'type': 'ir.actions.act_window',
'views': [[False, "form"]],
'res_id': self.id,
}
def start_preset(self):
"""
开始组装
"""
# 设置初始值
self.start_preset_bool = True
self.assemble_status = '01'
self.after_assembly_coarse_middle_thin = '3'
self.after_assembly_new_former = '0'
# 调用功能刀具名称对应的清单的BOM获取对应刀具物料信息
bom = self._get_inventory_bom(self.tool_inventory_id)
# 根据BOM自动配置物料的值
self._set_tool_material(bom)
logging.info('功能刀具开始组装初始化值成功!')
def _set_tool_material(self, bom):
"""根据BOM对刀具物料进行初始配置"""
options = bom.get('options')
# 配置刀柄信息
for handle_id in bom.get('handle_ids'):
if handle_id:
if not self.handle_product_id:
self.handle_product_id = handle_id.id
break
# 刀柄之外的物料配置
if options == '刀柄+整体式刀具':
# 配置整体式刀具
integra_lot_id = self._get_old_tool_material_lot(bom.get('integral_ids'))
integra_location_lot_id = self._get_shelf_location_lot(integra_lot_id)
self.integral_freight_barcode_id = integra_location_lot_id.shelf_location_id.id
self.integral_lot_id = integra_location_lot_id.lot_id.id
else:
# 配置刀片
blade_lot_id = self._get_old_tool_material_lot(bom.get('blade_ids'))
blade_location_lot_id = self._get_shelf_location_lot(blade_lot_id)
self.blade_freight_barcode_id = blade_location_lot_id.shelf_location_id.id
self.blade_lot_id = blade_location_lot_id.lot_id.id
if options == '刀柄+刀杆+刀片':
# 配置刀杆
bar_lot_id = self._get_old_tool_material_lot(bom.get('bar_ids'))
bar_location_lot_id = self._get_shelf_location_lot(bar_lot_id)
self.bar_freight_barcode_id = bar_location_lot_id.shelf_location_id.id
self.bar_lot_id = bar_location_lot_id.lot_id.id
elif options == '刀柄+刀盘+刀片':
# 配置刀盘
pad_lot_id = self._get_old_tool_material_lot(bom.get('pad_ids'))
pad_location_lot_id = self._get_shelf_location_lot(pad_lot_id)
self.pad_freight_barcode_id = pad_location_lot_id.shelf_location_id.id
self.pad_lot_id = pad_location_lot_id.lot_id.id
def _get_old_tool_material_lot(self, material_ids):
""" 根据先进先出原则选择物料批次 """
location_id = self.env['stock.location'].search([('name', '=', '刀具房')])
stock_quant = self.env['stock.quant'].sudo().search(
[('location_id', '=', location_id.id), ('product_id', 'in', material_ids.ids), ('quantity', '>', '0')],
order='lot_id', limit=1)
if stock_quant:
return stock_quant.lot_id
else:
raise ValidationError(f'{material_ids[0].cutting_tool_material_id.name}】物料库存不足,请先进行盘点或采购')
def _get_shelf_location_lot(self, lot_id):
"""根据所给的刀具物料批次号,返回一个刀具物料货位、批次信息"""
location_lots = self.env['sf.shelf.location.lot'].sudo().search([('lot_id', '=', lot_id.id)])
if not location_lots:
raise ValidationError(f'没有查询到批次为【{lot_id.name}】物料的货位信息!')
else:
return location_lots[0]
def _get_inventory_bom(self, inventory_id):
"""获取BOM的刀具物料产品信息"""
product_ids = inventory_id.jikimo_bom_ids.product_ids # BOM配置的物料产品
options = inventory_id.jikimo_bom_ids.options # BOM产品组装类型
if not product_ids or not options:
raise ValidationError('功能刀具清单的BOM未进行配置请先配置BOM信息')
handle_ids = product_ids.filtered(lambda a: a.cutting_tool_material_id.name == '刀柄')
integral_ids = product_ids.filtered(lambda a: a.cutting_tool_material_id.name == '整体式刀具')
blade_ids = product_ids.filtered(lambda a: a.cutting_tool_material_id.name == '刀片')
bar_ids = product_ids.filtered(lambda a: a.cutting_tool_material_id.name == '刀杆')
pad_ids = product_ids.filtered(lambda a: a.cutting_tool_material_id.name == '刀盘')
if not handle_ids:
raise ValidationError('功能刀具清单的BOM未配置[刀柄]信息请先配置BOM再开始组装')
if options == '刀柄+整体式刀具':
if not integral_ids:
raise ValidationError('功能刀具清单的BOM未配置[整体式刀具]信息请先配置BOM再开始组装')
return {'options': options, 'handle_ids': handle_ids, 'integral_ids': integral_ids}
elif options == '刀柄+刀杆+刀片':
if not blade_ids:
raise ValidationError('功能刀具清单的BOM未配置[刀片]信息请先配置BOM再开始组装')
if not bar_ids:
raise ValidationError('功能刀具清单的BOM未配置[刀杆]信息请先配置BOM再开始组装')
return {'options': options, 'handle_ids': handle_ids, 'blade_ids': blade_ids, 'bar_ids': bar_ids}
elif options == '刀柄+刀盘+刀片':
if not blade_ids:
raise ValidationError('功能刀具清单的BOM未配置[刀片]信息请先配置BOM再开始组装')
if not pad_ids:
raise ValidationError('功能刀具清单的BOM未配置[刀盘]信息请先配置BOM再开始组装')
return {'options': options, 'handle_ids': handle_ids, 'blade_ids': blade_ids, 'pad_ids': pad_ids}
else:
raise ValidationError(f'功能刀具清单BOM的组装方式错误{options}')
def set_tool_lot(self):
# 获取BOM的刀具物料产品信息
tool_data = self._get_inventory_bom(self.tool_inventory_id)
# 获取刀具类型
tool_type = self.env.context.get('tool_type')
if tool_type == '刀柄':
tool_material_ids = tool_data.get('handle_ids')
tool_material_tree_id = self.env.ref('sf_tool_management.view_tool_product_tree')
elif tool_type == '整体式刀具':
tool_material_ids = self._get_all_material_location_lot(tool_data.get('integral_ids'))
tool_material_tree_id = self.env.ref('sf_tool_management.view_shelf_location_lot_tree_1')
elif tool_type == '刀片':
tool_material_ids = self._get_all_material_location_lot(tool_data.get('blade_ids'))
tool_material_tree_id = self.env.ref('sf_tool_management.view_shelf_location_lot_tree_2')
elif tool_type == '刀杆':
tool_material_ids = self._get_all_material_location_lot(tool_data.get('bar_ids'))
tool_material_tree_id = self.env.ref('sf_tool_management.view_shelf_location_lot_tree_3')
else:
tool_material_ids = self._get_all_material_location_lot(tool_data.get('pad_ids'))
tool_material_tree_id = self.env.ref('sf_tool_management.view_shelf_location_lot_tree_4')
if tool_type == '刀柄':
return {
"type": "ir.actions.act_window",
"res_model": "product.product",
"views": [[tool_material_tree_id.id, "tree"],
[self.env.ref('sf_tool_management.view_tool_product_search').id, "search"]],
"target": "new",
"domain": [('id', 'in', tool_material_ids.ids)],
"context": {'tool_id': self.id}
}
elif tool_type in ['整体式刀具', '刀片', '刀杆', '刀盘']:
return {
"type": "ir.actions.act_window",
"res_model": "sf.shelf.location.lot",
"views": [[tool_material_tree_id.id, "tree"]],
"target": "new",
"domain": [('id', 'in', tool_material_ids.ids)],
"context": {'tool_id': self.id}
}
def _get_all_material_location_lot(self, material_ids):
""" 获取所有满足条件 """
location_id = self.env['stock.location'].search([('name', '=', '刀具房')])
stock_quant_ids = self.env['stock.quant'].sudo().search(
[('location_id', '=', location_id.id), ('product_id', 'in', material_ids.ids if material_ids else []),
('quantity', '>', '0')])
lot_ids = []
for stock_quant_id in stock_quant_ids:
lot_ids.append(stock_quant_id.lot_id.id)
location_lots = self.env['sf.shelf.location.lot'].sudo().search([('lot_id', 'in', lot_ids)])
return location_lots
def functional_tool_assembly(self):
"""
功能刀具确认组装
:return:
"""
logging.info('功能刀具开始组装!')
# 对物料做必填判断
self.materials_must_be_judged()
product_id = self.env['product.product']
# 创建组装入库单
# 创建功能刀具批次/序列号记录
stock_lot = product_id.create_assemble_warehouse_receipt(self)
# 封装功能刀具数据,用于创建功能刀具记录
desc_2 = self.get_desc(stock_lot, self)
# 创建功能刀具组装入库单
self.env['stock.picking'].create_tool_stocking_picking(stock_lot, self)
# 创建刀具物料出库单
self.env['stock.picking'].create_tool_stocking_picking1(self)
# ============================创建功能刀具列表、安全库存记录===============================
# 创建功能刀具列表记录
record_1 = self.env['sf.functional.cutting.tool.entity'].create(desc_2)
# 创建安全库存信息
self.env['sf.real.time.distribution.of.functional.tools'].create_or_edit_safety_stock({
'functional_name_id': self.tool_inventory_id.id
}, record_1)
# =====================修改功能刀具组装单、机床换刀申请、CAM工单程序用刀计划的状态==============
if self.sf_machine_table_tool_changing_apply_id:
# 修改机床换刀申请的状态
self.env['sf.machine.table.tool.changing.apply'].sudo().search([
('id', '=', self.sf_machine_table_tool_changing_apply_id.id)
]).write({'status': '3'})
elif self.sf_cam_work_order_program_knife_plan_id:
# 修改CAM工单程序用刀计划状态
cam_plan = self.env['sf.cam.work.order.program.knife.plan'].sudo().search([
('id', '=', self.sf_cam_work_order_program_knife_plan_id.id)
])
cam_plan.write({'plan_execute_status': '2'})
# ============修改组装单状态为已组装=================================
self.write({
'assemble_status': '1',
'start_preset_bool': False,
'tool_loading_person': self.env.user.name,
'tool_loading_time': fields.Datetime.now()
})
logging.info('功能刀具组装完成!')
def materials_must_be_judged(self):
"""
功能刀具组装必填判断
"""
# 物料准确性校验
# 物料必填校验
if not self.handle_code_id:
raise ValidationError('请扫【刀柄】RFID录入【刀柄】物料信息')
if self.integral_lot_id:
if not self.integral_verify:
raise ValidationError('请扫【整体式刀具】的货位编码进行验证!')
elif self.blade_lot_id:
if not self.blade_verify:
raise ValidationError('请扫【刀片】的货位编码进行验证!')
if self.bar_lot_id:
if not self.bar_verify:
raise ValidationError('请扫【刀杆】的货位编码进行验证!')
elif self.pad_lot_id:
if not self.pad_verify:
raise ValidationError('请扫【刀盘】的货位编码进行验证!')
# 组装参数必填校验
if self.after_assembly_max_lifetime_value == 0:
raise ValidationError('组装参数信息【最大寿命值】不能为0')
if self.after_assembly_functional_tool_diameter <= 0:
raise ValidationError('组装参数信息【刀具直径】不能小于等于0')
if self.after_assembly_tool_loading_length == 0:
raise ValidationError('组装参数信息【总长度】不能为0')
if self.after_assembly_handle_length == 0:
raise ValidationError('组装参数信息【刀柄长度】不能为0')
if self.after_assembly_tool_loading_length < self.after_assembly_handle_length:
raise ValidationError('组装参数信息【刀柄长度】不能大于【总长度】!')
def get_desc(self, stock_lot, functional_tool_assembly_id):
return {
'barcode_id': stock_lot.id,
'code': self.code,
'name': self.tool_inventory_id.name,
'tool_name_id': self.tool_inventory_id.id,
'rfid': self.rfid,
'tool_groups_id': self.tool_groups_id.id,
'functional_tool_name_id': functional_tool_assembly_id.id,
'sf_cutting_tool_type_id': self.after_assembly_functional_tool_type_id.id,
'cutting_tool_integral_model_id': self.integral_product_id.id,
'cutting_tool_blade_model_id': self.blade_product_id.id,
'cutting_tool_cutterbar_model_id': self.bar_product_id.id,
'cutting_tool_cutterpad_model_id': self.pad_product_id.id,
'cutting_tool_cutterhandle_model_id': self.handle_product_id.id,
'cutting_tool_cutterhead_model_id': self.chuck_product_id.id,
'functional_tool_diameter': self.after_assembly_functional_tool_diameter,
'knife_tip_r_angle': self.after_assembly_knife_tip_r_angle,
'coarse_middle_thin': self.after_assembly_coarse_middle_thin,
'new_former': self.after_assembly_new_former,
'tool_loading_length': self.after_assembly_tool_loading_length,
'handle_length': self.after_assembly_handle_length,
'functional_tool_length': self.after_assembly_functional_tool_length,
'effective_length': self.after_assembly_effective_length,
'max_lifetime_value': self.after_assembly_max_lifetime_value,
'alarm_value': self.after_assembly_alarm_value,
'used_value': self.after_assembly_used_value,
'whether_standard_knife': self.after_assembly_whether_standard_knife,
'L_D_number': self.L_D_number,
'hiding_length': self.hiding_length,
'cut_time': self.cut_time,
'cut_length': self.cut_length,
'cut_number': self.cut_number,
'image': self.image,
}
# def put_start_preset(self):
# # 打开组装弹窗开始组装
# self.search([('start_preset_bool', '=', True)]).write({'start_preset_bool': False})
# self.write({
# 'after_assembly_tool_loading_length': 0,
# 'after_assembly_functional_tool_diameter': 0,
# 'after_assembly_knife_tip_r_angle': 0,
# 'start_preset_bool': True
# })
# return {
# 'type': 'ir.actions.act_window',
# 'res_model': 'sf.functional.tool.assembly.order',
# 'name': '功能刀具组装单',
# 'view_mode': 'form',
# 'target': 'new',
# 'context': {'default_name': self.name,
# 'default_assembly_order_code': self.assembly_order_code,
# 'default_production_line_name_id': self.production_line_name_id.id,
# 'default_machine_tool_name_id': self.machine_tool_name_id.id,
# 'default_cutter_spacing_code_id': self.cutter_spacing_code_id.id,
# 'default_functional_tool_name': self.functional_tool_name,
# 'default_functional_tool_type_id': self.functional_tool_type_id.id,
# 'default_tool_groups_id': self.tool_groups_id.id,
# 'default_functional_tool_diameter': self.functional_tool_diameter,
# 'default_knife_tip_r_angle': self.knife_tip_r_angle,
# 'default_tool_loading_length': self.tool_loading_length,
# 'default_functional_tool_length': self.functional_tool_length,
# 'default_effective_length': self.effective_length,
# 'default_whether_standard_knife': self.whether_standard_knife,
# 'default_coarse_middle_thin': self.coarse_middle_thin,
# 'default_new_former': self.new_former,
# 'default_use_tool_time': self.use_tool_time,
# 'default_reason_for_applying': self.reason_for_applying
# }
# }
def _get_code(self, loading_task_source):
"""
自动生成组装单编码
"""
new_time = str(fields.Date.today())
datetime = new_time[2:4] + new_time[5:7]
if loading_task_source == '0':
code = 'C' + datetime
elif loading_task_source == '1':
code = 'J' + datetime
elif loading_task_source == '2':
code = 'K' + datetime
elif loading_task_source == '3':
code = 'S' + datetime
else:
code = False
functional_tool_assembly = self.env['sf.functional.tool.assembly'].sudo().search(
[('loading_task_source', '=', loading_task_source),
('assembly_order_code', 'ilike', code)], limit=1, order="id desc")
if not functional_tool_assembly:
num = "%03d" % 1
else:
m = int(functional_tool_assembly.assembly_order_code[-3:]) + 1
num = "%03d" % m
return code + str(num)
def get_functional_tool(self, val):
functional_tools = self.env['sf.functional.tool.assembly'].search(
[('after_assembly_functional_tool_name', '=', val.get('after_assembly_functional_tool_name')),
('after_assembly_functional_tool_diameter', '=', val.get('after_assembly_functional_tool_diameter')),
('after_assembly_knife_tip_r_angle', '=', val.get('after_assembly_knife_tip_r_angle')),
('after_assembly_coarse_middle_thin', '=', val.get('after_assembly_coarse_middle_thin'))])
for functional_tool in functional_tools:
if functional_tool.barcode_id.quant_ids[-1].location_id.name == '刀具线边库':
return functional_tool
for functional_tool in functional_tools:
if functional_tool.barcode_id.quant_ids[-1].location_id.name == '刀具房':
return functional_tool
return False
bool_preset_parameter = fields.Boolean('', default=False)
def get_tool_preset_parameter(self):
if not self.bool_preset_parameter:
raise ValidationError('没有获取到测量数据, 请确认刀具预调仪操作是否正确!')
return True
def assemble_single_print(self):
"""
todo 组装单打印
:return:
"""
picking_num = fields.Integer('调拨单数量', compute='compute_picking_num', store=True)
@api.depends('assemble_status')
def compute_picking_num(self):
for item in self:
picking_ids = self.env['stock.picking'].sudo().search([('origin', '=', item.assembly_order_code)])
item.picking_num = len(picking_ids)
def open_tool_stock_picking(self):
action = self.env.ref('stock.action_picking_tree_all')
result = action.read()[0]
result['domain'] = [('origin', '=', self.assembly_order_code)]
return result
@api.model_create_multi
def create(self, vals):
obj = super(FunctionalToolAssembly, self).create(vals)
if obj.loading_task_source:
code = self._get_code(obj.loading_task_source)
obj.assembly_order_code = code
return obj
class FunctionalToolDismantle(models.Model):
_name = 'sf.functional.tool.dismantle'
_inherit = ["barcodes.barcode_events_mixin", 'mail.thread']
_description = '功能刀具拆解'
def on_barcode_scanned(self, barcode):
"""
扫码
"""
# 对barcode进行校验是否为货位编码
if not re.match(r'^[A-Za-z0-9]+-[A-Za-z0-9]+-\d{3}-\d{3}$', barcode):
tool_id = self.env['sf.functional.cutting.tool.entity'].sudo().search(
[('rfid', '=', barcode), ('functional_tool_status', '!=', '已拆除')])
if tool_id:
self.functional_tool_id = tool_id.id
else:
raise ValidationError('Rfid为【%s】的功能刀具不存在,请重新扫描!' % barcode)
else:
if self.dismantle_cause == '更换为其他刀具':
location = self.env['sf.shelf.location'].sudo().search([('barcode', '=', barcode)])
if not location:
raise ValidationError('%s】该货位不存在,请重新扫码!' % barcode)
else:
if not location.product_id:
# 判断功能刀具存在哪些刀具物料需要录入库位
if self.chuck_product_id: # 夹头
if not self.chuck_freight_id:
self.chuck_freight_id = location.id
return True
if self.integral_product_id: # 整体式刀具
if not self.integral_freight_id:
self.integral_freight_id = location.id
return True
elif self.blade_product_id: # 刀片
if not self.blade_freight_id:
self.blade_freight_id = location.id
return True
if self.bar_product_id: # 刀杆
if not self.bar_freight_id:
self.bar_freight_id = location.id
return True
elif self.pad_product_id: # 刀盘
if not self.pad_freight_id:
self.pad_freight_id = location.id
return True
else:
# 判断货位存放的是那个刀具物料产品
if self.integral_product_id == location.product_id: # 整体式刀具
self.integral_freight_id = location.id
elif self.blade_product_id == location.product_id: # 刀片
self.blade_freight_id = location.id
elif self.bar_product_id == location.product_id: # 刀杆
self.bar_freight_id = location.id
elif self.pad_product_id == location.product_id: # 刀盘
self.pad_freight_id = location.id
elif self.chuck_product_id == location.product_id: # 夹头
self.chuck_freight_id = location.id
elif self.dismantle_cause in ['寿命到期报废', '崩刀报废']:
raise ValidationError('该功能刀具因为%s拆解,无需录入库位' % self.dismantle_cause)
else:
raise ValidationError('该功能刀具因为%s拆解,无需录入库位' % self.dismantle_cause)
name = fields.Char('名称', related='functional_tool_id.name')
code = fields.Char('拆解单号', default=lambda self: self._get_code(), readonly=True)
def _get_code(self):
"""
自动生成拆解单编码
"""
new_time = str(fields.Date.today())
datetime = new_time[2:4] + new_time[5:7] + new_time[-2:]
functional_tool_dismantle = self.env['sf.functional.tool.dismantle'].sudo().search(
[('code', 'ilike', datetime)], limit=1, order="id desc")
if not functional_tool_dismantle:
num = "%03d" % 1
else:
m = int(functional_tool_dismantle.code[-3:]) + 1
num = "%03d" % m
return 'GNDJ-CJD-%s-%s' % (datetime, num)
functional_tool_id = fields.Many2one('sf.functional.cutting.tool.entity', '功能刀具', required=True, tracking=True,
domain=[('functional_tool_status', '!=', '已拆除'),
('current_location', '=', '刀具房')])
@api.onchange('functional_tool_id')
def _onchange_functional_tool_id(self):
for item in self:
if item:
dismantle_id = self.search([('functional_tool_id', '=', item.functional_tool_id.id)])
if dismantle_id:
raise ValidationError(f'Rfid为【{item.rfid}】的功能刀具已经存在拆解单,单号是【{dismantle_id[0].code}')
tool_type_id = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型', store=True,
compute='_compute_functional_tool_num')
tool_groups_id = fields.Many2one('sf.tool.groups', '刀具组', compute='_compute_functional_tool_num', store=True)
diameter = fields.Float(string='刀具直径(mm)', compute='_compute_functional_tool_num', store=True)
knife_tip_r_angle = fields.Float(string='刀尖R角(mm)', compute='_compute_functional_tool_num', store=True)
rfid = fields.Char('Rfid', compute='_compute_functional_tool_num', store=True)
rfid_dismantle = fields.Char('Rfid(已拆解)', readonly=True)
dismantle_cause = fields.Selection(
[('寿命到期报废', '寿命到期报废'), ('崩刀报废', '崩刀报废'), ('更换为其他刀具', '更换为其他刀具'),
('刀具需磨削', '刀具需磨削')], string='拆解原因', required=True, tracking=True)
dismantle_data = fields.Datetime('拆解日期', readonly=True)
dismantle_person = fields.Char('拆解人', readonly=True)
image = fields.Binary('图片', readonly=True)
scrap_ids = fields.One2many('stock.scrap', 'functional_tool_dismantle_id', string='报废单号', readonly=True)
grinding_id = fields.Char('磨削单号', readonly=True)
picking_id = fields.Many2one('stock.picking', string='刀具物料调拨单')
picking_num = fields.Integer('调拨单数量', default=0, compute='compute_picking_num', store=True)
@api.depends('picking_id')
def compute_picking_num(self):
for item in self:
if item.picking_id:
item.picking_num = 1
else:
item.picking_num = 0
state = fields.Selection([('待拆解', '待拆解'), ('已拆解', '已拆解')], default='待拆解', tracking=True)
active = fields.Boolean('有效', default=True, groups='base.user_root')
# 刀柄
handle_product_id = fields.Many2one('product.product', string='刀柄', compute='_compute_functional_tool_num',
store=True)
handle_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀柄型号',
related='handle_product_id.cutting_tool_model_id')
handle_brand_id = fields.Many2one('sf.machine.brand', string='刀柄品牌', related='handle_product_id.brand_id')
handle_rfid = fields.Char(string='刀柄Rfid', compute='_compute_functional_tool_num', store=True)
handle_lot_id = fields.Many2one('stock.lot', string='刀柄序列号', compute='_compute_functional_tool_num',
store=True)
scrap_boolean = fields.Boolean(string='刀柄是否报废', default=False, tracking=True, compute='compute_scrap_boolean',
store=True)
@api.depends('dismantle_cause')
def compute_scrap_boolean(self):
for item in self:
if item.dismantle_cause not in ['寿命到期报废', '崩刀报废']:
item.scrap_boolean = False
# 整体式
integral_product_id = fields.Many2one('product.product', string='整体式刀具',
compute='_compute_functional_tool_num', store=True)
integral_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='整体式刀具型号',
related='integral_product_id.cutting_tool_model_id')
integral_brand_id = fields.Many2one('sf.machine.brand', string='整体式刀具品牌',
related='integral_product_id.brand_id')
integral_lot_id = fields.Many2one('stock.lot', string='整体式刀具批次', compute='_compute_functional_tool_num',
store=True)
integral_freight_id = fields.Many2one('sf.shelf.location', '整体式刀具目标货位',
domain="[('product_id', 'in', (integral_product_id, False)),('rotative_Boolean', '=', True)]")
# 刀片
blade_product_id = fields.Many2one('product.product', string='刀片', compute='_compute_functional_tool_num',
store=True)
blade_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀片型号',
related='blade_product_id.cutting_tool_model_id')
blade_brand_id = fields.Many2one('sf.machine.brand', string='刀片品牌', related='blade_product_id.brand_id')
blade_lot_id = fields.Many2one('stock.lot', string='刀片批次', compute='_compute_functional_tool_num', store=True)
blade_freight_id = fields.Many2one('sf.shelf.location', '刀片目标货位',
domain="[('product_id', 'in', (blade_product_id, False)),('rotative_Boolean', '=', True)]")
# 刀杆
bar_product_id = fields.Many2one('product.product', string='刀杆', compute='_compute_functional_tool_num',
store=True)
bar_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀杆型号',
related='bar_product_id.cutting_tool_model_id')
bar_brand_id = fields.Many2one('sf.machine.brand', string='刀杆品牌', related='bar_product_id.brand_id')
bar_lot_id = fields.Many2one('stock.lot', string='刀杆批次', compute='_compute_functional_tool_num', store=True)
bar_freight_id = fields.Many2one('sf.shelf.location', '刀杆目标货位',
domain="[('product_id', 'in', (bar_product_id, False)),('rotative_Boolean', '=', True)]")
# 刀盘
pad_product_id = fields.Many2one('product.product', string='刀盘', compute='_compute_functional_tool_num',
store=True)
pad_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='刀盘型号',
related='pad_product_id.cutting_tool_model_id')
pad_brand_id = fields.Many2one('sf.machine.brand', string='刀盘品牌', related='pad_product_id.brand_id')
pad_lot_id = fields.Many2one('stock.lot', string='刀盘批次', compute='_compute_functional_tool_num', store=True)
pad_freight_id = fields.Many2one('sf.shelf.location', '刀盘目标货位',
domain="[('product_id', 'in', (pad_product_id, False)),('rotative_Boolean', '=', True)]")
# 夹头
chuck_product_id = fields.Many2one('product.product', string='夹头', compute='_compute_functional_tool_num',
store=True)
chuck_type_id = fields.Many2one('sf.cutting_tool.standard.library', string='夹头型号',
related='chuck_product_id.cutting_tool_model_id')
chuck_brand_id = fields.Many2one('sf.machine.brand', string='夹头品牌', related='chuck_product_id.brand_id')
chuck_lot_id = fields.Many2one('stock.lot', string='夹头批次', compute='_compute_functional_tool_num', store=True)
chuck_freight_id = fields.Many2one('sf.shelf.location', '夹头目标货位',
domain="[('product_id', 'in', (chuck_product_id, False)),('rotative_Boolean', '=', True)]")
@api.onchange('functional_tool_id')
def _onchange_freight(self):
for item in self:
item.integral_freight_id = False
item.blade_freight_id = False
item.bar_freight_id = False
item.pad_freight_id = False
item.chuck_freight_id = False
@api.depends('functional_tool_id')
def _compute_functional_tool_num(self):
for item in self:
if item.functional_tool_id:
item.tool_groups_id = item.functional_tool_id.tool_groups_id.id
item.tool_type_id = item.functional_tool_id.sf_cutting_tool_type_id.id
item.diameter = item.functional_tool_id.functional_tool_diameter
item.knife_tip_r_angle = item.functional_tool_id.knife_tip_r_angle
item.rfid = item.functional_tool_id.rfid
item.handle_rfid = item.functional_tool_id.rfid
# 产品
item.handle_product_id = item.functional_tool_id.functional_tool_name_id.handle_product_id.id
item.integral_product_id = item.functional_tool_id.functional_tool_name_id.integral_product_id.id
item.blade_product_id = item.functional_tool_id.functional_tool_name_id.blade_product_id.id
item.bar_product_id = item.functional_tool_id.functional_tool_name_id.bar_product_id.id
item.pad_product_id = item.functional_tool_id.functional_tool_name_id.pad_product_id.id
item.chuck_product_id = item.functional_tool_id.functional_tool_name_id.chuck_product_id.id
# 批次/序列号
item.handle_lot_id = item.functional_tool_id.functional_tool_name_id.handle_code_id.id
item.integral_lot_id = item.functional_tool_id.functional_tool_name_id.integral_lot_id.id
item.blade_lot_id = item.functional_tool_id.functional_tool_name_id.blade_lot_id.id
item.bar_lot_id = item.functional_tool_id.functional_tool_name_id.bar_lot_id.id
item.pad_lot_id = item.functional_tool_id.functional_tool_name_id.pad_lot_id.id
item.chuck_lot_id = item.functional_tool_id.functional_tool_name_id.chuck_lot_id.id
else:
item.tool_groups_id = False
item.tool_type_id = False
item.diameter = 0
item.knife_tip_r_angle = 0
item.rfid = ''
item.handle_rfid = ''
item.handle_product_id = False
item.integral_product_id = False
item.blade_product_id = False
item.bar_product_id = False
item.pad_product_id = False
item.chuck_product_id = False
item.handle_lot_id = False
item.integral_lot_id = False
item.blade_lot_id = False
item.bar_lot_id = False
item.pad_lot_id = False
item.chuck_lot_id = False
def location_duplicate_check(self):
"""
目标货位去重校验
"""
if self.blade_freight_id:
if self.bar_freight_id:
if self.blade_freight_id == self.bar_freight_id:
raise ValidationError('【刀片】和【刀杆】的目标货位重复,请重新选择!')
elif self.pad_freight_id:
if self.blade_freight_id == self.pad_freight_id:
raise ValidationError('【刀片】和【刀盘】的目标货位重复,请重新选择!')
if self.chuck_freight_id:
if self.chuck_freight_id == self.integral_freight_id:
raise ValidationError('【夹头】和【整体式刀具】的目标货位重复,请重新选择!')
if self.chuck_freight_id == self.blade_freight_id:
raise ValidationError('【夹头】和【刀片】的目标货位重复,请重新选择!')
if self.chuck_freight_id == self.bar_freight_id:
raise ValidationError('【夹头】和【刀杆】的目标货位重复,请重新选择!')
if self.chuck_freight_id == self.pad_freight_id:
raise ValidationError('【夹头】和【刀盘】的目标货位重复,请重新选择!')
def tool_scrap(self):
self.scrap_boolean = True
def tool_no_scrap(self):
self.scrap_boolean = False
def confirmation_disassembly(self):
logging.info('%s刀具确认开始拆解' % self.dismantle_cause)
code = self.code
if self.functional_tool_id.functional_tool_status == '已拆除':
raise ValidationError('Rfid为【%s】名称为【%s】的功能刀具已经拆解,请勿重复操作!' % (
self.functional_tool_id.rfid_dismantle, self.name))
# 对拆解的功能刀具进行校验,只有在刀具房的功能刀具才能拆解
elif self.functional_tool_id.functional_tool_status != '报警':
if self.functional_tool_id.tool_room_num == 0:
raise ValidationError('Rfid为【%s】的功能刀具当前位置为【%s】,不能进行拆解!' % (
self.rfid, self.functional_tool_id.current_location))
# 目标重复校验
self.location_duplicate_check()
datas = {'scrap': [], 'picking': []}
# =================刀柄是否[报废]拆解=======
if self.handle_rfid:
lot = self.env['stock.lot'].sudo().search([('rfid', '=', self.handle_rfid)])
if not lot:
raise ValidationError('Rfid为【%s】的刀柄序列号不存在!' % self.handle_rfid)
if self.scrap_boolean:
# 刀柄报废 入库到Scrap
datas['scrap'].append({'lot_id': lot})
lot.tool_material_status = '报废'
else:
# 刀柄不报废 入库到刀具房
datas['picking'].append({'lot_id': lot, 'destination': self.env['sf.shelf.location']})
lot.tool_material_status = '可用'
# ==============功能刀具[报废]拆解================
if self.dismantle_cause in ['寿命到期报废', '崩刀报废']:
# 除刀柄外物料报废 入库到Scrap
if self.integral_product_id:
datas['scrap'].append({'lot_id': self.integral_lot_id})
elif self.blade_product_id:
datas['scrap'].append({'lot_id': self.blade_lot_id})
if self.bar_product_id:
datas['scrap'].append({'lot_id': self.bar_lot_id})
elif self.pad_product_id:
datas['scrap'].append({'lot_id': self.pad_lot_id})
if self.chuck_product_id:
datas['scrap'].append({'lot_id': self.chuck_lot_id})
# ==============功能刀具[更换,磨削]拆解==============
elif self.dismantle_cause in ['更换为其他刀具', '刀具需磨削']:
# 除刀柄外物料拆解 入库到具体货位
if self.integral_freight_id:
datas['picking'].append({'lot_id': self.integral_lot_id, 'destination': self.integral_freight_id})
elif self.blade_freight_id:
datas['picking'].append({'lot_id': self.blade_lot_id, 'destination': self.blade_freight_id})
if self.bar_freight_id:
datas['picking'].append({'lot_id': self.bar_lot_id, 'destination': self.bar_freight_id})
elif self.pad_freight_id:
datas['picking'].append({'lot_id': self.pad_lot_id, 'destination': self.pad_freight_id})
if self.chuck_freight_id:
datas['picking'].append({'lot_id': self.chuck_lot_id, 'destination': self.chuck_freight_id})
self.create_tool_picking_scrap(datas)
# ===============创建功能刀具拆解移动记录=====
self.env['stock.move'].create_functional_tool_stock_move(self)
# 修改功能刀具数据
self.functional_tool_id.write({
'rfid_dismantle': self.functional_tool_id.rfid,
'rfid': '',
'functional_tool_status': '已拆除'
})
# 修改拆解单的值
self.write({
'dismantle_data': fields.Datetime.now(),
'dismantle_person': self.env.user.name,
'rfid': '%s(已拆解)' % self.rfid,
'state': '已拆解'
})
# ==================修改刀具预警信息的值============
warning_id = self.env['sf.functional.tool.warning'].sudo().search(
[('functional_tool_id', '=', self.functional_tool_id.id)])
if warning_id:
warning_id.sudo().write({
'dispose_user': self.env.user.name,
'dispose_time': fields.Datetime.now()
})
logging.info('%s】刀具拆解成功!' % self.name)
# ==================根据条件创建功能刀具组装单=======================
# 如果报废原因为【寿命到期报废】并且刀柄不报废时, 创建功能刀具组装单
if self.dismantle_cause in ['寿命到期报废'] and not self.scrap_boolean:
# 创建组装单
assembly_id = self.env['sf.functional.tool.assembly'].sudo().create({
'functional_tool_name': self.functional_tool_id.name,
'handle_code_id': self.handle_lot_id.id,
'handle_product_id': self.handle_product_id.id,
'loading_task_source': '3',
'use_tool_time': fields.Datetime.now() + timedelta(hours=4),
'reason_for_applying': '刀具寿命到期'
})
return {
'type': 'ir.actions.act_window',
'res_model': 'sf.functional.tool.assembly',
'view_type': 'form',
'view_mode': 'form',
'res_id': assembly_id.id,
# 'target': 'new'
}
# {
# 'type': 'ir.actions.client',
# 'tag': 'display_notification',
# 'params': {
# 'title': '组装单创建完成',
# 'message': '请组装同名称的功能刀具',
# 'type': 'info'
# }
# }
# 'params': {
# 'title': _('The following replenishment order has been generated'),
# 'message': '%s',
# 'links': [{
# 'label': order.display_name,
# 'url': f'#action={action.id}&id={order.id}&model=purchase.order',
# }],
# 'sticky': False,
# }
def create_tool_picking_scrap(self, datas):
scrap_data = datas['scrap']
picking_data = datas['picking']
if scrap_data:
for data in scrap_data:
if data:
self.env['stock.scrap'].create_tool_dismantle_stock_scrap(data['lot_id'], self)
if picking_data:
picking_id = self.env['stock.picking'].create_tool_dismantle_picking(self)
self.picking_id = picking_id.id
self.env['stock.move'].create_tool_stock_move({'data': picking_data, 'picking_id': picking_id})
# 将刀具物料出库库单的状态更改为就绪
picking_id.action_confirm()
# 修改刀具物料出库移动历史记录
self.env['stock.move'].write_tool_stock_move_line({'data': picking_data, 'picking_id': picking_id})
# 设置数量,并验证完成
picking_id.action_set_quantities_to_reservation()
picking_id.button_validate()
def action_open_reference1(self):
self.ensure_one()
return {
'res_model': self._name,
'type': 'ir.actions.act_window',
'views': [[False, "form"]],
'res_id': self.id,
}
def open_function_tool_stock_move_line(self):
action = self.env.ref('sf_tool_management.sf_inbound_and_outbound_records_of_functional_tools_view_act')
result = action.read()[0]
result['domain'] = [('functional_tool_dismantle_id', '=', self.id), ('qty_done', '>', 0)]
return result
def open_tool_stock_picking(self):
action = self.env.ref('stock.action_picking_tree_all')
result = action.read()[0]
result['domain'] = [('origin', '=', self.code)]
return result
class StockPicking(models.Model):
_inherit = 'stock.picking'
def create_tool_dismantle_picking(self, obj):
"""
创建刀具物料入库单
"""
# 获取名称为内部调拨的作业类型
picking_type_id = self.env['stock.picking.type'].sudo().search([('name', '=', '内部调拨')])
location_id = self.env['stock.location'].search([('name', '=', '刀具组装位置')])
location_dest_id = self.env['stock.location'].search([('name', '=', '刀具房')])
if not location_id:
raise ValidationError('缺少名称为【刀具组装位置】的仓库管理地点')
if not location_dest_id:
raise ValidationError('缺少名称为【刀具房】的仓库管理地点')
# 创建刀具物料出库单
picking_id = self.env['stock.picking'].create({
'name': self._get_name_stock1(picking_type_id),
'picking_type_id': picking_type_id.id,
'location_id': location_id.id,
'location_dest_id': location_dest_id.id,
'origin': obj.code
})
return picking_id
class StockMove(models.Model):
_inherit = 'stock.move'
def create_tool_stock_move(self, datas):
picking_id = datas['picking_id']
data = datas['data']
stock_move_ids = []
for res in data:
if res:
# 创建库存移动记录
stock_move_id = self.env['stock.move'].sudo().create({
'name': picking_id.name,
'picking_id': picking_id.id,
'product_id': res['lot_id'].product_id.id,
'location_id': picking_id.location_id.id,
'location_dest_id': picking_id.location_dest_id.id,
'product_uom_qty': 1.00,
'reserved_availability': 1.00
})
stock_move_ids.append(stock_move_id)
return stock_move_ids
def write_tool_stock_move_line(self, datas):
picking_id = datas['picking_id']
data = datas['data']
move_line_ids = picking_id.move_line_ids
for move_line_id in move_line_ids:
for res in data:
if move_line_id.product_id == res['lot_id'].product_id:
move_line_id.write({
'destination_location_id': res.get('destination').id,
'lot_id': res.get('lot_id').id
})
return True
def create_functional_tool_stock_move(self, dismantle_id):
"""
对功能刀具拆解过程的功能刀具进行库存移动,以及创建移动历史
"""
location_dismantle_id = self.env['stock.location'].search([('name', '=', '拆解')])
if not location_dismantle_id:
raise ValidationError('缺少名称为【拆解】的仓库管理地点')
tool_id = dismantle_id.functional_tool_id
# 创建库存移动记录
stock_move_id = self.env['stock.move'].sudo().create({
'name': dismantle_id.code,
'product_id': tool_id.barcode_id.product_id.id,
'location_id': tool_id.current_location_id.id,
'location_dest_id': location_dismantle_id.id,
'product_uom_qty': 1.00,
'state': 'done'
})
# 创建移动历史记录
stock_move_line_id = self.env['stock.move.line'].sudo().create({
'product_id': tool_id.barcode_id.product_id.id,
'functional_tool_dismantle_id': dismantle_id.id,
'lot_id': tool_id.barcode_id.id,
'move_id': stock_move_id.id,
'qty_done': 1.0,
'state': 'done',
'functional_tool_type_id': tool_id.sf_cutting_tool_type_id.id,
'diameter': tool_id.functional_tool_diameter,
'knife_tip_r_angle': tool_id.knife_tip_r_angle,
'code': tool_id.code,
'rfid': tool_id.rfid,
'functional_tool_name': tool_id.name,
'tool_groups_id': tool_id.tool_groups_id.id
})
return stock_move_id, stock_move_line_id
class CustomStockScrap(models.Model):
_inherit = 'stock.scrap'
functional_tool_dismantle_id = fields.Many2one('sf.functional.tool.dismantle', string="功能刀具拆解单")
def create_tool_dismantle_stock_scrap(self, lot, dismantle_id):
location_id = self.env['stock.location'].search([('name', '=', '刀具组装位置')])
scrap_location_id = self.env['stock.location'].search([('name', 'in', ('Scrap', '报废'))])
if not location_id:
raise ValidationError('缺少名称为【刀具组装位置】的仓库管理地点')
if not scrap_location_id:
raise ValidationError('缺少名称为【Scrap】或【Scrap】的仓库管理地点')
stock_scrap_id = self.create({
'product_id': lot.product_id.id,
'lot_id': lot.id,
'location_id': location_id.id,
'scrap_location_id': scrap_location_id.id,
'functional_tool_dismantle_id': dismantle_id.id,
'origin': dismantle_id.code
})
# 完成报废单
stock_scrap_id.action_validate()
return stock_scrap_id