feat(shipping_notice): 支持客户 ODM 白名单与整单 ODM 选择收敛导出
- 客户主数据新增 odm_ids 白名单字段(含 Alembic 迁移),表单与管理端支持维护 - 导出库存命中行按客户白名单与整单 ODM 裁决收敛,新增 notice_odm_choice_service - 工作台新增 odm-options / odm-choice 接口,投影行级候选并持久化用户选择 - 修复复核工作台跨 Run 切换时接收解析上下文残留 - 同步四语言 i18n、测试用例、funclist 与 shipping-notice 技术文档
This commit is contained in:
1 parent
a032354cb9
commit
d396371020
51 files changed
+2717
-109
No files matched your search
@@ -896,3 +896,260 @@ def test_export_commit_failure_removes_generated_file(app, tmp_path, monkeypatch
|
||||
with pytest.raises(RuntimeError, match='commit failed'):
|
||||
_commit_generated_export(str(path))
|
||||
assert not path.exists()
|
||||
|
||||
|
||||
def _build_multi_odm_notice(
|
||||
*, tmp_path, odm_values=None, customer_odm_ids=None, choice_odm=None, line_specs=None,
|
||||
):
|
||||
"""
|
||||
构造「订单行命中多个 ODM ID」的整单导出场景;须在 app_context 内调用。
|
||||
|
||||
Args:
|
||||
odm_values: 单订单行场景下库存行命中的 ODM 值列表。
|
||||
line_specs: 多订单行场景 [{'part': 料号, 'odm_values': [...]}];提供时忽略 odm_values。
|
||||
customer_odm_ids: 客户数据的 ODM ID 范围(None = 不限定)。
|
||||
choice_odm: 整单用户选择(写入 OT.chosen_odm_id)
|
||||
Returns:
|
||||
(notice, customer)。
|
||||
"""
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
|
||||
schema = ShippingOrderSchema(schema_code=DEFAULT_SCHEMA_FAMILY_CODE, display_name='Order')
|
||||
db.session.add(schema); db.session.flush()
|
||||
part = _lookup_standard_fields('part_number')['part_number']
|
||||
version = ShippingOrderSchemaVersion(schema_id=schema.id, version=1, status='published', definition_json={})
|
||||
db.session.add(version); db.session.flush()
|
||||
draft = excel_template.create_draft(_workbook_upload(), {
|
||||
'template_code': 'ODMSCOPE', 'display_name': 'OdmScope',
|
||||
'definition': {'filename_pattern': '{task_no}.xlsx', 'mappings': [
|
||||
{'sheet': 'Detail', 'row_mode': 'header', 'start_row': 2, 'target_column': 1, 'source_kind': 'inventory_field', 'source_key': _inventory_field_id('odm_id'), 'formatter': 'text'},
|
||||
]},
|
||||
}, actor_id=None)
|
||||
published = excel_template.publish(draft['id'], actor_id=None)
|
||||
parsing = ShippingInventoryParsingProfile(
|
||||
profile_code='ODMSCOPE', display_name='OdmScope', version=1, status='published', file_format='xlsx',
|
||||
definition_json={'field_mapping': {_inventory_field_id('part_number'): 'Part No.', _inventory_field_id('odm_id'): 'ODM ID'}, 'match_keys': [{'order_field_id': part.id, 'inventory_field_id': _inventory_field_id('part_number')}]},
|
||||
)
|
||||
login = ShippingRpaProfile(
|
||||
profile_code='ODMSCOPE', display_name='OdmScope', connector_type='dbs', login_url='https://invalid',
|
||||
login_config_json={'auth_mode': 'password'},
|
||||
)
|
||||
db.session.add_all([parsing, login]); db.session.flush()
|
||||
db.session.add(ShippingInventorySource(
|
||||
source_code='ODMSCOPE', display_name='OdmScope', connector_type='dbs', rpa_profile_id=login.id,
|
||||
parsing_profile_id=parsing.id, excel_template_id=published['id'], warehouse_resolution_mode='fixed',
|
||||
fixed_warehouse_id='DUB', report_path_json={}, query_parameters_json=[],
|
||||
))
|
||||
customer = ShippingCustomer(
|
||||
standard_name='ODM范围客户', short_code='ODMSC', status='active',
|
||||
odm_ids=list(customer_odm_ids) if customer_odm_ids is not None else None,
|
||||
)
|
||||
db.session.add(customer); db.session.flush()
|
||||
ot = ShippingOrderTask(task_no='OT-ODMSCOPE', user_id=1, status='ready', current_step='customer_review')
|
||||
db.session.add(ot); db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=ot.id, schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction); db.session.flush()
|
||||
specs = line_specs or [{'part': 'PN-A', 'odm_values': odm_values}]
|
||||
snapshot_lines = []
|
||||
for index, spec in enumerate(specs, start=1):
|
||||
order_line = ShippingOrderLine(extraction_id=extraction.id, position=index, line_no=index)
|
||||
db.session.add(order_line); db.session.flush()
|
||||
decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=order_line.id, field_id=part.id,
|
||||
decision_status='accepted', resolved_value_json=spec['part'],
|
||||
)
|
||||
db.session.add(decision); db.session.flush()
|
||||
db.session.add(ShippingOrderFieldValue(
|
||||
extraction_id=extraction.id, line_id=order_line.id, field_id=part.id,
|
||||
value_text=spec['part'], decision_id=decision.id,
|
||||
))
|
||||
snapshot_lines.append((index, part.id, spec['part'], 1, f'100000000{index}'))
|
||||
confirm_test_customer_resolution(ot, customer=customer, extraction=extraction)
|
||||
inventory_rows = []
|
||||
for spec in specs:
|
||||
for odm in spec['odm_values']:
|
||||
inventory_rows.append({_inventory_field_id('part_number'): spec['part'], _inventory_field_id('odm_id'): odm})
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-ODMSCOPE', user_id=None, source_file_name='odm-scope.xlsx',
|
||||
storage_path=str(tmp_path / 'odm-scope.xlsx'),
|
||||
inventory_lines_json=inventory_rows,
|
||||
line_count=len(inventory_rows), share_mode='task_only', warehouse_id='DUB', source_code='ODMSCOPE',
|
||||
snapshot_status='ready',
|
||||
)
|
||||
db.session.add(inventory_file); db.session.flush()
|
||||
snapshot = _freeze_export_snapshot(
|
||||
ot, extraction, version,
|
||||
lines=snapshot_lines,
|
||||
customer_id=customer.id, inventory_file_id=inventory_file.id,
|
||||
)
|
||||
notice = _materialize_export_notice(
|
||||
ot=ot, snapshot=snapshot, inventory_file=inventory_file, source_code='ODMSCOPE', notice_no='TASK-ODMSCOPE',
|
||||
)
|
||||
if choice_odm is not None:
|
||||
for match in ShippingNoticeLineInventoryMatch.query.filter_by(task_id=int(ot.id), is_current=True).all():
|
||||
match.chosen_odm_id = choice_odm
|
||||
db.session.flush()
|
||||
return notice, customer
|
||||
|
||||
|
||||
def test_export_narrows_odm_by_customer_range(app, tmp_path):
|
||||
"""客户 ODM ID 范围非空时,导出只取范围内库存行;范围命中唯一值写入表头。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['odm-2'],
|
||||
)
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_odm_range_excluding_all_raises_specific_error(app, tmp_path):
|
||||
"""客户范围把全部命中排除时,必须报指向客户数据的可行动错误而非「没有匹配」。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['ODM-9'],
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match='不在客户数据维护的 ODM ID 范围'):
|
||||
generate_shipment_excel(task)
|
||||
|
||||
|
||||
def test_export_applies_notice_odm_choice(app, tmp_path):
|
||||
"""整单用户选择优先于多值冲突;选择经 casefold 归一后命中库存行。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], choice_odm='odm-2',
|
||||
)
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_auto_narrows_unique_feasible_odm_without_manual_choice(app, tmp_path):
|
||||
"""无范围、无选择时,整单唯一可行 ODM(各行命中交集恰一个)自动收敛。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, line_specs=[
|
||||
{'part': 'PN-A', 'odm_values': ['ODM-1', 'ODM-2']},
|
||||
{'part': 'PN-B', 'odm_values': ['ODM-2']},
|
||||
])
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_keeps_conflict_when_odm_scope_cannot_decide(app, tmp_path):
|
||||
"""无范围、无选择且多值并存时维持折叠冲突阻断,等待用户整单选择。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'])
|
||||
|
||||
with pytest.raises(ValueError, match='库存字段“ODM 编号”'):
|
||||
generate_shipment_excel(task)
|
||||
|
||||
|
||||
def test_notice_odm_options_and_choice_roundtrip(app, tmp_path):
|
||||
"""整单候选按客户范围逐行投影;合法选择落库并回显,非法选择有明确错误。"""
|
||||
from app.core.errors import AppError
|
||||
from app.services.shipping_notice.notice_odm_choice_service import (
|
||||
notice_odm_options,
|
||||
set_notice_odm_choice,
|
||||
)
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['ODM-2', 'ODM-3'],
|
||||
)
|
||||
|
||||
options = notice_odm_options(task)
|
||||
|
||||
# [断言] 逐行候选是客户范围过滤后的集合,且保留库存行原始大小写。
|
||||
assert options['whitelist_applied'] is True
|
||||
assert options['customer_odm_ids'] == ['ODM-2', 'ODM-3']
|
||||
assert len(options['lines']) == 1
|
||||
assert options['lines'][0]['odm_values'] == ['ODM-2']
|
||||
assert options['lines'][0]['chosen_odm_id'] is None
|
||||
|
||||
snapshot_line_id = options['lines'][0]['snapshot_line_id']
|
||||
result = set_notice_odm_choice(task, snapshot_line_id=snapshot_line_id, odm_id='ODM-2')
|
||||
|
||||
assert result['odm_id'] == 'ODM-2'
|
||||
matches = ShippingNoticeLineInventoryMatch.query.filter_by(
|
||||
task_id=int(task.order_task_id), is_current=True,
|
||||
).all()
|
||||
assert matches and {m.chosen_odm_id for m in matches} == {'ODM-2'}
|
||||
options = notice_odm_options(task)
|
||||
assert options['lines'][0]['chosen_odm_id'] == 'ODM-2'
|
||||
|
||||
with pytest.raises(AppError) as exc:
|
||||
set_notice_odm_choice(task, snapshot_line_id=snapshot_line_id, odm_id='ODM-9')
|
||||
assert getattr(exc.value, 'code', '') == 'SHIPPING.NOTICE_ODM_CHOICE_INVALID'
|
||||
|
||||
|
||||
def test_notice_odm_choice_syncs_same_snapshot_file_lines(app, tmp_path):
|
||||
"""同一快照文件的行:改任一行 ODM ID,其余行同步落库同一选择。"""
|
||||
from app.services.shipping_notice.notice_odm_choice_service import (
|
||||
notice_odm_options,
|
||||
set_notice_odm_choice,
|
||||
)
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, line_specs=[
|
||||
{'part': 'PN-A', 'odm_values': ['ODM-1', 'ODM-2']},
|
||||
{'part': 'PN-B', 'odm_values': ['ODM-2', 'ODM-3']},
|
||||
])
|
||||
|
||||
options = notice_odm_options(task)
|
||||
# [断言] 每行有自己的候选;同文件两行的可行集=交集。
|
||||
by_sid = {str(item['snapshot_line_id']): item for item in options['lines']}
|
||||
assert len(by_sid) == 2
|
||||
assert options['lines'][0]['odm_values'] == ['ODM-1', 'ODM-2']
|
||||
|
||||
target_sid = options['lines'][0]['snapshot_line_id']
|
||||
result = set_notice_odm_choice(
|
||||
task, snapshot_line_id=target_sid, odm_id='ODM-2',
|
||||
)
|
||||
|
||||
# [断言] 同步范围=同一快照文件:两行当前匹配行写同一选择。
|
||||
assert result['synced_line_count'] == 2
|
||||
matches = ShippingNoticeLineInventoryMatch.query.filter_by(
|
||||
task_id=int(task.order_task_id), is_current=True,
|
||||
).all()
|
||||
assert len(matches) == 2
|
||||
assert {m.chosen_odm_id for m in matches} == {'ODM-2'}
|
||||
options = notice_odm_options(task)
|
||||
assert all(item['chosen_odm_id'] == 'ODM-2' for item in options['lines'])
|
||||
|
||||
|
||||
def test_notice_odm_options_empty_when_template_ignores_odm(app, tmp_path):
|
||||
"""模板不消费 odm 字段时投影为空候选,不产生任何选择入口。"""
|
||||
from app.services.shipping_notice.notice_odm_choice_service import notice_odm_options
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, odm_values=['ODM-1'])
|
||||
options = notice_odm_options(task)
|
||||
assert options['odm_field_id'] is not None
|
||||
assert options['lines'] and options['lines'][0]['odm_values'] == ['ODM-1']
|
||||
Reference in new issue
Block a user