refactor(shipping-notice): 统一料号字段,移除过时的 SKU 相关字段,更新相关逻辑以支持新字段命名

This commit is contained in:
hugo committed 2026-07-16 20:02:33 +08:00
1 parent dde25344ca
commit 46eb63f5bb
111 files changed
+3116 -1250

No files matched your search

+17 -16
View File
@@ -22,16 +22,17 @@ def _norm_reservation(value: Any) -> str:
return validated_reservation_id(value)
def _line_key(sku: str, reservation_id: str | None) -> tuple[str, str]:
return (_norm(sku), _norm_reservation(reservation_id))
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]:
"""按 (sku, reservation_id) 汇总库存 available_qty。"""
"""按库存 part_number 与 reservation_id 汇总 available_qty。"""
totals: dict[tuple[str, str], float] = {}
for row in inventory_lines:
sku = _norm(row.get("sku"))
if not sku:
part_number = _norm(row.get("part_number"))
if not part_number:
continue
reservation_id = _norm_reservation(row.get("reservation_id"))
if not reservation_id:
@@ -40,21 +41,21 @@ def _aggregate_inventory(inventory_lines: list[dict[str, Any]]) -> dict[tuple[st
qty = float(row.get("available_qty") or 0)
except (TypeError, ValueError):
qty = 0.0
key = _line_key(sku, reservation_id)
key = _line_key(part_number, reservation_id)
totals[key] = totals.get(key, 0.0) + qty
return totals
def _available_for_notice_line(
sku: str,
part_number: str,
reservation_id: str | None,
inv_totals: dict[tuple[str, str], float],
) -> float:
sku_n = _norm(sku)
part_number_normalized = _norm(part_number)
rid = _norm_reservation(reservation_id)
if not rid:
return 0.0
return inv_totals.get((sku_n, rid), 0.0)
return inv_totals.get((part_number_normalized, rid), 0.0)
def match_notice_lines(
@@ -64,14 +65,14 @@ def match_notice_lines(
progress_callback: Callable[[int, int], None] | None = None,
) -> tuple[str, list[dict[str, Any]], dict[str, Any]]:
"""
将订单行与库存行按 (sku, reservation_id) 匹配,计算 fulfillable/partial/unfulfillable。
将订单行与库存行按 (part_number, reservation_id) 匹配,计算 fulfillable/partial/unfulfillable。
核心逻辑:聚合库存 → 逐行比对 required vs available → 汇总 match_status 与 shortages。
Returns:
tuple: (match_status, line_results, shortage_json)。
"""
# [STEP 1] 按 (sku, reservation_id) 聚合库存可用量。
# [STEP 1] 库存 Supplier Sku 已规范为 part_number,与订单内部料号直接配对。
inv_totals = _aggregate_inventory(inventory_lines)
line_results: list[dict[str, Any]] = []
@@ -82,14 +83,14 @@ def match_notice_lines(
# [STEP 2] 逐订单行计算 ok/partial/shortage 并收集缺口明细。
total_lines = len(notice_lines)
for index, line in enumerate(notice_lines, start=1):
sku = _norm(line.get("sku"))
part_number = _norm(line.get("part_number"))
reservation_id = _norm_reservation(line.get("reservation_id")) or None
try:
required = float(line.get("qty") or 0)
except (TypeError, ValueError):
required = 0.0
available = _available_for_notice_line(sku, reservation_id, inv_totals)
available = _available_for_notice_line(part_number, reservation_id, inv_totals)
if available >= required:
line_status = "ok"
shortage = 0.0
@@ -103,7 +104,7 @@ def match_notice_lines(
has_shortage = True
result = {
"sku": sku,
"part_number": part_number,
"reservation_id": reservation_id or "",
"required_qty": required,
"available_qty": available,
@@ -170,7 +171,7 @@ def format_match_summary(
lines.append(
"| # | "
f"{t('field.reservation_id', lang=resolved)} | "
f"{t('field.sku', lang=resolved)} | "
f"{t('field.part_number', lang=resolved)} | "
f"{t('match.col.order_qty', lang=resolved)} | "
f"{t('match.col.inv_qty', lang=resolved)} | "
f"{t('match.col.gap', lang=resolved)} | "
@@ -203,7 +204,7 @@ def format_match_summary(
"| "
f"{index} | "
f"{_md_cell(row.get('reservation_id'))} | "
f"{_md_cell(row.get('sku'))} | "
f"{_md_cell(row.get('part_number'))} | "
f"{_fmt_qty(required)} | "
f"{_fmt_qty(available)} | "
f"{gap_cell} | "