feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理
清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
1 parent
7cfd5d167e
commit
1e5aedb9ed
235 files changed
+11394
-9613
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/tests/test_shipping_business_numbering.py
|
||||
|
||||
用途:验证货代业务编号格式、上海日期、两位流水上限及业务单版本继承。
|
||||
依赖:ShippingCase/Customer/Sequence、business_numbering_service、customer_identity_gate。
|
||||
逻辑:直接调用领域服务,覆盖幂等、01-99、修订共享与强制新建独立编号。
|
||||
用途:验证货代业务编号格式、上海日期、两位流水上限及 Notice 独立编号。
|
||||
依赖:ShippingNotice/Customer/Sequence、business_numbering_service、customer_identity_gate。
|
||||
逻辑:直接调用领域服务,覆盖幂等、01-99 与替代 Notice 独立编号。
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
@@ -12,9 +12,9 @@ import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ConflictError
|
||||
from app.models import ShippingBusinessSequence, ShippingCase, ShippingCustomer, ShippingNotice, ShippingTaskEvent
|
||||
from app.models import ShippingBusinessSequence, ShippingCustomer, ShippingNotice, ShippingTaskEvent
|
||||
from app.services.shipping_notice.business_numbering_service import (
|
||||
assign_business_number,
|
||||
assign_notice_business_number,
|
||||
format_business_no,
|
||||
shanghai_business_date,
|
||||
)
|
||||
@@ -22,25 +22,23 @@ from app.services.shipping_notice.customer_identity_gate import confirm_task_cus
|
||||
from app.services.shipping_notice.query_service import extract_task_no_from_text, get_task_by_ref
|
||||
|
||||
|
||||
def _seed_case(*, short_code: str = "WYC", task_no: str = "SN-NUMBER-001"):
|
||||
"""创建待客户确认的业务单、任务和启用客户。"""
|
||||
def _seed_notice(*, short_code: str = "WYC", task_no: str = "SN-NUMBER-001"):
|
||||
"""创建待客户确认的 Notice 和启用客户。"""
|
||||
customer = ShippingCustomer(
|
||||
standard_name=f"编号测试客户 {short_code}",
|
||||
short_code=short_code,
|
||||
status="active",
|
||||
)
|
||||
case = ShippingCase(case_no=f"CASE-{task_no}", numbering_status="pending")
|
||||
db.session.add_all([customer, case])
|
||||
db.session.add(customer)
|
||||
db.session.flush()
|
||||
task = ShippingNotice(
|
||||
task_no=task_no,
|
||||
shipping_case_id=case.id,
|
||||
customer_resolution_status="pending",
|
||||
source_type="manual",
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.commit()
|
||||
return task, case, customer
|
||||
return task, customer
|
||||
|
||||
|
||||
def test_number_format_and_shanghai_date_boundary(app):
|
||||
@@ -55,53 +53,89 @@ def test_number_format_and_shanghai_date_boundary(app):
|
||||
def test_confirmation_assigns_idempotent_business_number(app):
|
||||
"""重复确认同一客户不得重复占用流水,正式编号保持不变。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
task, customer = _seed_notice()
|
||||
first = confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
||||
first_no = first.shipping_case.business_no
|
||||
first_no = first.business_no
|
||||
second = confirm_task_customer(first, customer_id=customer.id, actor_id=None)
|
||||
|
||||
assert first_no
|
||||
assert second.shipping_case.business_no == first_no
|
||||
assert second.business_no == first_no
|
||||
assert ShippingBusinessSequence.query.one().current_value == 1
|
||||
assert second.shipping_case.customer_short_code_snapshot == "WYC"
|
||||
assert second.customer_short_code_snapshot == "WYC"
|
||||
assert ShippingTaskEvent.query.filter_by(event_type="business_number_assigned").count() == 1
|
||||
|
||||
|
||||
def test_revision_inherits_and_force_case_uses_next_sequence(app):
|
||||
"""同一业务单修订继承编号,独立业务单确认同客户时占用下一流水。"""
|
||||
def test_each_notice_gets_a_distinct_business_number_for_same_customer(app):
|
||||
"""同一客户 PO 的不同发货通知必须各自占用独立业务流水。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
customer = ShippingCustomer(
|
||||
standard_name='通知单波次客户', short_code='WVC', status='active',
|
||||
)
|
||||
db.session.add(customer)
|
||||
db.session.flush()
|
||||
first = ShippingNotice(task_no='SN-NOTICE-NO-1', customer_resolution_status='confirmed')
|
||||
second = ShippingNotice(task_no='SN-NOTICE-NO-2', customer_resolution_status='confirmed')
|
||||
db.session.add_all([first, second])
|
||||
db.session.flush()
|
||||
first.customer_id = customer.id
|
||||
second.customer_id = customer.id
|
||||
db.session.flush()
|
||||
|
||||
first_no = assign_notice_business_number(first)
|
||||
second_no = assign_notice_business_number(second)
|
||||
|
||||
assert first_no != second_no
|
||||
assert first.business_no == first_no
|
||||
assert second.business_no == second_no
|
||||
|
||||
|
||||
def test_notice_confirms_customer_and_assigns_number_without_shipping_case(app):
|
||||
"""新 Notice 必须自持客户快照与编号,不能要求 ShippingCase 作为前置容器。"""
|
||||
with app.app_context():
|
||||
customer = ShippingCustomer(
|
||||
standard_name='Notice 自持客户', short_code='NHC', status='active',
|
||||
)
|
||||
task = ShippingNotice(
|
||||
task_no='SN-NOTICE-SELF-CUSTOMER',
|
||||
customer_resolution_status='pending',
|
||||
source_type='manual',
|
||||
)
|
||||
db.session.add_all([customer, task])
|
||||
db.session.commit()
|
||||
|
||||
confirmed = confirm_task_customer(task, customer_id=customer.id, actor_id=7)
|
||||
|
||||
assert confirmed.customer_id == customer.id
|
||||
assert confirmed.customer_name_snapshot == 'Notice 自持客户'
|
||||
assert confirmed.customer_short_code_snapshot == 'NHC'
|
||||
assert confirmed.business_no
|
||||
assert confirmed.customer_resolution_status == 'confirmed'
|
||||
|
||||
|
||||
def test_replacement_notice_uses_next_sequence(app):
|
||||
"""替代 Notice 保留原号审计并为新 Notice 分配下一业务号。"""
|
||||
with app.app_context():
|
||||
task, customer = _seed_notice()
|
||||
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
||||
revision = ShippingNotice(
|
||||
task_no="SN-NUMBER-REV",
|
||||
shipping_case_id=case.id,
|
||||
supersedes_task_id=task.id,
|
||||
customer_resolution_status="confirmed",
|
||||
source_type="manual",
|
||||
)
|
||||
forced_case = ShippingCase(case_no="CASE-FORCE-NUMBER", numbering_status="pending")
|
||||
db.session.add_all([revision, forced_case])
|
||||
db.session.flush()
|
||||
forced_task = ShippingNotice(
|
||||
task_no="SN-NUMBER-FORCED",
|
||||
shipping_case_id=forced_case.id,
|
||||
customer_resolution_status="pending",
|
||||
source_type="manual",
|
||||
)
|
||||
db.session.add(forced_task)
|
||||
db.session.add(revision)
|
||||
db.session.commit()
|
||||
|
||||
confirm_task_customer(forced_task, customer_id=customer.id, actor_id=None)
|
||||
assert revision.shipping_case.business_no == case.business_no
|
||||
assert forced_task.shipping_case.business_no[:-2] == case.business_no[:-2]
|
||||
assert case.business_no.endswith("01")
|
||||
assert forced_task.shipping_case.business_no.endswith("02")
|
||||
confirm_task_customer(revision, customer_id=customer.id, actor_id=None)
|
||||
assert revision.business_no[:-2] == task.business_no[:-2]
|
||||
assert task.business_no.endswith("01")
|
||||
assert revision.business_no.endswith("02")
|
||||
|
||||
|
||||
def test_daily_sequence_blocks_one_hundredth_case(app):
|
||||
"""客户当日已有 99 笔时,第 100 笔必须阻断且不写业务号。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
task, customer = _seed_notice()
|
||||
business_date = shanghai_business_date()
|
||||
db.session.add(ShippingBusinessSequence(
|
||||
business_type="shipping_notice",
|
||||
@@ -110,19 +144,19 @@ def test_daily_sequence_blocks_one_hundredth_case(app):
|
||||
current_value=99,
|
||||
))
|
||||
db.session.commit()
|
||||
case.customer_id = customer.id
|
||||
task.customer_id = customer.id
|
||||
|
||||
with pytest.raises(ConflictError) as exc_info:
|
||||
assign_business_number(case)
|
||||
assign_notice_business_number(task)
|
||||
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_DAILY_LIMIT_REACHED"
|
||||
assert case.business_no is None
|
||||
assert task.business_no is None
|
||||
assert ShippingBusinessSequence.query.one().current_value == 99
|
||||
|
||||
|
||||
def test_numbering_failure_rolls_back_customer_confirmation(app):
|
||||
"""流水耗尽导致确认失败时,业务单客户和任务确认状态不得半落库。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
task, customer = _seed_notice()
|
||||
db.session.add(ShippingBusinessSequence(
|
||||
business_type="shipping_notice",
|
||||
customer_id=customer.id,
|
||||
@@ -135,43 +169,41 @@ def test_numbering_failure_rolls_back_customer_confirmation(app):
|
||||
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
||||
db.session.rollback()
|
||||
db.session.expire_all()
|
||||
persisted_case = db.session.get(ShippingCase, case.id)
|
||||
persisted_task = db.session.get(ShippingNotice, task.id)
|
||||
assert persisted_case.customer_id is None
|
||||
assert persisted_case.business_no is None
|
||||
assert persisted_task.customer_id is None
|
||||
assert persisted_task.business_no is None
|
||||
assert persisted_task.customer_resolution_status == "pending"
|
||||
|
||||
|
||||
def test_business_number_query_returns_latest_revision(app):
|
||||
"""使用正式业务号查询时应返回同业务单最新修订任务。"""
|
||||
def test_business_number_query_returns_exact_notice(app):
|
||||
"""使用正式业务号查询时必须返回该 Notice,不能跳到替代版本。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
task, customer = _seed_notice()
|
||||
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
||||
revision = ShippingNotice(
|
||||
task_no="SN-NUMBER-QUERY-REV",
|
||||
shipping_case_id=case.id,
|
||||
supersedes_task_id=task.id,
|
||||
document_version=2,
|
||||
customer_resolution_status="confirmed",
|
||||
customer_resolution_status="pending",
|
||||
source_type="manual",
|
||||
)
|
||||
db.session.add(revision)
|
||||
db.session.commit()
|
||||
|
||||
resolved = get_task_by_ref(case.business_no)
|
||||
assert resolved.id == revision.id
|
||||
assert extract_task_no_from_text(f"请继续处理 {case.business_no}") == case.business_no
|
||||
resolved = get_task_by_ref(task.business_no)
|
||||
assert resolved.id == task.id
|
||||
assert extract_task_no_from_text(f"请继续处理 {task.business_no}") == task.business_no
|
||||
|
||||
|
||||
def test_numbering_rejects_invalid_short_code_even_if_data_bypasses_service(app):
|
||||
"""客户简码绕过管理服务写入异常值时,编号服务仍必须阻断。"""
|
||||
with app.app_context():
|
||||
task, case, customer = _seed_case()
|
||||
case.customer_id = customer.id
|
||||
task, customer = _seed_notice()
|
||||
task.customer_id = customer.id
|
||||
customer.short_code = "W1"
|
||||
# 数据库 CHECK 已阻断非法落库;no_autoflush 用于单独验证编号服务仍保留纵深校验。
|
||||
with db.session.no_autoflush, pytest.raises(ConflictError) as exc_info:
|
||||
assign_business_number(case)
|
||||
assign_notice_business_number(task)
|
||||
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_SHORT_CODE_INVALID"
|
||||
assert case.business_no is None
|
||||
assert task.business_no is None
|
||||
db.session.rollback()
|
||||
Reference in new issue
Block a user