This commit is contained in:
zlwu committed 2026-07-05 21:31:07 +08:00
1 parent 31be9bfd95
commit 843e13bce3
722 files changed
+288067 -110680

No files matched your search

+177 -7
View File
@@ -1,4 +1,5 @@
"""运输计划管理 - 计划 API"""
import re
from datetime import date
from typing import Optional
@@ -34,6 +35,8 @@ from app.schemas.pl.plan import (
PlanCreateFull,
HazardCertReadyBody,
PlanInboundSlipTemplateSave,
PlanDeliverySlipTemplateSave,
PlanSubpackSlipTemplateSave,
PlanListQuery,
PlanListResponse,
PlanProductCertsUpdate,
@@ -95,6 +98,84 @@ def _generate_plan_no(db: Session) -> str:
return f"{prefix}{seq:0{_PLAN_NO_SEQ_WIDTH}d}"
_SUBPACK_SLIP_NO_SEQ_WIDTH = 3
_SUBPACK_SLIP_NO_CANONICAL_PATTERN = re.compile(r"^NO:SXFZ(\d{8})-(\d+)$")
_SUBPACK_SLIP_NO_BARE_PATTERN = re.compile(r"^SXFZ(\d{8})-(\d+)$")
_SUBPACK_SLIP_NO_LEGACY_PATTERNS = (
re.compile(r"^NO:SXFZ-(\d{4})-(\d{2})-(\d{2})-(\d+)$"),
re.compile(r"^SXFZ-(\d{4})-(\d{2})-(\d{2})-(\d+)$"),
)
def _format_subpack_slip_no(day: str, seq: int) -> str:
return f"NO:SXFZ{day}-{int(seq):0{_SUBPACK_SLIP_NO_SEQ_WIDTH}d}"
def _normalize_subpack_slip_no(slip_no: str) -> Optional[str]:
"""将历史分装单号统一为 NO:SXFZYYYYMMDD-###"""
raw = (slip_no or "").strip()
if not raw:
return None
matched = _SUBPACK_SLIP_NO_CANONICAL_PATTERN.match(raw)
if matched:
day, seq = matched.groups()
return _format_subpack_slip_no(day, int(seq))
matched = _SUBPACK_SLIP_NO_BARE_PATTERN.match(raw)
if matched:
day, seq = matched.groups()
return _format_subpack_slip_no(day, int(seq))
for pattern in _SUBPACK_SLIP_NO_LEGACY_PATTERNS:
matched = pattern.match(raw)
if matched:
year, month, day, seq = matched.groups()
return _format_subpack_slip_no(f"{year}{month}{day}", int(seq))
return None
def _subpack_slip_no_seq(slip_no: str, day_prefix: str) -> int:
normalized = _normalize_subpack_slip_no(slip_no)
if not normalized or not normalized.startswith(day_prefix):
return 0
suffix = normalized[len(day_prefix) :]
return int(suffix) if suffix.isdigit() else 0
def _ensure_subpack_slip_no(db: Session, tenant_no: str, plan: PlPlan) -> str:
"""分配分装单号 NO:SXFZYYYYMMDD-###(按租户每日递增)"""
existing = (plan.subpack_slip_no or "").strip()
if existing:
normalized = _normalize_subpack_slip_no(existing)
if normalized:
if normalized != existing:
plan.subpack_slip_no = normalized
return normalized
today = date.today()
prefix = f"NO:SXFZ{today.strftime('%Y%m%d')}-"
rows = (
db.query(PlPlan.subpack_slip_no)
.filter(
PlPlan.tenant_no == tenant_no,
PlPlan.subpack_slip_no.isnot(None),
PlPlan.subpack_slip_no != "",
)
.all()
)
max_seq = 0
for (slip_no,) in rows:
max_seq = max(max_seq, _subpack_slip_no_seq(slip_no or "", prefix))
next_no = f"{prefix}{max_seq + 1:0{_SUBPACK_SLIP_NO_SEQ_WIDTH}d}"
plan.subpack_slip_no = next_no
return next_no
def _determine_plan_type(supplier_name: Optional[str], destination_name: Optional[str]) -> str:
"""根据供应商和目的地自动判断单据类型"""
return determine_plan_type(supplier_name, destination_name)
@@ -234,7 +315,7 @@ def list_plans(
if snapshot and snapshot.personnel_summary:
plan_item.personnel_summary = snapshot.personnel_summary
elif plan_item.driver_name:
plan_item.personnel_summary = f"{plan_item.driver_name}(司机)"
plan_item.personnel_summary = f"{plan_item.driver_name}(驾)"
result_list.append(plan_item)
return PlanListResponse(total=total, list=result_list)
@@ -643,7 +724,11 @@ def update_plan(
)
old_hazard_product_ids = collect_hazard_product_ids(db, tenant_no, existing_products)
cert_by_product_id = {
row.product_id: (row.purchase_cert_url, row.transport_cert_url)
row.product_id: (
row.purchase_cert_url,
row.transport_cert_url,
row.legal_use_cert_url,
)
for row in existing_products
}
@@ -661,16 +746,19 @@ def update_plan(
)
purchase_cert = prod.purchase_cert_url
transport_cert = prod.transport_cert_url
if not purchase_cert or not transport_cert:
old_purchase, old_transport = cert_by_product_id.get(prod.product_id, (None, None))
purchase_cert = purchase_cert or old_purchase
transport_cert = transport_cert or old_transport
legal_use_cert = prod.legal_use_cert_url
old_certs = cert_by_product_id.get(prod.product_id, (None, None, None))
old_purchase, old_transport, old_legal_use = old_certs
purchase_cert = purchase_cert or old_purchase
transport_cert = transport_cert or old_transport
legal_use_cert = legal_use_cert or old_legal_use
prod_payload = prod.model_copy(
update={
"is_toxic": is_toxic,
"is_explosive": is_explosive,
"purchase_cert_url": purchase_cert,
"transport_cert_url": transport_cert,
"legal_use_cert_url": legal_use_cert,
}
)
pp = build_plan_product(
@@ -733,7 +821,7 @@ def patch_plan_product_certs(
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> PlanWithProducts:
"""更新计划危化品产品购买证/合法使用证明(不触发安排运输)"""
"""更新计划危化品产品证书(购买证/运输证/合法使用证明,不触发安排运输)"""
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
_get_plan(db, tenant_no, id, user)
update_plan_product_certs(
@@ -1057,6 +1145,88 @@ def save_inbound_slip_template(
db.commit()
# =============================================================================
# 送(提)货单打印模板
# =============================================================================
@router.get("/{id}/delivery-slip-template")
def get_delivery_slip_template(
id: str,
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> str:
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
plan = _get_plan(db, tenant_no, id, user)
return plan.delivery_slip_template_content or ""
@router.put("/{id}/delivery-slip-template", status_code=status.HTTP_204_NO_CONTENT)
def save_delivery_slip_template(
id: str,
payload: PlanDeliverySlipTemplateSave,
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> None:
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
plan = _get_plan(db, tenant_no, id, user)
if plan.plan_type == "inbound":
raise HTTPException(status_code=400, detail="入库单计划请使用入库单打印模板")
if plan.transport_status != "arranged":
raise HTTPException(status_code=400, detail="请先完成运输安排后再保存送(提)货单打印模板")
plan.delivery_slip_template_content = payload.delivery_slip_template_content or ""
plan.update_by = user.id
db.commit()
# =============================================================================
# 分装单打印模板
# =============================================================================
@router.get("/{id}/subpack-slip-template")
def get_subpack_slip_template(
id: str,
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> str:
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
plan = _get_plan(db, tenant_no, id, user)
return plan.subpack_slip_template_content or ""
@router.put("/{id}/subpack-slip-template", status_code=status.HTTP_204_NO_CONTENT)
def save_subpack_slip_template(
id: str,
payload: PlanSubpackSlipTemplateSave,
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> None:
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
plan = _get_plan(db, tenant_no, id, user)
if plan.plan_type == "inbound":
raise HTTPException(status_code=400, detail="入库单计划请使用入库单打印模板")
plan.subpack_slip_template_content = payload.subpack_slip_template_content or ""
plan.update_by = user.id
db.commit()
@router.post("/{id}/subpack-slip-no")
def ensure_subpack_slip_no(
id: str,
db: Session = Depends(get_db),
user: DbUserContext = Depends(get_db_current_user),
) -> str:
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
plan = _get_plan(db, tenant_no, id, user)
if plan.plan_type == "inbound":
raise HTTPException(status_code=400, detail="入库单计划不支持分装单号")
slip_no = _ensure_subpack_slip_no(db, tenant_no, plan)
plan.update_by = user.id
db.commit()
return slip_no
# =============================================================================
# 删除计划
# =============================================================================