# -*- coding: utf-8 -*- # Part of YiZuo. See LICENSE file for full copyright and licensing details. import logging from odoo.exceptions import UserError, ValidationError from datetime import datetime from odoo import models, api, fields, _ class ReworkWizard(models.TransientModel): _name = 'sf.rework.wizard' _description = '返工向导' workorder_id = fields.Many2one('mrp.workorder', string='工单') product_id = fields.Many2one('product.product') production_id = fields.Many2one('mrp.production', string='制造订单号') rework_reason = fields.Selection( [("programming", "编程"), ("cutter", "刀具"), ("clamping", "装夹"), ("operate computer", "操机"), ("technology", "工艺"), ("customer redrawing", "客户改图")], string="原因") detailed_reason = fields.Text('详细原因') routing_type = fields.Selection([ ('装夹预调', '装夹预调'), ('CNC加工', 'CNC加工')], string="工序类型") # 根据工单的加工面来显示 processing_panel_id = fields.Many2many('sf.processing.panel', string="加工面") is_reprogramming = fields.Boolean(string='申请重新编程', default=False) is_reprogramming_readonly = fields.Boolean(string='申请重新编程(只读)', default=False) reprogramming_num = fields.Integer('重新编程次数', default=0) programming_state = fields.Selection( [('待编程', '待编程'), ('编程中', '编程中'), ('已编程', '已编程'), ('已编程未下发', '已编程未下发'), ('已下发', '已下发')], string='编程状态') tool_state = fields.Selection(string='功能刀具状态', related='production_id.tool_state') def confirm(self): if self.routing_type in ['装夹预调', 'CNC加工']: self.workorder_id.is_rework = True self.production_id.write({'detection_result_ids': [(0, 0, { 'rework_reason': self.rework_reason, 'detailed_reason': self.detailed_reason, 'processing_panel': self.workorder_id.processing_panel, 'routing_type': self.workorder_id.routing_type, 'handle_result': '待处理' if self.workorder_id.test_results == '返工' or self.workorder_id.is_rework is True else '', 'test_results': '返工' if not self.routing_type == 'CNC加工' else self.workorder_id.test_results, 'test_report': self.workorder_id.detection_report})]}) self.workorder_id.button_finish() else: if self.production_id.workorder_ids: handle_result = self.production_id.detection_result_ids.filtered( lambda dr: dr.handle_result == '待处理') if handle_result: processing_panels_to_handle = set(handle_item.processing_panel for handle_item in handle_result) processing_panels_choice = set(dr_panel.name for dr_panel in self.processing_panel_id) # 使用集合的差集操作找出那些待处理结果中有但实际可用加工面中没有的加工面 processing_panels_missing = processing_panels_to_handle - processing_panels_choice # 存在不一致的加工面 if processing_panels_missing: processing_panels_str = ','.join(processing_panels_missing) raise UserError('您还有待处理的检测结果中为%s的加工面未选择' % processing_panels_str) # processing_panels = set() # for handle_item in handle_result: # for dr_panel in self.processing_panel_id: # if dr_panel.name == handle_item.processing_panel: # processing_panels.add(dr_panel.name) # if len(processing_panels) != len(handle_result): # processing_panels_str = ','.join(processing_panels) # return UserError(f'您还有待处理的检测结果中为{processing_panels_str}的加工面未选择') for panel in self.processing_panel_id: panel_workorder = self.production_id.workorder_ids.filtered( lambda ap: ap.processing_panel == panel.name and ap.state != 'rework') if panel_workorder: panel_workorder.write({'state': 'rework'}) # panel_workorder.filtered( # lambda wo: wo.routing_type == '装夹预调').workpiece_delivery_ids.filtered( # lambda wd: wd.status == '待下发').write({'status': '已取消'}) # workpiece = self.env['sf.workpiece.delivery'].search([('status', '=', '待下发'), ( # 'workorder_id', '=', # panel_workorder.filtered(lambda wd: wd.routing_type == '装夹预调').id)]) product_routing_workcenter = self.env['sf.product.model.type.routing.sort'].search( [('product_model_type_id', '=', self.production_id.product_id.product_model_type_id.id)], order='sequence asc' ) workorders_values = [] for route in product_routing_workcenter: if route.is_repeat is True: workorders_values.append( self.env['mrp.workorder'].json_workorder_str(panel.name, self.production_id, route, False)) if workorders_values: self.production_id.write({'workorder_ids': workorders_values, 'is_rework': True}) self.production_id._reset_work_order_sequence() self.production_id.detection_result_ids.filtered( lambda ap1: ap1.processing_panel == panel.name and ap1.handle_result == '待处理').write( {'handle_result': '已处理'}) if self.is_reprogramming is False: if self.programming_state in ['已编程', '已下发']: if self.reprogramming_num >= 1 and self.programming_state == '已编程': self.production_id.get_new_program(panel.name) if self.reprogramming_num >= 0 and self.programming_state == '已下发': ret = {'programming_list': []} cnc_rework = max( self.production_id.workorder_ids.filtered( lambda crw: crw.processing_panel == panel.name and crw.state == 'rework' and crw.routing_type == 'CNC加工'), key=lambda w: w.create_date ) if cnc_rework.cnc_ids: for item_line in cnc_rework.cnc_ids: vals = { 'sequence_number': item_line.sequence_number, 'program_name': item_line.program_name, 'cutting_tool_name': item_line.cutting_tool_name, 'cutting_tool_no': item_line.cutting_tool_no, 'processing_type': item_line.processing_type, 'margin_x_y': item_line.margin_x_y, 'margin_z': item_line.margin_z, 'depth_of_processing_z': item_line.depth_of_processing_z, 'cutting_tool_extension_length': item_line.cutting_tool_extension_length, 'estimated_processing_time': item_line.estimated_processing_time, 'cutting_tool_handle_type': item_line.cutting_tool_handle_type, 'program_path': item_line.program_path, 'ftp_path': item_line.program_path, 'processing_panel': panel.name, 'program_create_date': datetime.strftime(item_line.program_create_date, '%Y-%m-%d %H:%M:%S'), 'remark': item_line.remark } ret['programming_list'].append(vals) for cmm_line in cnc_rework.cmm_ids: vals = { 'sequence_number': cmm_line.sequence_number, 'program_name': cmm_line.program_name, 'program_path': cmm_line.program_path, 'ftp_path': cmm_line.program_path, 'processing_panel': panel.name, 'program_create_date': datetime.strftime( cmm_line.program_create_date, '%Y-%m-%d %H:%M:%S') } ret['programming_list'].append(vals) new_cnc_workorder = self.production_id.workorder_ids.filtered( lambda ap1: ap1.processing_panel == panel.name and ap1.state not in ( 'rework', 'done') and ap1.routing_type == 'CNC加工') if not new_cnc_workorder.cnc_ids: new_cnc_workorder.write({ 'cnc_ids': new_cnc_workorder.cnc_ids.sudo()._json_cnc_processing(panel.name, ret), 'cmm_ids': new_cnc_workorder.cmm_ids.sudo()._json_cmm_program(panel.name, ret), 'cnc_worksheet': cnc_rework.cnc_worksheet}) new_pre_workorder = self.production_id.workorder_ids.filtered(lambda p: p.routing_type == '装夹预调' and p.processing_panel == panel.name and p.state not in ( 'rework', 'done')) if new_pre_workorder: pre_rework = max(self.production_id.workorder_ids.filtered( lambda pr: pr.processing_panel == panel.name and pr.state in ( 'rework') and pr.routing_type == '装夹预调'), key=lambda w1: w1.create_date) new_pre_workorder.write( {'processing_drawing': pre_rework.processing_drawing}) self.production_id.write({'state': 'progress', 'is_rework': False}) elif self.programming_state in ['待编程', '编程中']: self.production_id.write( {'programming_state': '编程中', 'work_state': '编程中', 'is_rework': True}) if self.is_reprogramming is True: self.production_id.update_programming_state() self.production_id.write( {'programming_state': '编程中', 'work_state': '编程中'}) if self.production_id.state == 'progress': self.production_id.write({'programming_state': '已编程', 'work_state': '已编程'}) if self.reprogramming_num >= 1 and self.programming_state == '已编程': productions_not_delivered = self.env['mrp.production'].search( [('programming_no', '=', self.production_id.programming_no), ('programming_state', '=', '已编程未下发')]) if productions_not_delivered: productions_not_delivered.write( {'state': 'progress', 'programming_state': '已编程', 'work_state': '已编程', 'is_rework': False}) @api.onchange('production_id') def onchange_processing_panel_id(self): for item in self: domain = [('id', '=', False)] production_id = item.production_id if production_id: if self.env.user.has_group('sf_base.group_sf_order_user'): panel_ids = [] panel_arr = production_id.product_id.model_processing_panel for p in production_id.detection_result_ids.filtered( lambda ap1: ap1.handle_result == '待处理'): if p.processing_panel not in panel_arr: panel_arr += ','.join(p.processing_panel) for item in panel_arr.split(','): panel = self.env['sf.processing.panel'].search( [('name', 'ilike', item)]) if panel: panel_ids.append(panel.id) domain = {'processing_panel_id': [('id', 'in', panel_ids)]} return {'domain': domain}