feat(shipping-notice): 收料异动解析配置闭环与按需触发FK违例修复

- 收料单元 schedule_run_id 改可空,修复按需触发 FK 违例
- 收料配置绑定已发布异动解析规则(校验/回显/审计快照)
- 预检未绑异动规则 fail-closed,不再徒劳触发 RPA
- 标准异动字段目录只读接口;解析规则用途切换与批号映射表单(四语言)
- movement 解析规则可发布(跳过订单匹配键校验);站点启停门禁仅限库存站点页签
- 只读映射卡三列布局修复
This commit is contained in:
hugo committed 2026-09-21 19:12:29 +08:00
1 parent ab57ab3b1f
commit 20835dcfc0
28 files changed
+987 -73

No files matched your search

@@ -166,6 +166,102 @@ def test_receipt_config_rejects_date_selector_and_invalid_connector_rules(app, c
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_REPORT_CONFIG_INVALID'
def _create_movement_profile(profile_code: str, *, status: str, purpose: str):
"""创建异动/库存解析 Profile 种子;定义仅用于绑定校验,不参与解析。"""
from app import db
from app.models import ShippingInventoryParsingProfile
profile = ShippingInventoryParsingProfile(
purpose=purpose, profile_code=profile_code, display_name=f'{profile_code} 规则',
version=1, status=status, file_format='xlsx',
definition_json={'sheet_name': 'Movement', 'header_row_index': 1,
'field_mapping': {'doc_no': '单据编号', 'material_code': '料号',
'quantity': '数量', 'movement_date': '异动日期'},
'match_keys': []},
)
db.session.add(profile)
db.session.commit()
return profile
def test_receipt_config_binds_published_movement_profile(app, client):
"""绑定已发布 movement 规则 → 保存成功,payload 携带 parsing_profile_id 与摘要,审计留痕。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source_id = source.id
profile = _create_movement_profile('CRANE_MOVE_STD', status='published', purpose='movement')
profile_id = profile.id
payload = {**_dbs_payload(), 'parsing_profile_id': profile_id}
response = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json=payload, headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 200
config = parse_api_data(response.get_json())
# 关键断言:绑定 id 与规则摘要(码/版本/状态)必须回显,供抽屉展示当前绑定。
assert config['parsing_profile_id'] == profile_id
assert config['parsing_profile']['profile_code'] == 'CRANE_MOVE_STD'
assert config['parsing_profile']['status'] == 'published'
from app.models import ShippingAuditEvent
audit = ShippingAuditEvent.query.filter_by(
event_type='shipping_receipt_report_config_saved', aggregate_id=str(source_id),
).order_by(ShippingAuditEvent.id.desc()).first()
assert audit.metadata_json['parsing_profile_id'] == profile_id
def test_receipt_config_rejects_draft_or_non_movement_profile(app, client):
"""draft/库存用途/不存在 的规则一律拒绝,防止误绑导致预检无法解析收料文件。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source_id = source.id
draft = _create_movement_profile('MOVE_DRAFT', status='draft', purpose='movement')
inventory_profile = _create_movement_profile('INV_PUBLISHED', status='published', purpose='inventory')
cases = [draft.id, inventory_profile.id, 999999]
for profile_id in cases:
response = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json={**_dbs_payload(), 'parsing_profile_id': profile_id},
headers=_headers('receipt-admin', 'admin'),
)
assert response.status_code == 400, f'profile_id={profile_id}'
# 关键断言:非"已发布 movement"绑定必须走统一错误码,fail-closed。
assert parse_api_error(response.get_json())['code'] == 'SHIPPING.RECEIPT_MOVEMENT_PROFILE_INVALID'
def test_receipt_config_empty_profile_id_unbinds(app, client):
"""parsing_profile_id 传 None → 解绑(置空),不做校验也不报错。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source_id = source.id
profile = _create_movement_profile('MOVE_UNBIND', status='published', purpose='movement')
profile_id = profile.id
bound = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json={**_dbs_payload(), 'parsing_profile_id': profile_id},
headers=_headers('receipt-admin', 'admin'),
)
assert bound.status_code == 200
unbound = client.put(
f'/api/admin/shipping-notice/sources/{source_id}/receipt-report-config',
json={**_dbs_payload(), 'parsing_profile_id': None},
headers=_headers('receipt-admin', 'admin'),
)
assert unbound.status_code == 200
# 关键断言:None 表示显式解绑,payload 的绑定字段回到空值。
assert parse_api_data(unbound.get_json())['parsing_profile_id'] is None
def test_crane_receipt_config_rejects_query_parameters(app, client):
"""Crane 固定使用页面默认值直接 Run Report,管理员不能维护查询条件。"""
app.config['AUTH_JWT_REQUIRED'] = False