feat(in-transit-transfer): 实现匹配静默顶替并完善单号与明细表体验
This commit is contained in:
1 parent
f5611d6783
commit
79050d02ba
25 files changed
+806
-178
No files matched your search
@@ -1478,6 +1478,7 @@ def _serialize_backlog_item(item) -> dict[str, Any]:
|
||||
'item_id': item.item_id,
|
||||
'source_code': item.source_code,
|
||||
'invoice_no': item.invoice_no,
|
||||
'external_doc_no': item.external_doc_no,
|
||||
'material_no': item.material_no,
|
||||
'status': item.status,
|
||||
'reason': item.reason,
|
||||
|
||||
@@ -36,6 +36,7 @@ from app.models import TransferAttempt, TransferInvoiceItem, TransferMovementLed
|
||||
from app.services.in_transit_transfer.matching_service import (
|
||||
circle_ledger_record_ids_scope_union,
|
||||
pending_ledger_query,
|
||||
ledger_external_doc_no,
|
||||
transfer_type_code_of,
|
||||
)
|
||||
from app.services.in_transit_transfer.status import TransferStatus
|
||||
@@ -100,6 +101,8 @@ class BacklogItemDTO:
|
||||
result_code: Optional[str] = None
|
||||
# 出生匹配快照上的调拨类型 code。空串表示行没有匹配或快照未写类型。
|
||||
transfer_type: str = ""
|
||||
# 档上的外部单号。空串表示这行没有外部单号。
|
||||
external_doc_no: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -207,7 +210,7 @@ def _latest_attempt_times(item_ids: list[int]) -> dict[int, datetime]:
|
||||
def _backlog_base_query(statuses, *, source_code: str | None = None, keyword: str | None = None):
|
||||
"""待处理池基础查询(状态集合 + 可选来源/关键字过滤)。
|
||||
|
||||
keyword 与前端池内筛选同字段集(Invoice/料号/品名/来源,2026-10-16 随
|
||||
keyword 与前端池内筛选同字段集(单号/外部单号/料号/品名/来源,2026-10-16 随
|
||||
服务端分页下推),大小写不敏感子串匹配(ilike)。
|
||||
|
||||
Args:
|
||||
@@ -222,12 +225,19 @@ def _backlog_base_query(statuses, *, source_code: str | None = None, keyword: st
|
||||
query = query.filter_by(source_code=source_code)
|
||||
if keyword:
|
||||
like = f"%{keyword}%"
|
||||
query = query.filter(or_(
|
||||
TransferInvoiceItem.invoice_no.ilike(like),
|
||||
TransferInvoiceItem.material_no.ilike(like),
|
||||
TransferInvoiceItem.material_name.ilike(like),
|
||||
TransferInvoiceItem.source_code.ilike(like),
|
||||
))
|
||||
query = (
|
||||
query.outerjoin(
|
||||
TransferMovementLedger,
|
||||
TransferInvoiceItem.ledger_record_id == TransferMovementLedger.id,
|
||||
)
|
||||
.filter(or_(
|
||||
TransferInvoiceItem.invoice_no.ilike(like),
|
||||
TransferMovementLedger.external_doc_no.ilike(like),
|
||||
TransferInvoiceItem.material_no.ilike(like),
|
||||
TransferInvoiceItem.material_name.ilike(like),
|
||||
TransferInvoiceItem.source_code.ilike(like),
|
||||
))
|
||||
)
|
||||
return query
|
||||
|
||||
|
||||
@@ -266,6 +276,7 @@ def _backlog_dtos(rows: list[TransferInvoiceItem], current: datetime) -> list[Ba
|
||||
item_id=item.id,
|
||||
source_code=item.source_code,
|
||||
invoice_no=item.invoice_no,
|
||||
external_doc_no=ledger_external_doc_no(item),
|
||||
material_no=item.material_no,
|
||||
status=item.status,
|
||||
reason=item.result_message or item.result_code or "",
|
||||
@@ -303,7 +314,10 @@ def list_backlog(*, now: datetime | None = None, source_code: str | None = None)
|
||||
current = now or datetime.utcnow()
|
||||
rows = (
|
||||
_backlog_base_query(_ALL_BACKLOG_STATUSES, source_code=source_code)
|
||||
.options(joinedload(TransferInvoiceItem.matching))
|
||||
.options(
|
||||
joinedload(TransferInvoiceItem.matching),
|
||||
joinedload(TransferInvoiceItem.ledger_record),
|
||||
)
|
||||
.order_by(*_BACKLOG_ORDER_BY)
|
||||
.all()
|
||||
)
|
||||
@@ -330,7 +344,7 @@ def list_backlog_page(
|
||||
Args:
|
||||
now: 判定时刻(默认当前时间)。
|
||||
source_code: 可选来源过滤。
|
||||
keyword: 可选关键字(Invoice/料号/品名/来源,子串大小写不敏感)。
|
||||
keyword: 可选关键字(单号/外部单号/料号/品名/来源,子串大小写不敏感)。
|
||||
result_filter: 结果切片(all/recoverable/closed/unknown)。
|
||||
page: 页码(1 起)。
|
||||
page_size: 每页行数(1..100)。
|
||||
@@ -345,7 +359,10 @@ def list_backlog_page(
|
||||
base = _backlog_base_query(statuses, source_code=source_code, keyword=keyword or None)
|
||||
total = base.count()
|
||||
rows = (
|
||||
base.options(joinedload(TransferInvoiceItem.matching))
|
||||
base.options(
|
||||
joinedload(TransferInvoiceItem.matching),
|
||||
joinedload(TransferInvoiceItem.ledger_record),
|
||||
)
|
||||
.order_by(*_BACKLOG_ORDER_BY)
|
||||
.offset((page - 1) * page_size)
|
||||
.limit(page_size)
|
||||
|
||||
@@ -124,6 +124,8 @@ class MatchingItemDTO:
|
||||
# 出生匹配快照上的调拨类型 code。可调拨明细跨类型混排时用来展示类别;
|
||||
# 无匹配或快照未写类型时为空串。
|
||||
transfer_type: str = ""
|
||||
# 档上的外部单号(WMS Order #)。空串表示这行没有外部单号。
|
||||
external_doc_no: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
@@ -63,6 +63,7 @@ from app.services.in_transit_transfer.matching_service import (
|
||||
RouteMapping,
|
||||
TransferDeps,
|
||||
item_external_lot_no,
|
||||
ledger_external_doc_no,
|
||||
)
|
||||
from app.services.in_transit_transfer.quantity import classify_quantity_match
|
||||
from app.services.in_transit_transfer.orchestrator import (
|
||||
@@ -805,12 +806,12 @@ def _build_order_lines(items: list[TransferInvoiceItem]) -> list[dict]:
|
||||
ERP 行粒度 = 物料+批号:同单同料号同批号跨 Invoice 聚合为一行,数量按
|
||||
Decimal 精确相加后取 6 位小数;客户端行数量为数值型(能力 Schema number,
|
||||
且须可 JSON 序列化进引擎审计),快照文本(6 位小数字符串)随 D14 留档。
|
||||
行内附带 invoice_no 溯源(逗号连接,供测试替身/排障;ERP 契约无此字段,
|
||||
真实绑定未映射即透传为惰性键)。
|
||||
行内附带 invoice_no 与 external_doc_no 溯源(各自逗号连接,供清单展示与排障;
|
||||
ERP 契约无这两字段,真实绑定未映射即透传为惰性键)。
|
||||
|
||||
Returns:
|
||||
行 dict 列表:line_no/material_no/from_warehouse/to_warehouse/lot_no/
|
||||
quantity/unit/invoice_no/quantity_text(quantity_text 仅供快照,调用方裁剪)。
|
||||
quantity/unit/invoice_no/external_doc_no/quantity_text(quantity_text 仅供快照,调用方裁剪)。
|
||||
"""
|
||||
grouped: dict[tuple[str, str, str, str], list[TransferInvoiceItem]] = {}
|
||||
for item in items:
|
||||
@@ -831,6 +832,9 @@ def _build_order_lines(items: list[TransferInvoiceItem]) -> list[dict]:
|
||||
quantity = total.quantize(_QUANTITY_QUANT)
|
||||
unit = next((str(item.unit or "").strip() for item in group if str(item.unit or "").strip()), "")
|
||||
invoices = ",".join(sorted({item.invoice_no for item in group}))
|
||||
external_docs = ",".join(sorted({
|
||||
doc for item in group if (doc := ledger_external_doc_no(item))
|
||||
}))
|
||||
lines.append({
|
||||
"line_no": index,
|
||||
"material_no": key[0],
|
||||
@@ -840,6 +844,7 @@ def _build_order_lines(items: list[TransferInvoiceItem]) -> list[dict]:
|
||||
"quantity": float(quantity),
|
||||
"unit": unit,
|
||||
"invoice_no": invoices,
|
||||
"external_doc_no": external_docs,
|
||||
"quantity_text": f"{quantity:.6f}",
|
||||
})
|
||||
return lines
|
||||
@@ -1273,6 +1278,7 @@ def _build_run_result(run: TransferRun) -> RunResultDTO:
|
||||
material_name=item.material_name,
|
||||
specification=item.specification,
|
||||
invoice_no=item.invoice_no,
|
||||
external_doc_no=ledger_external_doc_no(item),
|
||||
movement_date=item.movement_date,
|
||||
from_warehouse=item.from_warehouse,
|
||||
from_quantity=Decimal(item.external_quantity) if item.external_quantity else None,
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
movement_date 非空)+ 类型快照范围规则(apply_scope_filters 单一编译器)+
|
||||
未被有效 items 占用(D17:ledger_record_id 引用或同匹配 grain
|
||||
(source, invoice/doc_no, material, movement_date) 已被有效 item 占用;
|
||||
取消匹配即释放)。tt14 档自然键扩维后同 grain 可对应多条档(如 carton
|
||||
取消匹配即释放;ADR 0007 匹配顶替在圈档前取消被新范围盖住、尚未写 ERP 的旧匹配)。tt14 档自然键扩维后同 grain 可对应多条档(如 carton
|
||||
空→有值的重解析双行);出生前按 grain 择一建 item(不累加数量,防翻倍),
|
||||
占用亦按 grain 互斥。业务字段=档原值;仓库经 resolve_warehouse;库存评估
|
||||
链以 items 为工作单元。create_matching 先落 Run + Matching 再驱动管道。
|
||||
@@ -295,11 +295,12 @@ def _dedupe_ledger_records_for_item_birth(
|
||||
def _effective_occupancy_filter(*, exclude_matching_id: int | None = None):
|
||||
"""构造「未被有效 items 占用」(D17)的 NOT EXISTS 过滤(圈档互斥单一口径)。
|
||||
|
||||
占用定义(规格 §9 + grain 互斥):存在有效 item(所属匹配未取消、行非
|
||||
blocked_file_removed)且满足其一——(a) ledger_record_id 直指本档;
|
||||
(b) 与本档同匹配 grain(source/invoice=doc_no/material/movement_date)。
|
||||
(b) 覆盖 tt14 自然键扩维后同业务 grain 的镜像分叉档,避免待匹配桶与二次
|
||||
匹配仍圈到兄弟档。取消匹配即释放;人工关闭/成功终态不释放。
|
||||
占用定义(规格 §9 + grain 互斥 + ADR 0007):存在已挂上匹配的有效 item
|
||||
(所属匹配未取消,或该行是人工关闭;行非 blocked_file_removed)且满足其一
|
||||
——(a) ledger_record_id 直指本档;(b) 与本档同匹配 grain
|
||||
(source/invoice=doc_no/material/movement_date)。没有挂上匹配的明细不占用。
|
||||
(b) 覆盖 tt14 自然键扩维后同业务 grain 的镜像分叉档。取消匹配即释放,
|
||||
人工关闭除外;成功终态不释放。
|
||||
|
||||
Args:
|
||||
exclude_matching_id: 重查对账场景传入本匹配 id——本匹配自有 items 不构成
|
||||
@@ -308,9 +309,8 @@ def _effective_occupancy_filter(*, exclude_matching_id: int | None = None):
|
||||
SQLAlchemy NOT EXISTS 子句(作用于 TransferMovementLedger 查询)。
|
||||
"""
|
||||
# material_code 可空:COALESCE 与出生侧 ``material_code or ""`` 对齐。
|
||||
# grain 互斥只认「已绑定未取消匹配」的行——出生翻转前 matching_id=NULL 的
|
||||
# 历史孤儿行不得按 grain 封死整票待匹配(本地曾因此从 ~2k 掉到 5)。
|
||||
# ledger_record_id 直指占用仍含 NULL 归属行(保持 D17 原口径)。
|
||||
# 【ADR 0007】没有挂上匹配的明细不占货(直指与 grain 都不算)。
|
||||
# 人工关闭在所属匹配被顶替之后仍然占用。
|
||||
grain_match = and_(
|
||||
TransferInvoiceItem.matching_id.isnot(None),
|
||||
TransferInvoiceItem.source_code == TransferMovementLedger.source_code,
|
||||
@@ -328,17 +328,21 @@ def _effective_occupancy_filter(*, exclude_matching_id: int | None = None):
|
||||
TransferInvoiceItem.result_code != RESULT_FILE_REMOVED),
|
||||
]
|
||||
if exclude_matching_id is None:
|
||||
# 匹配创建:任何未取消匹配(含无归属的历史行)的有效引用都算占用。
|
||||
# 匹配创建:必须已挂上匹配。未取消的匹配占用;人工关闭在匹配被顶替后仍占用。
|
||||
item_conditions.append(TransferInvoiceItem.matching_id.isnot(None))
|
||||
item_conditions.append(or_(
|
||||
TransferInvoiceItem.matching_id.is_(None),
|
||||
TransferMatching.status != _MATCHING_STATUS_CANCELLED,
|
||||
TransferInvoiceItem.status == status_machine.TransferStatus.MANUALLY_CLOSED.value,
|
||||
))
|
||||
else:
|
||||
# 重查对账:仅「其他匹配」的有效引用算占用(本匹配自有 items 允许刷新)。
|
||||
item_conditions.append(and_(
|
||||
TransferInvoiceItem.matching_id.isnot(None),
|
||||
TransferInvoiceItem.matching_id != exclude_matching_id,
|
||||
TransferMatching.status != _MATCHING_STATUS_CANCELLED,
|
||||
or_(
|
||||
TransferMatching.status != _MATCHING_STATUS_CANCELLED,
|
||||
TransferInvoiceItem.status == status_machine.TransferStatus.MANUALLY_CLOSED.value,
|
||||
),
|
||||
))
|
||||
occupancy = (
|
||||
db.session.query(TransferInvoiceItem.id)
|
||||
@@ -611,6 +615,23 @@ def matching_transform_rules(matching: TransferMatching) -> tuple[TransformRule,
|
||||
return parse_transform_rules(scope.get("transform_rules"))
|
||||
|
||||
|
||||
def ledger_external_doc_no(item: TransferInvoiceItem) -> str:
|
||||
"""读取明细行对应异动档上的外部单号。
|
||||
|
||||
核心逻辑:外部单号只在档上(WMS Order #),明细行经 ledger_record_id 指向档。
|
||||
没有档、或档上为空时返回空串,列表显示占位。
|
||||
|
||||
Args:
|
||||
item: 明细行。ledger_record 可已预加载;未预加载时按外键懒加载。
|
||||
Returns:
|
||||
外部单号;没有则空串。
|
||||
"""
|
||||
record = item.ledger_record
|
||||
if record is None:
|
||||
return ""
|
||||
return str(record.external_doc_no or "").strip()
|
||||
|
||||
|
||||
def item_external_lot_no(item: TransferInvoiceItem) -> str:
|
||||
"""行批号的外部形态派生(按行所属匹配快照的转换规则;无规则=恒等)。
|
||||
|
||||
@@ -1242,6 +1263,116 @@ def _item_evaluated_sha(item: TransferInvoiceItem) -> str | None:
|
||||
return lines[0].get("file_sha256")
|
||||
|
||||
|
||||
def _source_set(codes: list[str] | None) -> tuple[str, ...]:
|
||||
"""把来源编码收成可比较的集合。
|
||||
|
||||
核心逻辑:去掉空白后排序,使 [B, A] 与 [A, B] 视为同一组来源。
|
||||
Args:
|
||||
codes: 来源编码列表;None 当空。
|
||||
Returns:
|
||||
排序后的来源编码元组。
|
||||
"""
|
||||
return tuple(sorted(str(code).strip() for code in (codes or []) if str(code).strip()))
|
||||
|
||||
|
||||
def _date_range_covers(new_from: str | None, new_to: str | None, old_from: str | None, old_to: str | None) -> bool:
|
||||
"""新异动日期范围是否把旧范围整个盖住。
|
||||
|
||||
核心逻辑:任一侧缺少起或止,都视为没盖住(无日期范围的旧匹配不被顶替)。
|
||||
日期是 YYYY-MM-DD,字符串序与日历序一致。
|
||||
Args:
|
||||
new_from / new_to: 本次匹配的异动日期起止。
|
||||
old_from / old_to: 旧匹配快照上的异动日期起止。
|
||||
Returns:
|
||||
新范围包含旧范围时为 True。
|
||||
"""
|
||||
if not new_from or not new_to or not old_from or not old_to:
|
||||
return False
|
||||
return str(new_from) <= str(old_from) and str(new_to) >= str(old_to)
|
||||
|
||||
|
||||
def _matching_has_erp_activity(items: list[TransferInvoiceItem]) -> bool:
|
||||
"""匹配是否已经写出单据,或有行正在写、结果未知。
|
||||
|
||||
核心逻辑:有外部单号,或行状态为执行中、结果未知、成功,就不顶替,
|
||||
避免把在途 ERP 写入拆成两张单。
|
||||
Args:
|
||||
items: 该匹配名下的明细。
|
||||
Returns:
|
||||
存在上述活动时为 True。
|
||||
"""
|
||||
active = {
|
||||
status_machine.TransferStatus.EXECUTING.value,
|
||||
status_machine.TransferStatus.RESULT_UNKNOWN.value,
|
||||
status_machine.TransferStatus.SUCCESS.value,
|
||||
status_machine.TransferStatus.DUPLICATE_SUCCESS.value,
|
||||
}
|
||||
return any(
|
||||
(item.external_document_no or "").strip() or item.status in active
|
||||
for item in items
|
||||
)
|
||||
|
||||
|
||||
def _supersede_covered_matchings(
|
||||
*,
|
||||
transfer_type: str,
|
||||
source_codes: list[str],
|
||||
movement_date_from: str | None,
|
||||
movement_date_to: str | None,
|
||||
exclude_matching_id: int,
|
||||
) -> None:
|
||||
"""圈档前顶替被新范围盖住、尚未写 ERP 的旧匹配(ADR 0007)。
|
||||
|
||||
核心逻辑:同一调拨类型、同一组来源、旧日期范围被新范围整个盖住,且名下没有
|
||||
ERP 单据、没有正在写或结果未知的行,则匹配改为 cancelled,名下明细一并取消。
|
||||
盖不住或已在写 ERP 的旧匹配保持原样,清单不列举它们的编号。
|
||||
|
||||
Args:
|
||||
transfer_type: 本次匹配的调拨类型编码。
|
||||
source_codes: 本次来源集合。
|
||||
movement_date_from / movement_date_to: 本次异动日期范围;缺一则不顶替任何人。
|
||||
exclude_matching_id: 刚落库、尚未圈档的本次匹配,不能把自己顶掉。
|
||||
"""
|
||||
new_sources = _source_set(source_codes)
|
||||
if not new_sources:
|
||||
return
|
||||
candidates = (
|
||||
TransferMatching.query
|
||||
.filter(TransferMatching.id != exclude_matching_id)
|
||||
.filter(TransferMatching.status != _MATCHING_STATUS_CANCELLED)
|
||||
.all()
|
||||
)
|
||||
for matching in candidates:
|
||||
scope = matching.scope_json if isinstance(matching.scope_json, dict) else {}
|
||||
if str(scope.get("transfer_type") or "") != transfer_type:
|
||||
continue
|
||||
if _source_set(scope.get("source_codes")) != new_sources:
|
||||
continue
|
||||
items = TransferInvoiceItem.query.filter_by(matching_id=matching.id).all()
|
||||
covered = _date_range_covers(
|
||||
movement_date_from,
|
||||
movement_date_to,
|
||||
scope.get("movement_date_from"),
|
||||
scope.get("movement_date_to"),
|
||||
)
|
||||
# 匹配状态 success 只表示没有可执行行(全阻断也会落成 success),
|
||||
# 不能当成已经写出 ERP。是否写过只看行上的单据号和执行状态。
|
||||
if (
|
||||
not covered
|
||||
or matching.status == status_machine.TransferStatus.EXECUTING.value
|
||||
or _matching_has_erp_activity(items)
|
||||
):
|
||||
continue
|
||||
matching.status = _MATCHING_STATUS_CANCELLED
|
||||
for item in items:
|
||||
# 人工关闭是仓库人员的裁定,顶替不把它改回可圈状态。
|
||||
if item.status == status_machine.TransferStatus.MANUALLY_CLOSED.value:
|
||||
continue
|
||||
item.status = status_machine.TransferStatus.CANCELLED.value
|
||||
item.result_code = "cancelled"
|
||||
item.result_message = "superseded by a new matching"
|
||||
|
||||
|
||||
def create_matching(
|
||||
*,
|
||||
source_codes: list[str],
|
||||
@@ -1259,6 +1390,7 @@ def create_matching(
|
||||
管道回写 status/counts/estimated/hash 后随主事务统一提交。类型必填且配置
|
||||
驱动(REQUIRED/NOT_CONFIGURED/DISABLED fail-closed,D8)。圈档记录类型由
|
||||
run_matching 对已加载的 scope_rules 调用 record_kinds_of,不再写死入站。
|
||||
【ADR 0007】圈档前顶替被本次日期范围盖住、尚未写 ERP 的同类型同来源旧匹配。
|
||||
|
||||
Args:
|
||||
source_codes / business_date / deps: 同 run_matching。
|
||||
@@ -1299,6 +1431,14 @@ def create_matching(
|
||||
)
|
||||
db.session.add(matching)
|
||||
db.session.flush()
|
||||
# [ADR 0007] 先释放被盖住的旧匹配,本次圈档才能看到那批货。
|
||||
_supersede_covered_matchings(
|
||||
transfer_type=config.code,
|
||||
source_codes=list(source_codes),
|
||||
movement_date_from=movement_date_from,
|
||||
movement_date_to=movement_date_to,
|
||||
exclude_matching_id=matching.id,
|
||||
)
|
||||
|
||||
result = run_matching(
|
||||
source_codes=source_codes,
|
||||
@@ -1404,6 +1544,7 @@ def restore_matching(
|
||||
# 同口径(2026-09-29 统一),同一批行在匹配卡/可调拨明细/待处理池顺序一致。
|
||||
rows_all = (
|
||||
TransferInvoiceItem.query
|
||||
.options(joinedload(TransferInvoiceItem.ledger_record))
|
||||
.filter(TransferInvoiceItem.matching_id == matching.id)
|
||||
.order_by(
|
||||
TransferInvoiceItem.movement_date.asc().nulls_last(),
|
||||
@@ -1531,6 +1672,7 @@ def _item_dto(
|
||||
material_name=item.material_name,
|
||||
specification=item.specification,
|
||||
invoice_no=item.invoice_no,
|
||||
external_doc_no=ledger_external_doc_no(item),
|
||||
movement_date=item.movement_date,
|
||||
lot_no=item.lot_no or "",
|
||||
movement_warehouse_code=item.movement_warehouse_code or "",
|
||||
@@ -1600,7 +1742,10 @@ def list_executable_items(*, deps: TransferDeps | None = None, now: datetime | N
|
||||
# 2026-09-29 与待处理池/匹配卡视图统一为同一口径(用户按同序处理两个池子)。
|
||||
rows = (
|
||||
TransferInvoiceItem.query
|
||||
.options(joinedload(TransferInvoiceItem.matching))
|
||||
.options(
|
||||
joinedload(TransferInvoiceItem.matching),
|
||||
joinedload(TransferInvoiceItem.ledger_record),
|
||||
)
|
||||
# [防御性双保险] 所属匹配已取消的行不入队:取消路径已把匹配名下 ready
|
||||
# 行退役为 cancelled(cancellation.py),此处按匹配状态再拒一次——
|
||||
# 防绕过取消路径的状态组合(如手工改库/历史数据)产生可执行行。
|
||||
|
||||
@@ -12,7 +12,10 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app.services.in_transit_transfer.matching_service import item_external_lot_no
|
||||
from app.services.in_transit_transfer.matching_service import (
|
||||
item_external_lot_no,
|
||||
ledger_external_doc_no,
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 单状态 → 展示分组映射(Task 17:首页「已执行」区块统计口径)
|
||||
@@ -71,6 +74,7 @@ def serialize_item(item) -> dict[str, Any]:
|
||||
"material_name": item.material_name,
|
||||
"specification": item.specification,
|
||||
"invoice_no": item.invoice_no,
|
||||
"external_doc_no": getattr(item, "external_doc_no", "") or "",
|
||||
"lot_no": item.lot_no,
|
||||
"movement_date": item.movement_date,
|
||||
"from_warehouse": item.from_warehouse,
|
||||
@@ -177,6 +181,7 @@ def serialize_order(doc, items: list) -> dict[str, Any]:
|
||||
# 行级溯源来源单号(2026-09-29 契约扩充,随 ERP 写入行同款口径
|
||||
# 透传):业务上一行恒对应一张 invoice,前端明细表首列展示。
|
||||
"invoice_no": line["invoice_no"],
|
||||
"external_doc_no": line.get("external_doc_no") or "",
|
||||
"material_no": line["material_no"],
|
||||
"material_name": name_by_key.get(
|
||||
(line["material_no"], line["lot_no"],
|
||||
@@ -222,6 +227,7 @@ def serialize_executed_order(doc, items: list) -> dict[str, Any]:
|
||||
"items": [
|
||||
{
|
||||
"invoice_no": item.invoice_no,
|
||||
"external_doc_no": ledger_external_doc_no(item),
|
||||
"material_no": item.material_no,
|
||||
"material_name": item.material_name,
|
||||
# 批号保持文件证据原形态(raw 原则);展示侧拨出批号由前端
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""在途调拨匹配服务测试(L1,SQLite 内存库)。
|
||||
|
||||
用途:验证日期范围校验(截止≤业务日期)、严格数量匹配、来源隔离、
|
||||
匹配快照 Hash 稳定、缺文件等待与进度恢复;专项用例锁生产默认 lag=2。
|
||||
匹配快照 Hash 稳定、缺文件等待与进度恢复、匹配顶替;专项用例锁生产默认 lag=2。
|
||||
依赖:app fixture、transfer_db、fakes、repo_fakes、matching_service。
|
||||
逻辑:全部经真实 ORM 落库(create_matching/restore_matching),
|
||||
外部依赖注入 Fake 替身。圈档记录类型用例直接插出站档行,
|
||||
@@ -1152,3 +1152,182 @@ def test_cancel_matching_releases_ledger_occupancy(transfer_db, dbs_ready_file):
|
||||
born = TransferInvoiceItem.query.filter_by(matching_id=second_matching.id).all()
|
||||
assert len(born) == 2
|
||||
assert all(row.status == "ready_to_execute" for row in born)
|
||||
|
||||
|
||||
def test_create_matching_supersedes_covered_unwritten_matching(transfer_db, dbs_ready_file):
|
||||
"""新匹配盖住同一类型、同一来源、尚未写 ERP 的旧匹配时,旧匹配取消,货出现在新匹配上。
|
||||
|
||||
夹具两行异动日都是 2026-08-04。第一次与第二次都用 08-01~08-04,新范围盖住旧范围。
|
||||
"""
|
||||
from app.models import TransferInvoiceItem, TransferMatching
|
||||
|
||||
deps, erp, _ = _deps(dbs_ready_file)
|
||||
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1200")
|
||||
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
|
||||
seed_rows_from_file("DBS-AWSBI", deps, "2026-08-06")
|
||||
covered = dict(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-01",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
|
||||
first = create_matching(deps=deps, **covered)
|
||||
# [断言意图] 顶替前旧匹配自己先圈到夹具的两张单,证明后面的 2 不是「本来就没占上」。
|
||||
assert first.counts.executable_count == 2
|
||||
|
||||
second = create_matching(deps=deps, **covered)
|
||||
|
||||
first_matching = TransferMatching.query.filter_by(matching_no=first.matching_id).one()
|
||||
# [断言意图] 被盖住且未写 ERP 的旧匹配不再有效,其可执行行不能留在队列里二次写 ERP。
|
||||
assert first_matching.status == "cancelled"
|
||||
retired = TransferInvoiceItem.query.filter_by(matching_id=first_matching.id).all()
|
||||
assert retired
|
||||
assert all(row.status == "cancelled" for row in retired)
|
||||
# [断言意图] 同一批货(HOM-001、HOX-002)出现在新匹配上,且仍可执行。
|
||||
assert second.counts.executable_count == 2
|
||||
second_matching = TransferMatching.query.filter_by(matching_no=second.matching_id).one()
|
||||
reborn = {
|
||||
row.invoice_no
|
||||
for row in TransferInvoiceItem.query.filter_by(matching_id=second_matching.id).all()
|
||||
}
|
||||
assert reborn == {"HOM-001", "HOX-002"}
|
||||
|
||||
|
||||
def test_create_matching_supersedes_covered_matching_with_only_blocked_rows(transfer_db, dbs_ready_file):
|
||||
"""全阻断的旧匹配状态虽是 success,只要还没写 ERP,新范围盖住时仍要顶替。"""
|
||||
from app.models import TransferInvoiceItem, TransferMatching
|
||||
|
||||
deps, erp, _ = _deps(dbs_ready_file)
|
||||
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1")
|
||||
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1")
|
||||
seed_rows_from_file("DBS-AWSBI", deps, "2026-08-06")
|
||||
covered = dict(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-01",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
first = create_matching(deps=deps, **covered)
|
||||
# [断言意图] 数量对不上时匹配状态是 success,不能因此被当成「已经写出单据」。
|
||||
assert first.status == "success"
|
||||
assert first.counts.executable_count == 0
|
||||
|
||||
second = create_matching(deps=deps, **covered)
|
||||
|
||||
first_matching = TransferMatching.query.filter_by(matching_no=first.matching_id).one()
|
||||
assert first_matching.status == "cancelled"
|
||||
second_matching = TransferMatching.query.filter_by(matching_no=second.matching_id).one()
|
||||
reborn = {
|
||||
row.invoice_no
|
||||
for row in TransferInvoiceItem.query.filter_by(matching_id=second_matching.id).all()
|
||||
}
|
||||
assert reborn == {"HOM-001", "HOX-002"}
|
||||
|
||||
|
||||
def test_create_matching_keeps_previous_when_new_range_does_not_cover(transfer_db, dbs_ready_file):
|
||||
"""新范围盖不住旧范围时,旧匹配保持有效,货仍留在旧匹配上。"""
|
||||
from app.models import TransferMatching
|
||||
|
||||
deps, erp, _ = _deps(dbs_ready_file)
|
||||
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1200")
|
||||
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
|
||||
seed_rows_from_file("DBS-AWSBI", deps, "2026-08-06")
|
||||
first = create_matching(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
deps=deps,
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-01",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
second = create_matching(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
deps=deps,
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-04",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
|
||||
first_matching = TransferMatching.query.filter_by(matching_no=first.matching_id).one()
|
||||
# [断言意图] 08-04~08-04 盖不住 08-01~08-04,旧匹配不能被取消。
|
||||
assert first_matching.status == "ready_to_execute"
|
||||
# [断言意图] 货仍被旧匹配占用,新匹配圈不到,清单不另列历史匹配编号。
|
||||
assert second.counts.executable_count == 0
|
||||
|
||||
|
||||
def test_supersede_keeps_manually_closed_goods_out_of_the_new_matching(transfer_db, dbs_ready_file):
|
||||
"""顶替后,人工关闭的单号不回到新匹配。"""
|
||||
from app.models import TransferInvoiceItem, TransferMatching
|
||||
from app.services.in_transit_transfer.manual_close_service import manual_close_item
|
||||
|
||||
deps, erp, _ = _deps(dbs_ready_file)
|
||||
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1200")
|
||||
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
|
||||
seed_rows_from_file("DBS-AWSBI", deps, "2026-08-06")
|
||||
covered = dict(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-01",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
first = create_matching(deps=deps, **covered)
|
||||
first_matching = TransferMatching.query.filter_by(matching_no=first.matching_id).one()
|
||||
closed = TransferInvoiceItem.query.filter_by(matching_id=first_matching.id, invoice_no="HOX-002").one()
|
||||
manual_close_item(item_id=closed.id, reason="这批不再调拨")
|
||||
|
||||
second = create_matching(deps=deps, **covered)
|
||||
|
||||
# [断言意图] 人工关闭跨过顶替仍然有效,新匹配不能把 HOX-002 再圈成可执行。
|
||||
assert db.session.get(TransferInvoiceItem, closed.id).status == "manually_closed"
|
||||
second_matching = TransferMatching.query.filter_by(matching_no=second.matching_id).one()
|
||||
reborn = {
|
||||
row.invoice_no
|
||||
for row in TransferInvoiceItem.query.filter_by(matching_id=second_matching.id).all()
|
||||
}
|
||||
assert reborn == {"HOM-001"}
|
||||
|
||||
|
||||
def test_unattached_item_does_not_block_a_new_matching(transfer_db, dbs_ready_file):
|
||||
"""没有挂上任何匹配的明细不占货,新匹配仍能圈到那张单。"""
|
||||
from app.models import TransferInvoiceItem, TransferMatching, TransferMovementLedger
|
||||
|
||||
deps, erp, _ = _deps(dbs_ready_file)
|
||||
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1200")
|
||||
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
|
||||
seed_rows_from_file("DBS-AWSBI", deps, "2026-08-06")
|
||||
ledger = TransferMovementLedger.query.filter_by(source_code="DBS-AWSBI", doc_no="HOM-001").first()
|
||||
db.session.add(TransferInvoiceItem(
|
||||
matching_id=None,
|
||||
ledger_record_id=ledger.id,
|
||||
source_code="DBS-AWSBI",
|
||||
invoice_no="HOM-001",
|
||||
material_no="MAT-001",
|
||||
movement_date="2026-08-04",
|
||||
from_warehouse="DBS-TRANSIT",
|
||||
to_warehouse="HUB-CN01",
|
||||
movement_quantity="1200",
|
||||
status="matching",
|
||||
result_code="pending_stock_check",
|
||||
))
|
||||
db.session.commit()
|
||||
|
||||
summary = create_matching(
|
||||
source_codes=["DBS-AWSBI"],
|
||||
business_date="2026-08-06",
|
||||
deps=deps,
|
||||
transfer_type="in_transit_to_hub",
|
||||
movement_date_from="2026-08-01",
|
||||
movement_date_to="2026-08-04",
|
||||
)
|
||||
matching = TransferMatching.query.filter_by(matching_no=summary.matching_id).one()
|
||||
# [断言意图] 悬挂行没有匹配编号,不能挡住 HOM-001;两张夹具单都进新匹配。
|
||||
invoices = {
|
||||
row.invoice_no
|
||||
for row in TransferInvoiceItem.query.filter_by(matching_id=matching.id).all()
|
||||
}
|
||||
assert invoices == {"HOM-001", "HOX-002"}
|
||||
Reference in new issue
Block a user