Files
jikimo_sf/sf_tool_management/models/maintenance_equipment.py

126 lines
5.5 KiB
Python

import qrcode
import base64
from odoo import models, api, fields
from odoo.exceptions import ValidationError
from io import BytesIO
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')
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.Integer('直径(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('机内装刀时间')
@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 StockLot(models.Model):
_inherit = 'stock.lot'
tool_material_search_id = fields.Many2one('sf.tool.material.search', string='刀具物料搜索')
tool_material_status = fields.Selection([('可用', '可用'), ('在用', '在用'), ('报废', '报废')], string='状态',
compute='_compute_tool_material_status')
@api.depends('quant_ids')
def _compute_tool_material_status(self):
for record in self:
if record:
if record.quant_ids[-1].location_id.name == '刀具组装位置':
record.tool_material_status = '在用'
else:
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(
[('cutting_tool_material_id', '=', record.product_id.cutting_tool_material_id.id),
('cutting_tool_standard_library_id', '=', record.product_id.cutting_tool_model_id.id),
('specification_id', '=', record.product_id.specification_id.id)])
if tool_material_search:
record.tool_material_search_id = tool_material_search
return records
qr_code_image = fields.Binary(string='二维码', compute='_generate_qr_code')
@api.depends('name')
def _generate_qr_code(self):
for record in self:
# Generate QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(record.name)
qr.make(fit=True)
# Create an image from the QR Code instance
qr_image = qr.make_image(fill_color="black", back_color="white")
# Save the image to a BytesIO object
image_stream = BytesIO()
qr_image.save(image_stream, format="PNG")
# Store the BytesIO object in the binary field
record.qr_code_image = base64.b64decode(image_stream.getvalue())
# record.qr_code_image = image_stream.getvalue()
print(record.qr_code_image)
class ProductProduct(models.Model):
_inherit = 'product.product'
@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 == '刀具':
self.env['sf.tool.material.search'].sudo().create({
'product_id': record.id
})
return records