提交
This commit is contained in:
1 parent
33c038b8d9
commit
54b27a3f0e
253 files changed
+30060
-14298
No files matched your search
@@ -427,6 +427,13 @@ def delete_inventory(
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_inventory(db, tenant_no, id, user)
|
||||
|
||||
if row.status == "已完成":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="已完成的记录不能删除",
|
||||
)
|
||||
|
||||
assert_row_deletable_by_creator(row, user_info_from_context(user))
|
||||
row.del_flag = 1
|
||||
row.update_by = user.id
|
||||
|
||||
@@ -296,6 +296,13 @@ def delete_plan(
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id, user)
|
||||
|
||||
if plan.execution_status in ("已入库", "已出库"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="已完成的计划不能删除",
|
||||
)
|
||||
|
||||
assert_row_deletable_by_creator(plan, user_info_from_context(user))
|
||||
plan.del_flag = 1
|
||||
plan.update_by = user.id
|
||||
|
||||
@@ -296,6 +296,9 @@ def list_document_reminders(
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[DocumentReminderRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
scan_cert_expiry(db, tenant_no)
|
||||
db.commit()
|
||||
|
||||
query = db.query(PlDocumentReminder).filter(
|
||||
PlDocumentReminder.tenant_no == tenant_no,
|
||||
PlDocumentReminder.del_flag == 0,
|
||||
|
||||
+68
-14
@@ -32,6 +32,7 @@ from app.schemas.pl.plan import (
|
||||
PlanArchiveBody,
|
||||
PlanCreate,
|
||||
PlanCreateFull,
|
||||
HazardCertReadyBody,
|
||||
PlanListQuery,
|
||||
PlanListResponse,
|
||||
PlanProductCertsUpdate,
|
||||
@@ -52,9 +53,13 @@ from app.services.pl_plan_service import (
|
||||
build_plan_product,
|
||||
calc_plan_total_amount,
|
||||
clear_draft_transport_for_plan,
|
||||
clear_hazard_cert_ready,
|
||||
collect_hazard_product_ids,
|
||||
determine_plan_type,
|
||||
fetch_hazard_flags_map,
|
||||
record_plan_change,
|
||||
resolve_hazard_flags_from_crm,
|
||||
set_hazard_cert_ready,
|
||||
sync_transport_prefill_for_plan,
|
||||
update_plan_product_certs,
|
||||
validate_create_full,
|
||||
@@ -201,6 +206,12 @@ def list_plans(
|
||||
for product in all_products:
|
||||
products_by_plan.setdefault(product.plan_id, []).append(product)
|
||||
|
||||
hazard_map = fetch_hazard_flags_map(
|
||||
db,
|
||||
tenant_no,
|
||||
[product.product_id for product in all_products if product.product_id],
|
||||
)
|
||||
|
||||
result_list: list[PlanWithProducts] = []
|
||||
for plan in plans:
|
||||
enriched_products: list[PlanProductRead] = []
|
||||
@@ -209,12 +220,10 @@ def list_plans(
|
||||
db,
|
||||
tenant_no,
|
||||
product.product_id,
|
||||
product.is_toxic,
|
||||
product.is_explosive,
|
||||
hazard_map=hazard_map,
|
||||
)
|
||||
item = PlanProductRead.model_validate(product)
|
||||
if is_toxic != int(product.is_toxic or 0) or is_explosive != int(product.is_explosive or 0):
|
||||
item = item.model_copy(update={"is_toxic": is_toxic, "is_explosive": is_explosive})
|
||||
item = item.model_copy(update={"is_toxic": is_toxic, "is_explosive": is_explosive})
|
||||
enriched_products.append(item)
|
||||
|
||||
plan_item = PlanWithProducts.model_validate(plan)
|
||||
@@ -409,8 +418,6 @@ def create_plan_full(
|
||||
db,
|
||||
tenant_no,
|
||||
prod.product_id,
|
||||
prod.is_toxic,
|
||||
prod.is_explosive,
|
||||
)
|
||||
prod_payload = prod.model_copy(update={"is_toxic": is_toxic, "is_explosive": is_explosive})
|
||||
pp = build_plan_product(
|
||||
@@ -526,8 +533,6 @@ def create_from_contract(
|
||||
db,
|
||||
tenant_no,
|
||||
cp.product_id,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
pp = PlPlanProduct(
|
||||
id=generate_uuid(),
|
||||
@@ -577,18 +582,22 @@ def get_plan(
|
||||
.all()
|
||||
)
|
||||
|
||||
hazard_map = fetch_hazard_flags_map(
|
||||
db,
|
||||
tenant_no,
|
||||
[product.product_id for product in products if product.product_id],
|
||||
)
|
||||
|
||||
enriched_products = []
|
||||
for product in products:
|
||||
is_toxic, is_explosive = resolve_hazard_flags_from_crm(
|
||||
db,
|
||||
tenant_no,
|
||||
product.product_id,
|
||||
product.is_toxic,
|
||||
product.is_explosive,
|
||||
hazard_map=hazard_map,
|
||||
)
|
||||
item = PlanProductRead.model_validate(product)
|
||||
if is_toxic != int(product.is_toxic or 0) or is_explosive != int(product.is_explosive or 0):
|
||||
item = item.model_copy(update={"is_toxic": is_toxic, "is_explosive": is_explosive})
|
||||
item = item.model_copy(update={"is_toxic": is_toxic, "is_explosive": is_explosive})
|
||||
enriched_products.append(item)
|
||||
|
||||
result = PlanWithProducts.model_validate(plan)
|
||||
@@ -611,6 +620,9 @@ def update_plan(
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id, user)
|
||||
|
||||
if plan.follow_status == "cancelled":
|
||||
raise HTTPException(status_code=400, detail="已作废的计划不能修改")
|
||||
|
||||
# 更新主表字段
|
||||
update_data = payload.model_dump(exclude_unset=True, exclude={"products", "transport"})
|
||||
for key, value in update_data.items():
|
||||
@@ -628,6 +640,7 @@ def update_plan(
|
||||
)
|
||||
.all()
|
||||
)
|
||||
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)
|
||||
for row in existing_products
|
||||
@@ -644,8 +657,6 @@ def update_plan(
|
||||
db,
|
||||
tenant_no,
|
||||
prod.product_id,
|
||||
prod.is_toxic,
|
||||
prod.is_explosive,
|
||||
)
|
||||
purchase_cert = prod.purchase_cert_url
|
||||
transport_cert = prod.transport_cert_url
|
||||
@@ -673,6 +684,10 @@ def update_plan(
|
||||
db.add(pp)
|
||||
plan_products.append(pp)
|
||||
|
||||
new_hazard_product_ids = collect_hazard_product_ids(db, tenant_no, payload.products)
|
||||
if old_hazard_product_ids != new_hazard_product_ids:
|
||||
clear_hazard_cert_ready(plan)
|
||||
|
||||
plan.total_amount = calc_plan_total_amount(payload.products)
|
||||
else:
|
||||
plan_products = (
|
||||
@@ -742,6 +757,38 @@ def patch_plan_product_certs(
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/{id}/hazard-cert-ready", response_model=PlanWithProducts)
|
||||
def patch_plan_hazard_cert_ready(
|
||||
id: str,
|
||||
payload: HazardCertReadyBody,
|
||||
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)
|
||||
set_hazard_cert_ready(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
plan_id=id,
|
||||
ready=payload.ready,
|
||||
user_id=user.id,
|
||||
)
|
||||
plan = _get_plan(db, tenant_no, id, user)
|
||||
products = (
|
||||
db.query(PlPlanProduct)
|
||||
.filter(
|
||||
PlPlanProduct.plan_id == id,
|
||||
PlPlanProduct.del_flag == 0,
|
||||
)
|
||||
.order_by(PlPlanProduct.order_num)
|
||||
.all()
|
||||
)
|
||||
result = PlanWithProducts.model_validate(plan)
|
||||
result.products = products
|
||||
return result
|
||||
|
||||
|
||||
def _build_driver_url(token: str) -> str:
|
||||
origin = (settings.PUBLIC_WEB_ORIGIN or "").rstrip("/")
|
||||
return f"{origin}/#/public/pl/transport/{token}"
|
||||
@@ -989,6 +1036,13 @@ def delete_plan(
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id, user)
|
||||
|
||||
if plan.follow_status != "draft":
|
||||
raise HTTPException(status_code=400, detail="只能删除草稿状态的运输计划")
|
||||
if plan.transport_status != "pending":
|
||||
raise HTTPException(status_code=400, detail="已安排运输的计划不能删除")
|
||||
if plan.archive_status == "archived":
|
||||
raise HTTPException(status_code=400, detail="已归档的计划不能删除")
|
||||
|
||||
assert_row_deletable_by_creator(plan, user_info_from_context(user))
|
||||
plan.del_flag = 1
|
||||
plan.update_by = user.id
|
||||
|
||||
@@ -15,12 +15,16 @@ from app.core.database import get_db
|
||||
from app.core.response import error_response, success_response
|
||||
from app.core.storage import storage
|
||||
from app.crud import oss as oss_svc
|
||||
from app.models.pl_models import PlPlan, PlTransportSiteLink
|
||||
from app.schemas.db.public_inventory import PublicInventoryFeedback, PublicInventoryPageRead
|
||||
from app.models.pl_models import PlPlan, PlPlanProduct, PlTransportSiteLink
|
||||
from app.schemas.db.public_inventory import PublicInventoryFeedback
|
||||
from app.schemas.pl.transport import PublicTransportPlanProductRead, PublicTransportSitePageRead
|
||||
from app.services.db.mappers import inventory_date_to_api
|
||||
from app.utils.excel_export import load_dict_label_maps
|
||||
|
||||
router = APIRouter(prefix="/public/pl/site", tags=["运输计划-现场公共"])
|
||||
|
||||
CRM_PRODUCT_UNIT = "crm_product_unit"
|
||||
|
||||
|
||||
def _parse_feedback_images(raw: Optional[str]) -> list[str]:
|
||||
if not raw:
|
||||
@@ -53,24 +57,90 @@ def _resolve_plan_voided(db: Session, link: PlTransportSiteLink) -> bool:
|
||||
return bool(plan and plan.follow_status == "cancelled")
|
||||
|
||||
|
||||
def _build_public_page(db: Session, link: PlTransportSiteLink) -> PublicInventoryPageRead:
|
||||
def _resolve_unit_label(unit_map: dict[str, str], unit_code: Optional[str]) -> str:
|
||||
code = (unit_code or "").strip()
|
||||
if not code:
|
||||
return "吨"
|
||||
return unit_map.get(code, code)
|
||||
|
||||
|
||||
def _resolve_plan_product(db: Session, link: PlTransportSiteLink) -> Optional[PlPlanProduct]:
|
||||
if not link.plan_product_id:
|
||||
return None
|
||||
return (
|
||||
db.query(PlPlanProduct)
|
||||
.filter(
|
||||
PlPlanProduct.id == link.plan_product_id,
|
||||
PlPlanProduct.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def _build_site_product_read(
|
||||
db: Session,
|
||||
link: PlTransportSiteLink,
|
||||
plan_product: Optional[PlPlanProduct],
|
||||
) -> PublicTransportPlanProductRead:
|
||||
unit_map = load_dict_label_maps(
|
||||
db, [CRM_PRODUCT_UNIT], tenant_no=link.tenant_no
|
||||
).get(CRM_PRODUCT_UNIT, {})
|
||||
quantity = link.quantity
|
||||
unit = "吨"
|
||||
remark = None
|
||||
if plan_product:
|
||||
quantity = (
|
||||
plan_product.quantity
|
||||
or plan_product.inbound_estimate_qty
|
||||
or plan_product.outbound_estimate_qty
|
||||
or link.quantity
|
||||
)
|
||||
unit = _resolve_unit_label(unit_map, plan_product.unit)
|
||||
remark = (plan_product.remark or "").strip() or None
|
||||
return PublicTransportPlanProductRead(
|
||||
product_name=link.product or (plan_product.product_name if plan_product else "") or "",
|
||||
model=link.specification or (plan_product.model if plan_product else None),
|
||||
package_spec=plan_product.package_spec if plan_product else None,
|
||||
unit=unit,
|
||||
quantity=quantity,
|
||||
remark=remark,
|
||||
)
|
||||
|
||||
|
||||
def _build_public_page(db: Session, link: PlTransportSiteLink) -> PublicTransportSitePageRead:
|
||||
voided = _resolve_plan_voided(db, link)
|
||||
return PublicInventoryPageRead(
|
||||
batch_no=None,
|
||||
plan = (
|
||||
db.query(PlPlan)
|
||||
.filter(PlPlan.id == link.plan_id, PlPlan.del_flag == 0)
|
||||
.first()
|
||||
)
|
||||
plan_product = _resolve_plan_product(db, link)
|
||||
product_read = _build_site_product_read(db, link, plan_product)
|
||||
|
||||
return PublicTransportSitePageRead(
|
||||
business_type=link.business_type,
|
||||
date=inventory_date_to_api(link.plan_date) or None,
|
||||
partner_company=link.partner_company or "",
|
||||
product=link.product or "",
|
||||
specification=link.specification,
|
||||
quantity=float(link.quantity or 0),
|
||||
vehicle_no=link.vehicle_no or "",
|
||||
plan_no=link.plan_no,
|
||||
status=None,
|
||||
remark=link.remark,
|
||||
pickup_address=plan.pickup_address if plan else None,
|
||||
pickup_lng=plan.pickup_lng if plan else None,
|
||||
pickup_lat=plan.pickup_lat if plan else None,
|
||||
pickup_time=plan.pickup_time if plan else None,
|
||||
pickup_notes=plan.pickup_notes if plan else None,
|
||||
destination_name=plan.destination_name if plan else None,
|
||||
destination_address=(
|
||||
(plan.destination_address or plan.destination_name) if plan else None
|
||||
),
|
||||
destination_lng=plan.destination_lng if plan else None,
|
||||
destination_lat=plan.destination_lat if plan else None,
|
||||
delivery_time=plan.delivery_time if plan else None,
|
||||
destination_notes=plan.destination_notes if plan else None,
|
||||
products=[product_read],
|
||||
site_feedback_remark=link.site_feedback_remark,
|
||||
site_feedback_images=_parse_feedback_images(link.site_feedback_images),
|
||||
site_feedback_time=link.site_feedback_time,
|
||||
read_only=False,
|
||||
voided=voided,
|
||||
)
|
||||
|
||||
|
||||
@@ -16,7 +16,13 @@ from app.core.database import get_db
|
||||
from app.core.response import error_response, success_response
|
||||
from app.crud import oss as oss_svc
|
||||
from app.models.pl_models import PlPlan, PlPlanProduct, PlPlanTransport, PlTransport
|
||||
from app.schemas.pl.transport import PublicTransportFeedback, PublicTransportPageRead, PublicTransportPlanProductRead
|
||||
from app.schemas.pl.resource_snapshot import parse_resource_snapshot
|
||||
from app.schemas.pl.transport import (
|
||||
PublicTransportFeedback,
|
||||
PublicTransportPageRead,
|
||||
PublicTransportPersonnelRead,
|
||||
PublicTransportPlanProductRead,
|
||||
)
|
||||
from app.utils.excel_export import load_dict_label_maps
|
||||
|
||||
router = APIRouter(prefix="/public/pl/transport", tags=["运输计划-司机公共"])
|
||||
@@ -67,6 +73,35 @@ def _format_plan_date(value) -> Optional[str]:
|
||||
return str(value)
|
||||
|
||||
|
||||
def _resolve_other_personnel(transport: PlTransport, plan: Optional[PlPlan]) -> list[PublicTransportPersonnelRead]:
|
||||
raw = transport.resource_snapshot or (plan.resource_snapshot if plan else None)
|
||||
snapshot = parse_resource_snapshot(raw)
|
||||
if not snapshot or not snapshot.personnel_list:
|
||||
return []
|
||||
|
||||
selected_driver_id = (snapshot.selected_driver_id or transport.driver_id or "").strip()
|
||||
driver_name = (transport.driver_name or "").strip()
|
||||
rows: list[PublicTransportPersonnelRead] = []
|
||||
for person in snapshot.personnel_list:
|
||||
personnel_id = (person.personnel_id or "").strip()
|
||||
personnel_name = (person.personnel_name or "").strip()
|
||||
is_primary_driver = person.role == "driver" and (
|
||||
person.is_default == 1
|
||||
or (selected_driver_id and personnel_id == selected_driver_id)
|
||||
or (driver_name and personnel_name == driver_name)
|
||||
)
|
||||
if is_primary_driver:
|
||||
continue
|
||||
rows.append(
|
||||
PublicTransportPersonnelRead(
|
||||
personnel_name=personnel_name or "—",
|
||||
phone_no=person.phone_no,
|
||||
role=person.role,
|
||||
)
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def _build_public_page(db: Session, transport: PlTransport) -> PublicTransportPageRead:
|
||||
link = (
|
||||
db.query(PlPlanTransport)
|
||||
@@ -105,9 +140,12 @@ def _build_public_page(db: Session, transport: PlTransport) -> PublicTransportPa
|
||||
package_spec=prod.package_spec,
|
||||
unit=_resolve_unit_label(unit_map, prod.unit),
|
||||
quantity=prod.quantity or prod.inbound_estimate_qty or prod.outbound_estimate_qty,
|
||||
remark=(prod.remark or "").strip() or None,
|
||||
)
|
||||
)
|
||||
|
||||
other_personnel = _resolve_other_personnel(transport, plan)
|
||||
|
||||
return PublicTransportPageRead(
|
||||
plan_no=plan.plan_no if plan else transport.transport_no,
|
||||
plan_date=_format_plan_date(plan.plan_date if plan else None),
|
||||
@@ -115,14 +153,20 @@ def _build_public_page(db: Session, transport: PlTransport) -> PublicTransportPa
|
||||
pickup_address=plan.pickup_address if plan else transport.start_point,
|
||||
pickup_lng=plan.pickup_lng if plan else None,
|
||||
pickup_lat=plan.pickup_lat if plan else None,
|
||||
pickup_time=plan.pickup_time if plan else None,
|
||||
pickup_notes=plan.pickup_notes if plan else None,
|
||||
destination_name=plan.destination_name if plan else None,
|
||||
destination_address=(
|
||||
(plan.destination_address or plan.destination_name) if plan else transport.end_point
|
||||
),
|
||||
destination_lng=plan.destination_lng if plan else None,
|
||||
destination_lat=plan.destination_lat if plan else None,
|
||||
delivery_time=plan.delivery_time if plan else None,
|
||||
destination_notes=plan.destination_notes if plan else None,
|
||||
license_plate=transport.license_plate,
|
||||
driver_name=transport.driver_name,
|
||||
driver_phone=transport.driver_phone,
|
||||
other_personnel=other_personnel,
|
||||
products=product_reads,
|
||||
feedback_remark=transport.feedback_remark,
|
||||
feedback_images=_parse_feedback_images(transport.feedback_images),
|
||||
|
||||
@@ -51,7 +51,10 @@ from app.services.pl_transport_site_service import (
|
||||
revoke_site_links_for_transport,
|
||||
sync_site_links_for_transport,
|
||||
)
|
||||
from app.services.pl_plan_service import is_ton_product_unit
|
||||
from app.services.pl_plan_service import (
|
||||
is_ton_product_unit,
|
||||
validate_hazard_cert_ready_for_transport,
|
||||
)
|
||||
from app.services.pl_transport_resource_snapshot_service import (
|
||||
apply_resource_snapshot_json,
|
||||
build_transport_resource_snapshot,
|
||||
@@ -526,19 +529,13 @@ def arrange_transport(
|
||||
if not driver_info:
|
||||
raise HTTPException(status_code=400, detail="司机不存在")
|
||||
|
||||
# 检查产品属性(易制毒/易制爆需要证件)
|
||||
# 检查产品属性(易制毒/易制爆需要证件,以 CRM 产品主数据为准)
|
||||
products = db.query(PlPlanProduct).filter(
|
||||
PlPlanProduct.plan_id == plan_id,
|
||||
PlPlanProduct.del_flag == 0,
|
||||
).all()
|
||||
|
||||
for prod in products:
|
||||
if prod.is_explosive or prod.is_toxic:
|
||||
if not prod.purchase_cert_url or not prod.transport_cert_url:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"产品【{prod.product_name}】为{'易制爆' if prod.is_explosive else '易制毒'},必须上传购买证和合法使用证明"
|
||||
)
|
||||
validate_hazard_cert_ready_for_transport(db, tenant_no, plan, products)
|
||||
|
||||
# 计算运费
|
||||
freight_amount = payload.freight_amount
|
||||
|
||||
Reference in new issue
Block a user