61 lines
2.8 KiB
Python
61 lines
2.8 KiB
Python
from odoo import models, fields, api
|
||
import logging
|
||
|
||
_logger = logging.getLogger(__name__)
|
||
|
||
class QualityPoint(models.Model):
|
||
_inherit = 'quality.point'
|
||
|
||
quality_status = fields.Selection([
|
||
('waiting', '等待'),
|
||
('none', '待处理')
|
||
], string='默认质检状态',
|
||
help='收料入库时质量检查单的默认状态。如果设置了此值,系统会在创建质检单时自动使用该状态。')
|
||
|
||
class QualityCheck(models.Model):
|
||
_inherit = 'quality.check'
|
||
|
||
@api.model_create_multi
|
||
def create(self, vals_list):
|
||
"""
|
||
重写create方法,根据quality.point配置动态设置quality_state
|
||
当picking_type为收料入库时,从质量控制点配置中获取默认状态
|
||
"""
|
||
# 先调用父类方法创建记录
|
||
records = super().create(vals_list)
|
||
|
||
# 对创建的记录进行状态调整
|
||
for record in records:
|
||
try:
|
||
# 只处理收料入库的质检单且未手动设置状态的记录
|
||
if (record.picking_id and
|
||
record.picking_id.picking_type_id.code == 'incoming' and
|
||
record.quality_state in ['none', 'waiting']): # 只调整默认状态
|
||
|
||
# 查找匹配的质量控制点配置
|
||
quality_point = record.point_id
|
||
if not quality_point and record.product_id:
|
||
# 如果没有直接关联的质量点,则根据产品和作业类型查找
|
||
domain = self.env['quality.point']._get_domain(
|
||
record.product_id,
|
||
record.picking_id.picking_type_id,
|
||
measure_on='product'
|
||
)
|
||
quality_point = self.env['quality.point'].search(domain, limit=1)
|
||
|
||
# 如果找到配置且有默认状态值,则更新记录状态
|
||
if quality_point and quality_point.quality_status:
|
||
original_state = record.quality_state
|
||
record.quality_state = quality_point.quality_status
|
||
|
||
_logger.info(
|
||
f"质量检查单状态已更新: {record.name}, "
|
||
f"产品: {record.product_id.name}, "
|
||
f"收料单: {record.picking_id.name}, "
|
||
f"状态: {original_state} -> {quality_point.quality_status}"
|
||
)
|
||
|
||
except Exception as e:
|
||
_logger.warning(f"更新质量检查单状态时发生错误: {record.name}, 错误: {str(e)}")
|
||
|
||
return records |