feat(shipping-notice): 收敛订单处理状态与履约门禁
This commit is contained in:
1 parent
bffcd55b40
commit
70dd0051b8
153 files changed
+10916
-4296
No files matched your search
@@ -4,6 +4,8 @@ services/shipping_notice/export.py
|
||||
用途:按库存源绑定的已发布模板版本生成发货 Excel,并记录不可变导出产物。
|
||||
依赖:ShippingExcelTemplate/ShippingExportArtifact、冻结 ShippingOrderSnapshot、库存快照与 openpyxl。
|
||||
逻辑:锁定 Source、模板、冻结订单快照、库存解析 Profile 和库存快照,按白名单映射写入模板;
|
||||
导出前按本单 match_keys(通常 reservation+part)裁剪库存候选再匹配,避免全量快照进匹配;
|
||||
Header 的 system_fact:warehouse_id 取本 Notice 唯一行库位(PVMI 契约);多库位冲突失败;
|
||||
在最终导出行集上按通知明细项次写入模板;内部遍历序号不属于业务字段。
|
||||
订单内部项次 line_no 只能通过 order_field 映射,不得再作为系统事实。
|
||||
"""
|
||||
@@ -195,6 +197,34 @@ def _template_requires_system_warehouse_id(mappings: list[dict[str, Any]]) -> bo
|
||||
)
|
||||
|
||||
|
||||
def _unique_notice_warehouse_id(notice_lines: list[Any]) -> str | None:
|
||||
"""
|
||||
从表头可写的 Notice 行推导唯一 Warehouse ID。
|
||||
|
||||
核心逻辑:PVMI 等模板把 Warehouse ID 写在 Header;多行须同库位才可写入表头,
|
||||
多库位冲突时失败,避免把某一行的库位误写到整单表头。
|
||||
|
||||
Args:
|
||||
notice_lines: 本 Notice 的 ``ShippingNoticeLine`` 列表。
|
||||
|
||||
Returns:
|
||||
唯一非空库位码;全部为空时返回 None。
|
||||
|
||||
Raises:
|
||||
ValueError: 多行存在多个不同库位。
|
||||
"""
|
||||
warehouses = {
|
||||
str(getattr(line, 'inventory_warehouse_id', None) or '').strip().upper()
|
||||
for line in (notice_lines or [])
|
||||
if str(getattr(line, 'inventory_warehouse_id', None) or '').strip()
|
||||
}
|
||||
if len(warehouses) > 1:
|
||||
raise ValueError('多库位发货通知不能把 Warehouse ID 映射为表头')
|
||||
if len(warehouses) == 1:
|
||||
return next(iter(warehouses))
|
||||
return None
|
||||
|
||||
|
||||
def _format_value(value: Any, formatter: str) -> Any:
|
||||
"""应用发布契约允许的确定性格式化器。"""
|
||||
if value is None:
|
||||
@@ -286,6 +316,50 @@ def _match_text(value: Any) -> str:
|
||||
return str(value).strip().casefold()
|
||||
|
||||
|
||||
def _narrow_inventory_lines_by_wanted_match_keys(
|
||||
inventory_lines: list[dict[str, Any]],
|
||||
*,
|
||||
inventory_field_keys: list[str],
|
||||
wanted_keys: set[tuple[str, ...]],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
仅保留与本单导出行 match_keys 命中的库存行。
|
||||
|
||||
核心逻辑:导出前按 reservation/part 等键裁剪候选,避免上万行全量快照进入
|
||||
逐行模板匹配;键集合为空时返回空列表(由后续匹配/放行逻辑裁决)。
|
||||
|
||||
Args:
|
||||
inventory_lines: 活动库存快照行。
|
||||
inventory_field_keys: 与 match_keys 对齐的库存字段键(通常为字段目录 ID)。
|
||||
wanted_keys: 本单导出行规范化后的键元组集合。
|
||||
|
||||
Returns:
|
||||
子集库存行;无候选时为空列表。
|
||||
"""
|
||||
if not inventory_lines or not inventory_field_keys or not wanted_keys:
|
||||
return []
|
||||
return [
|
||||
row for row in inventory_lines
|
||||
if tuple(_match_text(row.get(field)) for field in inventory_field_keys) in wanted_keys
|
||||
]
|
||||
|
||||
|
||||
def _wanted_order_match_keys(
|
||||
order_lines: list[dict[str, Any]],
|
||||
*,
|
||||
order_field_ids: list[str],
|
||||
) -> set[tuple[str, ...]]:
|
||||
"""从本单导出行收集完整 match_keys 元组(缺任一键的行跳过)。"""
|
||||
wanted: set[tuple[str, ...]] = set()
|
||||
if not order_lines or not order_field_ids:
|
||||
return wanted
|
||||
for order_line in order_lines:
|
||||
key = tuple(_match_text(order_line.get(field_id)) for field_id in order_field_ids)
|
||||
if all(key):
|
||||
wanted.add(key)
|
||||
return wanted
|
||||
|
||||
|
||||
def _inventory_fields_by_mode(mappings: list[dict[str, Any]], row_mode: str) -> set[str]:
|
||||
"""返回模板直接引用的库存字段 ID,用于多命中一致性验证。"""
|
||||
raw_ids = {
|
||||
@@ -625,8 +699,38 @@ def generate_shipment_excel(
|
||||
line_values.append(row)
|
||||
from app.services.shipping_notice.inventory_file_store import resolve_inventory_lines_for_execution
|
||||
|
||||
# [STEP 3] 活动库存 Link 是运行时唯一事实源;仅无 Link 的历史任务由读取服务回退旧缓存。
|
||||
# [STEP 3] 活动库存 Link 为事实源;先按本单 match_keys(通常 reservation+part)
|
||||
# 裁成候选子集,再做模板字段匹配——禁止上万行全量快照进入逐行匹配。
|
||||
inventory_lines = resolve_inventory_lines_for_execution(source_notice)
|
||||
if (line_inventory_fields or header_inventory_fields) and match_keys and line_values:
|
||||
from app.models import ShippingInventoryField
|
||||
|
||||
match_field_ids = {
|
||||
int(item.get('inventory_field_id'))
|
||||
for item in match_keys
|
||||
if isinstance(item, dict) and str(item.get('inventory_field_id') or '').isdigit()
|
||||
}
|
||||
catalog = {
|
||||
str(item.id): str(item.id)
|
||||
for item in ShippingInventoryField.query.filter(ShippingInventoryField.id.in_(match_field_ids)).all()
|
||||
} if match_field_ids else {}
|
||||
order_field_ids: list[str] = []
|
||||
inventory_field_keys: list[str] = []
|
||||
for item in match_keys:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
inv_key = catalog.get(str(item.get('inventory_field_id') or '').strip())
|
||||
if not inv_key:
|
||||
continue
|
||||
order_field_ids.append(str(item.get('order_field_id') or ''))
|
||||
inventory_field_keys.append(inv_key)
|
||||
if order_field_ids and inventory_field_keys:
|
||||
wanted_keys = _wanted_order_match_keys(line_values, order_field_ids=order_field_ids)
|
||||
inventory_lines = _narrow_inventory_lines_by_wanted_match_keys(
|
||||
inventory_lines,
|
||||
inventory_field_keys=inventory_field_keys,
|
||||
wanted_keys=wanted_keys,
|
||||
)
|
||||
release_overrides_by_line = _release_overrides_for_export_lines(
|
||||
line_values=line_values,
|
||||
release_overrides_by_source_line=release_overrides_by_source_line,
|
||||
@@ -679,26 +783,36 @@ def generate_shipment_excel(
|
||||
}
|
||||
for sheet, start_row in data_start_rows.items():
|
||||
_clear_template_data_rows(wb[sheet], start_row)
|
||||
# [STEP] Notice 行一次加载:明细写各行库位;Header warehouse 仅在模板需要时解析。
|
||||
notice_lines = ShippingNoticeLine.query.filter_by(notice_id=int(task.id)).order_by(
|
||||
ShippingNoticeLine.position.asc(), ShippingNoticeLine.id.asc(),
|
||||
).all()
|
||||
header_warehouse_id: str | None = None
|
||||
header_warehouse_resolved = False
|
||||
for mapping in mappings:
|
||||
ws = wb[mapping['sheet']]
|
||||
start_row = int(mapping['start_row'])
|
||||
column = int(mapping['target_column'])
|
||||
if mapping['row_mode'] == 'header':
|
||||
# PVMI Header 允许 system_fact:warehouse_id;值取本 Notice 唯一库位。
|
||||
mapping_warehouse_id = None
|
||||
if _template_requires_system_warehouse_id([mapping]):
|
||||
raise ValueError('Warehouse ID 仅可作为发货通知行字段导出,模板不得将其映射为表头')
|
||||
if not header_warehouse_resolved:
|
||||
header_warehouse_id = _unique_notice_warehouse_id(notice_lines)
|
||||
header_warehouse_resolved = True
|
||||
if not header_warehouse_id:
|
||||
raise ValueError('缺少 Warehouse ID,无法写入发货 Excel 表头')
|
||||
mapping_warehouse_id = header_warehouse_id
|
||||
ws.cell(start_row, column).value = _mapping_value(
|
||||
mapping, task=task, order_values=header_values, inventory_values=header_inventory,
|
||||
excel_business_no=excel_business_no,
|
||||
export_date=export_date,
|
||||
notice_line_no=None,
|
||||
notice_line_warehouse_id=None,
|
||||
notice_line_warehouse_id=mapping_warehouse_id,
|
||||
field_id_by_code=field_id_by_code,
|
||||
)
|
||||
continue
|
||||
row_count = max(len(line_values), 1)
|
||||
notice_lines = ShippingNoticeLine.query.filter_by(notice_id=int(task.id)).order_by(
|
||||
ShippingNoticeLine.position.asc(), ShippingNoticeLine.id.asc(),
|
||||
).all()
|
||||
for index in range(row_count):
|
||||
order_line = line_values[index] if index < len(line_values) else {}
|
||||
inventory_line = matched_inventory_lines[index] if index < len(matched_inventory_lines) else {}
|
||||
|
||||
Reference in new issue
Block a user