优化扫码部分

This commit is contained in:
mgw
2023-01-08 16:51:24 +08:00
parent 7c4baa4d91
commit d088bc1c86

View File

@@ -4,6 +4,7 @@ import math
import requests import requests
import logging import logging
import base64 import base64
import hashlib
# import subprocess # import subprocess
from datetime import datetime from datetime import datetime
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
@@ -36,20 +37,22 @@ class ResMrpWorkOrder(models.Model):
uid = self.env.uid uid = self.env.uid
for workorder in self: for workorder in self:
if workorder.users_ids: if workorder.users_ids:
list_user_id=[]
for item in workorder.users_ids: for item in workorder.users_ids:
if item.id == uid: list_user_id.append(item.id)
workorder.user_permissions = True if uid in list_user_id:
else: workorder.user_permissions = True
workorder.user_permissions = False else:
workorder.user_permissions = False
else:workorder.user_permissions=False else:workorder.user_permissions=False
user_permissions = fields.Boolean('用户权限', compute='get_user_permissions') user_permissions = fields.Boolean('用户权限', compute='get_user_permissions')
programming_no = fields.Char('编程单号') programming_no = fields.Char('编程单号')
is_programming = fields.Boolean('是否编程', default=False) work_state = fields.Char('业务状态')
programming_state = fields.Char('编程状态')
cnc_worksheet = fields.Binary( cnc_worksheet = fields.Binary(
'工作指令', readonly=True) '工作指令', readonly=True)
material_center_point = fields.Char(string='料中心点') material_center_point = fields.Char(string='料中心点')
X1_axis = fields.Float(default=0) X1_axis = fields.Float(default=0)
Y1_axis = fields.Float(default=0) Y1_axis = fields.Float(default=0)
Z1_axis = fields.Float(default=0) Z1_axis = fields.Float(default=0)
@@ -136,6 +139,7 @@ class ResMrpWorkOrder(models.Model):
'name': route.route_workcenter_id.name, 'name': route.route_workcenter_id.name,
'processing_panel': k, 'processing_panel': k,
'routing_type': route.routing_type, 'routing_type': route.routing_type,
'work_state': '' if not route.routing_type == '获取CNC加工程序' else '待发起',
'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids), 'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids),
'date_planned_start': False, 'date_planned_start': False,
'date_planned_finished': False, 'date_planned_finished': False,
@@ -253,23 +257,34 @@ class ResMrpWorkOrder(models.Model):
else: else:
return True return True
def fetchCNCing(self):
return None
# cnc程序获取 # cnc程序获取
def fetchCNC(self): def fetchCNC(self):
cnc = self.env['mrp.workorder'].search( cnc = self.env['mrp.workorder'].search(
[('routing_type', '=', 'CNC加工'), ('production_id', '=', self.production_id.id)], limit=1) [('routing_type', '=', 'CNC加工'), ('production_id', '=', self.production_id.id)], limit=1)
res = {'model_code': cnc.product_id.barcode, 'production_no': self.production_id.name, if cnc.product_id.upload_model_file:
attachment = cnc.product_id.upload_model_file[0]
base64_data = base64.b64encode(attachment.datas)
base64_datas = base64_data.decode('utf-8')
model_code = hashlib.sha1(base64_datas.encode('utf-8')).hexdigest()
res = {'model_code': '' if not cnc.product_id.upload_model_file else model_code,
'production_no': self.production_id.name,
'machine_tool_code': cnc.workcenter_id.machine_tool_id.code, 'machine_tool_code': cnc.workcenter_id.machine_tool_id.code,
'material_code': cnc.env['sf.production.materials'].search( 'material_code': cnc.env['sf.production.materials'].search(
[('id', '=', cnc.product_id.materials_id.id)]).materials_no, [('id', '=', cnc.product_id.materials_id.id)]).materials_no,
'material_type_code': cnc.env['sf.materials.model'].search( 'material_type_code': cnc.env['sf.materials.model'].search(
[('id', '=', cnc.product_id.materials_type_id.id)]).materials_no, [('id', '=', cnc.product_id.materials_type_id.id)]).materials_no,
'machining_processing_panel': cnc.product_id.model_processing_panel,
'machining_precision': cnc.product_id.model_machining_precision, 'machining_precision': cnc.product_id.model_machining_precision,
'embryo_long': cnc.product_id.bom_ids.bom_line_ids.product_id.length, 'embryo_long': cnc.product_id.bom_ids.bom_line_ids.product_id.length,
'embryo_height': cnc.product_id.bom_ids.bom_line_ids.product_id.height, 'embryo_height': cnc.product_id.bom_ids.bom_line_ids.product_id.height,
'embryo_width': cnc.product_id.bom_ids.bom_line_ids.product_id.width, 'embryo_width': cnc.product_id.bom_ids.bom_line_ids.product_id.width,
'order_no': cnc.production_id.origin, 'order_no': cnc.production_id.origin,
'user': self.env.user.name, 'user': self.env.user.name,
'model_file': base64.b64encode(cnc.product_id.model_file).decode('utf-8') 'model_file': '' if not cnc.product_id.model_file else base64.b64encode(
cnc.product_id.model_file).decode('utf-8')
} }
logging.info('res:%s' % res) logging.info('res:%s' % res)
configsettings = self.env['res.config.settings'].get_values() configsettings = self.env['res.config.settings'].get_values()
@@ -279,9 +294,16 @@ class ResMrpWorkOrder(models.Model):
# res_str = json.dumps(res) # res_str = json.dumps(res)
ret = requests.post(config_url, json={}, data=res, headers=config_header) ret = requests.post(config_url, json={}, data=res, headers=config_header)
ret = ret.json() ret = ret.json()
result = json.loads(ret['result']) if ret['status'] == 1:
if result['status'] == 1: self.write({'programming_no': ret['programming_no'], 'programming_state': '编程中', 'work_state': '编程中'})
return self.write({'programming_no': result['programming_no'], 'is_programming': True}) return {
'name': _("工单"),
'view_mode': 'form',
'res_model': 'mrp.workorder',
'res_id': self.id,
'type': 'ir.actions.act_window',
'target': 'new'
}
def json_workorder_str1(self, k, production, route): def json_workorder_str1(self, k, production, route):
workorders_values_str = [0, '', { workorders_values_str = [0, '', {
@@ -291,6 +313,7 @@ class ResMrpWorkOrder(models.Model):
'name': route.route_workcenter_id.name, 'name': route.route_workcenter_id.name,
'processing_panel': k, 'processing_panel': k,
'routing_type': route.routing_type, 'routing_type': route.routing_type,
'work_state': '' if not route.routing_type == '获取CNC加工程序' else '待发起',
'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids), 'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids),
'date_planned_start': False, 'date_planned_start': False,
'date_planned_finished': False, 'date_planned_finished': False,
@@ -303,7 +326,6 @@ class ResMrpWorkOrder(models.Model):
def button_start(self): def button_start(self):
if self.state == 'waiting' or self.state == 'ready': if self.state == 'waiting' or self.state == 'ready':
self.ensure_one() self.ensure_one()
if any(not time.date_end for time in self.time_ids.filtered(lambda t: t.user_id.id == self.env.user.id)): if any(not time.date_end for time in self.time_ids.filtered(lambda t: t.user_id.id == self.env.user.id)):
return True return True
# As button_start is automatically called in the new view # As button_start is automatically called in the new view
@@ -370,28 +392,39 @@ class CNCprocessing(models.Model):
workorder_id = fields.Many2one('mrp.workorder', string="工单") workorder_id = fields.Many2one('mrp.workorder', string="工单")
# mrs下发编程单创建CNC加工 # mrs下发编程单创建CNC加工
def cnc_processing_create(self, obj): def cnc_processing_create(self, cncObj, production_order_no):
workorder = self.env['mrp.workorder'].search([('production_id.name', '=', obj['production_order_no']), cnc_processing = self.env['mrp.workorder'].search([('production_id.name', '=', production_order_no),
('processing_panel', '=', obj['processing_panel']), ('routing_type', '=', '获取CNC加工程序'),
('routing_type', '=', 'CNC加工')]) ('state', '=', 'progress')])
vals = { # cnc.workorder_id.state = 'done'
'workorder_id': workorder.id, if cnc_processing:
'sequence_number': obj['sequence_number'], for obj in cncObj:
'program_name': obj['program_name'], workorder = self.env['mrp.workorder'].search([('production_id.name', '=', production_order_no),
'cutting_tool_name': obj['cutting_tool_name'], ('processing_panel', '=', obj['processing_panel']),
'cutting_tool_no': obj['cutting_tool_no'], ('routing_type', '=', 'CNC加工')])
'processing_type': obj['processing_type'], self.env['sf.cnc.processing'].create({
'margin_x_y': obj['margin_x_y'], 'workorder_id': workorder.id,
'margin_z': obj['margin_z'], 'sequence_number': obj['sequence_number'],
'depth_of_processing_z': obj['depth_of_processing_z'], 'program_name': obj['program_name'],
'cutting_tool_extension_length': obj['cutting_tool_extension_length'], 'cutting_tool_name': obj['cutting_tool_name'],
'cutting_tool_handle_type': obj['cutting_tool_handle_type'], 'cutting_tool_no': obj['cutting_tool_no'],
'estimated_processing_time': obj['estimated_processing_time'], 'processing_type': obj['processing_type'],
'remark': obj['remark'] 'margin_x_y': obj['margin_x_y'],
} 'margin_z': obj['margin_z'],
return self.env['sf.cnc.processing'].create(vals) 'depth_of_processing_z': obj['depth_of_processing_z'],
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
'estimated_processing_time': obj['estimated_processing_time'],
'remark': obj['remark']
})
cnc_processing.state = 'done'
cnc_processing.work_state = '已编程'
return True
else:
return False
# 创建附件(nc文件)
# 创建附件(nc文件)
def attachment_create(self, name, data): def attachment_create(self, name, data):
attachment = self.env['ir.attachment'].create({ attachment = self.env['ir.attachment'].create({
'datas': base64.b64encode(data), 'datas': base64.b64encode(data),
@@ -402,9 +435,9 @@ class CNCprocessing(models.Model):
return attachment return attachment
# 将FTP的nc文件下载到临时目录 # 将FTP的nc文件下载到临时目录
def download_file_tmp(self, model_code, processing_panel): def download_file_tmp(self, production_no, processing_panel):
remotepath = os.path.join('/', model_code, 'return', processing_panel) remotepath = os.path.join('/', production_no, 'return', processing_panel)
serverdir = os.path.join('/tmp', model_code, 'return', processing_panel) serverdir = os.path.join('/tmp', production_no, 'return', processing_panel)
ftp = FtpController() ftp = FtpController()
ftp.download_file_tree(remotepath, serverdir) ftp.download_file_tree(remotepath, serverdir)
return serverdir return serverdir
@@ -420,6 +453,20 @@ class CNCprocessing(models.Model):
else: else:
return False return False
# 将nc文件对应的excel清单转为pdf
# def to_pdf(self, excel_path, pdf_path):
# """
# 需要在linux中下载好libreoffice
# """
# logging.info('pdf_path:%s' % pdf_path)
# logging.info('pdf_path:%s' % excel_path)
# # 注意cmd中的libreoffice要和linux中安装的一致
# cmd = 'soffice --headless --convert-to pdf'.split() + [excel_path] + ['--outdir'] + [pdf_path]
# p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
# # p.wait(timeout=30) # 停顿30秒等待转化
# # stdout, stderr = p.communicate()
# p.communicate()
class SfWorkOrderBarcodes(models.Model): class SfWorkOrderBarcodes(models.Model):
""" """
@@ -430,6 +477,9 @@ class SfWorkOrderBarcodes(models.Model):
def on_barcode_scanned(self, barcode): def on_barcode_scanned(self, barcode):
tray_code = self.env['sf.tray'].search([('code', '=', barcode)]) tray_code = self.env['sf.tray'].search([('code', '=', barcode)])
self.tray_code = tray_code.code # 在这里,用等号赋值只是一次性的,只是一种展示
# self.tray_code = tray_code.code
# 用write方法写入数据库是永久的
self.write({'tray_code': tray_code.code})
workorder = self.env['mrp.workorder'].browse(self.ids) workorder = self.env['mrp.workorder'].browse(self.ids)
workorder.gettray_auto(barcode) workorder.gettray_auto(barcode)