把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
165 lines
7.7 KiB
Python
165 lines
7.7 KiB
Python
"""
|
||
services/shipping_notice/lifecycle_projection.py
|
||
|
||
用途:为批次进度、任务详情与订单生命周期工作台裁决同一份五阶段业务生命周期。
|
||
依赖:process_gate_contract 的阻断原因归属;调用方提供正式 Gate、Case/Candidate 或已核验成功事实。
|
||
逻辑:已核验上传成功 → 已持久化处理阶段 → 正式 Process Gate → 可解释阻断原因;
|
||
[B''] 处理阶段是阶段唯一权威,正式 Gate 不再覆盖/升降阶段,仅在处理阶段缺失时
|
||
兜底;无正式事实时保持未分类。
|
||
本模块只做只读业务投影,不授权任何写操作;阶段入口与实际动作权限必须分开。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
from app.services.shipping_notice.process_gate_contract import PROCESS_GATE_REASON_OWNING_GATE
|
||
|
||
|
||
LIFECYCLE_STAGES: tuple[dict[str, str], ...] = (
|
||
{'code': 'intake_parse', 'label': '接收解析'},
|
||
{'code': 'order_review', 'label': '订单复核'},
|
||
{'code': 'fulfillment_prep', 'label': '通知准备'},
|
||
{'code': 'freight_upload', 'label': '货代上传'},
|
||
{'code': 'upload_complete', 'label': '通知完成'},
|
||
)
|
||
|
||
LIFECYCLE_STAGE_CODES: tuple[str, ...] = tuple(stage['code'] for stage in LIFECYCLE_STAGES)
|
||
|
||
# 正式 Gate 到用户生命周期的唯一映射。任何页面均不得维护第二份 Gate 映射。
|
||
LIFECYCLE_STAGE_BY_GATE: dict[str, str] = {
|
||
'document_materialization': 'intake_parse',
|
||
'layout_parsing': 'intake_parse',
|
||
'document_profile_routing': 'intake_parse',
|
||
'order_field_extraction': 'intake_parse',
|
||
'field_completeness': 'order_review',
|
||
'reservation_validation': 'order_review',
|
||
'customer_resolution': 'order_review',
|
||
'duplicate_detection': 'order_review',
|
||
'field_conflict_resolution': 'order_review',
|
||
'order_freeze_readiness': 'order_review',
|
||
# 建单前已存在当前有效 Candidate 的未确认 Review Case 仍属「订单复核」工作面:
|
||
# 用户在复核并决定标记非订单/修订草稿之前,订单尚未正式创建。
|
||
'order_review_build': 'order_review',
|
||
'task_creation': 'order_review',
|
||
'onhand_source_routing': 'fulfillment_prep',
|
||
'onhand_preparation': 'fulfillment_prep',
|
||
'onhand_field_mapping': 'fulfillment_prep',
|
||
'onhand_matching': 'fulfillment_prep',
|
||
'onhand_sufficiency': 'fulfillment_prep',
|
||
'excel_readiness': 'fulfillment_prep',
|
||
'artifact_generation': 'fulfillment_prep',
|
||
'freight_upload_rpa': 'freight_upload',
|
||
'manual_upload_handoff': 'freight_upload',
|
||
}
|
||
|
||
# 已持久化的 Processing 技术阶段到五阶段业务工作面的唯一映射。它只用于
|
||
# 读模型展示,不推进订单、不创建任务,也不授予任何动作。[B''] 它是阶段的唯一
|
||
# 权威;正式 Gate 仅在技术阶段缺失时提供兜底,不能覆盖有效处理阶段。
|
||
LIFECYCLE_STAGE_BY_PROCESSING_STAGE: dict[str, str] = {
|
||
'upload_received': 'intake_parse',
|
||
'email_parsing': 'intake_parse',
|
||
'document_parsing': 'intake_parse',
|
||
'email_adjudicating': 'intake_parse',
|
||
'candidate_creating': 'intake_parse',
|
||
'waiting_review': 'order_review',
|
||
'customer_resolving': 'order_review',
|
||
'duplicate_checking': 'order_review',
|
||
'onhand_source_resolving': 'fulfillment_prep',
|
||
'order_creating': 'fulfillment_prep',
|
||
'onhand_fetching': 'fulfillment_prep',
|
||
'onhand_preparation': 'fulfillment_prep',
|
||
'onhand_matching': 'fulfillment_prep',
|
||
'onhand_sufficiency': 'fulfillment_prep',
|
||
'excel_generating': 'fulfillment_prep',
|
||
'freight_uploading': 'freight_upload',
|
||
'manual_upload_handoff': 'freight_upload',
|
||
'completed': 'upload_complete',
|
||
}
|
||
|
||
def lifecycle_entries(
|
||
*,
|
||
lifecycle_stage: str | None,
|
||
has_candidate_context: bool,
|
||
has_intake_parse_context: bool = False,
|
||
) -> list[dict[str, str]]:
|
||
"""返回只读生命周期入口;空 Case 仅允许停留在接收解析阶段。"""
|
||
if lifecycle_stage not in LIFECYCLE_STAGE_CODES:
|
||
return []
|
||
if not has_candidate_context:
|
||
# 接收解析 Case 是已持久化的受控对象,允许在批次页展示其当前阶段;
|
||
# 但不能借此开放订单复核及后续阶段。
|
||
if not has_intake_parse_context or lifecycle_stage != 'intake_parse':
|
||
return []
|
||
return [
|
||
{
|
||
'code': code,
|
||
'state': 'current' if code == 'intake_parse' else 'unavailable',
|
||
'target_surface': 'order_lifecycle_workbench',
|
||
}
|
||
for code in LIFECYCLE_STAGE_CODES
|
||
]
|
||
current_index = LIFECYCLE_STAGE_CODES.index(str(lifecycle_stage))
|
||
return [
|
||
{
|
||
'code': code,
|
||
'state': 'current' if index == current_index else ('available' if index < current_index else 'unavailable'),
|
||
'target_surface': 'order_lifecycle_workbench',
|
||
}
|
||
for index, code in enumerate(LIFECYCLE_STAGE_CODES)
|
||
]
|
||
|
||
|
||
def project_lifecycle(
|
||
*,
|
||
current_gate: dict[str, Any] | None,
|
||
blocking_reason: dict[str, Any] | None,
|
||
has_candidate_context: bool,
|
||
has_intake_parse_context: bool = False,
|
||
verified_upload_success: bool = False,
|
||
processing_stage: str | None = None,
|
||
) -> dict[str, Any]:
|
||
"""按成功事实 → 已持久化阶段 → 正式 Gate → 阻断归属的固定优先级裁决生命周期。
|
||
|
||
[B''] 阶段唯一权威:只要 ``processing_stage``(OT.stage / Run.processing_stage)是合法
|
||
15 阶成员,就直接映射五阶段,正式 Gate 不能覆盖或升降它。``gate_code`` 只在处理
|
||
阶段缺失时兜底(如任务详情无 OT.stage 上下文),且仅用于「订单检查」面板诊断;
|
||
它不再能改变阶段。``authority`` 仅用于诊断/迁移观测;前端不得据此再计算阶段或放开按钮。
|
||
"""
|
||
gate = current_gate if isinstance(current_gate, dict) else {}
|
||
gate_code = str(gate.get('gate_code') or gate.get('code') or '').strip().lower()
|
||
# 成功事实高于当前 Gate/历史阶段:异步回调成功后旧 Gate 尚未清理时,用户仍应
|
||
# 看到已完成;反之任何技术完成、回执文本或旧 completed 都不能越过此条件。
|
||
if verified_upload_success:
|
||
lifecycle_stage = 'upload_complete'
|
||
authority = 'verified_upload_success'
|
||
# [B''] 处理阶段是阶段唯一权威。合法 15 阶直接映射五阶段;Gate 不再覆盖它。
|
||
elif str(processing_stage or '').strip().lower() in LIFECYCLE_STAGE_BY_PROCESSING_STAGE:
|
||
lifecycle_stage = LIFECYCLE_STAGE_BY_PROCESSING_STAGE[
|
||
str(processing_stage or '').strip().lower()
|
||
]
|
||
authority = 'processing_stage'
|
||
# [B''] 正式 Gate 仅在处理阶段缺失时兜底,用于「订单检查」面板,不改变阶段。
|
||
elif gate_code in LIFECYCLE_STAGE_BY_GATE:
|
||
lifecycle_stage = LIFECYCLE_STAGE_BY_GATE[gate_code]
|
||
authority = 'process_gate'
|
||
else:
|
||
reason = blocking_reason if isinstance(blocking_reason, dict) else {}
|
||
reason_code = str(reason.get('code') or reason.get('blocking_reason_code') or '').strip()
|
||
owning_gate = PROCESS_GATE_REASON_OWNING_GATE.get(reason_code)
|
||
if owning_gate in LIFECYCLE_STAGE_BY_GATE:
|
||
lifecycle_stage = LIFECYCLE_STAGE_BY_GATE[owning_gate]
|
||
authority = 'blocking_reason'
|
||
else:
|
||
lifecycle_stage = None
|
||
authority = 'unclassified'
|
||
return {
|
||
'lifecycle_stage': lifecycle_stage,
|
||
'lifecycle_entries': lifecycle_entries(
|
||
lifecycle_stage=lifecycle_stage,
|
||
has_candidate_context=has_candidate_context,
|
||
has_intake_parse_context=has_intake_parse_context,
|
||
),
|
||
'lifecycle_authority': authority,
|
||
}
|