feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议
This commit is contained in:
1 parent
46eb63f5bb
commit
e9bdd81f1a
320 files changed
+28361
-10487
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
services/shipping_notice/match.py
|
||||
|
||||
用途:执行订单行与库存行的匹配、缺货判断与匹配结果汇总。
|
||||
依赖:shipping_notice.i18n、reservation_id 归一化工具、标准聚合逻辑。
|
||||
逻辑:由 fulfillment_workflow.match_inventory 调用本文件,把通知订单行与库存快照比对后返回匹配状态和缺货明细。
|
||||
用途:按解析方案配置的字段关系执行库存匹配、缺货判断与结果汇总。
|
||||
依赖:shipping_notice.i18n、动态订单字段投影、标准聚合逻辑。
|
||||
逻辑:由 fulfillment_workflow 传入订单字段 ID、库存字段 ID 和数量字段 ID,字段名称不参与匹配。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -11,69 +11,56 @@ from __future__ import annotations
|
||||
from typing import Any, Callable
|
||||
|
||||
from app.services.shipping_notice.i18n import resolve_lang, t
|
||||
from app.services.shipping_notice.reservation_id import validated_reservation_id
|
||||
|
||||
|
||||
def _norm(value: Any) -> str:
|
||||
return str(value or "").strip()
|
||||
|
||||
|
||||
def _norm_reservation(value: Any) -> str:
|
||||
return validated_reservation_id(value)
|
||||
def _line_key(row: dict[str, Any], fields: list[str]) -> tuple[str, ...] | None:
|
||||
"""按配置字段列表构造确定性匹配键;任一字段为空时该行不可匹配。"""
|
||||
values = tuple(_norm(row.get(field)).casefold() for field in fields)
|
||||
return values if values and all(values) else None
|
||||
|
||||
|
||||
def _line_key(part_number: str, reservation_id: str | None) -> tuple[str, str]:
|
||||
"""构造内部料号与 Reservation 的库存定位键。"""
|
||||
return (_norm(part_number), _norm_reservation(reservation_id))
|
||||
|
||||
|
||||
def _aggregate_inventory(inventory_lines: list[dict[str, Any]]) -> dict[tuple[str, str], float]:
|
||||
"""按库存 part_number 与 reservation_id 汇总 available_qty。"""
|
||||
totals: dict[tuple[str, str], float] = {}
|
||||
def _aggregate_inventory(
|
||||
inventory_lines: list[dict[str, Any]], *, match_fields: list[str], quantity_field: str,
|
||||
) -> dict[tuple[str, ...], float]:
|
||||
"""按用户配置的库存匹配字段组合汇总唯一数量字段。"""
|
||||
totals: dict[tuple[str, ...], float] = {}
|
||||
for row in inventory_lines:
|
||||
part_number = _norm(row.get("part_number"))
|
||||
if not part_number:
|
||||
continue
|
||||
reservation_id = _norm_reservation(row.get("reservation_id"))
|
||||
if not reservation_id:
|
||||
key = _line_key(row, match_fields)
|
||||
if key is None:
|
||||
continue
|
||||
try:
|
||||
qty = float(row.get("available_qty") or 0)
|
||||
qty = float(row.get(quantity_field) or 0)
|
||||
except (TypeError, ValueError):
|
||||
qty = 0.0
|
||||
key = _line_key(part_number, reservation_id)
|
||||
totals[key] = totals.get(key, 0.0) + qty
|
||||
return totals
|
||||
|
||||
|
||||
def _available_for_notice_line(
|
||||
part_number: str,
|
||||
reservation_id: str | None,
|
||||
inv_totals: dict[tuple[str, str], float],
|
||||
) -> float:
|
||||
part_number_normalized = _norm(part_number)
|
||||
rid = _norm_reservation(reservation_id)
|
||||
if not rid:
|
||||
return 0.0
|
||||
return inv_totals.get((part_number_normalized, rid), 0.0)
|
||||
|
||||
|
||||
def match_notice_lines(
|
||||
notice_lines: list[dict[str, Any]],
|
||||
inventory_lines: list[dict[str, Any]],
|
||||
*,
|
||||
match_keys: list[dict[str, str]],
|
||||
quantity_field_id: str,
|
||||
progress_callback: Callable[[int, int], None] | None = None,
|
||||
) -> tuple[str, list[dict[str, Any]], dict[str, Any]]:
|
||||
"""
|
||||
将订单行与库存行按 (part_number, reservation_id) 匹配,计算 fulfillable/partial/unfulfillable。
|
||||
按订单字段 ID 到库存字段 ID 的配置关系匹配,计算 fulfillable/partial/unfulfillable。
|
||||
|
||||
核心逻辑:聚合库存 → 逐行比对 required vs available → 汇总 match_status 与 shortages。
|
||||
|
||||
Returns:
|
||||
tuple: (match_status, line_results, shortage_json)。
|
||||
"""
|
||||
# [STEP 1] 库存 Supplier Sku 已规范为 part_number,与订单内部料号直接配对。
|
||||
inv_totals = _aggregate_inventory(inventory_lines)
|
||||
order_fields = [str(item.get('order_field_id') or '') for item in match_keys]
|
||||
inventory_fields = [str(item.get('inventory_field_id') or '') for item in match_keys]
|
||||
if not order_fields or any(not item for item in order_fields + inventory_fields) or not quantity_field_id:
|
||||
raise ValueError('库存解析方案缺少有效的匹配字段或库存数量字段')
|
||||
inv_totals = _aggregate_inventory(
|
||||
inventory_lines, match_fields=inventory_fields, quantity_field=quantity_field_id,
|
||||
)
|
||||
|
||||
line_results: list[dict[str, Any]] = []
|
||||
shortages: list[dict[str, Any]] = []
|
||||
@@ -83,14 +70,13 @@ def match_notice_lines(
|
||||
# [STEP 2] 逐订单行计算 ok/partial/shortage 并收集缺口明细。
|
||||
total_lines = len(notice_lines)
|
||||
for index, line in enumerate(notice_lines, start=1):
|
||||
part_number = _norm(line.get("part_number"))
|
||||
reservation_id = _norm_reservation(line.get("reservation_id")) or None
|
||||
order_key = _line_key(line, order_fields)
|
||||
try:
|
||||
required = float(line.get("qty") or 0)
|
||||
required = float(line.get("__required_qty") or 0)
|
||||
except (TypeError, ValueError):
|
||||
required = 0.0
|
||||
|
||||
available = _available_for_notice_line(part_number, reservation_id, inv_totals)
|
||||
available = inv_totals.get(order_key, 0.0) if order_key is not None else 0.0
|
||||
if available >= required:
|
||||
line_status = "ok"
|
||||
shortage = 0.0
|
||||
@@ -104,8 +90,11 @@ def match_notice_lines(
|
||||
has_shortage = True
|
||||
|
||||
result = {
|
||||
"part_number": part_number,
|
||||
"reservation_id": reservation_id or "",
|
||||
"part_number": _norm(line.get("__display_part_number")),
|
||||
"reservation_id": _norm(line.get("__display_reservation_id")),
|
||||
"order_match_values": {
|
||||
order_field: line.get(order_field) for order_field in order_fields
|
||||
},
|
||||
"required_qty": required,
|
||||
"available_qty": available,
|
||||
"shortage_qty": shortage,
|
||||
|
||||
Reference in new issue
Block a user