Files
syops/backend/app/services/shipping_notice/parse_summary.py
T
hugo 8deae3146b feat(shipping-notice): 完成货代发货通知全流程开发与代码重构
- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑
- 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示
- 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败
- 新增路径工具类统一管理相对路径,优化配置文件处理逻辑
- 归档旧文档,将部署、Dify配置合并到`shipping_notice.md`
- 新增完整单元测试与集成测试覆盖核心流程
- 更新前端国际化与会话管理逻辑,优化界面交互
2026-06-04 15:26:34 +08:00

133 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from typing import Any
def _cell(value: Any) -> str:
text = str(value or "").strip()
return text if text else "-"
def _fmt_qty(qty: Any, uom: Any = None) -> str:
if qty is None or qty == "":
return "-"
try:
num = float(qty)
except (TypeError, ValueError):
return _cell(qty)
qty_text = str(int(num)) if num == int(num) else f"{num:g}"
unit = str(uom or "").strip() or "PCS"
return f"{qty_text} {unit}"
def _fmt_money(value: Any, currency: Any = None) -> str:
if value is None or value == "":
return "-"
try:
num = float(value)
except (TypeError, ValueError):
return _cell(value)
curr = str(currency or "").strip().upper()
formatted = f"{num:,.2f}"
if curr == "USD":
return f"${formatted}"
if curr == "CNY" or curr == "RMB":
return f"¥{formatted}"
if curr:
return f"{formatted} {curr}"
return formatted
def _append_table_row(rows: list[str], label: str, value: Any) -> None:
rows.append(f"| {label} | {_cell(value)} |")
def _format_line_block(line: dict[str, Any], *, doc_required_date: str = "") -> list[str]:
currency = line.get("currency")
required_date = str(line.get("required_date") or doc_required_date or "").strip()
rows: list[str] = ["| 字段 | 值 |", "|------|-----|"]
_append_table_row(rows, "SKU", line.get("sku"))
_append_table_row(rows, "Reservation ID", line.get("reservation_id"))
_append_table_row(rows, "数量", _fmt_qty(line.get("qty"), line.get("uom")))
_append_table_row(rows, "单价", _fmt_money(line.get("unit_price"), currency))
_append_table_row(rows, "金额", _fmt_money(line.get("amount"), currency))
_append_table_row(rows, "需求日期", required_date)
vendor_pn = str(line.get("vendor_part_number") or "").strip()
if vendor_pn:
_append_table_row(rows, "供应商料号", vendor_pn)
line_no = str(line.get("line_no") or "").strip()
if line_no:
_append_table_row(rows, "项次", line_no)
return rows
def format_parse_summary(
*,
task_no: str,
po_number: str | None = None,
ship_to: str | None = None,
customer_name: str | None = None,
required_date: str | None = None,
lines: list[dict[str, Any]] | None = None,
warnings: list[Any] | None = None,
low_confidence: bool = False,
) -> str:
"""Fixed Markdown summary for parse_notice / Agent display."""
order_lines = [line for line in (lines or []) if isinstance(line, dict)]
doc_date = str(required_date or "").strip()
parts = [f"【发货通知解析确认】任务编号:{task_no}", ""]
if not order_lines:
parts.extend(["| 字段 | 值 |", "|------|-----|"])
_append_table_row(parts, "PO", po_number)
_append_table_row(parts, "收货方", ship_to or customer_name)
_append_table_row(parts, "需求日期", doc_date)
parts.append("")
parts.append("未解析到订单明细行,请重新上传或重新解析。")
elif len(order_lines) == 1:
line = order_lines[0]
parts.extend(["| 字段 | 值 |", "|------|-----|"])
_append_table_row(parts, "PO", po_number)
_append_table_row(parts, "SKU", line.get("sku"))
_append_table_row(parts, "Reservation ID", line.get("reservation_id"))
_append_table_row(parts, "数量", _fmt_qty(line.get("qty"), line.get("uom")))
_append_table_row(parts, "单价", _fmt_money(line.get("unit_price"), line.get("currency")))
_append_table_row(parts, "金额", _fmt_money(line.get("amount"), line.get("currency")))
line_date = str(line.get("required_date") or doc_date or "").strip()
_append_table_row(parts, "需求日期", line_date)
recipient = str(ship_to or customer_name or "").strip()
_append_table_row(parts, "收货方", recipient)
vendor_pn = str(line.get("vendor_part_number") or "").strip()
if vendor_pn:
_append_table_row(parts, "供应商料号", vendor_pn)
else:
if po_number:
parts.append(f"PO:{_cell(po_number)}")
recipient = str(ship_to or customer_name or "").strip()
if recipient:
parts.append(f"收货方:{recipient}")
if doc_date:
parts.append(f"需求日期:{doc_date}")
parts.append("")
for index, line in enumerate(order_lines, 1):
line_no = str(line.get("line_no") or index).strip()
parts.append(f"【明细 {index}】项次 {line_no}")
parts.extend(_format_line_block(line, doc_required_date=doc_date))
parts.append("")
warn_items = [str(w).strip() for w in (warnings or []) if str(w).strip()]
if warn_items:
parts.append("")
parts.append("提示:")
for warning in warn_items[:8]:
parts.append(f"- {warning}")
parts.append("")
if low_confidence:
parts.append("解析置信度较低,请重点核对 SKU、数量、Reservation ID、单价与金额。")
parts.append("请确认以上信息是否正确?")
parts.append("回复「确认」进入库存检查;「重新解析」重试;「驳回」终止本任务。")
parts.append("(Hxops 聊天页也可直接点击操作按钮,无需输入文字。)")
return "\n".join(parts).strip()