- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑 - 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示 - 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败 - 新增路径工具类统一管理相对路径,优化配置文件处理逻辑 - 归档旧文档,将部署、Dify配置合并到`shipping_notice.md` - 新增完整单元测试与集成测试覆盖核心流程 - 更新前端国际化与会话管理逻辑,优化界面交互
162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
from app.services.shipping_notice.reservation_id import extract_reservation_id
|
||
|
||
|
||
def _norm(value: Any) -> str:
|
||
return str(value or "").strip()
|
||
|
||
|
||
def _norm_reservation(value: Any) -> str:
|
||
return extract_reservation_id(value)
|
||
|
||
|
||
def _line_key(sku: str, reservation_id: str | None) -> tuple[str, str]:
|
||
return (_norm(sku), _norm_reservation(reservation_id))
|
||
|
||
|
||
def _aggregate_inventory(inventory_lines: list[dict[str, Any]]) -> dict[tuple[str, str], float]:
|
||
"""Sum available_qty by (AWS Sku, Reservation ID). Same key rows are aggregated."""
|
||
totals: dict[tuple[str, str], float] = {}
|
||
for row in inventory_lines:
|
||
sku = _norm(row.get("sku"))
|
||
if not sku:
|
||
continue
|
||
reservation_id = _norm_reservation(row.get("reservation_id"))
|
||
if not reservation_id:
|
||
continue
|
||
try:
|
||
qty = float(row.get("available_qty") or 0)
|
||
except (TypeError, ValueError):
|
||
qty = 0.0
|
||
key = _line_key(sku, reservation_id)
|
||
totals[key] = totals.get(key, 0.0) + qty
|
||
return totals
|
||
|
||
|
||
def _available_for_notice_line(
|
||
sku: str,
|
||
reservation_id: str | None,
|
||
inv_totals: dict[tuple[str, str], float],
|
||
) -> float:
|
||
sku_n = _norm(sku)
|
||
rid = _norm_reservation(reservation_id)
|
||
if not rid:
|
||
return 0.0
|
||
return inv_totals.get((sku_n, rid), 0.0)
|
||
|
||
|
||
def match_notice_lines(
|
||
notice_lines: list[dict[str, Any]],
|
||
inventory_lines: list[dict[str, Any]],
|
||
) -> tuple[str, list[dict[str, Any]], dict[str, Any]]:
|
||
inv_totals = _aggregate_inventory(inventory_lines)
|
||
|
||
line_results: list[dict[str, Any]] = []
|
||
shortages: list[dict[str, Any]] = []
|
||
has_partial = False
|
||
has_shortage = False
|
||
|
||
for line in notice_lines:
|
||
sku = _norm(line.get("sku"))
|
||
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)
|
||
if available >= required:
|
||
line_status = "ok"
|
||
shortage = 0.0
|
||
elif available > 0:
|
||
line_status = "partial"
|
||
shortage = required - available
|
||
has_partial = True
|
||
else:
|
||
line_status = "shortage"
|
||
shortage = required
|
||
has_shortage = True
|
||
|
||
result = {
|
||
"sku": sku,
|
||
"reservation_id": reservation_id or "",
|
||
"required_qty": required,
|
||
"available_qty": available,
|
||
"shortage_qty": shortage,
|
||
"line_status": line_status,
|
||
}
|
||
line_results.append(result)
|
||
if shortage > 0:
|
||
shortages.append(result)
|
||
|
||
if not notice_lines:
|
||
return "unfulfillable", line_results, {"lines": shortages}
|
||
|
||
if has_shortage and not has_partial and all(r["line_status"] == "shortage" for r in line_results):
|
||
match_status = "unfulfillable"
|
||
elif has_partial or has_shortage:
|
||
match_status = "partial"
|
||
else:
|
||
match_status = "fulfillable"
|
||
|
||
return match_status, line_results, {"lines": shortages}
|
||
|
||
|
||
def _fmt_qty(value: float) -> str:
|
||
if value == int(value):
|
||
return str(int(value))
|
||
return f"{value:g}"
|
||
|
||
|
||
def format_match_summary(
|
||
match_status: str,
|
||
line_results: list[dict[str, Any]],
|
||
) -> str:
|
||
"""Human-readable order qty vs inventory qty for match_inventory responses."""
|
||
if not line_results:
|
||
return "无订单明细,无法比对数量。"
|
||
|
||
lines = ["匹配明细(订单数量 vs 库存可用量):"]
|
||
for i, row in enumerate(line_results, 1):
|
||
sku = str(row.get("sku") or "-")
|
||
reservation_id = str(row.get("reservation_id") or "-")
|
||
try:
|
||
required = float(row.get("required_qty") or 0)
|
||
except (TypeError, ValueError):
|
||
required = 0.0
|
||
try:
|
||
available = float(row.get("available_qty") or 0)
|
||
except (TypeError, ValueError):
|
||
available = 0.0
|
||
try:
|
||
shortage = float(row.get("shortage_qty") or 0)
|
||
except (TypeError, ValueError):
|
||
shortage = 0.0
|
||
status = str(row.get("line_status") or "")
|
||
status_label = {"ok": "充足", "partial": "部分满足", "shortage": "不足"}.get(
|
||
status, status or "未知"
|
||
)
|
||
detail = (
|
||
f"{i}. SKU {sku} | Reservation ID {reservation_id} | "
|
||
f"订单 {_fmt_qty(required)} / 库存 {_fmt_qty(available)} | {status_label}"
|
||
)
|
||
if shortage > 0:
|
||
detail += f"(缺口 {_fmt_qty(shortage)})"
|
||
lines.append(detail)
|
||
|
||
lines.append("")
|
||
if match_status == "fulfillable":
|
||
lines.append("结论:库存充足,可生成发货 Excel 并上传货代系统。")
|
||
elif match_status == "partial":
|
||
lines.append("结论:部分行库存不足,请复核缺口后决定是否部分发货。")
|
||
elif match_status == "unfulfillable":
|
||
lines.append("结论:库存均不足,无法整单发货。")
|
||
else:
|
||
lines.append(f"结论:match_status={match_status}")
|
||
|
||
return "\n".join(lines)
|