feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理

清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
hugo committed 2026-08-26 02:47:07 +08:00
1 parent 7cfd5d167e
commit 1e5aedb9ed
235 files changed
+11394 -9613

No files matched your search

@@ -1,25 +1,23 @@
"""
backend/tests/test_shipping_notice_business_case.py
用途:验证货代业务单聚合在普通建单、修订和强制新建场景下的归属规则。
依赖:app fixture、ShippingNotice/ShippingCase、business_case_service。
逻辑:直接调用领域服务并断言业务单关系,不触发解析和库存副作用。
用途:验证 Notice 建单和替代关系不依赖 ShippingCase。
依赖:app fixture、ShippingNotice、task_workflow。
逻辑:直接调用领域服务并断言 Notice 独立事实,不触发解析和库存副作用。
"""
from app import db
import pytest
from app import db
from app.core.errors import ConflictError
from app.models import ShippingCase, ShippingCustomer, ShippingNotice
from app.services.shipping_notice.business_case_service import resolve_shipping_case_for_new_task
from app.models import ShippingCustomer, ShippingNotice
from app.services.shipping_notice.task_workflow import create_task
def _task(task_no: str, *, case_id: int | None = None, supersedes_task_id: int | None = None) -> ShippingNotice:
"""构造最小货代任务,供业务单关系断言使用。"""
def _task(task_no: str, *, supersedes_task_id: int | None = None) -> ShippingNotice:
"""构造最小 Notice,供替代关系断言使用。"""
return ShippingNotice(
task_no=task_no,
shipping_case_id=case_id,
supersedes_task_id=supersedes_task_id,
source_type='manual',
)
@@ -33,76 +31,32 @@ def _active_customer(*, short_code: str = 'CASE') -> ShippingCustomer:
return customer
def test_normal_and_force_create_use_different_cases(app):
"""普通建单和强制新建必须各自创建独立业务单。"""
def test_notices_are_independent_without_business_case(app):
"""不同 Notice 无需中间聚合实体,业务事实由自身保存。"""
with app.app_context():
normal = resolve_shipping_case_for_new_task(created_by_id=None)
forced = resolve_shipping_case_for_new_task(
duplicate_resolution_type='force_create',
created_by_id=None,
)
assert normal.id != forced.id
assert normal.business_no is None
assert forced.business_no is None
first = _task('SN-NOTICE-INDEPENDENT-1')
second = _task('SN-NOTICE-INDEPENDENT-2')
db.session.add_all([first, second])
db.session.commit()
assert first.id != second.id
assert first.business_no is None
assert second.business_no is None
def test_revision_inherits_existing_parent_case(app):
"""修订任务必须继承父任务已有业务单,不能创建新业务单。"""
def test_replacement_links_to_parent_notice(app):
"""替代 Notice 仅链接原 Notice,不继承或依赖 Case。"""
with app.app_context():
case = resolve_shipping_case_for_new_task(created_by_id=None)
parent = _task('SN-CASE-PARENT-1', case_id=case.id)
parent = _task('SN-NOTICE-PARENT-1')
db.session.add(parent)
db.session.flush()
resolved = resolve_shipping_case_for_new_task(
supersedes_task_id=parent.id,
duplicate_resolution_type='revision',
)
assert resolved.id == case.id
assert ShippingCase.query.count() == 1
replacement = _task('SN-NOTICE-REPLACEMENT-1', supersedes_task_id=parent.id)
db.session.add(replacement)
db.session.commit()
assert replacement.supersedes_task_id == parent.id
def test_revision_rejects_parent_without_business_case(app):
"""修订父任务缺少业务单时必须阻断,禁止隐式补建。"""
with app.app_context():
parent = _task('SN-CASE-LEGACY-1')
db.session.add(parent)
db.session.flush()
with pytest.raises(ConflictError) as exc_info:
resolve_shipping_case_for_new_task(
supersedes_task_id=parent.id,
duplicate_resolution_type='revision',
)
assert exc_info.value.code == 'SHIPPING_NOTICE.REVISION_CASE_MISSING'
assert ShippingCase.query.count() == 0
def test_revision_chain_reuses_root_case(app):
"""连续修订版本必须沿用同一业务单聚合。"""
with app.app_context():
case = resolve_shipping_case_for_new_task(created_by_id=None)
parent = _task('SN-CASE-CHAIN-1', case_id=case.id)
db.session.add(parent)
db.session.flush()
revision_case = resolve_shipping_case_for_new_task(
supersedes_task_id=parent.id,
duplicate_resolution_type='revision',
)
revision = _task('SN-CASE-CHAIN-2', case_id=revision_case.id, supersedes_task_id=parent.id)
db.session.add(revision)
db.session.flush()
next_case = resolve_shipping_case_for_new_task(
supersedes_task_id=revision.id,
duplicate_resolution_type='revision',
)
assert next_case.id == case.id
assert ShippingCase.query.count() == 1
def test_create_task_assigns_case_and_serializes_it(app):
"""真实单票建单路径必须创建业务单并在 Tool 投影中返回关联信息。"""
def test_create_task_assigns_notice_customer_and_serializes_it(app):
"""真实单票建单路径直接写 Notice 客户事实,不创建 ShippingCase。"""
with app.app_context():
customer = _active_customer(short_code='CRT')
result = create_task({
@@ -111,11 +65,9 @@ def test_create_task_assigns_case_and_serializes_it(app):
'customer_id': customer.id,
})
task = ShippingNotice.query.filter_by(task_no=result['task_id']).one()
assert task.shipping_case_id is not None
assert task.customer_resolution_status == 'confirmed'
assert result['data']['shipping_case_id'] == task.shipping_case_id
assert result['data']['case_no'] == task.shipping_case.case_no
assert task.shipping_case.customer_id == customer.id
assert task.customer_id == customer.id
assert result['data']['customer_id'] == customer.id
def test_create_task_rejects_without_customer(app):