feat(shipping-notice): 添加货代履约策略管理功能,支持策略创建、发布及版本控制,增强订单冻结和预约管理逻辑
This commit is contained in:
1 parent
abd8fc1e16
commit
d62f5107b2
82 files changed
+4914
-514
No files matched your search
@@ -21,6 +21,7 @@ from app.models import (
|
||||
ShippingOrderFieldDecision,
|
||||
ShippingOrderFieldObservation,
|
||||
ShippingOrderFieldValue,
|
||||
ShippingFulfillmentPolicy,
|
||||
ShippingOrderLine,
|
||||
ShippingOrderSchema,
|
||||
ShippingOrderSchemaVersion,
|
||||
@@ -30,28 +31,28 @@ from app.models import (
|
||||
User,
|
||||
)
|
||||
from app.services.shipping_notice.order_freeze_service import (
|
||||
HEADER_FIELDS,
|
||||
LINE_FIELDS,
|
||||
HEADER_FIELD_PROJECTIONS,
|
||||
LINE_FIELD_PROJECTIONS,
|
||||
assert_task_order_is_editable,
|
||||
freeze_order_snapshot,
|
||||
)
|
||||
|
||||
|
||||
def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension: bool = False):
|
||||
def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension: bool = False, suffix: str = '001'):
|
||||
"""创建一份含预置核心字段的已接受解析;可加入扩展字段验证配置化履约。"""
|
||||
user = User(username='snapshot-user', email='snapshot-user@example.test', password_hash='x')
|
||||
task = ShippingTask(task_no='SN-SNAPSHOT-001', source_type='test', order_data_version=7)
|
||||
user = User(username=f'snapshot-user-{suffix}', email=f'snapshot-user-{suffix}@example.test', password_hash='x')
|
||||
task = ShippingTask(task_no=f'SN-SNAPSHOT-{suffix}', source_type='test', order_data_version=7)
|
||||
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='Frozen order')
|
||||
db.session.add_all([user, task, schema])
|
||||
db.session.flush()
|
||||
definitions = []
|
||||
fields: dict[str, ShippingOrderField] = {}
|
||||
field_codes = [*HEADER_FIELDS, *LINE_FIELDS]
|
||||
field_codes = [*HEADER_FIELD_PROJECTIONS, *LINE_FIELD_PROJECTIONS]
|
||||
if with_extension:
|
||||
field_codes.append('packing_reference')
|
||||
for position, code in enumerate(field_codes, start=1):
|
||||
scope = 'header' if code in HEADER_FIELDS else 'line'
|
||||
value_type = 'decimal' if code in {'qty', 'unit_price', 'amount'} else ('date' if code in {'required_date', 'order_date'} else 'string')
|
||||
scope = 'header' if code in HEADER_FIELD_PROJECTIONS else 'line'
|
||||
value_type = 'decimal' if code in {'qty', 'unit_price', 'line_amount_with_tax'} else ('date' if code in {'required_date', 'order_date'} else 'string')
|
||||
field = ShippingOrderField(
|
||||
schema_id=schema.id, field_code=code, scope=scope, value_type=value_type,
|
||||
field_category='order_header' if scope == 'header' else 'order_line',
|
||||
@@ -64,6 +65,27 @@ def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension:
|
||||
version = ShippingOrderSchemaVersion(schema_id=schema.id, version=1, status='published', definition_json={'fields': definitions}, checksum='snapshot-test')
|
||||
db.session.add(version)
|
||||
db.session.flush()
|
||||
bindings = [
|
||||
('fulfillment.reservation_identity', 'reservation_id'),
|
||||
('fulfillment.part_number', 'part_number'),
|
||||
('fulfillment.quantity', 'qty'),
|
||||
('routing.inventory_source_hint', 'inventory_source_hint'),
|
||||
]
|
||||
db.session.add(ShippingFulfillmentPolicy(
|
||||
schema_id=schema.id, policy_code='SHIP_AVAILABLE_ROLLING_V1', version=1, status='published',
|
||||
checksum='policy-test', definition_json={
|
||||
'policy_code': 'SHIP_AVAILABLE_ROLLING_V1', 'fulfillment_mode': 'ship_available_rolling',
|
||||
'requires_confirmed_customer': True, 'allows_partial_reservation': False,
|
||||
'field_bindings': [
|
||||
{
|
||||
'capability_code': capability, 'field_id': fields[field_code].id,
|
||||
'field_code': field_code, 'scope': 'line',
|
||||
'value_type': 'decimal' if field_code == 'qty' else 'string',
|
||||
}
|
||||
for capability, field_code in bindings
|
||||
],
|
||||
},
|
||||
))
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=task.id, schema_version_id=version.id, input_snapshot_hash='a' * 64, status='accepted',
|
||||
)
|
||||
@@ -73,15 +95,15 @@ def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension:
|
||||
db.session.add(line)
|
||||
db.session.flush()
|
||||
values = {
|
||||
'po_number': 'PO-0001', 'customer_code': 'C-001', 'customer_name': 'Customer',
|
||||
'customer_po_number': 'PO-0001', 'customer_code': 'C-001', 'customer_name': 'Customer',
|
||||
'part_number': '000123', 'reservation_id': '0012345678', 'qty': Decimal('12.50'),
|
||||
'unit_price': Decimal('2.30'), 'amount': Decimal('28.75'), 'uom': 'PCS',
|
||||
'unit_price': Decimal('2.30'), 'line_amount_with_tax': Decimal('28.75'), 'uom': 'PCS',
|
||||
'packing_reference': 'PK-0007',
|
||||
}
|
||||
for code, field in fields.items():
|
||||
value = values.get(code)
|
||||
decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=None if code in HEADER_FIELDS else line.id,
|
||||
extraction_id=extraction.id, line_id=None if code in HEADER_FIELD_PROJECTIONS else line.id,
|
||||
field_id=field.id, decision_status='accepted', resolved_value_json=str(value) if isinstance(value, Decimal) else value,
|
||||
supporting_observation_ids_json=[],
|
||||
)
|
||||
@@ -89,7 +111,7 @@ def _build_accepted_order(*, with_unverified_llm: bool = False, with_extension:
|
||||
db.session.flush()
|
||||
if value is not None:
|
||||
db.session.add(ShippingOrderFieldValue(
|
||||
extraction_id=extraction.id, line_id=None if code in HEADER_FIELDS else line.id,
|
||||
extraction_id=extraction.id, line_id=None if code in HEADER_FIELD_PROJECTIONS else line.id,
|
||||
field_id=field.id, decision_id=decision.id, value_json=str(value) if isinstance(value, Decimal) else value,
|
||||
value_text=str(value) if isinstance(value, str) else None,
|
||||
value_decimal=value if isinstance(value, Decimal) else None,
|
||||
@@ -187,6 +209,38 @@ def test_freeze_rejects_core_field_type_drift(app):
|
||||
assert error.value.code == 'SHIPPING.ORDER_CONTRACT_INVALID'
|
||||
|
||||
|
||||
def test_freeze_allows_schema_without_optional_standard_fields(app):
|
||||
"""关键断言:当前滚动发货策略只要求预约号、料号和正数数量,不要求全部标准字段。"""
|
||||
with app.app_context():
|
||||
task, user = _build_accepted_order()
|
||||
extraction = db.session.get(ShippingOrderExtraction, task.current_order_extraction_id)
|
||||
required = {'reservation_id', 'part_number', 'qty'}
|
||||
extraction.schema_version.definition_json['fields'] = [
|
||||
item for item in extraction.schema_version.definition_json['fields']
|
||||
if item['field_code'] in required
|
||||
]
|
||||
|
||||
snapshot = freeze_order_snapshot(task, actor_id=user.id, expected_order_data_version=7)
|
||||
|
||||
assert snapshot.lines[0].reservation_id == '0012345678'
|
||||
assert snapshot.currency is None
|
||||
assert snapshot.rule_versions_json['fulfillment_policy']['code'] == 'SHIP_AVAILABLE_ROLLING_V1'
|
||||
|
||||
|
||||
def test_freeze_rejects_when_no_fulfillment_policy_is_published(app):
|
||||
"""关键断言:Schema 已发布不等于可履约,未发布策略时必须在冻结入口阻断。"""
|
||||
with app.app_context():
|
||||
task, user = _build_accepted_order()
|
||||
ShippingFulfillmentPolicy.query.delete()
|
||||
db.session.commit()
|
||||
|
||||
with pytest.raises(ConflictError) as error:
|
||||
freeze_order_snapshot(task, actor_id=user.id, expected_order_data_version=7)
|
||||
|
||||
assert error.value.code == 'SHIPPING.FULFILLMENT_POLICY_NOT_PUBLISHED'
|
||||
assert task.order_snapshot_id is None
|
||||
|
||||
|
||||
def test_freeze_rejects_unconfirmed_or_caller_supplied_customer(app):
|
||||
"""冻结只能采纳已确认订购客户,调用者传 customer_id 不能绕过该链路。"""
|
||||
with app.app_context():
|
||||
|
||||
Reference in new issue
Block a user