feat(pl): require load products to belong to selected contract

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-08-30 10:49:36 +08:00
1 parent eb1bfc5732
commit fbcd27b213
2 files changed
+138 -2

No files matched your search

@@ -10,6 +10,7 @@ from fastapi import HTTPException
from sqlalchemy.orm import Session
from app.core.utils import generate_uuid
from app.models.crm_models import CrmContractProduct
from app.models.pl_models import (
PlPlan,
PlPlanContract,
@@ -479,6 +480,43 @@ def assert_at_most_one_contract(
return links
def assert_load_products_in_selected_contract(
db: Session,
tenant_no: str,
load: PlanLoadBody,
) -> None:
if is_internal_customer_name(load.customer_name):
return
links = _normalize_contract_links(load.contracts)
if not links:
return
contract_id = (links[0].contract_id or "").strip()
if not contract_id:
return
rows = (
db.query(CrmContractProduct)
.filter(
CrmContractProduct.contract_id == contract_id,
CrmContractProduct.tenant_no == tenant_no,
CrmContractProduct.del_flag == 0,
)
.all()
)
allowed = {
str(r.product_id).strip()
for r in rows
if getattr(r, "product_id", None) and str(r.product_id).strip()
}
for prod in load.products or []:
pid = str(getattr(prod, "product_id", None) or "").strip()
name = (getattr(prod, "product_name", None) or pid or "未命名").strip()
if not pid or pid not in allowed:
raise HTTPException(
status_code=422,
detail=f"装货产品「{name}」不在所选合同明细中",
)
def _resolve_stop_contracts(
db: Session,
tenant_no: str,
@@ -551,6 +589,7 @@ def validate_plan_stops_write(
resolved_deliveries.append(delivery)
enrich_stop_addresses_from_library(db, tenant_no, load, resolved_deliveries)
assert_load_products_in_selected_contract(db, tenant_no, load)
return load, resolved_deliveries