diff --git a/yuthon_stock/models/inherit_stock_picking.py b/yuthon_stock/models/inherit_stock_picking.py index f9ec95113..34dd00a58 100644 --- a/yuthon_stock/models/inherit_stock_picking.py +++ b/yuthon_stock/models/inherit_stock_picking.py @@ -57,6 +57,12 @@ class StockPicking(models.Model): compute='_compute_can_certify', store=False ) + # 触发器字段:用于在打开记录时触发自动填充 + trigger_auto_fill = fields.Boolean( + string="触发自动填充", + compute='_compute_trigger_auto_fill', + store=False + ) @api.depends('user_id', 'employee_id') def _compute_can_certify(self): @@ -74,23 +80,70 @@ class StockPicking(models.Model): # 其他情况可以证明 record.can_certify = True - def read(self, fields=None, load='_classic_read'): - """重写read方法,打开验收/证明状态的入库单时自动更新时间""" - result = super(StockPicking, self).read(fields, load) - # 导入Fields模块 + @api.depends('receipt_status') + def _compute_trigger_auto_fill(self): + """计算触发器字段,在打开记录时自动填充验收人/证明人(仅当字段为空时)""" from odoo import fields as odoo_fields + current_user = self.env.user + for record in self: + record.trigger_auto_fill = False # 默认值 + + # 只在已保存的记录中执行(有id) + if not record.id or isinstance(record.id, models.NewId): + continue + if record.receipt_status == 'acceptance': - # 使用write方法更新验收时间 - record.write({ - 'acceptance_date': odoo_fields.Datetime.now() - }) + # 验收状态:只在验收人为空时才自动填充 + if not record.employee_id: + current_employee = current_user.employee_ids[:1] if current_user.employee_ids else False + if current_employee: + record.sudo().write({ + 'employee_id': current_employee.id, + 'acceptance_date': odoo_fields.Datetime.now() + }) elif record.receipt_status == 'certification': - # 使用write方法更新证明时间 - record.write({ - 'certification_date': odoo_fields.Datetime.now() - }) - return result + # 证明状态:只在证明人为空时才自动填充 + # 且需要检查当前用户不是验收人 + if not record.certifier_id: + # 检查当前用户是否是验收人 + is_acceptor = False + if record.employee_id and record.employee_id.user_id: + is_acceptor = record.employee_id.user_id.id == current_user.id + + # 只有当前用户不是验收人时,才自动填充为证明人 + if not is_acceptor: + record.sudo().write({ + 'certifier_id': current_user.id, + 'certification_date': odoo_fields.Datetime.now() + }) + + @api.onchange('receipt_status') + def _onchange_receipt_status_auto_fill(self): + """当receipt_status变化时,自动填充验收人/证明人(仅当字段为空时)""" + from odoo import fields as odoo_fields + current_user = self.env.user + + if self.receipt_status == 'acceptance': + # 验收状态:只在验收人为空时才自动填充 + if not self.employee_id: + current_employee = current_user.employee_ids[:1] if current_user.employee_ids else False + if current_employee: + self.employee_id = current_employee.id + self.acceptance_date = odoo_fields.Datetime.now() + elif self.receipt_status == 'certification': + # 证明状态:只在证明人为空时才自动填充 + # 且需要检查当前用户不是验收人 + if not self.certifier_id: + # 检查当前用户是否是验收人 + is_acceptor = False + if self.employee_id and self.employee_id.user_id: + is_acceptor = self.employee_id.user_id.id == current_user.id + + # 只有当前用户不是验收人时,才自动填充为证明人 + if not is_acceptor: + self.certifier_id = current_user.id + self.certification_date = odoo_fields.Datetime.now() @api.depends('location_id', 'picking_type_code') def _compute_available_product_ids(self): @@ -230,9 +283,13 @@ class StockPicking(models.Model): } def action_acceptance(self): - """验收按钮""" + """验收按钮:自动填充验收人和验收时间""" self.ensure_one() + # 获取当前用户对应的员工记录 + current_employee = self.env.user.employee_ids[:1] if self.env.user.employee_ids else False self.acceptance_date = fields.Datetime.now() + if current_employee: + self.employee_id = current_employee.id self.receipt_status = 'certification' # 状态变为证明 return True diff --git a/yuthon_stock/views/inherit_stock_picking.xml b/yuthon_stock/views/inherit_stock_picking.xml index b29f956a6..418144044 100644 --- a/yuthon_stock/views/inherit_stock_picking.xml +++ b/yuthon_stock/views/inherit_stock_picking.xml @@ -12,7 +12,7 @@ [('id', 'in', available_location_ids)] - state != 'draft' and receipt_status in ['acceptance', 'certification'] + state != 'draft' and (receipt_status in ['acceptance', 'certification'] or receipt_status == 'receipt') 1 @@ -24,6 +24,7 @@ + {'no_create': True} @@ -35,14 +36,14 @@ - - - + + + - - - + + + @@ -70,10 +71,10 @@ 供应商 - state != 'draft' and receipt_status in ['acceptance', 'certification'] + state != 'draft' and (receipt_status in ['acceptance', 'certification'] or receipt_status == 'receipt') - state != 'draft' and receipt_status in ['acceptance', 'certification'] + state != 'draft' and (receipt_status in ['acceptance', 'certification'] or receipt_status == 'receipt')