- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑 - 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示 - 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败 - 新增路径工具类统一管理相对路径,优化配置文件处理逻辑 - 归档旧文档,将部署、Dify配置合并到`shipping_notice.md` - 新增完整单元测试与集成测试覆盖核心流程 - 更新前端国际化与会话管理逻辑,优化界面交互
181 lines
6.0 KiB
Python
181 lines
6.0 KiB
Python
from app.services.shipping_notice.extract import extract_notice_fields
|
|
from app.services.shipping_notice.notice_lines import (
|
|
normalize_notice_line,
|
|
parse_notice_table_rows,
|
|
parse_notice_table_text,
|
|
validate_notice_lines,
|
|
)
|
|
from app.services.shipping_notice.reservation_id import (
|
|
extract_reservation_id,
|
|
find_reservation_id_in_document,
|
|
looks_like_reservation_id,
|
|
normalize_reservation_id,
|
|
)
|
|
|
|
|
|
def test_extract_reservation_id_from_pr_number():
|
|
assert extract_reservation_id("ADU_S-ENCWIW260528126265@1000140351") == "1000140351"
|
|
assert find_reservation_id_in_document("PR Number: ADU_S-ENCWIW260528126265@1000140351") == "1000140351"
|
|
|
|
|
|
def test_wistron_po_table_with_pr_number_in_document():
|
|
text = """
|
|
Vendor: HOXIN
|
|
PR Number: ADU_S-ENCWIW260528126265@1000140351
|
|
Line No\tMaterial\tVendor Part Number\tDescription\tQuantity\tETA\tUnit Price\tAmount
|
|
10\t504-010213-001\tB3-A08AR11000001\tENCLOSURE\t9.000 PCS\t2026/06/15\t1,607.72 USD\t14,469.48 USD
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert len(lines) == 1
|
|
assert lines[0]["sku"] == "504-010213-001"
|
|
assert lines[0]["line_no"] == "10"
|
|
assert lines[0]["qty"] == 9.0
|
|
assert lines[0]["unit_price"] == 1607.72
|
|
assert lines[0]["amount"] == 14469.48
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
assert lines[0]["required_date"] == "2026-06-15"
|
|
assert lines[0]["vendor_part_number"] == "B3-A08AR11000001"
|
|
assert lines[0]["currency"] == "USD"
|
|
|
|
|
|
def test_normalize_reservation_id_from_excel_float():
|
|
assert normalize_reservation_id(1000140351.0) == "1000140351"
|
|
assert normalize_reservation_id("1000140351.0") == "1000140351"
|
|
assert looks_like_reservation_id("1000140351")
|
|
|
|
|
|
def test_parse_notice_table_numeric_reservation_id():
|
|
text = """
|
|
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
|
|
10-40594-001\t1000140351\t2\t15.5\t31.0
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
|
|
|
|
def test_infer_reservation_id_without_header():
|
|
headers = ["AWS Sku", "Ref", "Qty"]
|
|
rows = [["10-40594-001", 1000140351, 2]]
|
|
lines = parse_notice_table_rows(headers, rows)
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
|
|
|
|
def test_parse_notice_table_with_all_fields():
|
|
text = """
|
|
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
|
|
10-40594-001\tAWS-311\t2\t15.5\t31.0
|
|
10-40594-002\tORD-3002\t1\t20\t20
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert len(lines) == 2
|
|
assert lines[0]["sku"] == "10-40594-001"
|
|
assert lines[0]["reservation_id"] == "AWS-311"
|
|
assert lines[0]["qty"] == 2
|
|
assert lines[0]["unit_price"] == 15.5
|
|
assert lines[0]["amount"] == 31.0
|
|
|
|
|
|
def test_normalize_notice_line_aliases():
|
|
line = normalize_notice_line(
|
|
{
|
|
"part_number": "A-001",
|
|
"quantity": "3",
|
|
"com_id": "COM-99",
|
|
"price": "12.5",
|
|
"delivery_date": "2026/06/15",
|
|
"vendorPartNumber": "VND-001",
|
|
"currency": "USD",
|
|
"line_no": "10",
|
|
}
|
|
)
|
|
assert line == {
|
|
"line_no": "10",
|
|
"sku": "A-001",
|
|
"qty": 3.0,
|
|
"uom": "EA",
|
|
"reservation_id": "COM-99",
|
|
"unit_price": 12.5,
|
|
"amount": 37.5,
|
|
"currency": "USD",
|
|
"required_date": "2026-06-15",
|
|
"vendor_part_number": "VND-001",
|
|
}
|
|
|
|
|
|
def test_enrich_notice_with_document_currency():
|
|
from app.services.shipping_notice.notice_lines import enrich_notice_with_document_currency
|
|
|
|
parsed = enrich_notice_with_document_currency(
|
|
{
|
|
"currency": "USD",
|
|
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "currency": "EUR", "uom": "EA"}],
|
|
}
|
|
)
|
|
assert parsed["lines"][0]["currency"] == "USD"
|
|
assert parsed["lines"][1]["currency"] == "EUR"
|
|
|
|
|
|
def test_extract_currency_from_price_string():
|
|
from app.services.shipping_notice.notice_lines import _extract_currency
|
|
|
|
assert _extract_currency("1,607.72 USD") == "USD"
|
|
assert _extract_currency("$15.5") == "USD"
|
|
assert _extract_currency("¥100") == "CNY"
|
|
|
|
|
|
def test_enrich_notice_with_document_required_date():
|
|
from app.services.shipping_notice.notice_lines import enrich_notice_with_document_required_date
|
|
|
|
parsed = enrich_notice_with_document_required_date(
|
|
{
|
|
"required_date": "2026-06-20",
|
|
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "required_date": "2026-06-18", "uom": "EA"}],
|
|
}
|
|
)
|
|
assert parsed["lines"][0]["required_date"] == "2026-06-20"
|
|
assert parsed["lines"][1]["required_date"] == "2026-06-18"
|
|
|
|
|
|
def test_extract_notice_fields_structured():
|
|
structured = {
|
|
"customer_name": "ACME",
|
|
"lines": [
|
|
{
|
|
"sku": "10-40594-001",
|
|
"qty": 2,
|
|
"reservation_id": "AWS-311",
|
|
"unit_price": 10,
|
|
"amount": 20,
|
|
}
|
|
],
|
|
"confidence": 0.9,
|
|
}
|
|
parsed, confidence, warnings = extract_notice_fields(raw_text="", structured=structured)
|
|
assert parsed["lines"][0]["reservation_id"] == "AWS-311"
|
|
assert parsed["lines"][0]["amount"] == 20
|
|
assert confidence >= 0.8
|
|
assert not any("未解析到有效订单行" in w for w in warnings)
|
|
|
|
|
|
def test_validate_notice_lines_missing_reservation():
|
|
warnings = validate_notice_lines([{"sku": "A001", "qty": 1, "uom": "EA"}])
|
|
assert any("Reservation ID" in w for w in warnings)
|
|
|
|
|
|
def test_heuristic_table_skips_title_row():
|
|
from app.services.shipping_notice.extract import _heuristic_extract
|
|
|
|
text = """
|
|
Shipping Notice
|
|
AWS Sku\tReservation Order\tOrder Qty\tUnit Price\tLine Amount
|
|
10-40594-001\tORD-3002\t4\t5\t20
|
|
"""
|
|
parsed, confidence, _ = _heuristic_extract(text)
|
|
assert len(parsed["lines"]) == 1
|
|
assert parsed["lines"][0]["sku"] == "10-40594-001"
|
|
assert parsed["lines"][0]["reservation_id"] == "ORD-3002"
|
|
assert parsed["lines"][0]["qty"] == 4
|
|
assert parsed["lines"][0]["unit_price"] == 5
|
|
assert parsed["lines"][0]["amount"] == 20
|
|
assert confidence >= 0.5
|