import json import requests import logging from odoo import models, api, fields from odoo.exceptions import ValidationError from datetime import datetime, timedelta from odoo.addons.sf_base.commons.common import Common class SfMaintenanceEquipmentTool(models.Model): _inherit = 'maintenance.equipment.tool' functional_tool_name_id = fields.Many2one('sf.functional.cutting.tool.entity', '功能刀具名称') image = fields.Binary('图片', related='functional_tool_name_id.image') tool_code = fields.Char('功能刀具编码', related='functional_tool_name_id.code') rfid = fields.Char('Rfid', related='functional_tool_name_id.rfid') functional_tool_type = fields.Char('功能刀具类型', related='functional_tool_name_id.sf_cutting_tool_type_id.name') tool_groups = fields.Char('刀具组', related='functional_tool_name_id.tool_groups_id.name') diameter = fields.Float('直径(mm)', related='functional_tool_name_id.functional_tool_diameter') knife_tip_r_angle = fields.Float('刀尖R角(mm)', related='functional_tool_name_id.knife_tip_r_angle') life_value_max = fields.Integer('最大寿命值(min)', related='functional_tool_name_id.max_lifetime_value') alarm_value = fields.Integer('报警值(min)', related='functional_tool_name_id.alarm_value') used_value = fields.Integer('已使用值(min)', related='functional_tool_name_id.used_value') tool_install_time = fields.Datetime('机内装刀时间') # def write_tool(self, datas): # if datas: # print(datas) # @api.model_create_multi # def create(self, vals_list): # tools = super().create(vals_list) # for tool in tools: # self.env['sf.machine.table.tool.changing.apply'].sudo().create({ # 'maintenance_equipment_id': tool.equipment_id.id, # 'cutter_spacing_code_id': tool.id # }) # return tools class SfMaintenanceEquipment(models.Model): _inherit = 'maintenance.equipment' _description = '设备机床刀库' # ==========机床当前刀库实时信息接口========== def register_equipment_tool(self): try: 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/GetToolInfos" params = {"DeviceId": self.name} r = requests.get(crea_url, params=params, headers=headers) ret = r.json() logging.info('机床刀库register_equipment_tool():%s' % ret) datas = ret['Datas'] self.write_maintenance_equipment_tool(datas) if ret['Succeed']: return "机床当前刀库实时信息指令发送成功" else: raise ValidationError("机床当前刀库实时信息指令发送失败") except Exception as e: logging.info("register_equipment_tool()捕获错误信息:%s" % e) def write_maintenance_equipment_tool(self, datas): try: if datas: # 清除设备机床刀位的刀具信息 for obj in self.product_template_ids: obj.write({ 'functional_tool_name_id': False, 'tool_install_time': None }) for data in datas: maintenance_equipment_id = self.search([('name', '=', data['DeviceId'])]) if maintenance_equipment_id: tool_id = '%s%s' % (data['ToolId'][0:1], data['ToolId'][1:].zfill(2)) equipment_tool_id = self.env['maintenance.equipment.tool'].sudo().search( [('equipment_id', '=', maintenance_equipment_id.id), ('code', '=', tool_id)]) functional_tool_id = self.env['sf.functional.cutting.tool.entity'].sudo().search( [('rfid', '=', data['RfidCode'])]) if functional_tool_id: if len(functional_tool_id) > 1: functional_tool_id = functional_tool_id[-1] # 查询该功能刀具是否已经装在机床内其他位置,如果是就删除 equipment_tools = self.env['maintenance.equipment.tool'].sudo().search( [('functional_tool_name_id', '=', functional_tool_id.id), ('code', '!=', tool_id)]) if equipment_tools: for item in equipment_tools: item.write({ 'functional_tool_name_id': False, 'tool_install_time': None }) else: logging.info('Rfid为【%s】的功能刀具不存在!' % data['RfidCode']) time = None if data['AddDatetime']: datatime = str(data['AddDatetime']) time = fields.Datetime.from_string(datatime[0:10] + ' ' + datatime[11:19]) if equipment_tool_id and functional_tool_id: tool_install_time = {'Nomal': '正常', 'Warning': '报警'} equipment_tool_id.write({ 'functional_tool_name_id': functional_tool_id.id, 'tool_install_time': time - timedelta(hours=8) }) if functional_tool_id.current_location != '机内刀库': # 对功能刀具进行移动到生产线 functional_tool_id.tool_inventory_displacement_out() data_tool = { 'max_lifetime_value': data['MaxLife'], 'used_value': data['UseLife'], 'functional_tool_status': tool_install_time.get(data['State']) } if (functional_tool_id.functional_tool_status != '报警' and tool_install_time.get(data['State']) == '报警'): functional_tool_id.write(data_tool) functional_tool_id.create_tool_dismantle() else: functional_tool_id.write(data_tool) else: logging.info('获取的【%s】设备不存在!!!' % data['DeviceId']) else: logging.info('没有获取到【%s】设备的刀具库信息!!!' % self.name) except Exception as e: logging.info("write_maintenance_equipment_tool()捕获错误信息:%s" % e) class StockLot(models.Model): _inherit = 'stock.lot' tool_material_search_id = fields.Many2one('sf.tool.material.search', string='刀具物料搜索') fixture_material_search_id = fields.Many2one('sf.fixture.material.search', string='夹具物料搜索') tool_material_status = fields.Selection( [('未入库', '未入库'), ('可用', '空闲'), ('在用', '占用'), ('报废', '报废')], string='状态', compute='_compute_tool_material_status', store=True) @api.depends('quant_ids') def _compute_tool_material_status(self): for record in self: if record: if record.product_id.categ_id.name in ['刀具']: if record.quant_ids: if record.quant_ids[-1].location_id.name in ['刀具房']: record.tool_material_status = '可用' elif record.quant_ids[-1].location_id.name == '刀具组装位置': record.tool_material_status = '在用' elif record.quant_ids[-1].location_id.name == 'Scrap': record.tool_material_status = '报废' else: record.tool_material_status = '未入库' elif record.product_id.categ_id.name in ['夹具']: if record.quant_ids: if record.quant_ids[-1].location_id.name in ['夹具房']: record.tool_material_status = '可用' @api.model def name_search(self, name='', args=None, operator='ilike', limit=100): # 调用父类的name_search方法 records = super(StockLot, self).name_search(name=name, args=args, operator=operator, limit=limit) if records: return records else: # 在调用父类方法之后执行自定义逻辑 self.tool_verify(args, name) return records def tool_verify(self, args, name): # 刀具物料验证 if 5 >= len(args) > 3: objs = self.search([('name', '=', name), ('quant_ids.location_id.name', 'in', ['刀具房']), ('quant_ids.quantity', '>', 0)]) if args[2][2] in ['整体式刀具', '刀片', '刀杆', '刀盘', '刀柄', '夹头']: if objs.product_id.categ_id.name == '刀具': raise ValidationError('这是【%s】物料,请扫入正确的【%s】物料!!!' % ( objs.product_id.cutting_tool_material_id.name, args[2][2])) @api.model_create_multi def create(self, vals_list): records = super(StockLot, self).create(vals_list) for record in records: if record.product_id.categ_id.name == '刀具': tool_material_search = self.env['sf.tool.material.search'].sudo().search( [('id', '=', record.product_id.tool_material_id)]) if tool_material_search: record.tool_material_status = '未入库' record.tool_material_search_id = tool_material_search.id elif record.product_id.categ_id.name == '夹具': fixture_material_search = self.env['sf.fixture.material.search'].sudo().search( [('id', '=', record.product_id.fixture_material_search_id)]) if fixture_material_search: record.tool_material_status = '未入库' record.fixture_material_search_id = fixture_material_search.id return records class ProductProduct(models.Model): _inherit = 'product.product' tool_material_id = fields.Char('刀具物料搜索模型ID') fixture_material_search_id = fields.Char('夹具物料搜索模型ID') @api.model_create_multi def create(self, vals_list): records = super(ProductProduct, self).create(vals_list) for record in records: if record.categ_id.name == '刀具': tool_material = self.env['sf.tool.material.search'].sudo().create({ 'product_id': record.id }) record.tool_material_id = tool_material.id elif record.categ_id.name == '夹具': fixture_material = self.env['sf.fixture.material.search'].sudo().create({ 'product_id': record.id }) record.fixture_material_search_id = fixture_material.id return records