feat(in-transit-transfer): 打通ERP 写路径按票执行与已执行调拨单卡片入口

- 新增 TransferDocument 按票执行模型与单据号序列生成(document_id 独立模块,迁移 tt0d/tt8b)
- 发票行新增单位字段、matching_id 放宽可空,支持行与票两级执行口径(迁移 tt9c/tt1a)
- capability_client 新增 create_transfer 写契约与六态结果裁决,执行链补写前复查门禁
- 抽取/匹配/执行 Job 按 run/item/match 拆分并入 jobs store 队列,transfer 队列单实例 worker 配套本地启动脚本
- 首页新增「已执行调拨单」四统计卡入口与明细列表子视图,重查/重试/关闭/重新触发与筛选分页齐全
- 行评估新鲜度 TTL 从 10 分钟放宽为当日有效(86400s),跨日由文件版本对账门禁兜底
- 同步 chat/errors 文案四语言、测试全家与两份实施计划文档
This commit is contained in:
hugo committed 2026-09-27 18:13:25 +08:00
1 parent e750fa8602
commit 9f2b023a10
101 files changed
+11109 -2112

No files matched your search

@@ -1,15 +1,38 @@
"""在途调拨 DTO 序列化(API 与 Chat Action 共用)。
用途:将领域 DTO 转为前端稳定 JSON(匹配摘要/明细/来源进度/执行结果)。
依赖:contracts、execution_service。
逻辑:单一序列化来源,避免 API 与 Chat Action 各自实现导致契约漂移。
用途:将领域 DTO 转为前端稳定 JSON(匹配摘要/明细/来源进度/执行结果/单投影)。
依赖:contracts、execution_service(serialize_order 内按需延迟引用其行聚合,
避免模块级循环导入:execution_service 顶层引用本模块的 serialize_order)。
逻辑:单一序列化来源,避免 API 与 Chat Action 各自实现导致契约漂移;
单(TransferDocument)投影经 serialize_order 统一(Task 11 融合卡调拨单清单
与 Task 17 已执行单查询共用同一契约源)。
"""
from __future__ import annotations
from typing import Any
from app.services.in_transit_transfer.execution_service import serialize_run_result
from app.services.in_transit_transfer.movement_contract import external_lot_no
# ---------------------------------------------------------------------------
# 单状态 → 展示分组映射(Task 17:首页「已执行」区块统计口径)
# ---------------------------------------------------------------------------
# success=累计产出(含重复提交成功);failed/executing/result_unknown=未完结即待办;
# cancelled 不展示(无产生路径,且不在任何分组计数中)。
DOCUMENT_STATUS_GROUPS: dict[str, tuple[str, ...]] = {
"success": ("success", "duplicate_success"),
"failed": ("business_failed", "technical_failed"),
"executing": ("pending", "executing"),
"result_unknown": ("result_unknown",),
}
def order_status_group(status: str) -> str | None:
"""单状态 → 展示分组键;组外状态(cancelled)返回 None(不展示)。"""
for group, statuses in DOCUMENT_STATUS_GROUPS.items():
if status in statuses:
return group
return None
def serialize_summary(summary) -> dict[str, Any]:
@@ -26,6 +49,10 @@ def serialize_summary(summary) -> dict[str, Any]:
"matched_count": summary.counts.matched_count,
"executable_count": summary.counts.executable_count,
"blocked_count": summary.counts.blocked_count,
# 待查行(文件已落地未查库存)与失效行(blocked_file_removed)透传
# (2026-09-25 counts 契约扩展,键名与 DTO 字段同名)。
"pending_stock_check_count": summary.counts.pending_stock_check_count,
"invalid_count": summary.counts.invalid_count,
},
"estimated_transfer_quantity": summary.estimated_transfer_quantity,
"estimated_external_write_count": summary.estimated_external_write_count,
@@ -52,6 +79,8 @@ def serialize_item(item) -> dict[str, Any]:
"from_quantity": str(item.from_quantity) if item.from_quantity is not None else None,
"to_warehouse": item.to_warehouse,
"to_quantity": str(item.to_quantity),
# 交易单位原值透传(movement 契约第 11 字段 unit,T7 交接:序列化缺口补齐)。
"unit": item.unit,
"result": item.result,
"reason": item.reason,
"raw_lines": [
@@ -85,3 +114,113 @@ def serialize_matching_detail(detail: dict[str, Any]) -> dict[str, Any]:
"items": [serialize_item(item) for item in detail["items"]],
"sources": [serialize_source(source) for source in detail["sources"]],
}
def serialize_order(doc, items: list) -> dict[str, Any]:
"""单投影公共序列化(Task 11 融合卡调拨单清单与 Task 17 已执行单查询共用契约源)。
【AI 触发时机】:任何需要把一张 TransferDocument(单)转成前端 JSON 的场景
(GET /runs/{run_no} 的 orders 数组、GET /documents 的单行),禁止再各自拼装。
核心逻辑:[STEP 1] 行集按 ERP 行粒度(物料+批号+拨出/拨入库位)聚合,数量
Decimal 相加取 6 位小数字符串(D14 快照口径),unit/material_name 取组内首个
非空原值;[STEP 2] 行级 reason 契约(T13-3):键恒出现——失败/未知行=组内
首个非空行级 result_message,成功行/无原因为 None(前端按
「reason ?? 单 error_message」展示)。
Args:
doc: TransferDocument ORM 实例。
items: 单挂接行集(TransferInvoiceItem 列表,调用方保证顺序)。
Returns:
单投影 dict(8 键):{doc_no, receipt_date, from_warehouse, to_warehouse,
status, erp_document_no, error_message, lines}。
"""
# 延迟引用 execution_service 的行聚合(模块级互相引用会循环导入;
# 运行期 sys.modules 已就绪)。聚合键/序与 ERP 写入行一致。
from app.services.in_transit_transfer.execution_service import _build_order_lines
ordered = sorted(items, key=lambda row: row.id)
aggregated = _build_order_lines(ordered)
name_by_key: dict[tuple[str, str, str, str], str] = {}
reason_by_key: dict[tuple[str, str, str, str], str] = {}
for item in ordered:
# [外部形态] join 键与 _build_order_lines 聚合键同形(批号 WTP→HOW 派生),
# 否则 WTP 前缀行的品名/原因投影 lookup 会 miss。
key = (
str(item.material_no or "").strip(),
external_lot_no(item.lot_no),
str(item.from_warehouse or "").strip(),
str(item.to_warehouse or "").strip(),
)
name = str(item.material_name or "").strip()
if name and key not in name_by_key:
name_by_key[key] = name
# [T13-3] 行级失败/未知原因(result_message)随行组投影;成功行为 None。
message = str(item.result_message or "").strip()
if message and key not in reason_by_key:
reason_by_key[key] = message
return {
"doc_no": doc.doc_no,
"receipt_date": doc.receipt_date,
"from_warehouse": doc.from_warehouse,
"to_warehouse": doc.to_warehouse,
"status": doc.status,
"erp_document_no": doc.erp_document_no,
"error_message": doc.error_message,
"lines": [
{
"material_no": line["material_no"],
"material_name": name_by_key.get(
(line["material_no"], line["lot_no"],
line["from_warehouse"], line["to_warehouse"]),
"",
),
"lot_no": line["lot_no"],
"quantity": line["quantity_text"],
"unit": line["unit"],
# [T13-3 契约] reason=行级 result_message(失败/未知行),成功行 None。
"reason": reason_by_key.get(
(line["material_no"], line["lot_no"],
line["from_warehouse"], line["to_warehouse"]),
),
}
for line in aggregated
],
}
def serialize_executed_order(doc, items: list) -> dict[str, Any]:
"""已执行单查询投影(Task 17 GET /documents orders 逐键,首页区块数据源)。
在公共单投影(serialize_order)之上补齐查询视图字段:submit_timestamp/
status_group/created_at/run_no/items(invoice 粒度行集——与融合卡的聚合
lines 不同,本视图逐 Invoice 行展示,供展开明细与影响面核对)。
run_no 为 Task 17 契约的必要补充键(契约未列但 result_unknown 行
[重新触发] 必须持 run_no 调 POST /runs/{run_no}/documents/{doc_no}/retrigger)。
"""
base = serialize_order(doc, items)
return {
"doc_no": base["doc_no"],
"submit_timestamp": doc.submit_timestamp,
"status": base["status"],
"status_group": order_status_group(doc.status),
"receipt_date": base["receipt_date"],
"from_warehouse": base["from_warehouse"],
"to_warehouse": base["to_warehouse"],
"erp_document_no": base["erp_document_no"],
"error_message": base["error_message"],
"created_at": doc.created_at.isoformat() if doc.created_at else None,
"run_no": doc.run.run_no if doc.run_id and doc.run else None,
"items": [
{
"invoice_no": item.invoice_no,
"material_no": item.material_no,
"material_name": item.material_name,
"lot_no": item.lot_no or "",
"quantity": item.receipt_quantity,
"unit": item.unit or "",
}
for item in sorted(items, key=lambda row: row.id)
],
}