151 lines
6.7 KiB
Python
151 lines
6.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import api, fields, models
|
|
from odoo.exceptions import UserError
|
|
from datetime import datetime as dt
|
|
|
|
|
|
class StockPickingTransferWizard(models.TransientModel):
|
|
_name = 'stock.picking.transfer.wizard'
|
|
_description = '入库单转交向导'
|
|
|
|
picking_id = fields.Many2one('stock.picking', string='入库单', required=True)
|
|
transfer_type = fields.Selection([
|
|
('acceptance', '转交验收'),
|
|
('certifier', '转交证明'),
|
|
], string='转交类型', required=True)
|
|
user_id = fields.Many2one('res.users', string='转交给', required=True,
|
|
domain=[('active', '=', True)])
|
|
|
|
def _get_inbound_workflow(self):
|
|
"""动态查找入库单对应的 soong.workflow
|
|
# 与 soong_workflow_mixin._compute_workflow_id 查询逻辑一致,只按 model_id + is_enable 查
|
|
# 注意:picking_type_id.name 含公司前缀(如"集团本部:入库"),不能直接和 workflow.name 匹配
|
|
# 多结果时优先选 name 含"入库"的,否则取第一个(与 _compute_workflow_id 的行为一致)
|
|
"""
|
|
workflows = self.env['soong.workflow'].search([
|
|
('model_id.model', '=', 'stock.picking'),
|
|
('is_enable', '=', True),
|
|
])
|
|
if not workflows:
|
|
return None
|
|
# 优先级:name 含"入库"关键词 > 第一个
|
|
for wf in workflows:
|
|
if '入库' in wf.name:
|
|
return wf
|
|
return workflows[0]
|
|
|
|
def _get_workflow_record(self, picking):
|
|
"""获取入库单对应的 workflow record"""
|
|
return self.env['soong.workflow.record'].search([
|
|
('res_model', '=', 'stock.picking'),
|
|
('res_id', '=', picking.id),
|
|
('reclaim_state', '=', False),
|
|
], limit=1)
|
|
|
|
def _get_department(self, picking):
|
|
"""获取部门"""
|
|
current_user = self.env.user
|
|
employee = current_user.employee_id
|
|
department = employee.department_id if employee else self.env['hr.department']
|
|
return department or picking.department_id
|
|
|
|
def action_confirm(self):
|
|
self.ensure_one()
|
|
picking = self.picking_id
|
|
current_user = self.env.user
|
|
|
|
if self.transfer_type == 'acceptance':
|
|
if picking.receipt_status != 'acceptance':
|
|
raise UserError('当前入库单状态不是验收状态,无法转交验收!')
|
|
picking.write({'acceptance_user_id': self.user_id.id})
|
|
|
|
# 创建或更新 workflow record,验收人待办
|
|
acceptance_user = self.user_id
|
|
department = self._get_department(picking)
|
|
# 动态查找入库单工作流(替代硬编码 ID,避免 ID 变更后报外键约束错误)
|
|
inbound_workflow = self._get_inbound_workflow()
|
|
if not inbound_workflow:
|
|
raise UserError('未找到入库单对应的工作流(soong.workflow),请联系管理员配置!')
|
|
wf = self._get_workflow_record(picking)
|
|
all_user_ids = [current_user.id, acceptance_user.id]
|
|
if wf:
|
|
wf.sudo().write({
|
|
'name': picking.origin or picking.name, # 名称 ← 源单据
|
|
'code': picking.name, # 流水号 ← 入库单编号
|
|
'users_ids': [(6, 0, all_user_ids)],
|
|
'approve_state': 'pending',
|
|
'soong_state': 'to_do',
|
|
'node_update_time': dt.utcnow(), # 到达时间 ← 当前时间
|
|
})
|
|
else:
|
|
wf = self.env['soong.workflow.record'].sudo().create({
|
|
'name': picking.origin or picking.name, # 名称 ← 源单据
|
|
'code': picking.name, # 流水号 ← 入库单编号
|
|
'res_model': 'stock.picking',
|
|
'res_id': picking.id,
|
|
'users_ids': [(6, 0, all_user_ids)],
|
|
'employee_ids': [(5,)],
|
|
'done_by': current_user.id,
|
|
'approve_state': 'pending',
|
|
'soong_state': 'to_do',
|
|
'node_update_time': dt.utcnow(), # 到达时间 ← 当前时间
|
|
'department_id': department.id if department else False,
|
|
'workflow_id': inbound_workflow.id,
|
|
})
|
|
# 创建两条 log:转交人已办,验收人待办
|
|
self.env['soong.workflow.log'].sudo().create([
|
|
{
|
|
'model': 'stock.picking',
|
|
'res_id': picking.id,
|
|
'done_by': current_user.id,
|
|
'approval_state': '2',
|
|
'lase_click_date': dt.utcnow(),
|
|
'comment': '已转交验收',
|
|
},
|
|
{
|
|
'model': 'stock.picking',
|
|
'res_id': picking.id,
|
|
'done_by': acceptance_user.id,
|
|
'approval_state': '0',
|
|
'lase_click_date': dt.utcnow(),
|
|
'comment': '待验收',
|
|
},
|
|
])
|
|
|
|
elif self.transfer_type == 'certifier':
|
|
if picking.receipt_status != 'certification':
|
|
raise UserError('当前入库单状态不是证明状态,无法转交证明!')
|
|
picking.write({'certifier_user_id': self.user_id.id})
|
|
|
|
# 更新 workflow record,证明人加入待办
|
|
certifier_user = self.user_id
|
|
wf = self._get_workflow_record(picking)
|
|
if wf:
|
|
existing_user_ids = wf.users_ids.ids
|
|
if certifier_user.id not in existing_user_ids:
|
|
existing_user_ids.append(certifier_user.id)
|
|
wf.sudo().write({
|
|
'users_ids': [(6, 0, existing_user_ids)],
|
|
})
|
|
# 创建两条 log:验收人已办,证明人待办
|
|
self.env['soong.workflow.log'].sudo().create([
|
|
{
|
|
'model': 'stock.picking',
|
|
'res_id': picking.id,
|
|
'done_by': current_user.id,
|
|
'approval_state': '2',
|
|
'lase_click_date': dt.utcnow(),
|
|
'comment': '已转交证明',
|
|
},
|
|
{
|
|
'model': 'stock.picking',
|
|
'res_id': picking.id,
|
|
'done_by': certifier_user.id,
|
|
'approval_state': '0',
|
|
'lase_click_date': dt.utcnow(),
|
|
'comment': '待证明',
|
|
},
|
|
])
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|