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
@@ -3,7 +3,7 @@ backend/tests/test_shipping_customer_identity_gate.py
|
||||
|
||||
用途:验证客户人工确认与库存/RPA/匹配/Excel/上传统一 Gate。
|
||||
依赖:内存数据库、customer_identity_gate、各货代公共 workflow 入口。
|
||||
逻辑:构造 pending 或缺失业务单的任务逐入口断言无副作用;确认后验证业务单/修订同步。
|
||||
逻辑:构造 pending 或未确认客户的 Notice 逐入口断言无副作用;确认后验证 Notice 客户和编号事实。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -15,7 +15,7 @@ from werkzeug.datastructures import FileStorage
|
||||
|
||||
from app import db
|
||||
from app.core.errors import AppError
|
||||
from app.models import ShippingCase, ShippingCustomer, ShippingNotice, ShippingTaskEvent, User
|
||||
from app.models import ShippingCustomer, ShippingNotice, ShippingTaskEvent, User
|
||||
from app.services.shipping_notice.customer_identity_gate import (
|
||||
assert_customer_identity_gate,
|
||||
confirm_task_customer,
|
||||
@@ -31,7 +31,7 @@ from app.services.shipping_notice.progress_workflow import auto_progress
|
||||
from app.services.shipping_notice.task_workflow import retry_task
|
||||
|
||||
|
||||
def _seed_task(*, with_case: bool = True) -> tuple[ShippingNotice, ShippingCustomer, User]:
|
||||
def _seed_task(*, customer_pending: bool = True) -> tuple[ShippingNotice, ShippingCustomer, User]:
|
||||
"""创建待确认任务、启用客户和任务用户。"""
|
||||
sequence = db.session.query(User).count()
|
||||
user = User(username=f'gate-user-{sequence}', email=f'gate-{sequence}@example.com', role='user')
|
||||
@@ -39,14 +39,9 @@ def _seed_task(*, with_case: bool = True) -> tuple[ShippingNotice, ShippingCusto
|
||||
customer = ShippingCustomer(standard_name=f'测试客户 {sequence}', short_code=short_code, status='active')
|
||||
db.session.add_all([user, customer])
|
||||
db.session.flush()
|
||||
case = ShippingCase(case_no=f'CASE-GATE-{user.id}') if with_case else None
|
||||
if case:
|
||||
db.session.add(case)
|
||||
db.session.flush()
|
||||
task = ShippingNotice(
|
||||
task_no=f'SN-GATE-{user.id}', user_id=user.id,
|
||||
shipping_case_id=case.id if case else None,
|
||||
customer_resolution_status='pending' if case else None,
|
||||
customer_resolution_status='pending' if customer_pending else None,
|
||||
lines_json=[{'part_number': 'A001', 'reservation_id': '1000140351', 'quantity': 1}],
|
||||
inventory_source_status='user_confirmed', inventory_source_code='DSV',
|
||||
)
|
||||
@@ -111,12 +106,12 @@ def test_pending_customer_blocks_retry_before_clearing_review_state(app):
|
||||
assert task.error_message == '标准客户尚未确认'
|
||||
|
||||
|
||||
def test_confirm_customer_syncs_revision_chain_and_assigns_number(app):
|
||||
"""客户确认同步同业务单全部任务,并在同事务生成正式业务号。"""
|
||||
def test_confirm_customer_does_not_assign_notice_number_before_excel(app):
|
||||
"""客户确认只固化客户事实;正式单号只能在首个 Excel 生成时分配。"""
|
||||
with app.app_context():
|
||||
task, customer, user = _seed_task()
|
||||
revision = ShippingNotice(
|
||||
task_no='SN-GATE-REV', user_id=user.id, shipping_case_id=task.shipping_case_id,
|
||||
task_no='SN-GATE-REV', user_id=user.id,
|
||||
supersedes_task_id=task.id, document_version=2, customer_resolution_status='pending',
|
||||
)
|
||||
db.session.add(revision)
|
||||
@@ -124,24 +119,60 @@ def test_confirm_customer_syncs_revision_chain_and_assigns_number(app):
|
||||
|
||||
updated = confirm_task_customer(task, customer_id=customer.id, actor_id=user.id)
|
||||
db.session.refresh(revision)
|
||||
case = db.session.get(ShippingCase, task.shipping_case_id)
|
||||
assert updated.customer_resolution_status == 'confirmed'
|
||||
assert revision.customer_resolution_status == 'confirmed'
|
||||
assert revision.customer_resolution_status == 'pending'
|
||||
assert updated.customer_resolution_method == 'manual'
|
||||
assert case.customer_id == customer.id
|
||||
assert case.customer_name_snapshot == customer.standard_name
|
||||
assert case.customer_short_code_snapshot == customer.short_code
|
||||
assert case.business_no.startswith(customer.short_code)
|
||||
assert len(case.business_no) == len(customer.short_code) + 8
|
||||
assert case.sequence_value == 1
|
||||
assert case.numbering_status == 'assigned'
|
||||
assert updated.customer_id == customer.id
|
||||
assert updated.customer_name_snapshot == customer.standard_name
|
||||
assert updated.customer_short_code_snapshot == customer.short_code
|
||||
assert updated.business_no is None
|
||||
assert updated.business_date is None
|
||||
assert updated.business_sequence_value is None
|
||||
assert customer_identity_is_confirmed(updated) is True
|
||||
assert db.session.query(ShippingTaskEvent).filter_by(event_type='customer_confirmed').count() == 2
|
||||
assert db.session.query(ShippingTaskEvent).filter_by(event_type='business_number_assigned').count() == 2
|
||||
assert db.session.query(ShippingTaskEvent).filter_by(event_type='customer_confirmed').count() == 1
|
||||
assert db.session.query(ShippingTaskEvent).filter_by(event_type='business_number_assigned').count() == 0
|
||||
|
||||
|
||||
def test_inactive_customer_reblocks_and_missing_case_is_blocked(app):
|
||||
"""已确认客户停用后重新阻断;缺少业务单的任务也必须阻断。"""
|
||||
def test_confirm_customer_locks_only_notice_when_joined_customer_is_loaded(app, monkeypatch):
|
||||
"""PostgreSQL 行锁只覆盖 Notice,不能锁 joined customer 的外连接空侧。"""
|
||||
with app.app_context():
|
||||
task, customer, user = _seed_task()
|
||||
original_query = db.session.query
|
||||
lock_arguments: list[dict] = []
|
||||
|
||||
class NoticeQuerySpy:
|
||||
"""保留真实 Query 行为,仅记录客户确认路径的行锁参数。"""
|
||||
|
||||
def __init__(self, query):
|
||||
self._query = query
|
||||
|
||||
def filter(self, *criteria):
|
||||
self._query = self._query.filter(*criteria)
|
||||
return self
|
||||
|
||||
def with_for_update(self, **kwargs):
|
||||
lock_arguments.append(kwargs)
|
||||
self._query = self._query.with_for_update(**kwargs)
|
||||
return self
|
||||
|
||||
def one(self):
|
||||
return self._query.one()
|
||||
|
||||
def traced_query(*entities, **kwargs):
|
||||
query = original_query(*entities, **kwargs)
|
||||
if len(entities) == 1 and entities[0] is ShippingNotice:
|
||||
return NoticeQuerySpy(query)
|
||||
return query
|
||||
|
||||
monkeypatch.setattr(db.session, 'query', traced_query)
|
||||
|
||||
confirm_task_customer(task, customer_id=customer.id, actor_id=user.id)
|
||||
|
||||
assert lock_arguments == [{'of': ShippingNotice}]
|
||||
|
||||
|
||||
def test_inactive_customer_reblocks_and_unconfirmed_notice_is_blocked(app):
|
||||
"""已确认客户停用后重新阻断;未确认客户的 Notice 也必须阻断。"""
|
||||
with app.app_context():
|
||||
task, customer, user = _seed_task()
|
||||
confirm_task_customer(task, customer_id=customer.id, actor_id=user.id)
|
||||
@@ -151,11 +182,11 @@ def test_inactive_customer_reblocks_and_missing_case_is_blocked(app):
|
||||
assert_customer_identity_gate(task, operation='match_inventory')
|
||||
assert exc_info.value.code == 'SHIPPING.CUSTOMER_CONFIRMATION_REQUIRED'
|
||||
|
||||
missing_case, _other_customer, _other_user = _seed_task(with_case=False)
|
||||
with pytest.raises(AppError) as missing_case_error:
|
||||
assert_customer_identity_gate(missing_case, operation='match_inventory')
|
||||
assert missing_case_error.value.code == 'SHIPPING.CUSTOMER_CONFIRMATION_REQUIRED'
|
||||
assert customer_identity_is_confirmed(missing_case) is False
|
||||
unconfirmed, _other_customer, _other_user = _seed_task(customer_pending=False)
|
||||
with pytest.raises(AppError) as unconfirmed_error:
|
||||
assert_customer_identity_gate(unconfirmed, operation='match_inventory')
|
||||
assert unconfirmed_error.value.code == 'SHIPPING.CUSTOMER_CONFIRMATION_REQUIRED'
|
||||
assert customer_identity_is_confirmed(unconfirmed) is False
|
||||
|
||||
|
||||
def test_workspace_customer_search_and_confirmation_api(app, client):
|
||||
|
||||
Reference in new issue
Block a user