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:
1 parent
e750fa8602
commit
9f2b023a10
101 files changed
+11109
-2112
No files matched your search
@@ -4,9 +4,13 @@
|
||||
并把每一次外部调用完整记录到 call log,供断言副作用与调用次数。
|
||||
依赖:标准库(dataclasses / decimal / zoneinfo)、capability_client(域类型)。
|
||||
逻辑:FakeCapabilityClient 以 (料号, 拨出库位, 拨出批号) 三元组维护账面库存;
|
||||
query_stock 一次返回组内全部命中行;create_transfer 按注入的 scenario
|
||||
返回已分类的 TransferWriteResult(success / duplicate_success /
|
||||
business_failed / technical_failed / result_unknown)。
|
||||
query_stock 一次返回组内全部命中行;create_transfer 为单据级签名(与 T9 真实
|
||||
CapabilityClient 一致:operation_id + TransferOrder + final_only),按注入的
|
||||
scenario 返回已分类的 TransferWriteResult(success / duplicate_success /
|
||||
business_failed / technical_failed / result_unknown)。场景解析顺序:
|
||||
write_scenarios_by_doc[单号] → write_scenarios[单内行溯源 Invoice] →
|
||||
write_scenario 全局;final_only 仅记录进 call log(fake 不模拟 ERP 内部
|
||||
幂等细节,重触发命中已存在单证 → duplicate_success + 原单号)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -53,10 +57,17 @@ class ExternalCallRecord:
|
||||
call_seq: int
|
||||
returned_status: str
|
||||
method: str
|
||||
# 查询调用:记录目标元组;写调用:invoice_no + 数量。
|
||||
# 查询调用:记录目标元组;写调用(单据级):单头字段 + 完整行集 + final_only。
|
||||
targets: tuple[tuple[str, str, str], ...] = ()
|
||||
invoice_no: str = ""
|
||||
document_no: str | None = None
|
||||
doc_no: str = ""
|
||||
submit_timestamp: str = ""
|
||||
doc_date: str = ""
|
||||
posting_date: str = ""
|
||||
final_only: bool = False
|
||||
on_nonfinal_success: bool = False
|
||||
lines: tuple[dict, ...] = ()
|
||||
|
||||
|
||||
class FakeCapabilityClient:
|
||||
@@ -64,21 +75,31 @@ class FakeCapabilityClient:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.inventory: dict[tuple[str, str, str], Decimal] = {}
|
||||
# ERP 库存行单位(img09)按三元组维护;与真实 ERP 契约一致默认恒非空(EA),
|
||||
# 需要「ERP 未给单位」场景时显式 seed unit=""。
|
||||
self.stock_units: dict[tuple[str, str, str], str] = {}
|
||||
self.documents: dict[str, str] = {}
|
||||
self.verified_posted: dict[str, bool] = {}
|
||||
self.query_scenario: str = "normal"
|
||||
self.write_scenario: str = "success"
|
||||
# 按 Invoice 覆盖写场景(Key=invoice_no);未配置时回落到 write_scenario。
|
||||
self.write_scenarios: dict[str, str] = {}
|
||||
# 按单号覆盖写场景(Key=doc_no,T10 单据级执行主键);优先级最高。
|
||||
self.write_scenarios_by_doc: dict[str, str] = {}
|
||||
self.call_log: list[ExternalCallRecord] = []
|
||||
self._seq = 0
|
||||
|
||||
# -- 控制注入 ----------------------------------------------------------
|
||||
def seed_stock(self, material_no: str, from_warehouse: str, lot_no: str,
|
||||
quantity: str | int | Decimal) -> None:
|
||||
"""设置指定 (料号, 拨出库位, 拨出批号) 的账面库存。"""
|
||||
key = (str(material_no).strip(), str(from_warehouse).strip(), str(lot_no).strip())
|
||||
def seed_stock(self, material_no: str, warehouse_code: str, lot_no: str,
|
||||
quantity: str | int | Decimal, *, unit: str = "EA") -> None:
|
||||
"""设置指定 (料号, 仓库, 批号) 的账面库存与 ERP 单位(键=查询契约三元组)。
|
||||
|
||||
unit 模拟 ERP img09 库存单位(调拨行交易单位唯一来源);默认 "EA" 对应
|
||||
ERP「img09 恒非空」契约;显式传空串可构造「ERP 未给单位」场景。
|
||||
"""
|
||||
key = (str(material_no).strip(), str(warehouse_code).strip(), str(lot_no).strip())
|
||||
self.inventory[key] = Decimal(str(quantity))
|
||||
self.stock_units[key] = str(unit)
|
||||
|
||||
def clear_stock(self) -> None:
|
||||
"""清空全部账面库存(模拟 外部系统 库存被移走/未上账)。"""
|
||||
@@ -103,7 +124,7 @@ class FakeCapabilityClient:
|
||||
"""按组织批量返回库存行;timeout/error 场景按客户端语义抛错。"""
|
||||
self._seq += 1
|
||||
target_keys = [
|
||||
(str(t.material_no).strip(), str(t.from_warehouse).strip(), str(t.lot_no).strip())
|
||||
(str(t.material_no).strip(), str(t.warehouse_code).strip(), str(t.lot_no).strip())
|
||||
for t in targets
|
||||
]
|
||||
if self.query_scenario == "timeout":
|
||||
@@ -115,13 +136,16 @@ class FakeCapabilityClient:
|
||||
))
|
||||
raise RuntimeError("binding not found for capability query_inventory")
|
||||
rows: list[StockRow] = []
|
||||
for key in target_keys:
|
||||
# 同键多目标去重(真实 ERP 每料号+批号仅一行账面;重复目标不重复计行,
|
||||
# 否则 lookup 的同键累加会放大库存)。
|
||||
for key in dict.fromkeys(target_keys):
|
||||
if self.query_scenario == "inventory_not_found":
|
||||
continue
|
||||
if key in self.inventory:
|
||||
rows.append(StockRow(
|
||||
material_no=key[0], from_warehouse=key[1], lot_no=key[2],
|
||||
material_no=key[0], warehouse_code=key[1], lot_no=key[2],
|
||||
quantity=self.inventory[key],
|
||||
stock_unit=self.stock_units.get(key, "EA"),
|
||||
))
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq,
|
||||
@@ -130,90 +154,110 @@ class FakeCapabilityClient:
|
||||
))
|
||||
return StockQueryResult(rows=tuple(rows))
|
||||
|
||||
def create_transfer(self, *, operation_id: str, run_no: str, attempt_no: int,
|
||||
invoice_no: str, external_org: str, from_warehouse: str,
|
||||
to_warehouse: str, quantity: Decimal) -> TransferWriteResult:
|
||||
"""原子建单/审核/过账;按注入场景返回已分类结果并记录调用。"""
|
||||
def create_transfer(self, *, operation_id: str, order, final_only: bool = False,
|
||||
on_nonfinal_success=None, business_ref: str | None = None) -> TransferWriteResult:
|
||||
"""单据级原子建单/审核/过账(签名与 T9 真实 CapabilityClient 一致)。
|
||||
|
||||
business_ref(2026-09-27 调用日志 Task 4):随真实客户端签名同步新增的可选
|
||||
业务锚点参,替身仅接受不消费(调用日志断言走真实网关,见
|
||||
tests/services/integration_connector/test_engine.py)。
|
||||
|
||||
按注入场景返回已分类结果并记录调用(call log 携带单头字段 + 完整行集 +
|
||||
final_only + on_nonfinal_success 携带标记,供 D7②/D14/R7 断言)。场景解析
|
||||
顺序:write_scenarios_by_doc[doc_no] → write_scenarios[行溯源 invoice_no] →
|
||||
write_scenario;单号或行溯源 Invoice 命中已存在单证时走重复路径:
|
||||
已过账 → duplicate_success + 原单号(ERP 重触发回带原单号的域内表达);
|
||||
未证实过账 → duplicate_unverified(收敛为 result_unknown,禁自动重试)。
|
||||
R7 语义:非 timeout_before_commit 场景(stage 成功)在返回前回调一次
|
||||
on_nonfinal_success(模拟引擎「stage 成功、submit 之前」时序);该场景
|
||||
或 final_only=True 不回调。
|
||||
"""
|
||||
self._seq += 1
|
||||
key = str(invoice_no).strip()
|
||||
scenario = self.write_scenarios.get(key, self.write_scenario)
|
||||
doc_key = str(order.doc_no).strip()
|
||||
invoice_keys = []
|
||||
for line in order.lines:
|
||||
for inv in str(line.get("invoice_no") or "").split(","):
|
||||
inv = inv.strip()
|
||||
if inv and inv not in invoice_keys:
|
||||
invoice_keys.append(inv)
|
||||
lines = tuple(dict(line) for line in order.lines)
|
||||
|
||||
def _record(status: str, document_no: str | None = None) -> None:
|
||||
"""记录一次单据级写调用(单头 + 行集 + final_only/on_nonfinal_success 留痕)。"""
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id=operation_id, external_org=order.external_org,
|
||||
call_seq=self._seq, returned_status=status, method="create_transfer",
|
||||
invoice_no=invoice_keys[0] if invoice_keys else "",
|
||||
document_no=document_no, doc_no=doc_key,
|
||||
submit_timestamp=order.submit_timestamp, doc_date=order.doc_date,
|
||||
posting_date=order.posting_date, final_only=final_only,
|
||||
on_nonfinal_success=on_nonfinal_success is not None, lines=lines,
|
||||
))
|
||||
|
||||
def _fire_stage_callback() -> None:
|
||||
"""模拟引擎时序:stage 成功后、submit 前触发一次非末步成功回调。"""
|
||||
if on_nonfinal_success is not None and not final_only and scenario != "timeout_before_commit":
|
||||
on_nonfinal_success(None, 0)
|
||||
|
||||
# [场景解析] 单号 → 行溯源 Invoice → 全局。
|
||||
scenario = self.write_scenarios_by_doc.get(doc_key)
|
||||
if scenario is None:
|
||||
scenario = next((self.write_scenarios[k] for k in invoice_keys if k in self.write_scenarios), None)
|
||||
scenario = scenario or self.write_scenario
|
||||
|
||||
if scenario == "timeout_before_commit":
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, "timeout_before_commit",
|
||||
"create_transfer", invoice_no=key,
|
||||
))
|
||||
# 可证明未落单 → 明确技术失败(不产生单据)。
|
||||
# 可证明未落单 → 明确技术失败(不产生单据);stage 未成功 → 不触发回调。
|
||||
_record("timeout_before_commit")
|
||||
return TransferWriteResult(status="technical_failed", error_code="EXTERNAL_TIMEOUT_BEFORE_COMMIT")
|
||||
if self.write_scenario == "timeout_after_commit" or scenario == "timeout_after_commit":
|
||||
# [R7 时序模拟] 自此处起 stage 已成功:先触发回调(断点标记),再走 submit/返回。
|
||||
_fire_stage_callback()
|
||||
if scenario == "timeout_after_commit":
|
||||
# 已落单但无法确认:调用方必须投 result_unknown,禁止自动重试。
|
||||
self.documents.setdefault(key, f"TRF-{self._seq:05d}")
|
||||
document_no = self.documents[key]
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, "timeout_after_commit",
|
||||
"create_transfer", invoice_no=key, document_no=document_no,
|
||||
))
|
||||
document_no = self.documents.setdefault(doc_key, f"TRF-{self._seq:05d}")
|
||||
self.verified_posted[doc_key] = True
|
||||
_record("timeout_after_commit", document_no)
|
||||
return TransferWriteResult(
|
||||
status="result_unknown", error_code="EXTERNAL_TIMEOUT_AFTER_COMMIT",
|
||||
document_no=document_no,
|
||||
)
|
||||
if scenario == "business_failed" or self.write_scenario == "business_failed":
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, "business_failed",
|
||||
"create_transfer", invoice_no=key,
|
||||
))
|
||||
if scenario == "business_failed":
|
||||
_record("business_failed")
|
||||
return TransferWriteResult(
|
||||
status="business_failed", error_code="EXTERNAL_BUSINESS_REJECT",
|
||||
error_message="rejected by external system",
|
||||
)
|
||||
if key in self.documents:
|
||||
document_no = self.documents[key]
|
||||
posted = self.verified_posted.get(key, False)
|
||||
# [重复路径] 单号或行溯源 Invoice 命中已存在单证(幂等重触发场景)。
|
||||
existing = self.documents.get(doc_key)
|
||||
existing_key = doc_key
|
||||
if existing is None:
|
||||
for inv in invoice_keys:
|
||||
if inv in self.documents:
|
||||
existing = self.documents[inv]
|
||||
existing_key = inv
|
||||
break
|
||||
if existing is not None:
|
||||
posted = self.verified_posted.get(existing_key, False)
|
||||
status = "duplicate_success" if posted else "duplicate_unverified"
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, status,
|
||||
"create_transfer", invoice_no=key, document_no=document_no,
|
||||
))
|
||||
_record(status, existing)
|
||||
return TransferWriteResult(
|
||||
status=status, document_no=document_no, verified_posted=posted,
|
||||
status=status, document_no=existing, verified_posted=posted,
|
||||
error_code=None if posted else "TRANSFER_DUPLICATE_NOT_VERIFIED",
|
||||
)
|
||||
document_no = f"TRF-{self._seq:05d}"
|
||||
self.documents[key] = document_no
|
||||
self.verified_posted[key] = True
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, "success",
|
||||
"create_transfer", invoice_no=key, document_no=document_no,
|
||||
))
|
||||
self.documents[doc_key] = document_no
|
||||
self.verified_posted[doc_key] = True
|
||||
for inv in invoice_keys:
|
||||
self.documents.setdefault(inv, document_no)
|
||||
self.verified_posted.setdefault(inv, True)
|
||||
_record("success", document_no)
|
||||
return TransferWriteResult(status="success", document_no=document_no, verified_posted=True)
|
||||
|
||||
def verify_duplicate(self, *, operation_id: str, run_no: str, attempt_no: int,
|
||||
invoice_no: str, external_org: str, from_warehouse: str,
|
||||
to_warehouse: str, quantity: Decimal) -> TransferWriteResult:
|
||||
"""核实重复 Invoice 原单状态(已审核过账/未确认)。"""
|
||||
self._seq += 1
|
||||
key = str(invoice_no).strip()
|
||||
if key not in self.documents:
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, "duplicate_unverified", "verify_duplicate",
|
||||
invoice_no=key,
|
||||
))
|
||||
return TransferWriteResult(
|
||||
status="duplicate_unverified", error_code="TRANSFER_DUPLICATE_NOT_VERIFIED",
|
||||
)
|
||||
document_no = self.documents[key]
|
||||
posted = self.verified_posted.get(key, False)
|
||||
status = "duplicate_success" if posted else "duplicate_unverified"
|
||||
self.call_log.append(ExternalCallRecord(
|
||||
operation_id, external_org, self._seq, status, "verify_duplicate",
|
||||
invoice_no=key, document_no=document_no,
|
||||
))
|
||||
return TransferWriteResult(
|
||||
status=status, document_no=document_no,
|
||||
verified_posted=posted if posted else None,
|
||||
error_code=None if posted else "TRANSFER_DUPLICATE_NOT_VERIFIED",
|
||||
)
|
||||
|
||||
# -- 断言辅助 ----------------------------------------------------------
|
||||
@property
|
||||
def query_calls(self) -> int:
|
||||
"""库存查询(query_stock)调用次数计数器(零外部调用断言用)。"""
|
||||
return sum(1 for record in self.call_log if record.method == "query_stock")
|
||||
|
||||
def calls(self, *, invoice_no: str | None = None) -> list[ExternalCallRecord]:
|
||||
"""按 Invoice 过滤调用记录(默认返回全部;查询调用 invoice_no 为空串)。"""
|
||||
if invoice_no is None:
|
||||
|
||||
Reference in new issue
Block a user