Merge branch 'hugo-shipping-notice' into test

This commit is contained in:
hugo committed 2026-09-16 11:40:37 +08:00
commit 97cb27c632
2 files changed
+87 -3

No files matched your search

@@ -5,7 +5,8 @@ services/shipping_notice/inventory_file_store.py
依赖:models.ShippingInventoryFile/ShippingTaskInventoryLink、inventory_snapshot、export._storage_root。
逻辑:inventory_workflow 上传或解析库存时写入库存快照(Link 挂在正式 OT);
fulfillment_workflow / Excel 导出经 resolve_inventory_lines_for_execution:
冻结候选 OT 读 prepare 合并行,单源读 active Link 文件;禁止用 Notice.id 漏读 OT Link。
冻结候选读所属 OT 的 prepare 合并行(传入 Notice 时回源 order_task_id),单源读 active Link 文件;
禁止用 Notice.id 漏读 OT Link,导出数据源不得偏离行级匹配门禁命中的候选事实。
"""
from __future__ import annotations
@@ -242,6 +243,9 @@ def resolve_inventory_lines_for_execution(
本次执行使用的库存行;不会回写任务或通知单的库存行缓存。
"""
# [STEP 1] STP 冻结候选:匹配输入来自各源快照合并,不读被污染的单文件缓存。
# Notice 主键 ≠ OT 且不带候选集指针;冻结候选事实统一回源所属 OT 读取,
# 防止导出路径把多源任务退化成单源 Link,出现「行匹配过门禁但生成 Excel 失败」。
from app.models import ShippingOrderTask
from app.services.shipping_notice.stp_inventory_freeze import (
task_frozen_inventory_source_codes,
)
@@ -249,8 +253,12 @@ def resolve_inventory_lines_for_execution(
prepare_candidate_inventory_lines,
)
if len(task_frozen_inventory_source_codes(task)) >= 1:
prep = prepare_candidate_inventory_lines(task)
owner_task = task
order_task_id = getattr(task, 'order_task_id', None)
if order_task_id:
owner_task = db.session.get(ShippingOrderTask, int(order_task_id)) or task
if len(task_frozen_inventory_source_codes(owner_task)) >= 1:
prep = prepare_candidate_inventory_lines(owner_task)
if prep.get('status') == 'ready':
rows = [row for row in list(prep.get('lines') or []) if isinstance(row, dict)]
if rows:
@@ -73,3 +73,79 @@ def test_active_inventory_file_for_notice_uses_order_task_link(app, tmp_path) ->
assert active_inventory_file_for_task(notice) is not None
assert active_inventory_file_for_task(notice).id == inventory_file.id
assert len(resolve_inventory_lines_for_execution(notice)) == 1
def test_resolve_inventory_lines_for_notice_reads_owner_ot_frozen_candidates(app, monkeypatch) -> None:
"""Notice 导出必须回源所属 OT 读冻结候选合并行;不得退化成 active Link 单源。
复现:多源路由任务行级匹配命中 B 源快照后,生成 Excel 传 Notice 取数,
冻结候选分支因 Notice 无候选集指针被跳过,退化成 A 源单文件 → 误报
「订单行没有匹配的库存记录」。数据源在导出时禁止被切换。
"""
with app.app_context():
# 先占用 Notice 自增 id=1,确保后续 Notice.id ≠ OT.id(复现生产错位)。
db.session.add(ShippingNotice(
task_no='SN-INV-NOTICE-CAND-BURN',
user_id=1,
source_type='manual',
))
db.session.flush()
ot = ShippingOrderTask(task_no='OT-INV-NOTICE-CAND', user_id=1, source_type='direct_pdf')
link_file = ShippingInventoryFile(
inventory_file_no='SIF-INV-NOTICE-CAND-LINK',
storage_path='unused://notice-cand-link.xlsx',
source_file_name='link-only.xlsx',
snapshot_status='ready',
inventory_lines_json=[{'part_number': 'ONLY_LINK_ROW'}],
line_count=1,
)
db.session.add_all([ot, link_file])
db.session.flush()
notice = ShippingNotice(
task_no='SN-INV-NOTICE-CAND',
user_id=1,
source_type='inventory_gate',
order_task_id=int(ot.id),
)
db.session.add(notice)
db.session.flush()
assert int(notice.id) != int(ot.id)
db.session.add(ShippingTaskInventoryLink(
task_id=int(ot.id),
inventory_file_id=int(link_file.id),
is_active=True,
access_mode='readonly',
))
db.session.commit()
# 冻结候选事实只挂在 OT 上;Notice 自身查不到任何冻结源。
monkeypatch.setattr(
'app.services.shipping_notice.stp_inventory_freeze.task_frozen_inventory_source_codes',
lambda task: ['SRC_A', 'SRC_B'] if isinstance(task, ShippingOrderTask) else [],
)
prepared_with = []
def _fake_prepare(task):
prepared_with.append(task)
return {
'status': 'ready',
'lines': [
{'part_number': 'P1', '__source_code': 'SRC_A'},
{'part_number': 'P2', '__source_code': 'SRC_B'},
],
}
monkeypatch.setattr(
'app.services.shipping_notice.stp_line_inventory_match.prepare_candidate_inventory_lines',
_fake_prepare,
)
rows = resolve_inventory_lines_for_execution(notice)
# 断言:合并候选行生效,且 prepare 回源到所属 OT(而非 Notice)。
assert [row['__source_code'] for row in rows] == ['SRC_A', 'SRC_B']
assert [int(t.id) for t in prepared_with] == [int(ot.id)]
# 断言:Link 单源文件行不得混入导出输入(导出数据源未被切换)。
assert all(row.get('part_number') != 'ONLY_LINK_ROW' for row in rows)