工作流时间倒序,补全办结时间

This commit is contained in:
李鹏宇
2026-01-30 19:41:37 +08:00
parent f3554340a8
commit 4e177f1b12
2 changed files with 48 additions and 26 deletions
+41 -19
View File
@@ -91,6 +91,22 @@ class SoongWorkflowRecord(models.Model):
# 办结时间(上一节点的结束时间)
node_update_time = fields.Datetime(string="办结时间")
# 显示用的办结时间(如果node_update_time为空,则显示write_date2)
display_end_time = fields.Datetime(
string="办结时间",
compute='_compute_display_end_time',
store=True # 存储到数据库,以便用于排序
)
@api.depends('node_update_time', 'write_date2')
def _compute_display_end_time(self):
"""
计算显示用的办结时间
逻辑:如果node_update_time有值则使用,否则使用write_date2
"""
for record in self:
record.display_end_time = record.node_update_time or record.write_date2
@api.depends('res_model', 'res_id')
def _compute_urgency_level(self):
@@ -645,36 +661,42 @@ class SoongWorkflowRecord(models.Model):
def action_update_history_end_time(self):
"""
批量计算已结束流程的办结时间
批量计算已结杞流程的办结时间
"""
# 寻找所有审批状态为结束(end)且办结时间为空的记录
records = self.search([
('approve_state', '=', 'end'),
('node_update_time', '=', False)
])
count = 0
for rec in records:
if not rec.res_model or not rec.res_id:
continue
node_update_time = None
try:
# 获取对应的业务单据
source_record = self.env[rec.res_model].browse(rec.res_id)
if not source_record.exists():
continue
# 尝试从业务单据的log_ids获取办结旷间
if rec.res_model and rec.res_id:
try:
# 获取对应的业务单据
source_record = self.env[rec.res_model].browse(rec.res_id)
if source_record.exists():
# 提取该单据最后一条审批日志的时间
if hasattr(source_record, 'log_ids') and source_record.log_ids:
# 按ID排序,取最后一条记录
sorted_logs = source_record.log_ids.sorted(lambda l: l.id)
if sorted_logs and sorted_logs[-1].lase_click_date:
node_update_time = sorted_logs[-1].lase_click_date
except Exception as e:
_logger.error(f"计算记录 {rec.id} 办结时间失败: {e}")
# 提取该单据最后一条审批日志的时间
if hasattr(source_record, 'log_ids') and source_record.log_ids:
# 按ID排序,取最后一条记录
sorted_logs = source_record.log_ids.sorted(lambda l: l.id)
if sorted_logs and sorted_logs[-1].lase_click_date:
# 更新办结时间
rec.write({'node_update_time': sorted_logs[-1].lase_click_date})
count += 1
except Exception as e:
_logger.error(f"计算记录 {rec.id} 办结时间失败: {e}")
# 如果没有从 log_ids 获取到时间,则使用 write_date2 作为备选
if not node_update_time:
node_update_time = rec.write_date2
# 更新办结时间
if node_update_time:
rec.write({'node_update_time': node_update_time})
count += 1
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
@@ -86,7 +86,7 @@
<field name="arch" type="xml">
<tree string="流程查询" action="open_record" type="object"
delete="false"
create="false" default_order="node_update_time desc, write_date2 desc, id desc" expand="1" limit="10">
create="false" default_order="display_end_time desc, write_date2 desc, id desc" expand="1" limit="10">
<header>
<button name="admin_end" type="object" string="管理员强制结束" class="oe_highlight"
groups="soong_workflow.group_soong_workflow_manager"/>
@@ -103,7 +103,7 @@
<field name="create_uid"/>
<field name="employee_ids" widget="many2many_tags" string="当前经办人"/>
<field name="soong_state"/>
<field name="node_update_time" string="办结时间" optional="show"/>
<field name="display_end_time" string="办结时间" optional="show"/>
<field name="member_of_department" column_invisible="1"/>
<field name="node_id" string="当前节点" column_invisible="1"/>
<field name="documents_ids" widget="preview_many2many" column_invisible="1"/>
@@ -137,7 +137,7 @@
<field name="arch" type="xml">
<tree string="流程查询" type="object"
delete="false"
create="false" default_order="node_update_time desc, write_date2 desc, id desc" expand="1" limit="10">
create="false" default_order="display_end_time desc, write_date2 desc, id desc" expand="1" limit="10">
<header>
<button name="admin_end" type="object" string="管理员强制结束" class="oe_highlight"
groups="soong_workflow.group_soong_workflow_manager"/>
@@ -154,7 +154,7 @@
<field name="create_uid"/>
<field name="employee_ids" widget="many2many_tags" string="当前经办人"/>
<field name="soong_state"/>
<field name="node_update_time" string="办结时间" optional="show"/>
<field name="display_end_time" string="办结时间" optional="show"/>
<field name="member_of_department" column_invisible="1"/>
<field name="node_id" string="当前节点" column_invisible="1"/>
<field name="documents_ids" widget="preview_many2many" column_invisible="1"/>
@@ -252,7 +252,7 @@
decoration-danger="approve_state == 'end'" decoration-success="approve_state == 'pending'"/>
<field name="create_uid"/>
<field name="employee_ids" widget="many2many_tags" string="当前经办人" class="list_ids_width"/>
<field name="soong_state"/><field name="node_update_time" string="到达时间" optional="show"/>
<field name="soong_state"/><field name="display_end_time" string="到达时间" optional="show"/>
<field name="is_manager" column_invisible="1"/>
<field name="is_unlink" column_invisible="1"/>
<field name="is_log2" column_invisible="1"/>
@@ -292,7 +292,7 @@
<field name="approve_state" string="审批状态" widget="badge"
decoration-danger="approve_state == 'end'" decoration-success="approve_state == 'pending'"/>
<field name="create_uid"/>
<field name="node_update_time" string="办结时间" optional="show"/>
<field name="display_end_time" string="办结时间" optional="show"/>
<field name="is_manager" column_invisible="1"/>
<field name="is_unlink" column_invisible="1"/>
<field name="is_log2" column_invisible="1"/>
@@ -352,7 +352,7 @@
decoration-danger="approve_state == 'end'" decoration-success="approve_state == 'pending'"/>
<field name="create_uid"/>
<field name="employee_ids" widget="many2many_tags" string="当前经办人" class="list_ids_width"/>
<field name="soong_state"/><field name="node_update_time" string="办结时间" optional="show"/>
<field name="soong_state"/><field name="display_end_time" string="办结时间" optional="show"/>
<field name="is_manager" column_invisible="1"/>
</tree>
</field>