ForgeFlow-ERP/backend/app/services/logistics.py
2026-06-12 16:00:56 +08:00

37 lines
1.5 KiB
Python

from __future__ import annotations
from decimal import Decimal
from typing import Any
from fastapi import HTTPException
from app.services.operations import to_decimal
def normalize_logistics_fields(
waybill_no: str | None,
freight_amount: Any,
*,
required: bool,
order_photo_url: str | None = None,
freight_required: bool | None = None,
) -> tuple[str | None, Decimal | None, str | None]:
clean_waybill = str(waybill_no or "").strip()
clean_photo_url = str(order_photo_url or "").strip()
freight_missing = freight_amount is None or str(freight_amount).strip() == ""
must_have_freight = required if freight_required is None else freight_required
if required and (not clean_waybill and not clean_photo_url) and freight_missing and freight_required is None:
raise HTTPException(status_code=400, detail="该出入库业务必须填写运单号或上传辅助照片,并填写运费")
if required and (not clean_waybill and not clean_photo_url):
raise HTTPException(status_code=400, detail="该出入库业务必须填写运单号或上传辅助照片")
if must_have_freight and freight_missing:
raise HTTPException(status_code=400, detail="该出入库业务必须填写运费")
if freight_missing:
return clean_waybill or None, None, clean_photo_url or None
freight = to_decimal(freight_amount, "0.01")
if freight < 0:
raise HTTPException(status_code=400, detail="运费不能小于0")
return clean_waybill or None, freight, clean_photo_url or None