98 lines
4.3 KiB
Python
98 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
import logging
|
||
from odoo import fields, models, api
|
||
from odoo.exceptions import UserError, ValidationError
|
||
from odoo.tools import str2bool
|
||
|
||
|
||
class SfProductionProcessParameter(models.Model):
|
||
_inherit = 'sf.production.process.parameter'
|
||
outsourced_service_products = fields.One2many(
|
||
'product.template', # 另一个模型的名称
|
||
'server_product_process_parameters_id', # 对应的 Many2one 字段名称
|
||
string='外协服务产品'
|
||
)
|
||
is_product_button = fields.Boolean(compute='_compute_is_product_button',default=False)
|
||
is_delete_button = fields.Boolean(compute='_compute_is_delete_button', default=False)
|
||
routing_id = fields.Many2one('mrp.routing.workcenter', string="工序")
|
||
|
||
@api.constrains('outsourced_service_products')
|
||
def _validate_partner_limit(self):
|
||
for record in self:
|
||
if len(record.outsourced_service_products) > 1:
|
||
raise ValidationError("工艺参数不能与多个产品关联")
|
||
|
||
@api.onchange('outsourced_service_products')
|
||
def _onchange_validate_partner_limit(self):
|
||
for record in self:
|
||
if len(record.outsourced_service_products) > 1:
|
||
raise ValidationError("工艺参数不能与多个产品关联")
|
||
@api.depends('outsourced_service_products')
|
||
def _compute_is_product_button(self):
|
||
for record in self:
|
||
if record.outsourced_service_products:
|
||
record.is_product_button = True
|
||
else:
|
||
record.is_product_button = False
|
||
|
||
def has_wksp_prefix(self,code):
|
||
"""
|
||
判断字符串是否以WKSP开头(不区分大小写)
|
||
:param text: 要检查的字符串
|
||
:return: True/False
|
||
"""
|
||
return code.upper().startswith('WKSP')
|
||
@api.depends('outsourced_service_products','code')
|
||
def _compute_is_delete_button(self):
|
||
for record in self:
|
||
if record.outsourced_service_products and self.has_wksp_prefix(record.code):
|
||
record.is_delete_button = False
|
||
elif record.outsourced_service_products:
|
||
record.is_delete_button = True
|
||
else:
|
||
record.is_delete_button = True
|
||
@api.model
|
||
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
|
||
if self._context.get('route_id'):
|
||
parameter = []
|
||
routing = self.env['mrp.routing.workcenter'].search([('id', '=', self._context.get('route_id'))])
|
||
technology_design = self.env['sf.technology.design'].search(
|
||
[('production_id', '=', self._context.get('production_id')), ('routing_tag', '=', 'special'),
|
||
('route_id', '=', self._context.get('route_id'))])
|
||
for t in technology_design:
|
||
if t.process_parameters_id:
|
||
parameter.append(t.process_parameters_id.id)
|
||
domain = [('process_id', '=', routing.surface_technics_id.id), ('id', 'not in', parameter)]
|
||
return self._search(domain, limit=limit, access_rights_uid=name_get_uid)
|
||
return super()._name_search(name, args, operator, limit, name_get_uid)
|
||
|
||
def action_create_service_product(self):
|
||
if self.id: # 如果是已存在的记录
|
||
self.write({}) # 空写入会触发保存
|
||
else: # 如果是新记录
|
||
self = self.create(self._convert_to_write(self.read()[0]))
|
||
return {
|
||
'type': 'ir.actions.act_window',
|
||
'name': '向导名称',
|
||
'res_model': 'product.creation.wizard',
|
||
'view_mode': 'form',
|
||
'target': 'new',
|
||
'context': {'default_process_parameter_id': self.id}, # 传递当前记录ID
|
||
}
|
||
#
|
||
# return {
|
||
# 'name': '创建服务产品',
|
||
# 'type': 'ir.actions.act_window',
|
||
# 'res_model': 'product.product',
|
||
# 'view_mode': 'form',
|
||
# 'view_id': self.env.ref('product.product_normal_form_view').id,
|
||
# 'target': 'new', # 关键参数,使窗口以弹窗形式打开
|
||
# 'context': {
|
||
# 'default_' + k: v for k, v in default_values.items()
|
||
# },
|
||
# }
|
||
|
||
def action_hide_service_products(self):
|
||
# self.outsourced_service_products.active = False
|
||
self.active = False
|