提交
This commit is contained in:
1 parent
725774ef0c
commit
c3be9d1dce
176 files changed
+41925
-6342
No files matched your search
@@ -7,6 +7,7 @@ from app.api.leave import router as leave_router
|
||||
from app.api.crm import router as crm_router
|
||||
from app.api.monitor import router as monitor_router
|
||||
from app.api.bx import bx_router
|
||||
from app.api.db import db_router
|
||||
|
||||
__all__ = [
|
||||
"public_router",
|
||||
@@ -17,4 +18,5 @@ __all__ = [
|
||||
"crm_router",
|
||||
"monitor_router",
|
||||
"bx_router",
|
||||
"db_router",
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
"""地磅 API 路由聚合 — REST 风格,协议对齐原 simple-dibang-xiaoshou。"""
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.db import dashboard, inventory, plans, vehicles, weighing
|
||||
|
||||
db_router = APIRouter(tags=["地磅管理"])
|
||||
db_router.include_router(vehicles.router)
|
||||
db_router.include_router(plans.router)
|
||||
db_router.include_router(inventory.router)
|
||||
db_router.include_router(weighing.router)
|
||||
db_router.include_router(dashboard.router)
|
||||
|
||||
|
||||
@db_router.get("/db/health")
|
||||
def db_health():
|
||||
"""P0 健康检查:验证 db 路由已挂载。"""
|
||||
return {"status": "ok", "module": "db"}
|
||||
@@ -0,0 +1,180 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy import func, text
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.models.db_models import DbInboundOutbound, DbWeighingSlip
|
||||
from app.schemas.db.dashboard import (
|
||||
DashboardCargoStat,
|
||||
DashboardPartnerCargoStat,
|
||||
DashboardStats,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/dashboard", tags=["dashboard"])
|
||||
|
||||
|
||||
@router.get("/stats", response_model=DashboardStats)
|
||||
def get_dashboard_stats(
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> DashboardStats:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
today = date.today()
|
||||
|
||||
today_inbound = float(
|
||||
db.query(func.coalesce(func.sum(DbInboundOutbound.quantity), 0))
|
||||
.filter(
|
||||
DbInboundOutbound.date == today,
|
||||
DbInboundOutbound.business_type == "入库",
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
today_outbound = float(
|
||||
db.query(func.coalesce(func.sum(DbInboundOutbound.quantity), 0))
|
||||
.filter(
|
||||
DbInboundOutbound.date == today,
|
||||
DbInboundOutbound.business_type == "出库",
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
abnormal_orders = int(
|
||||
db.query(func.count(DbWeighingSlip.id))
|
||||
.filter(
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
DbWeighingSlip.tenant_no == tenant_no,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
return DashboardStats(
|
||||
today_inbound=today_inbound,
|
||||
today_outbound=today_outbound,
|
||||
abnormal_orders=abnormal_orders,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/inbound/by-cargo", response_model=list[DashboardCargoStat])
|
||||
def get_inbound_by_cargo(
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[DashboardCargoStat]:
|
||||
return _cargo_stats(db, user, business_type="入库")
|
||||
|
||||
|
||||
@router.get("/inbound/by-partner-cargo", response_model=list[DashboardPartnerCargoStat])
|
||||
def get_inbound_by_partner_cargo(
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[DashboardPartnerCargoStat]:
|
||||
return _partner_cargo_stats(db, user, business_type="入库")
|
||||
|
||||
|
||||
@router.get("/outbound/by-cargo", response_model=list[DashboardCargoStat])
|
||||
def get_outbound_by_cargo(
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[DashboardCargoStat]:
|
||||
return _cargo_stats(db, user, business_type="出库")
|
||||
|
||||
|
||||
@router.get("/outbound/by-partner-cargo", response_model=list[DashboardPartnerCargoStat])
|
||||
def get_outbound_by_partner_cargo(
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[DashboardPartnerCargoStat]:
|
||||
return _partner_cargo_stats(db, user, business_type="出库")
|
||||
|
||||
|
||||
def _cargo_stats(db: Session, user: DbUserContext, *, business_type: str) -> list[DashboardCargoStat]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
today = date.today()
|
||||
query = text(
|
||||
"""
|
||||
SELECT
|
||||
COALESCE(io.product, ws.cargo_name, '未填写货物') AS label,
|
||||
COALESCE(SUM(io.quantity), 0) AS planned,
|
||||
COALESCE(SUM(ws.net_weight) / 1000.0, 0) AS actual
|
||||
FROM db_inbound_outbound io
|
||||
LEFT JOIN db_weighing_slip ws
|
||||
ON ws.inbound_outbound_id = io.id
|
||||
AND ws.is_voided = 0
|
||||
AND ws.del_flag = 0
|
||||
AND ws.tenant_no = io.tenant_no
|
||||
WHERE io.date = :today
|
||||
AND io.business_type = :business_type
|
||||
AND io.del_flag = 0
|
||||
AND io.tenant_no = :tenant_no
|
||||
GROUP BY COALESCE(io.product, ws.cargo_name, '未填写货物')
|
||||
ORDER BY planned DESC
|
||||
"""
|
||||
)
|
||||
rows = db.execute(
|
||||
query,
|
||||
{"today": today, "business_type": business_type, "tenant_no": tenant_no},
|
||||
).all()
|
||||
return [
|
||||
DashboardCargoStat(label=row.label, planned=float(row.planned), actual=float(row.actual))
|
||||
for row in rows
|
||||
]
|
||||
|
||||
|
||||
def _partner_cargo_stats(
|
||||
db: Session,
|
||||
user: DbUserContext,
|
||||
*,
|
||||
business_type: str,
|
||||
) -> list[DashboardPartnerCargoStat]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
today = date.today()
|
||||
query = text(
|
||||
"""
|
||||
SELECT
|
||||
CONCAT(
|
||||
COALESCE(io.partner_company, ''),
|
||||
' - ',
|
||||
COALESCE(io.product, ws.cargo_name, '未填写货物')
|
||||
) AS label,
|
||||
COALESCE(SUM(io.quantity), 0) AS planned,
|
||||
COALESCE(SUM(ws.net_weight) / 1000.0, 0) AS actual
|
||||
FROM db_inbound_outbound io
|
||||
LEFT JOIN db_weighing_slip ws
|
||||
ON ws.inbound_outbound_id = io.id
|
||||
AND ws.is_voided = 0
|
||||
AND ws.del_flag = 0
|
||||
AND ws.tenant_no = io.tenant_no
|
||||
WHERE io.date = :today
|
||||
AND io.business_type = :business_type
|
||||
AND io.del_flag = 0
|
||||
AND io.tenant_no = :tenant_no
|
||||
GROUP BY io.partner_company, io.product, ws.cargo_name
|
||||
ORDER BY io.partner_company, planned DESC
|
||||
"""
|
||||
)
|
||||
rows = db.execute(
|
||||
query,
|
||||
{"today": today, "business_type": business_type, "tenant_no": tenant_no},
|
||||
).all()
|
||||
return [
|
||||
DashboardPartnerCargoStat(
|
||||
label=row.label,
|
||||
planned=float(row.planned),
|
||||
actual=float(row.actual),
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
@@ -0,0 +1,488 @@
|
||||
from typing import Optional
|
||||
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user, get_db_current_user_optional
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.services.db.inventory_id import next_inbound_outbound_id
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbVehicle, DbWeighingSlip
|
||||
from app.schemas.db.inventory import (
|
||||
CreateLinkForPlanPayload,
|
||||
InboundOutboundCreate,
|
||||
InboundOutboundRead,
|
||||
InboundOutboundUpdate,
|
||||
)
|
||||
from app.schemas.db.plans import PurchasePlanRead
|
||||
from app.schemas.db.weighing import WeighingSlipRead
|
||||
from app.services.db.mappers import (
|
||||
EMPTY_INVENTORY_DATE,
|
||||
inventory_to_read,
|
||||
is_empty_inventory_date,
|
||||
parse_date_str,
|
||||
plan_to_read,
|
||||
weighing_to_read,
|
||||
)
|
||||
from app.services.db.product_helpers import (
|
||||
apply_product_snapshot_to_inventory,
|
||||
resolve_crm_product,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/inventory", tags=["inventory"])
|
||||
|
||||
STATUS_THIRD_PARTY_FILL = "待三方填写"
|
||||
_INVENTORY_REMARK_FIELD = "remarks"
|
||||
|
||||
|
||||
def _base_query(db: Session, tenant_no: str):
|
||||
return db.query(DbInboundOutbound).filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
)
|
||||
|
||||
|
||||
def _generate_batch_no(db: Session, tenant_no: str) -> str:
|
||||
rows = (
|
||||
db.query(DbInboundOutbound.batch_no)
|
||||
.filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.batch_no.like("CRKDH%"),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
max_seq = 0
|
||||
pattern = re.compile(r"^CRKDH(\d+)$")
|
||||
for (batch_no,) in rows:
|
||||
if batch_no:
|
||||
match = pattern.match(batch_no)
|
||||
if match:
|
||||
max_seq = max(max_seq, int(match.group(1)))
|
||||
return f"CRKDH{max_seq + 1:07d}"
|
||||
|
||||
|
||||
def _resolve_tenant_no(
|
||||
db: Session,
|
||||
user: Optional[DbUserContext],
|
||||
purchase_plan_id: Optional[str] = None,
|
||||
) -> str:
|
||||
if user:
|
||||
return get_tenant_no(db, user.to_tenant_user_dict())
|
||||
if purchase_plan_id:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(DbPurchasePlan.id == purchase_plan_id, DbPurchasePlan.del_flag == 0)
|
||||
.one_or_none()
|
||||
)
|
||||
if plan:
|
||||
return plan.tenant_no
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="无法确定租户")
|
||||
|
||||
|
||||
def _get_inventory_by_id(db: Session, inventory_id: str) -> DbInboundOutbound:
|
||||
row = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(DbInboundOutbound.id == inventory_id, DbInboundOutbound.del_flag == 0)
|
||||
.one_or_none()
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Inventory {inventory_id} not found",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _get_inventory(db: Session, tenant_no: str, inventory_id: str) -> DbInboundOutbound:
|
||||
row = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(DbInboundOutbound.id == inventory_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Inventory {inventory_id} not found",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _ensure_vehicle_exists(
|
||||
db: Session,
|
||||
tenant_no: str,
|
||||
vehicle_no: str,
|
||||
*,
|
||||
create_by: Optional[str] = None,
|
||||
create_dept_id: Optional[str] = None,
|
||||
) -> None:
|
||||
if not (vehicle_no and vehicle_no.strip()):
|
||||
return
|
||||
normalized = vehicle_no.strip().lower()
|
||||
exists = (
|
||||
db.query(DbVehicle)
|
||||
.filter(
|
||||
DbVehicle.tenant_no == tenant_no,
|
||||
DbVehicle.del_flag == 0,
|
||||
func.lower(DbVehicle.vehicle_no) == normalized,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if exists:
|
||||
return
|
||||
db.add(
|
||||
DbVehicle(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
vehicle_no=vehicle_no.strip(),
|
||||
remark=None,
|
||||
create_by=create_by,
|
||||
create_dept_id=create_dept_id or "",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _apply_inventory_product_update(
|
||||
db: Session, tenant_no: str, row: DbInboundOutbound, payload_dict: dict
|
||||
) -> None:
|
||||
if "product" in payload_dict and "product_id" not in payload_dict:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请从 CRM 重新选择产品",
|
||||
)
|
||||
product_id = payload_dict.pop("product_id", None)
|
||||
payload_dict.pop("product", None)
|
||||
payload_dict.pop("specification", None)
|
||||
if product_id is not None:
|
||||
snapshot = resolve_crm_product(db, tenant_no, product_id)
|
||||
apply_product_snapshot_to_inventory(row, snapshot)
|
||||
|
||||
|
||||
def _apply_inventory_update(row: DbInboundOutbound, payload_dict: dict) -> None:
|
||||
for key, value in payload_dict.items():
|
||||
if key == _INVENTORY_REMARK_FIELD:
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
row.remark = value
|
||||
continue
|
||||
if key == "date":
|
||||
if value is None or (isinstance(value, str) and not value.strip()):
|
||||
row.date = EMPTY_INVENTORY_DATE
|
||||
else:
|
||||
row.date = parse_date_str(str(value)) or EMPTY_INVENTORY_DATE
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
setattr(row, key, value)
|
||||
|
||||
|
||||
def _maybe_promote_third_party_status(row: DbInboundOutbound) -> None:
|
||||
if row.status != STATUS_THIRD_PARTY_FILL:
|
||||
return
|
||||
has_date = not is_empty_inventory_date(row.date)
|
||||
has_qty = row.quantity is not None and row.quantity != 0
|
||||
has_vehicle = bool(row.vehicle_no and row.vehicle_no.strip())
|
||||
if has_date and has_qty and has_vehicle:
|
||||
row.status = "待入库" if row.business_type == "入库" else "待出库"
|
||||
|
||||
|
||||
@router.get("/", response_model=list[InboundOutboundRead])
|
||||
def list_inventory(
|
||||
q: Optional[str] = Query(default=None, description="模糊搜索关键词"),
|
||||
plan_no: Optional[str] = Query(default=None, alias="planNo", description="计划号"),
|
||||
batch_no: Optional[str] = Query(default=None, alias="batchNo", description="出入库单号"),
|
||||
partner_company: Optional[str] = Query(
|
||||
default=None, alias="partnerCompany", description="往来单位"
|
||||
),
|
||||
product: Optional[str] = Query(default=None, description="产品"),
|
||||
vehicle_no: Optional[str] = Query(default=None, alias="vehicleNo", description="车牌号"),
|
||||
date_from: Optional[str] = Query(default=None, alias="dateFrom", description="开始日期"),
|
||||
date_to: Optional[str] = Query(default=None, alias="dateTo", description="结束日期"),
|
||||
status_filter: Optional[str] = Query(default=None, alias="status", description="状态筛选"),
|
||||
business_type: Optional[str] = Query(default=None, alias="businessType", description="业务类型"),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[InboundOutboundRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(
|
||||
DbInboundOutbound.date.desc(),
|
||||
DbInboundOutbound.id.desc(),
|
||||
)
|
||||
|
||||
if q:
|
||||
keyword = q.lower()
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.lower(DbInboundOutbound.partner_company).contains(keyword),
|
||||
func.lower(DbInboundOutbound.product).contains(keyword),
|
||||
func.lower(DbInboundOutbound.plan_no).contains(keyword),
|
||||
func.lower(DbInboundOutbound.batch_no).contains(keyword),
|
||||
func.lower(DbInboundOutbound.vehicle_no).contains(keyword),
|
||||
)
|
||||
)
|
||||
|
||||
if plan_no:
|
||||
query = query.filter(DbInboundOutbound.plan_no.contains(plan_no))
|
||||
|
||||
if batch_no:
|
||||
query = query.filter(DbInboundOutbound.batch_no.contains(batch_no))
|
||||
|
||||
if partner_company:
|
||||
query = query.filter(
|
||||
func.lower(DbInboundOutbound.partner_company).contains(partner_company.lower())
|
||||
)
|
||||
|
||||
if product:
|
||||
query = query.filter(func.lower(DbInboundOutbound.product).contains(product.lower()))
|
||||
|
||||
if vehicle_no:
|
||||
query = query.filter(DbInboundOutbound.vehicle_no.contains(vehicle_no))
|
||||
|
||||
if date_from:
|
||||
start = parse_date_str(date_from)
|
||||
if start:
|
||||
query = query.filter(DbInboundOutbound.date >= start)
|
||||
|
||||
if date_to:
|
||||
end = parse_date_str(date_to)
|
||||
if end:
|
||||
query = query.filter(DbInboundOutbound.date <= end)
|
||||
|
||||
if status_filter:
|
||||
query = query.filter(DbInboundOutbound.status == status_filter)
|
||||
|
||||
if business_type:
|
||||
query = query.filter(DbInboundOutbound.business_type == business_type)
|
||||
|
||||
inventories = query.all()
|
||||
inventory_ids = [inv.id for inv in inventories]
|
||||
weighing_map: dict[str, DbWeighingSlip] = {}
|
||||
if inventory_ids:
|
||||
weighings = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.tenant_no == tenant_no,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
DbWeighingSlip.inbound_outbound_id.in_(inventory_ids),
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
)
|
||||
.order_by(DbWeighingSlip.id.asc())
|
||||
.all()
|
||||
)
|
||||
for weighing in weighings:
|
||||
if (
|
||||
weighing.inbound_outbound_id
|
||||
and weighing.inbound_outbound_id not in weighing_map
|
||||
):
|
||||
weighing_map[weighing.inbound_outbound_id] = weighing
|
||||
|
||||
result_list: list[InboundOutboundRead] = []
|
||||
for inventory in inventories:
|
||||
try:
|
||||
result_list.append(
|
||||
inventory_to_read(
|
||||
inventory,
|
||||
weighing=weighing_map.get(inventory.id),
|
||||
)
|
||||
)
|
||||
except Exception:
|
||||
continue
|
||||
return result_list
|
||||
|
||||
|
||||
@router.post("/", response_model=InboundOutboundRead, status_code=status.HTTP_201_CREATED)
|
||||
def create_inventory(
|
||||
payload: InboundOutboundCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: Optional[DbUserContext] = Depends(get_db_current_user_optional),
|
||||
) -> InboundOutboundRead:
|
||||
tenant_no = _resolve_tenant_no(db, user, payload.purchase_plan_id)
|
||||
|
||||
batch_no = payload.batch_no.strip() if payload.batch_no else None
|
||||
if not batch_no:
|
||||
batch_no = _generate_batch_no(db, tenant_no)
|
||||
|
||||
status_value = (
|
||||
payload.status.strip()
|
||||
if payload.status and str(payload.status).strip()
|
||||
else ("待入库" if payload.business_type == "入库" else "待出库")
|
||||
)
|
||||
|
||||
snapshot = resolve_crm_product(db, tenant_no, payload.product_id)
|
||||
|
||||
row = DbInboundOutbound(
|
||||
id=next_inbound_outbound_id(db, tenant_no),
|
||||
tenant_no=tenant_no,
|
||||
date=parse_date_str(payload.date) or datetime.now().date(),
|
||||
partner_company=payload.partner_company.strip(),
|
||||
business_type=payload.business_type,
|
||||
quantity=payload.quantity,
|
||||
plan_no=payload.plan_no.strip() if payload.plan_no else None,
|
||||
batch_no=batch_no,
|
||||
vehicle_no=payload.vehicle_no.strip(),
|
||||
status=status_value,
|
||||
remark=payload.remarks,
|
||||
purchase_plan_id=payload.purchase_plan_id,
|
||||
create_by=user.id if user else None,
|
||||
create_dept_id=user.dept_id if user else "",
|
||||
)
|
||||
apply_product_snapshot_to_inventory(row, snapshot)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return inventory_to_read(row)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/create-link-for-plan",
|
||||
response_model=InboundOutboundRead,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
def create_link_for_plan(
|
||||
payload: CreateLinkForPlanPayload,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> InboundOutboundRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
DbPurchasePlan.id == payload.purchase_plan_id,
|
||||
DbPurchasePlan.tenant_no == tenant_no,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
if not plan:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Plan not found")
|
||||
|
||||
if not plan.product_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="计划未关联 CRM 产品,请先编辑计划并选择产品",
|
||||
)
|
||||
|
||||
row = DbInboundOutbound(
|
||||
id=next_inbound_outbound_id(db, tenant_no),
|
||||
tenant_no=tenant_no,
|
||||
date=EMPTY_INVENTORY_DATE,
|
||||
partner_company=(plan.customer_name or "").strip() or "待填写",
|
||||
business_type=plan.business_type,
|
||||
product_id=plan.product_id,
|
||||
product=(plan.product or "").strip(),
|
||||
specification=plan.specification,
|
||||
quantity=0.0,
|
||||
plan_no=(plan.plan_no or "").strip() or None,
|
||||
batch_no=_generate_batch_no(db, tenant_no),
|
||||
vehicle_no="",
|
||||
status=STATUS_THIRD_PARTY_FILL,
|
||||
remark=payload.remarks,
|
||||
purchase_plan_id=plan.id,
|
||||
create_by=user.id,
|
||||
create_dept_id=user.dept_id,
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return inventory_to_read(row)
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=InboundOutboundRead)
|
||||
def get_inventory(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
) -> InboundOutboundRead:
|
||||
"""单条查询(三方 H5 填写页无需门户登录)。"""
|
||||
row = _get_inventory_by_id(db, id)
|
||||
weighing = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.inbound_outbound_id == row.id,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
)
|
||||
.order_by(DbWeighingSlip.id.asc())
|
||||
.first()
|
||||
)
|
||||
return inventory_to_read(row, weighing=weighing)
|
||||
|
||||
|
||||
@router.patch("/{id}", response_model=InboundOutboundRead)
|
||||
def update_inventory(
|
||||
id: str,
|
||||
payload: InboundOutboundUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
) -> InboundOutboundRead:
|
||||
"""更新(三方 H5 填写页无需门户登录)。"""
|
||||
row = _get_inventory_by_id(db, id)
|
||||
payload_dict = payload.model_dump(exclude_unset=True)
|
||||
|
||||
vehicle_no_updated = payload_dict.get("vehicle_no")
|
||||
if vehicle_no_updated is not None and str(vehicle_no_updated).strip():
|
||||
_ensure_vehicle_exists(db, row.tenant_no, str(vehicle_no_updated).strip())
|
||||
|
||||
_apply_inventory_product_update(db, row.tenant_no, row, payload_dict)
|
||||
_apply_inventory_update(row, payload_dict)
|
||||
_maybe_promote_third_party_status(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return inventory_to_read(row)
|
||||
|
||||
|
||||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_inventory(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_inventory(db, tenant_no, id)
|
||||
row.del_flag = 1
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
|
||||
|
||||
class InventoryRelatedData(BaseModel):
|
||||
plan: Optional[PurchasePlanRead]
|
||||
weighing: Optional[WeighingSlipRead]
|
||||
|
||||
|
||||
@router.get("/{id}/related", response_model=InventoryRelatedData)
|
||||
def get_inventory_related(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
) -> InventoryRelatedData:
|
||||
row = _get_inventory_by_id(db, id)
|
||||
|
||||
plan = None
|
||||
if row.purchase_plan_id:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
DbPurchasePlan.id == row.purchase_plan_id,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
weighing = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.inbound_outbound_id == row.id,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
)
|
||||
.limit(1)
|
||||
.first()
|
||||
)
|
||||
|
||||
return InventoryRelatedData(
|
||||
plan=plan_to_read(plan) if plan else None,
|
||||
weighing=weighing_to_read(weighing) if weighing else None,
|
||||
)
|
||||
@@ -0,0 +1,475 @@
|
||||
from typing import Optional
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbWeighingSlip
|
||||
from app.schemas.db.inventory import InboundOutboundRead
|
||||
from app.schemas.db.plans import (
|
||||
PurchasePlanCreate,
|
||||
PurchasePlanRead,
|
||||
PurchasePlanUpdate,
|
||||
PurchasePlanWithFillLink,
|
||||
)
|
||||
from app.schemas.db.weighing import WeighingSlipRead
|
||||
from app.services.db.mappers import (
|
||||
inventory_to_read,
|
||||
parse_date_str,
|
||||
parse_datetime_str,
|
||||
plan_to_read,
|
||||
plan_to_read_with_fill_link,
|
||||
weighing_to_read,
|
||||
)
|
||||
from app.services.db.product_helpers import (
|
||||
apply_product_snapshot_to_plan,
|
||||
resolve_crm_product,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/plans", tags=["plans"])
|
||||
|
||||
STATUS_THIRD_PARTY_FILL = "待三方填写"
|
||||
|
||||
_PLAN_DATE_FIELDS = {"plan_date", "start_date", "end_date"}
|
||||
_PLAN_REMARK_FIELD = "remarks"
|
||||
|
||||
|
||||
def _base_query(db: Session, tenant_no: str):
|
||||
return db.query(DbPurchasePlan).filter(
|
||||
DbPurchasePlan.tenant_no == tenant_no,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[PurchasePlanWithFillLink])
|
||||
def list_plans(
|
||||
q: Optional[str] = Query(default=None, description="模糊搜索往来单位或产品"),
|
||||
plan_no: Optional[str] = Query(default=None, alias="planNo", description="计划号"),
|
||||
customer_name: Optional[str] = Query(
|
||||
default=None, alias="customerName", description="往来单位"
|
||||
),
|
||||
product: Optional[str] = Query(default=None, description="产品"),
|
||||
business_type: Optional[str] = Query(default=None, alias="businessType", description="业务类型"),
|
||||
execution_status: Optional[str] = Query(
|
||||
default=None, alias="executionStatus", description="执行状态"
|
||||
),
|
||||
created_from: Optional[str] = Query(default=None, alias="createdFrom", description="创建开始日期"),
|
||||
created_to: Optional[str] = Query(default=None, alias="createdTo", description="创建结束日期"),
|
||||
start_date_from: Optional[str] = Query(
|
||||
default=None, alias="startDateFrom", description="计划起始日期开始"
|
||||
),
|
||||
start_date_to: Optional[str] = Query(
|
||||
default=None, alias="startDateTo", description="计划起始日期结束"
|
||||
),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[PurchasePlanWithFillLink]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(DbPurchasePlan.create_time.desc())
|
||||
|
||||
if q:
|
||||
keyword = q.lower()
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.lower(DbPurchasePlan.customer_name).contains(keyword),
|
||||
func.lower(DbPurchasePlan.product).contains(keyword),
|
||||
func.lower(DbPurchasePlan.plan_no).contains(keyword),
|
||||
)
|
||||
)
|
||||
|
||||
if plan_no:
|
||||
query = query.filter(DbPurchasePlan.plan_no.contains(plan_no))
|
||||
|
||||
if customer_name:
|
||||
query = query.filter(
|
||||
func.lower(DbPurchasePlan.customer_name).contains(customer_name.lower())
|
||||
)
|
||||
|
||||
if product:
|
||||
query = query.filter(func.lower(DbPurchasePlan.product).contains(product.lower()))
|
||||
|
||||
if business_type:
|
||||
query = query.filter(DbPurchasePlan.business_type == business_type)
|
||||
|
||||
if execution_status:
|
||||
query = query.filter(DbPurchasePlan.execution_status == execution_status)
|
||||
|
||||
if created_from:
|
||||
query = query.filter(DbPurchasePlan.create_time >= created_from)
|
||||
|
||||
if created_to:
|
||||
query = query.filter(DbPurchasePlan.create_time <= f"{created_to} 23:59:59")
|
||||
|
||||
if start_date_from:
|
||||
start_from = parse_date_str(start_date_from)
|
||||
if start_from:
|
||||
query = query.filter(DbPurchasePlan.start_date >= start_from)
|
||||
|
||||
if start_date_to:
|
||||
start_to = parse_date_str(start_date_to)
|
||||
if start_to:
|
||||
query = query.filter(DbPurchasePlan.start_date <= start_to)
|
||||
|
||||
plans = query.all()
|
||||
if not plans:
|
||||
return []
|
||||
|
||||
plan_ids = [p.id for p in plans]
|
||||
fill_links = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.purchase_plan_id.in_(plan_ids),
|
||||
DbInboundOutbound.status == STATUS_THIRD_PARTY_FILL,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
plan_id_to_inv_id = {
|
||||
inv.purchase_plan_id: inv.id for inv in fill_links if inv.purchase_plan_id
|
||||
}
|
||||
|
||||
plan_nos = [p.plan_no for p in plans if p.plan_no]
|
||||
executed_sum_map: dict[tuple[Optional[str], Optional[str]], float] = {}
|
||||
|
||||
if plan_ids or plan_nos:
|
||||
weighing_filters = [DbWeighingSlip.is_voided == 0, DbWeighingSlip.del_flag == 0]
|
||||
weighing_filters.append(DbWeighingSlip.tenant_no == tenant_no)
|
||||
id_filters = []
|
||||
if plan_ids:
|
||||
id_filters.append(DbWeighingSlip.purchase_plan_id.in_(plan_ids))
|
||||
if plan_nos:
|
||||
id_filters.append(DbWeighingSlip.plan_no.in_(plan_nos))
|
||||
weighing_filters.append(or_(*id_filters))
|
||||
|
||||
weighing_rows = (
|
||||
db.query(
|
||||
DbWeighingSlip.purchase_plan_id,
|
||||
DbWeighingSlip.plan_no,
|
||||
func.sum(DbWeighingSlip.net_weight).label("total_executed"),
|
||||
)
|
||||
.filter(*weighing_filters)
|
||||
.group_by(DbWeighingSlip.purchase_plan_id, DbWeighingSlip.plan_no)
|
||||
.all()
|
||||
)
|
||||
for row in weighing_rows:
|
||||
key = (row.purchase_plan_id, row.plan_no)
|
||||
executed_sum_map[key] = float(row.total_executed or 0)
|
||||
|
||||
def calc_executed_for_plan(plan: DbPurchasePlan) -> float:
|
||||
if not executed_sum_map:
|
||||
return 0.0
|
||||
total = 0.0
|
||||
for (pid, pno), executed in executed_sum_map.items():
|
||||
if pid is not None and pid == plan.id:
|
||||
total += executed
|
||||
elif pno and plan.plan_no and pno == plan.plan_no:
|
||||
total += executed
|
||||
return total
|
||||
|
||||
result_list: list[PurchasePlanWithFillLink] = []
|
||||
for plan in plans:
|
||||
result_list.append(
|
||||
plan_to_read_with_fill_link(
|
||||
plan,
|
||||
executed_quantity=calc_executed_for_plan(plan),
|
||||
fill_link_inventory_id=plan_id_to_inv_id.get(plan.id),
|
||||
)
|
||||
)
|
||||
return result_list
|
||||
|
||||
|
||||
def _generate_plan_no() -> str:
|
||||
from app.services.db.plan_helpers import generate_plan_no
|
||||
|
||||
return generate_plan_no()
|
||||
|
||||
|
||||
@router.post("/", response_model=PurchasePlanRead, status_code=status.HTTP_201_CREATED)
|
||||
def create_plan(
|
||||
payload: PurchasePlanCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> PurchasePlanRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan_no = payload.plan_no if payload.plan_no else _generate_plan_no()
|
||||
|
||||
execution_status = payload.execution_status
|
||||
if not execution_status:
|
||||
execution_status = "待入库" if payload.business_type == "入库" else "待出库"
|
||||
|
||||
snapshot = resolve_crm_product(db, tenant_no, payload.product_id)
|
||||
|
||||
plan = DbPurchasePlan(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
plan_no=plan_no,
|
||||
plan_date=parse_date_str(payload.plan_date) or date.today(),
|
||||
start_date=parse_date_str(payload.start_date),
|
||||
end_date=parse_date_str(payload.end_date),
|
||||
business_type=payload.business_type,
|
||||
planned_quantity=payload.planned_quantity,
|
||||
vehicle_no=payload.vehicle_no or "",
|
||||
customer_name=payload.customer_name.strip() if payload.customer_name else None,
|
||||
remark=payload.remarks,
|
||||
execution_status=execution_status,
|
||||
executed_at=None,
|
||||
last_weighing_slip_id=None,
|
||||
executed_quantity=0,
|
||||
auto_generate_enabled=payload.auto_generate_enabled or 0,
|
||||
auto_generate_end_month=payload.auto_generate_end_month,
|
||||
auto_generate_last_month=None,
|
||||
create_by=user.id,
|
||||
create_dept_id=user.dept_id,
|
||||
)
|
||||
apply_product_snapshot_to_plan(plan, snapshot)
|
||||
db.add(plan)
|
||||
db.commit()
|
||||
db.refresh(plan)
|
||||
return plan_to_read(plan)
|
||||
|
||||
|
||||
class AutoGenerateMonthlyResult(BaseModel):
|
||||
created_count: int
|
||||
skipped_reason: Optional[str] = None
|
||||
current_month: str
|
||||
|
||||
|
||||
@router.post("/run-auto-generate", response_model=AutoGenerateMonthlyResult)
|
||||
def run_auto_generate_monthly(
|
||||
force: bool = Query(default=False, description="true 时跳过「仅每月1日」限制,供验收手动触发"),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> AutoGenerateMonthlyResult:
|
||||
"""手动触发计划按月自动生成(定时任务同款逻辑)。"""
|
||||
get_tenant_no(db, user.to_tenant_user_dict())
|
||||
from app.services.db.auto_generate_plans import run_auto_generate_monthly_plans
|
||||
|
||||
result = run_auto_generate_monthly_plans(db, force=force)
|
||||
return AutoGenerateMonthlyResult(**result)
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=PurchasePlanRead)
|
||||
def get_plan(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> PurchasePlanRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id)
|
||||
return plan_to_read(plan)
|
||||
|
||||
|
||||
@router.patch("/{id}", response_model=PurchasePlanRead)
|
||||
def update_plan(
|
||||
id: str,
|
||||
payload: PurchasePlanUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> PurchasePlanRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id)
|
||||
payload_dict = payload.model_dump(exclude_unset=True)
|
||||
_apply_plan_product_update(db, tenant_no, plan, payload_dict)
|
||||
_apply_plan_update(plan, payload_dict)
|
||||
plan.update_by = user.id
|
||||
db.commit()
|
||||
db.refresh(plan)
|
||||
return plan_to_read(plan)
|
||||
|
||||
|
||||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_plan(
|
||||
id: str,
|
||||
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)
|
||||
plan.del_flag = 1
|
||||
plan.update_by = user.id
|
||||
db.commit()
|
||||
|
||||
|
||||
def _get_plan(db: Session, tenant_no: str, plan_id: str) -> DbPurchasePlan:
|
||||
plan = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(DbPurchasePlan.id == plan_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if plan is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Plan {plan_id} not found",
|
||||
)
|
||||
return plan
|
||||
|
||||
|
||||
def _get_plan_by_id_only(db: Session, plan_id: str) -> DbPurchasePlan:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(DbPurchasePlan.id == plan_id, DbPurchasePlan.del_flag == 0)
|
||||
.one_or_none()
|
||||
)
|
||||
if plan is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Plan {plan_id} not found",
|
||||
)
|
||||
return plan
|
||||
|
||||
|
||||
def _apply_plan_product_update(
|
||||
db: Session, tenant_no: str, plan: DbPurchasePlan, payload_dict: dict
|
||||
) -> None:
|
||||
if "product" in payload_dict and "product_id" not in payload_dict:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请从 CRM 重新选择产品",
|
||||
)
|
||||
product_id = payload_dict.pop("product_id", None)
|
||||
payload_dict.pop("product", None)
|
||||
payload_dict.pop("specification", None)
|
||||
if product_id is not None:
|
||||
snapshot = resolve_crm_product(db, tenant_no, product_id)
|
||||
apply_product_snapshot_to_plan(plan, snapshot)
|
||||
|
||||
|
||||
def _apply_plan_update(plan: DbPurchasePlan, payload_dict: dict) -> None:
|
||||
for key, value in payload_dict.items():
|
||||
if key == _PLAN_REMARK_FIELD:
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
plan.remark = value
|
||||
continue
|
||||
if key in _PLAN_DATE_FIELDS:
|
||||
if value is None:
|
||||
setattr(plan, key, None)
|
||||
else:
|
||||
setattr(plan, key, parse_date_str(str(value)))
|
||||
continue
|
||||
if key == "executed_at":
|
||||
plan.executed_at = parse_datetime_str(str(value)) if value else None
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
setattr(plan, key, value)
|
||||
|
||||
|
||||
class PlanRelatedData(BaseModel):
|
||||
inventories: list[InboundOutboundRead]
|
||||
weighings: list[WeighingSlipRead]
|
||||
|
||||
|
||||
@router.get("/{id}/related", response_model=PlanRelatedData)
|
||||
def get_plan_related(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> PlanRelatedData:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
plan = _get_plan(db, tenant_no, id)
|
||||
|
||||
inventories = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.purchase_plan_id == plan.id,
|
||||
)
|
||||
.order_by(DbInboundOutbound.date.desc(), DbInboundOutbound.id.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
weighing_query = db.query(DbWeighingSlip).filter(
|
||||
DbWeighingSlip.tenant_no == tenant_no,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
)
|
||||
if plan.plan_no:
|
||||
weighing_query = weighing_query.filter(
|
||||
or_(
|
||||
DbWeighingSlip.plan_no == plan.plan_no,
|
||||
DbWeighingSlip.purchase_plan_id == plan.id,
|
||||
)
|
||||
)
|
||||
else:
|
||||
weighing_query = weighing_query.filter(DbWeighingSlip.purchase_plan_id == plan.id)
|
||||
weighings = weighing_query.order_by(DbWeighingSlip.create_time.desc()).all()
|
||||
|
||||
return PlanRelatedData(
|
||||
inventories=[inventory_to_read(inv) for inv in inventories],
|
||||
weighings=[weighing_to_read(w) for w in weighings],
|
||||
)
|
||||
|
||||
|
||||
class ThirdPartyPlanPageData(BaseModel):
|
||||
plan: PurchasePlanRead
|
||||
today_inventories: list[InboundOutboundRead]
|
||||
month_inventories: list[InboundOutboundRead]
|
||||
|
||||
|
||||
@router.get("/{id}/third-party-page", response_model=ThirdPartyPlanPageData)
|
||||
def get_third_party_plan_page(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
) -> ThirdPartyPlanPageData:
|
||||
"""三方 H5 填写页初始化数据(无需门户登录,与原系统一致)。"""
|
||||
plan = _get_plan_by_id_only(db, id)
|
||||
tenant_no = plan.tenant_no
|
||||
|
||||
today = date.today()
|
||||
start = plan.start_date
|
||||
end = plan.end_date
|
||||
|
||||
if start and today < start:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="计划尚未生效,当前日期不允许填写出入库单",
|
||||
)
|
||||
if end and today > end:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="计划已过期,当前日期不允许填写出入库单",
|
||||
)
|
||||
|
||||
today_str = today.strftime("%Y-%m-%d")
|
||||
today_date = parse_date_str(today_str)
|
||||
inventories = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.purchase_plan_id == plan.id,
|
||||
DbInboundOutbound.date == today_date,
|
||||
)
|
||||
.order_by(DbInboundOutbound.date.desc(), DbInboundOutbound.id.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
month_start = today.replace(day=1)
|
||||
month_history = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
DbInboundOutbound.purchase_plan_id == plan.id,
|
||||
DbInboundOutbound.date >= month_start,
|
||||
DbInboundOutbound.date < today,
|
||||
)
|
||||
.order_by(DbInboundOutbound.date.desc(), DbInboundOutbound.id.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
return ThirdPartyPlanPageData(
|
||||
plan=plan_to_read(plan),
|
||||
today_inventories=[inventory_to_read(inv) for inv in inventories],
|
||||
month_inventories=[inventory_to_read(inv) for inv in month_history],
|
||||
)
|
||||
@@ -0,0 +1,146 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbVehicle
|
||||
from app.schemas.db.master_data import VehicleCreate, VehicleRead, VehicleUpdate
|
||||
from app.services.db.mappers import vehicle_to_read
|
||||
|
||||
router = APIRouter(prefix="/master-data/vehicles", tags=["vehicles"])
|
||||
|
||||
|
||||
def _base_query(db: Session, tenant_no: str):
|
||||
return db.query(DbVehicle).filter(
|
||||
DbVehicle.tenant_no == tenant_no,
|
||||
DbVehicle.del_flag == 0,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[VehicleRead])
|
||||
def list_vehicles(
|
||||
q: Optional[str] = Query(default=None, description="模糊搜索车牌号"),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[VehicleRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(DbVehicle.create_time.desc())
|
||||
if q:
|
||||
keyword = q.lower()
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.lower(DbVehicle.vehicle_no).contains(keyword),
|
||||
func.lower(DbVehicle.remark).contains(keyword),
|
||||
)
|
||||
)
|
||||
rows = query.all()
|
||||
return [vehicle_to_read(row) for row in rows]
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}", response_model=VehicleRead)
|
||||
def get_vehicle(
|
||||
vehicle_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.post("/", response_model=VehicleRead, status_code=status.HTTP_201_CREATED)
|
||||
def create_vehicle(
|
||||
payload: VehicleCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
_ensure_unique_vehicle_no(db, tenant_no, payload.vehicle_no)
|
||||
row = DbVehicle(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
vehicle_no=payload.vehicle_no.strip(),
|
||||
remark=payload.remarks,
|
||||
create_by=user.id,
|
||||
create_dept_id=user.dept_id,
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.put("/{vehicle_id}", response_model=VehicleRead)
|
||||
def update_vehicle(
|
||||
vehicle_id: str,
|
||||
payload: VehicleUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
if payload.vehicle_no and payload.vehicle_no.strip().lower() != row.vehicle_no.lower():
|
||||
_ensure_unique_vehicle_no(db, tenant_no, payload.vehicle_no)
|
||||
row.vehicle_no = payload.vehicle_no.strip()
|
||||
if payload.remarks is not None:
|
||||
row.remark = payload.remarks
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return vehicle_to_read(row)
|
||||
|
||||
|
||||
@router.patch("/{vehicle_id}", response_model=VehicleRead)
|
||||
def patch_vehicle(
|
||||
vehicle_id: str,
|
||||
payload: VehicleUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> VehicleRead:
|
||||
return update_vehicle(vehicle_id, payload, db, user)
|
||||
|
||||
|
||||
@router.delete("/{vehicle_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_vehicle(
|
||||
vehicle_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_vehicle(db, tenant_no, vehicle_id)
|
||||
row.del_flag = 1
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
|
||||
|
||||
def _get_vehicle(db: Session, tenant_no: str, vehicle_id: str) -> DbVehicle:
|
||||
row = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(DbVehicle.id == vehicle_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Vehicle {vehicle_id} not found",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _ensure_unique_vehicle_no(db: Session, tenant_no: str, vehicle_no: str) -> None:
|
||||
normalized = vehicle_no.strip().lower()
|
||||
exists = (
|
||||
_base_query(db, tenant_no)
|
||||
.filter(func.lower(DbVehicle.vehicle_no) == normalized)
|
||||
.first()
|
||||
)
|
||||
if exists:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Vehicle number already exists",
|
||||
)
|
||||
@@ -0,0 +1,414 @@
|
||||
from typing import Optional
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.db_auth import DbUserContext, get_db_current_user
|
||||
from app.core.tenant_context import get_tenant_no
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbWeighingSlip
|
||||
from app.schemas.db.inventory import InboundOutboundRead
|
||||
from app.schemas.db.plans import PurchasePlanRead
|
||||
from app.schemas.db.weighing import WeighingSlipCreate, WeighingSlipRead, WeighingSlipUpdate
|
||||
from app.services.db.mappers import (
|
||||
datetime_end_of_day,
|
||||
inventory_date_to_api,
|
||||
inventory_to_read,
|
||||
parse_date_str,
|
||||
parse_datetime_str,
|
||||
plan_to_read,
|
||||
weighing_to_read,
|
||||
)
|
||||
from app.services.db.weighing_logic import process_weighing_business_logic
|
||||
from app.services.db.product_helpers import (
|
||||
apply_product_snapshot_to_weighing,
|
||||
resolve_crm_product,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/weighing", tags=["weighing"])
|
||||
|
||||
_WEIGHING_REMARK_FIELD = "remarks"
|
||||
|
||||
|
||||
def _base_query(db: Session, tenant_no: str):
|
||||
return db.query(DbWeighingSlip).filter(
|
||||
DbWeighingSlip.tenant_no == tenant_no,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
)
|
||||
|
||||
|
||||
def _get_weighing(db: Session, tenant_no: str, weighing_id: str) -> DbWeighingSlip:
|
||||
row = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.id == weighing_id,
|
||||
DbWeighingSlip.tenant_no == tenant_no,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Weighing {weighing_id} not found",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _apply_weighing_product_update(
|
||||
db: Session, tenant_no: str, row: DbWeighingSlip, payload_dict: dict
|
||||
) -> None:
|
||||
if "cargo_name" in payload_dict and "product_id" not in payload_dict:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请从 CRM 重新选择产品",
|
||||
)
|
||||
product_id = payload_dict.pop("product_id", None)
|
||||
payload_dict.pop("cargo_name", None)
|
||||
payload_dict.pop("specification", None)
|
||||
if product_id is not None:
|
||||
snapshot = resolve_crm_product(db, tenant_no, product_id)
|
||||
apply_product_snapshot_to_weighing(row, snapshot)
|
||||
|
||||
|
||||
def _apply_weighing_update(row: DbWeighingSlip, payload_dict: dict) -> None:
|
||||
for key, value in payload_dict.items():
|
||||
if key == _WEIGHING_REMARK_FIELD:
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
row.remark = value
|
||||
continue
|
||||
if key in {"gross_weight_time", "tare_weight_time"}:
|
||||
if value is None or (isinstance(value, str) and not str(value).strip()):
|
||||
if key == "tare_weight_time":
|
||||
row.tare_weight_time = None
|
||||
else:
|
||||
parsed = parse_datetime_str(str(value))
|
||||
if parsed:
|
||||
setattr(row, key, parsed)
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
setattr(row, key, value)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[WeighingSlipRead])
|
||||
def list_weighing(
|
||||
q: Optional[str] = Query(default=None, description="模糊搜索关键词"),
|
||||
date_from: Optional[str] = Query(default=None, alias="dateFrom", description="开始日期"),
|
||||
date_to: Optional[str] = Query(default=None, alias="dateTo", description="结束日期"),
|
||||
plan_no: Optional[str] = Query(default=None, alias="planNo", description="计划号"),
|
||||
batch_no: Optional[str] = Query(default=None, alias="batchNo", description="出入库单号"),
|
||||
vehicle_no: Optional[str] = Query(default=None, alias="vehicleNo", description="车牌号"),
|
||||
cargo_name: Optional[str] = Query(default=None, alias="cargoName", description="货物名称"),
|
||||
supplier: Optional[str] = Query(default=None, description="供货单位"),
|
||||
consignee: Optional[str] = Query(default=None, description="收货单位"),
|
||||
business_type: Optional[str] = Query(default=None, alias="businessType", description="业务类型"),
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> list[WeighingSlipRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(DbWeighingSlip.gross_weight_time.desc())
|
||||
|
||||
if q:
|
||||
keyword = q.lower()
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.lower(DbWeighingSlip.vehicle_no).contains(keyword),
|
||||
func.lower(DbWeighingSlip.plan_no).contains(keyword),
|
||||
func.lower(DbWeighingSlip.batch_no).contains(keyword),
|
||||
func.lower(DbWeighingSlip.cargo_name).contains(keyword),
|
||||
func.lower(DbWeighingSlip.supplier).contains(keyword),
|
||||
func.lower(DbWeighingSlip.consignee).contains(keyword),
|
||||
)
|
||||
)
|
||||
|
||||
if date_from:
|
||||
start_date = parse_date_str(date_from)
|
||||
if start_date:
|
||||
query = query.filter(
|
||||
DbWeighingSlip.gross_weight_time >= datetime.combine(start_date, datetime.min.time())
|
||||
)
|
||||
|
||||
if date_to:
|
||||
end_date = parse_date_str(date_to)
|
||||
if end_date:
|
||||
query = query.filter(DbWeighingSlip.gross_weight_time <= datetime_end_of_day(end_date))
|
||||
|
||||
if plan_no:
|
||||
query = query.filter(DbWeighingSlip.plan_no.contains(plan_no))
|
||||
|
||||
if batch_no:
|
||||
query = query.filter(DbWeighingSlip.batch_no.contains(batch_no))
|
||||
|
||||
if vehicle_no:
|
||||
query = query.filter(DbWeighingSlip.vehicle_no.contains(vehicle_no))
|
||||
|
||||
if cargo_name:
|
||||
query = query.filter(DbWeighingSlip.cargo_name.contains(cargo_name))
|
||||
|
||||
if supplier:
|
||||
query = query.filter(func.lower(DbWeighingSlip.supplier).contains(supplier.lower()))
|
||||
|
||||
if consignee:
|
||||
query = query.filter(func.lower(DbWeighingSlip.consignee).contains(consignee.lower()))
|
||||
|
||||
if business_type:
|
||||
query = query.filter(DbWeighingSlip.business_type == business_type)
|
||||
|
||||
weighings = query.all()
|
||||
inventory_ids = [w.inbound_outbound_id for w in weighings if w.inbound_outbound_id]
|
||||
inventory_date_map: dict[str, str] = {}
|
||||
if inventory_ids:
|
||||
inventories = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.id.in_(inventory_ids),
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for inventory in inventories:
|
||||
inventory_date_map[inventory.id] = inventory_date_to_api(inventory.date)
|
||||
|
||||
return [
|
||||
weighing_to_read(
|
||||
weighing,
|
||||
inventory_date=inventory_date_map.get(weighing.inbound_outbound_id or ""),
|
||||
)
|
||||
for weighing in weighings
|
||||
]
|
||||
|
||||
|
||||
@router.post("/", response_model=WeighingSlipRead, status_code=status.HTTP_201_CREATED)
|
||||
def create_weighing(
|
||||
payload: WeighingSlipCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> WeighingSlipRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
inbound_outbound_id = payload.inbound_outbound_id
|
||||
|
||||
if inbound_outbound_id:
|
||||
existing = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.inbound_outbound_id == inbound_outbound_id,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="该出入库单已经存在关联的地磅单,无法重复创建",
|
||||
)
|
||||
|
||||
planned_quantity = payload.planned_quantity
|
||||
current_quantity = payload.current_quantity
|
||||
purchase_plan_id = payload.purchase_plan_id
|
||||
batch_no = payload.batch_no.strip() if payload.batch_no else None
|
||||
|
||||
if inbound_outbound_id:
|
||||
inventory = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.id == inbound_outbound_id,
|
||||
DbInboundOutbound.tenant_no == tenant_no,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
if inventory:
|
||||
if current_quantity is None:
|
||||
current_quantity = inventory.quantity * 1000
|
||||
if purchase_plan_id is None and inventory.purchase_plan_id:
|
||||
purchase_plan_id = inventory.purchase_plan_id
|
||||
if purchase_plan_id:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(DbPurchasePlan.id == purchase_plan_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if plan and planned_quantity is None:
|
||||
planned_quantity = plan.planned_quantity * 1000
|
||||
if not batch_no and inventory.batch_no:
|
||||
batch_no = inventory.batch_no
|
||||
|
||||
gross_time = parse_datetime_str(payload.gross_weight_time) if payload.gross_weight_time else None
|
||||
tare_time = (
|
||||
parse_datetime_str(payload.tare_weight_time)
|
||||
if payload.tare_weight_time and payload.tare_weight_time.strip()
|
||||
else None
|
||||
)
|
||||
|
||||
snapshot = resolve_crm_product(db, tenant_no, payload.product_id)
|
||||
|
||||
row = DbWeighingSlip(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
vehicle_no=payload.vehicle_no.strip(),
|
||||
plan_no=payload.plan_no.strip() if payload.plan_no else None,
|
||||
business_type=payload.business_type,
|
||||
batch_no=batch_no,
|
||||
supplier=payload.supplier.strip(),
|
||||
consignee=payload.consignee.strip(),
|
||||
gross_weight=payload.gross_weight or 0,
|
||||
gross_weight_time=gross_time or datetime.now(),
|
||||
tare_weight=payload.tare_weight or 0,
|
||||
tare_weight_time=tare_time,
|
||||
deduction=payload.deduction or 0,
|
||||
net_weight=payload.net_weight or 0,
|
||||
planned_quantity=planned_quantity,
|
||||
executed_quantity=payload.executed_quantity,
|
||||
purchase_plan_id=purchase_plan_id,
|
||||
inbound_outbound_id=inbound_outbound_id,
|
||||
weighing_point=payload.weighing_point or "山峡化工",
|
||||
remark=payload.remarks,
|
||||
current_quantity=current_quantity,
|
||||
is_voided=0,
|
||||
create_by=user.id,
|
||||
create_dept_id=user.dept_id,
|
||||
)
|
||||
apply_product_snapshot_to_weighing(row, snapshot)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
|
||||
process_weighing_business_logic(db, row)
|
||||
db.refresh(row)
|
||||
return weighing_to_read(row)
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=WeighingSlipRead)
|
||||
def get_weighing(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> WeighingSlipRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_weighing(db, tenant_no, id)
|
||||
return weighing_to_read(row)
|
||||
|
||||
|
||||
@router.patch("/{id}", response_model=WeighingSlipRead)
|
||||
def update_weighing(
|
||||
id: str,
|
||||
payload: WeighingSlipUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> WeighingSlipRead:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_weighing(db, tenant_no, id)
|
||||
|
||||
if row.is_voided == 1:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot update voided weighing record",
|
||||
)
|
||||
|
||||
payload_dict = payload.model_dump(exclude_unset=True)
|
||||
_apply_weighing_product_update(db, tenant_no, row, payload_dict)
|
||||
_apply_weighing_update(row, payload_dict)
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
|
||||
process_weighing_business_logic(db, row)
|
||||
db.refresh(row)
|
||||
return weighing_to_read(row)
|
||||
|
||||
|
||||
@router.post("/{id}/void", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def void_weighing(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_weighing(db, tenant_no, id)
|
||||
|
||||
if row.is_voided == 1:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Weighing record is already voided",
|
||||
)
|
||||
|
||||
row.is_voided = 1
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
process_weighing_business_logic(db, row)
|
||||
|
||||
|
||||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_weighing(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> None:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_weighing(db, tenant_no, id)
|
||||
row.is_voided = 1
|
||||
row.update_by = user.id
|
||||
db.commit()
|
||||
process_weighing_business_logic(db, row)
|
||||
|
||||
|
||||
class WeighingRelatedData(BaseModel):
|
||||
plan: Optional[PurchasePlanRead]
|
||||
inventory: Optional[InboundOutboundRead]
|
||||
|
||||
|
||||
@router.get("/{id}/related", response_model=WeighingRelatedData)
|
||||
def get_weighing_related(
|
||||
id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: DbUserContext = Depends(get_db_current_user),
|
||||
) -> WeighingRelatedData:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
row = _get_weighing(db, tenant_no, id)
|
||||
|
||||
plan = None
|
||||
if row.purchase_plan_id:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
DbPurchasePlan.id == row.purchase_plan_id,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
elif row.plan_no:
|
||||
plan = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
DbPurchasePlan.plan_no == row.plan_no,
|
||||
DbPurchasePlan.tenant_no == tenant_no,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
)
|
||||
.limit(1)
|
||||
.first()
|
||||
)
|
||||
|
||||
inventory = None
|
||||
if row.inbound_outbound_id:
|
||||
inventory = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(
|
||||
DbInboundOutbound.id == row.inbound_outbound_id,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
return WeighingRelatedData(
|
||||
plan=plan_to_read(plan) if plan else None,
|
||||
inventory=inventory_to_read(inventory) if inventory else None,
|
||||
)
|
||||
@@ -140,6 +140,7 @@ class Settings(BaseSettings):
|
||||
"/api/web/admin/users/upload-avatar,"
|
||||
"/api/invoice/download,"
|
||||
"/api/bx/health,"
|
||||
"/api/db/health,"
|
||||
"/api/expense-items,"
|
||||
"/api/workflows,"
|
||||
"/api/advances,"
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""地磅模块认证适配:门户 JWT 用户 → db 业务上下文。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from fastapi import Depends, HTTPException
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.auth import get_current_user_by_token
|
||||
from app.core.database import get_db
|
||||
from app.core.menu_guard import ADMIN_ROLES, require_admin_or_super_admin
|
||||
|
||||
optional_oauth2_scheme = OAuth2PasswordBearer(
|
||||
tokenUrl="/api/public/login",
|
||||
auto_error=False,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class DbUserContext:
|
||||
"""db Router/Service 使用的当前用户上下文。"""
|
||||
|
||||
id: str
|
||||
name: str
|
||||
dept_id: str
|
||||
role: str
|
||||
tenant_no: str
|
||||
|
||||
def to_tenant_user_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"user_id": self.id,
|
||||
"role": self.role,
|
||||
"tenant_no": self.tenant_no,
|
||||
}
|
||||
|
||||
|
||||
def get_db_current_user(
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
) -> DbUserContext:
|
||||
return DbUserContext(
|
||||
id=(user.get("user_id") or "").strip(),
|
||||
name=(user.get("real_name") or user.get("username") or "").strip(),
|
||||
dept_id=(user.get("region_id") or "").strip(),
|
||||
role=(user.get("role") or "").strip(),
|
||||
tenant_no=(user.get("tenant_no") or "").strip(),
|
||||
)
|
||||
|
||||
|
||||
async def get_db_current_user_optional(
|
||||
token: Optional[str] = Depends(optional_oauth2_scheme),
|
||||
db: Session = Depends(get_db),
|
||||
) -> DbUserContext | None:
|
||||
"""有 Bearer 且为 admin/super_admin 时返回上下文,否则 None(供三方 H5 无登录调用)。"""
|
||||
if not token:
|
||||
return None
|
||||
try:
|
||||
user = await get_current_user_by_token(token, db)
|
||||
except HTTPException:
|
||||
return None
|
||||
role = user.get("role")
|
||||
if role not in ADMIN_ROLES:
|
||||
return None
|
||||
return DbUserContext(
|
||||
id=(user.get("user_id") or "").strip(),
|
||||
name=(user.get("real_name") or user.get("username") or "").strip(),
|
||||
dept_id=(user.get("region_id") or "").strip(),
|
||||
role=(role or "").strip(),
|
||||
tenant_no=(user.get("tenant_no") or "").strip(),
|
||||
)
|
||||
@@ -14,6 +14,7 @@ from app.core.api_request_log_middleware import ApiRequestLogMiddleware
|
||||
from app.core.api_crypto_middleware import ApiCryptoMiddleware
|
||||
from app.models import models # noqa: F401
|
||||
import app.models.bx_models # noqa: F401
|
||||
import app.models.db_models # noqa: F401
|
||||
from app.api import (
|
||||
public_router,
|
||||
menu_router,
|
||||
@@ -23,6 +24,7 @@ from app.api import (
|
||||
crm_router,
|
||||
monitor_router,
|
||||
bx_router,
|
||||
db_router,
|
||||
)
|
||||
|
||||
|
||||
@@ -115,6 +117,7 @@ app.include_router(workflow_router, prefix=settings.API_V1_PREFIX)
|
||||
app.include_router(leave_router, prefix=settings.API_V1_PREFIX)
|
||||
app.include_router(crm_router, prefix=settings.API_V1_PREFIX)
|
||||
app.include_router(bx_router, prefix=settings.API_V1_PREFIX)
|
||||
app.include_router(db_router, prefix=settings.API_V1_PREFIX)
|
||||
app.include_router(monitor_router, prefix=settings.API_V1_PREFIX)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
"""地磅(db)模块数据模型 — 对齐 docs/sql/db_tables.sql"""
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, Date, DateTime, Float, Integer, SmallInteger, String
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class DbVehicle(Base):
|
||||
__tablename__ = "db_vehicle"
|
||||
|
||||
id = Column(String(64), primary_key=True, comment="主键ID(UUID)")
|
||||
vehicle_no = Column(String(255), nullable=False, comment="车牌号")
|
||||
tenant_no = Column(String(64), nullable=False, default="shanxia2.0", comment="租户编号")
|
||||
order_num = Column(Integer, nullable=True, comment="排序号")
|
||||
remark = Column(String(512), nullable=True, comment="备注")
|
||||
create_by = Column(String(64), nullable=True, comment="创建人")
|
||||
create_dept_id = Column(String(64), nullable=False, default="", comment="创建部门")
|
||||
create_time = Column(DateTime, nullable=False, default=datetime.now, comment="创建时间")
|
||||
update_by = Column(String(64), nullable=True, comment="更新人")
|
||||
update_time = Column(
|
||||
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment="更新时间"
|
||||
)
|
||||
del_flag = Column(SmallInteger, nullable=False, default=0, comment="删除标记")
|
||||
|
||||
|
||||
class DbPurchasePlan(Base):
|
||||
__tablename__ = "db_purchase_plan"
|
||||
|
||||
id = Column(String(64), primary_key=True, comment="主键ID(UUID)")
|
||||
plan_no = Column(String(255), nullable=True, comment="计划号")
|
||||
plan_date = Column(Date, nullable=False, comment="计划日期")
|
||||
start_date = Column(Date, nullable=True, comment="开始时间")
|
||||
end_date = Column(Date, nullable=True, comment="结束时间")
|
||||
business_type = Column(String(255), nullable=False, comment="业务类型")
|
||||
product_id = Column(String(64), nullable=True, comment="CRM产品ID")
|
||||
product = Column(String(255), nullable=False, comment="产品名称")
|
||||
specification = Column(String(255), nullable=True, comment="规格")
|
||||
planned_quantity = Column(Float, nullable=False, comment="计划数量(t)")
|
||||
vehicle_no = Column(String(255), nullable=True, default="", comment="车牌号")
|
||||
customer_name = Column(String(255), nullable=True, comment="往来单位")
|
||||
execution_status = Column(String(50), nullable=True, comment="执行状态")
|
||||
executed_at = Column(DateTime, nullable=True, comment="执行时间")
|
||||
last_weighing_slip_id = Column(String(64), nullable=True, comment="最后关联地磅单ID")
|
||||
executed_quantity = Column(Float, nullable=True, default=0, comment="已执行量(kg)")
|
||||
auto_generate_enabled = Column(SmallInteger, nullable=False, default=0, comment="是否自动按月生成")
|
||||
auto_generate_end_month = Column(String(7), nullable=True, comment="自动生成结束月份")
|
||||
auto_generate_last_month = Column(String(7), nullable=True, comment="最后一次自动生成月份")
|
||||
tenant_no = Column(String(64), nullable=False, default="shanxia2.0", comment="租户编号")
|
||||
order_num = Column(Integer, nullable=True, comment="排序号")
|
||||
remark = Column(String(512), nullable=True, comment="备注")
|
||||
create_by = Column(String(64), nullable=True, comment="创建人")
|
||||
create_dept_id = Column(String(64), nullable=False, default="", comment="创建部门")
|
||||
create_time = Column(DateTime, nullable=False, default=datetime.now, comment="创建时间")
|
||||
update_by = Column(String(64), nullable=True, comment="更新人")
|
||||
update_time = Column(
|
||||
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment="更新时间"
|
||||
)
|
||||
del_flag = Column(SmallInteger, nullable=False, default=0, comment="删除标记")
|
||||
|
||||
|
||||
class DbInboundOutbound(Base):
|
||||
__tablename__ = "db_inbound_outbound"
|
||||
|
||||
id = Column(String(64), primary_key=True, comment="主键ID(租户内纯数字字符串)")
|
||||
date = Column(Date, nullable=False, comment="日期")
|
||||
partner_company = Column(String(255), nullable=False, comment="往来单位")
|
||||
business_type = Column(String(255), nullable=False, comment="业务类型")
|
||||
product_id = Column(String(64), nullable=True, comment="CRM产品ID")
|
||||
product = Column(String(255), nullable=False, comment="产品")
|
||||
specification = Column(String(255), nullable=True, comment="规格")
|
||||
quantity = Column(Float, nullable=False, comment="本次量(t)")
|
||||
plan_no = Column(String(255), nullable=True, comment="计划号")
|
||||
batch_no = Column(String(255), nullable=True, comment="批次号")
|
||||
vehicle_no = Column(String(255), nullable=False, comment="车牌号")
|
||||
status = Column(String(50), nullable=True, comment="状态")
|
||||
purchase_plan_id = Column(String(64), nullable=True, comment="关联计划ID")
|
||||
tenant_no = Column(String(64), nullable=False, default="shanxia2.0", comment="租户编号")
|
||||
order_num = Column(Integer, nullable=True, comment="排序号")
|
||||
remark = Column(String(512), nullable=True, comment="备注")
|
||||
create_by = Column(String(64), nullable=True, comment="创建人")
|
||||
create_dept_id = Column(String(64), nullable=False, default="", comment="创建部门")
|
||||
create_time = Column(DateTime, nullable=False, default=datetime.now, comment="创建时间")
|
||||
update_by = Column(String(64), nullable=True, comment="更新人")
|
||||
update_time = Column(
|
||||
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment="更新时间"
|
||||
)
|
||||
del_flag = Column(SmallInteger, nullable=False, default=0, comment="删除标记")
|
||||
|
||||
|
||||
class DbWeighingSlip(Base):
|
||||
__tablename__ = "db_weighing_slip"
|
||||
|
||||
id = Column(String(64), primary_key=True, comment="主键ID(UUID)")
|
||||
vehicle_no = Column(String(255), nullable=False, comment="车号")
|
||||
plan_no = Column(String(255), nullable=True, comment="计划单号")
|
||||
product_id = Column(String(64), nullable=True, comment="CRM产品ID")
|
||||
cargo_name = Column(String(255), nullable=False, comment="货物名称")
|
||||
specification = Column(String(255), nullable=True, comment="规格")
|
||||
business_type = Column(String(255), nullable=False, comment="业务类型")
|
||||
batch_no = Column(String(255), nullable=True, comment="批次号")
|
||||
supplier = Column(String(255), nullable=False, comment="供货单位")
|
||||
consignee = Column(String(255), nullable=False, comment="收货单位")
|
||||
gross_weight = Column(Float, nullable=False, comment="毛重(kg)")
|
||||
gross_weight_time = Column(DateTime, nullable=False, comment="毛重时间")
|
||||
tare_weight = Column(Float, nullable=False, comment="皮重(kg)")
|
||||
tare_weight_time = Column(DateTime, nullable=True, comment="皮重时间")
|
||||
deduction = Column(Float, nullable=True, default=0, comment="扣杂(kg)")
|
||||
net_weight = Column(Float, nullable=False, comment="净重(kg)")
|
||||
planned_quantity = Column(Float, nullable=True, comment="计划数量(kg)")
|
||||
executed_quantity = Column(Float, nullable=True, comment="执行数量(kg)")
|
||||
purchase_plan_id = Column(String(64), nullable=True, comment="关联计划ID")
|
||||
inbound_outbound_id = Column(String(64), nullable=True, comment="关联出入库单ID")
|
||||
weighing_point = Column(String(255), nullable=True, default="山峡地磅", comment="称重点")
|
||||
current_quantity = Column(Float, nullable=True, comment="本次量(kg)")
|
||||
is_voided = Column(SmallInteger, nullable=False, default=0, comment="是否作废")
|
||||
tenant_no = Column(String(64), nullable=False, default="shanxia2.0", comment="租户编号")
|
||||
order_num = Column(Integer, nullable=True, comment="排序号")
|
||||
remark = Column(String(512), nullable=True, comment="备注")
|
||||
create_by = Column(String(64), nullable=True, comment="创建人")
|
||||
create_dept_id = Column(String(64), nullable=False, default="", comment="创建部门")
|
||||
create_time = Column(DateTime, nullable=False, default=datetime.now, comment="创建时间")
|
||||
update_by = Column(String(64), nullable=True, comment="更新人")
|
||||
update_time = Column(
|
||||
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment="更新时间"
|
||||
)
|
||||
del_flag = Column(SmallInteger, nullable=False, default=0, comment="删除标记")
|
||||
|
||||
|
||||
class DbSequence(Base):
|
||||
"""租户内业务序号(并发安全自增)。"""
|
||||
|
||||
__tablename__ = "db_sequence"
|
||||
|
||||
id = Column(String(64), primary_key=True, comment="主键ID(UUID)")
|
||||
tenant_no = Column(String(64), nullable=False, comment="租户编号")
|
||||
seq_name = Column(String(64), nullable=False, comment="序号名称")
|
||||
next_val = Column(Integer, nullable=False, comment="下一个待分配序号")
|
||||
update_time = Column(
|
||||
DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment="更新时间"
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
"""地磅(db)Pydantic schemas — 对齐原 simple-dibang-xiaoshou 契约。"""
|
||||
@@ -0,0 +1,23 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class DashboardStats(BaseModel):
|
||||
today_inbound: float = Field(alias="todayInbound")
|
||||
today_outbound: float = Field(alias="todayOutbound")
|
||||
abnormal_orders: int = Field(alias="abnormalOrders")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class DashboardCargoStat(BaseModel):
|
||||
label: str
|
||||
planned: float
|
||||
actual: float
|
||||
|
||||
|
||||
class DashboardPartnerCargoStat(BaseModel):
|
||||
label: str
|
||||
planned: float
|
||||
actual: float
|
||||
@@ -0,0 +1,75 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class InboundOutboundBase(BaseModel):
|
||||
date: str = Field(max_length=255)
|
||||
partner_company: str = Field(max_length=255, alias="partnerCompany")
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
product: str = Field(max_length=255)
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
quantity: float = Field()
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
vehicle_no: str = Field(max_length=255, alias="vehicleNo")
|
||||
status: Optional[str] = Field(default=None, max_length=50)
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
|
||||
model_config = {"from_attributes": True, "populate_by_name": True}
|
||||
|
||||
|
||||
class InboundOutboundCreate(BaseModel):
|
||||
date: str = Field(max_length=255)
|
||||
partner_company: str = Field(max_length=255, alias="partnerCompany")
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
product_id: str = Field(alias="productId")
|
||||
quantity: float = Field()
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
vehicle_no: str = Field(max_length=255, alias="vehicleNo")
|
||||
status: Optional[str] = Field(default=None, max_length=50)
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class InboundOutboundUpdate(BaseModel):
|
||||
date: Optional[str] = Field(default=None, max_length=255)
|
||||
partner_company: Optional[str] = Field(default=None, max_length=255, alias="partnerCompany")
|
||||
business_type: Optional[str] = Field(default=None, max_length=255, alias="businessType")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
product: Optional[str] = Field(default=None, max_length=255)
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
quantity: Optional[float] = Field(default=None)
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
vehicle_no: Optional[str] = Field(default=None, max_length=255, alias="vehicleNo")
|
||||
status: Optional[str] = Field(default=None, max_length=50)
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class CreateLinkForPlanPayload(BaseModel):
|
||||
purchase_plan_id: str = Field(alias="purchasePlanId")
|
||||
remarks: Optional[str] = None
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class InboundOutboundRead(InboundOutboundBase):
|
||||
id: str
|
||||
is_deleted: int = Field(default=0, alias="isDeleted")
|
||||
created_at: str = Field(alias="createdAt")
|
||||
gross_weight: Optional[float] = Field(default=None, alias="grossWeight")
|
||||
tare_weight: Optional[float] = Field(default=None, alias="tareWeight")
|
||||
deduction: Optional[float] = Field(default=None)
|
||||
net_weight: Optional[float] = Field(default=None, alias="netWeight")
|
||||
gross_weight_time: Optional[str] = Field(default=None, alias="grossWeightTime")
|
||||
tare_weight_time: Optional[str] = Field(default=None, alias="tareWeightTime")
|
||||
has_weighing_slip: bool = Field(default=False, alias="hasWeighingSlip")
|
||||
@@ -0,0 +1,25 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class VehicleBase(BaseModel):
|
||||
vehicle_no: str = Field(min_length=1, max_length=255, alias="vehicleNo")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
|
||||
model_config = {"from_attributes": True, "populate_by_name": True}
|
||||
|
||||
|
||||
class VehicleCreate(VehicleBase):
|
||||
pass
|
||||
|
||||
|
||||
class VehicleUpdate(BaseModel):
|
||||
vehicle_no: Optional[str] = Field(default=None, min_length=1, max_length=255, alias="vehicleNo")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class VehicleRead(VehicleBase):
|
||||
id: str
|
||||
@@ -0,0 +1,89 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class PurchasePlanBase(BaseModel):
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
plan_date: str = Field(max_length=255, alias="planDate")
|
||||
start_date: Optional[str] = Field(default=None, max_length=255, alias="startDate")
|
||||
end_date: Optional[str] = Field(default=None, max_length=255, alias="endDate")
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
product: str = Field(max_length=255)
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
planned_quantity: float = Field(alias="plannedQuantity")
|
||||
vehicle_no: Optional[str] = Field(default="", max_length=255, alias="vehicleNo")
|
||||
customer_name: Optional[str] = Field(default=None, max_length=255, alias="customerName")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
execution_status: Optional[str] = Field(default=None, max_length=50, alias="executionStatus")
|
||||
executed_at: Optional[str] = Field(default=None, max_length=255, alias="executedAt")
|
||||
last_weighing_slip_id: Optional[str] = Field(default=None, alias="lastWeighingSlipId")
|
||||
executed_quantity: Optional[float] = Field(default=0, alias="executedQuantity")
|
||||
auto_generate_enabled: int = Field(default=0, alias="autoGenerateEnabled")
|
||||
auto_generate_end_month: Optional[str] = Field(
|
||||
default=None, max_length=7, alias="autoGenerateEndMonth"
|
||||
)
|
||||
auto_generate_last_month: Optional[str] = Field(
|
||||
default=None, max_length=7, alias="autoGenerateLastMonth"
|
||||
)
|
||||
|
||||
model_config = {"from_attributes": True, "populate_by_name": True}
|
||||
|
||||
|
||||
class PurchasePlanCreate(BaseModel):
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
plan_date: str = Field(max_length=255, alias="planDate")
|
||||
start_date: Optional[str] = Field(default=None, max_length=255, alias="startDate")
|
||||
end_date: Optional[str] = Field(default=None, max_length=255, alias="endDate")
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
product_id: str = Field(alias="productId")
|
||||
planned_quantity: float = Field(alias="plannedQuantity")
|
||||
vehicle_no: Optional[str] = Field(default="", max_length=255, alias="vehicleNo")
|
||||
customer_name: Optional[str] = Field(default=None, max_length=255, alias="customerName")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
execution_status: Optional[str] = Field(default=None, max_length=50, alias="executionStatus")
|
||||
auto_generate_enabled: Optional[int] = Field(default=0, alias="autoGenerateEnabled")
|
||||
auto_generate_end_month: Optional[str] = Field(
|
||||
default=None, max_length=7, alias="autoGenerateEndMonth"
|
||||
)
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class PurchasePlanUpdate(BaseModel):
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
plan_date: Optional[str] = Field(default=None, max_length=255, alias="planDate")
|
||||
start_date: Optional[str] = Field(default=None, max_length=255, alias="startDate")
|
||||
end_date: Optional[str] = Field(default=None, max_length=255, alias="endDate")
|
||||
business_type: Optional[str] = Field(default=None, max_length=255, alias="businessType")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
product: Optional[str] = Field(default=None, max_length=255)
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
planned_quantity: Optional[float] = Field(default=None, alias="plannedQuantity")
|
||||
vehicle_no: Optional[str] = Field(default=None, max_length=255, alias="vehicleNo")
|
||||
customer_name: Optional[str] = Field(default=None, max_length=255, alias="customerName")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
execution_status: Optional[str] = Field(default=None, max_length=50, alias="executionStatus")
|
||||
executed_at: Optional[str] = Field(default=None, max_length=255, alias="executedAt")
|
||||
last_weighing_slip_id: Optional[str] = Field(default=None, alias="lastWeighingSlipId")
|
||||
executed_quantity: Optional[float] = Field(default=None, alias="executedQuantity")
|
||||
auto_generate_enabled: Optional[int] = Field(default=None, alias="autoGenerateEnabled")
|
||||
auto_generate_end_month: Optional[str] = Field(
|
||||
default=None, max_length=7, alias="autoGenerateEndMonth"
|
||||
)
|
||||
auto_generate_last_month: Optional[str] = Field(
|
||||
default=None, max_length=7, alias="autoGenerateLastMonth"
|
||||
)
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class PurchasePlanRead(PurchasePlanBase):
|
||||
id: str
|
||||
is_deleted: int = Field(default=0, alias="isDeleted")
|
||||
created_at: str = Field(alias="createdAt")
|
||||
|
||||
|
||||
class PurchasePlanWithFillLink(PurchasePlanRead):
|
||||
fill_link_inventory_id: Optional[str] = Field(default=None, alias="fillLinkInventoryId")
|
||||
@@ -0,0 +1,90 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class WeighingSlipBase(BaseModel):
|
||||
vehicle_no: str = Field(max_length=255, alias="vehicleNo")
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
cargo_name: str = Field(max_length=255, alias="cargoName")
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
supplier: str = Field(max_length=255)
|
||||
consignee: str = Field(max_length=255)
|
||||
gross_weight: float = Field(alias="grossWeight")
|
||||
gross_weight_time: str = Field(max_length=255, alias="grossWeightTime")
|
||||
tare_weight: float = Field(alias="tareWeight")
|
||||
tare_weight_time: str = Field(max_length=255, alias="tareWeightTime")
|
||||
deduction: Optional[float] = Field(default=0)
|
||||
net_weight: float = Field(alias="netWeight")
|
||||
planned_quantity: Optional[float] = Field(default=None, alias="plannedQuantity")
|
||||
executed_quantity: Optional[float] = Field(default=None, alias="executedQuantity")
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
inbound_outbound_id: Optional[str] = Field(default=None, alias="inboundOutboundId")
|
||||
weighing_point: Optional[str] = Field(default="山峡地磅", max_length=255, alias="weighingPoint")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
current_quantity: Optional[float] = Field(default=None, alias="currentQuantity")
|
||||
|
||||
model_config = {"from_attributes": True, "populate_by_name": True}
|
||||
|
||||
|
||||
class WeighingSlipCreate(BaseModel):
|
||||
vehicle_no: str = Field(max_length=255, alias="vehicleNo")
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
product_id: str = Field(alias="productId")
|
||||
business_type: str = Field(max_length=255, alias="businessType")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
supplier: str = Field(max_length=255)
|
||||
consignee: str = Field(max_length=255)
|
||||
gross_weight: float = Field(default=0, alias="grossWeight")
|
||||
gross_weight_time: str = Field(default="", max_length=255, alias="grossWeightTime")
|
||||
tare_weight: float = Field(default=0, alias="tareWeight")
|
||||
tare_weight_time: str = Field(default="", max_length=255, alias="tareWeightTime")
|
||||
deduction: Optional[float] = Field(default=0)
|
||||
net_weight: float = Field(default=0, alias="netWeight")
|
||||
planned_quantity: Optional[float] = Field(default=None, alias="plannedQuantity")
|
||||
executed_quantity: Optional[float] = Field(default=None, alias="executedQuantity")
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
inbound_outbound_id: Optional[str] = Field(default=None, alias="inboundOutboundId")
|
||||
weighing_point: Optional[str] = Field(default="山峡地磅", max_length=255, alias="weighingPoint")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
current_quantity: Optional[float] = Field(default=None, alias="currentQuantity")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class WeighingSlipUpdate(BaseModel):
|
||||
vehicle_no: Optional[str] = Field(default=None, max_length=255, alias="vehicleNo")
|
||||
plan_no: Optional[str] = Field(default=None, max_length=255, alias="planNo")
|
||||
product_id: Optional[str] = Field(default=None, alias="productId")
|
||||
cargo_name: Optional[str] = Field(default=None, max_length=255, alias="cargoName")
|
||||
specification: Optional[str] = Field(default=None, max_length=255)
|
||||
business_type: Optional[str] = Field(default=None, max_length=255, alias="businessType")
|
||||
batch_no: Optional[str] = Field(default=None, max_length=255, alias="batchNo")
|
||||
supplier: Optional[str] = Field(default=None, max_length=255)
|
||||
consignee: Optional[str] = Field(default=None, max_length=255)
|
||||
gross_weight: Optional[float] = Field(default=None, alias="grossWeight")
|
||||
gross_weight_time: Optional[str] = Field(default=None, max_length=255, alias="grossWeightTime")
|
||||
tare_weight: Optional[float] = Field(default=None, alias="tareWeight")
|
||||
tare_weight_time: Optional[str] = Field(default=None, max_length=255, alias="tareWeightTime")
|
||||
deduction: Optional[float] = Field(default=None)
|
||||
net_weight: Optional[float] = Field(default=None, alias="netWeight")
|
||||
planned_quantity: Optional[float] = Field(default=None, alias="plannedQuantity")
|
||||
executed_quantity: Optional[float] = Field(default=None, alias="executedQuantity")
|
||||
purchase_plan_id: Optional[str] = Field(default=None, alias="purchasePlanId")
|
||||
inbound_outbound_id: Optional[str] = Field(default=None, alias="inboundOutboundId")
|
||||
weighing_point: Optional[str] = Field(default=None, max_length=255, alias="weighingPoint")
|
||||
remarks: Optional[str] = Field(default=None)
|
||||
current_quantity: Optional[float] = Field(default=None, alias="currentQuantity")
|
||||
is_voided: Optional[int] = Field(default=None, alias="isVoided")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class WeighingSlipRead(WeighingSlipBase):
|
||||
id: str
|
||||
is_voided: int = Field(default=0, alias="isVoided")
|
||||
created_at: str = Field(alias="createdAt")
|
||||
inventory_date: Optional[str] = Field(default=None, alias="inventoryDate")
|
||||
Whitespace-only changes.
@@ -0,0 +1,139 @@
|
||||
"""地磅计划按月自动生成 — 迁移自 simple-dibang-xiaoshou/auto-create-plan/auto_create_monthly_plan.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
from sqlalchemy import and_, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbPurchasePlan
|
||||
from app.services.db.plan_helpers import default_execution_status, generate_plan_no
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def month_range(dt: date) -> tuple[date, date]:
|
||||
"""返回 dt 所在月份的首日、末日。"""
|
||||
first = dt.replace(day=1)
|
||||
if first.month == 12:
|
||||
next_month = first.replace(year=first.year + 1, month=1, day=1)
|
||||
else:
|
||||
next_month = first.replace(month=first.month + 1, day=1)
|
||||
last = next_month - timedelta(days=1)
|
||||
return first, last
|
||||
|
||||
|
||||
def get_last_month_range(now: date) -> tuple[date, date]:
|
||||
"""返回上个月的首日、末日。"""
|
||||
first_this, _ = month_range(now)
|
||||
last_prev = first_this - timedelta(days=1)
|
||||
return month_range(last_prev)
|
||||
|
||||
|
||||
def run_auto_generate_monthly_plans(
|
||||
db: Session,
|
||||
*,
|
||||
force: bool = False,
|
||||
now: date | None = None,
|
||||
) -> dict:
|
||||
"""
|
||||
按月自动生成采购计划。
|
||||
|
||||
- 默认仅在每月 1 日执行(force=True 时跳过该限制,供手动触发验收)
|
||||
- 遍历所有租户,按上月计划模板生成本月计划
|
||||
"""
|
||||
today = now or date.today()
|
||||
|
||||
if today.day != 1 and not force:
|
||||
logger.info("当前不是每月 1 日且未 force,跳过自动生成计划")
|
||||
return {
|
||||
"created_count": 0,
|
||||
"skipped_reason": "not_first_day_of_month",
|
||||
"current_month": today.strftime("%Y-%m"),
|
||||
}
|
||||
|
||||
current_month = today.strftime("%Y-%m")
|
||||
this_month_first, this_month_last = month_range(today)
|
||||
last_month_first, last_month_last = get_last_month_range(today)
|
||||
|
||||
logger.info(
|
||||
"开始执行自动生成计划任务,current_month=%s, last_month=[%s, %s], this_month=[%s, %s], force=%s",
|
||||
current_month,
|
||||
last_month_first,
|
||||
last_month_last,
|
||||
this_month_first,
|
||||
this_month_last,
|
||||
force,
|
||||
)
|
||||
|
||||
plans = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
and_(
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
DbPurchasePlan.auto_generate_enabled == 1,
|
||||
or_(
|
||||
DbPurchasePlan.auto_generate_last_month.is_(None),
|
||||
DbPurchasePlan.auto_generate_last_month != current_month,
|
||||
),
|
||||
or_(
|
||||
DbPurchasePlan.auto_generate_end_month.is_(None),
|
||||
DbPurchasePlan.auto_generate_end_month >= current_month,
|
||||
),
|
||||
DbPurchasePlan.start_date == last_month_first,
|
||||
DbPurchasePlan.end_date == last_month_last,
|
||||
)
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
if not plans:
|
||||
logger.info("未找到需要自动生成下月计划的记录")
|
||||
return {
|
||||
"created_count": 0,
|
||||
"skipped_reason": None,
|
||||
"current_month": current_month,
|
||||
}
|
||||
|
||||
logger.info("找到 %d 条可自动生成的上月计划", len(plans))
|
||||
created_count = 0
|
||||
|
||||
for source in plans:
|
||||
new_plan = DbPurchasePlan(
|
||||
id=generate_uuid(),
|
||||
tenant_no=source.tenant_no,
|
||||
plan_no=generate_plan_no(),
|
||||
plan_date=this_month_first,
|
||||
start_date=this_month_first,
|
||||
end_date=this_month_last,
|
||||
business_type=source.business_type,
|
||||
product_id=source.product_id,
|
||||
product=source.product,
|
||||
specification=source.specification,
|
||||
planned_quantity=source.planned_quantity,
|
||||
vehicle_no=source.vehicle_no or "",
|
||||
customer_name=source.customer_name,
|
||||
remark=source.remark,
|
||||
execution_status=default_execution_status(source.business_type),
|
||||
executed_at=None,
|
||||
last_weighing_slip_id=None,
|
||||
executed_quantity=0,
|
||||
auto_generate_enabled=source.auto_generate_enabled,
|
||||
auto_generate_end_month=source.auto_generate_end_month,
|
||||
auto_generate_last_month=None,
|
||||
create_by=None,
|
||||
create_dept_id="",
|
||||
)
|
||||
db.add(new_plan)
|
||||
source.auto_generate_last_month = current_month
|
||||
created_count += 1
|
||||
|
||||
db.commit()
|
||||
logger.info("本次自动生成完成,共生成 %d 条新计划", created_count)
|
||||
return {
|
||||
"created_count": created_count,
|
||||
"skipped_reason": None,
|
||||
"current_month": current_month,
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
"""出入库单 id 生成 — 租户内纯数字自增,并发安全。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.db_models import DbInboundOutbound, DbSequence
|
||||
|
||||
SEQ_NAME_INBOUND_OUTBOUND = "inbound_outbound"
|
||||
_NUMERIC_ID_PATTERN = r"^[0-9]+$"
|
||||
|
||||
|
||||
def max_numeric_inbound_outbound_id(db: Session, tenant_no: str) -> int:
|
||||
"""当前租户下纯数字 id 的最大值(不含 UUID 等非数字主键)。"""
|
||||
result = db.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT COALESCE(MAX(CAST(id AS UNSIGNED)), 0) AS max_id
|
||||
FROM db_inbound_outbound
|
||||
WHERE tenant_no = :tenant_no
|
||||
AND del_flag = 0
|
||||
AND id REGEXP :pattern
|
||||
"""
|
||||
),
|
||||
{"tenant_no": tenant_no, "pattern": _NUMERIC_ID_PATTERN},
|
||||
).scalar()
|
||||
return int(result or 0)
|
||||
|
||||
|
||||
def _ensure_sequence_row(db: Session, tenant_no: str) -> None:
|
||||
exists = (
|
||||
db.query(DbSequence.id)
|
||||
.filter(
|
||||
DbSequence.tenant_no == tenant_no,
|
||||
DbSequence.seq_name == SEQ_NAME_INBOUND_OUTBOUND,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if exists:
|
||||
return
|
||||
|
||||
initial_next = max_numeric_inbound_outbound_id(db, tenant_no) + 1
|
||||
db.add(
|
||||
DbSequence(
|
||||
id=generate_uuid(),
|
||||
tenant_no=tenant_no,
|
||||
seq_name=SEQ_NAME_INBOUND_OUTBOUND,
|
||||
next_val=initial_next,
|
||||
)
|
||||
)
|
||||
db.flush()
|
||||
|
||||
|
||||
def _allocate_inbound_outbound_id(db: Session, tenant_no: str) -> str:
|
||||
_ensure_sequence_row(db, tenant_no)
|
||||
row = (
|
||||
db.query(DbSequence)
|
||||
.filter(
|
||||
DbSequence.tenant_no == tenant_no,
|
||||
DbSequence.seq_name == SEQ_NAME_INBOUND_OUTBOUND,
|
||||
)
|
||||
.with_for_update()
|
||||
.one()
|
||||
)
|
||||
assigned = int(row.next_val)
|
||||
row.next_val = assigned + 1
|
||||
db.flush()
|
||||
return str(assigned)
|
||||
|
||||
|
||||
def next_inbound_outbound_id(db: Session, tenant_no: str) -> str:
|
||||
"""
|
||||
分配下一条出入库单 id(纯数字字符串)。
|
||||
使用 db_sequence 行锁 + next_val 递增,避免并发下重复 id。
|
||||
"""
|
||||
last_error: IntegrityError | None = None
|
||||
for _ in range(3):
|
||||
try:
|
||||
return _allocate_inbound_outbound_id(db, tenant_no)
|
||||
except IntegrityError as exc:
|
||||
db.rollback()
|
||||
last_error = exc
|
||||
if last_error is not None:
|
||||
raise last_error
|
||||
return _allocate_inbound_outbound_id(db, tenant_no)
|
||||
@@ -0,0 +1,196 @@
|
||||
"""地磅 ORM → API Read 模型映射。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, time
|
||||
|
||||
from app.core.utils import format_datetime
|
||||
|
||||
# 三方预生成出入库单「日期未填」占位(原系统 date="",DDL 为 NOT NULL DATE)
|
||||
EMPTY_INVENTORY_DATE = date(1970, 1, 1)
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbVehicle, DbWeighingSlip
|
||||
from app.schemas.db.inventory import InboundOutboundRead
|
||||
from app.schemas.db.master_data import VehicleRead
|
||||
from app.schemas.db.plans import PurchasePlanRead, PurchasePlanWithFillLink
|
||||
from app.schemas.db.weighing import WeighingSlipRead
|
||||
|
||||
|
||||
def fmt_date(value: date | datetime | str | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
return value.strftime("%Y-%m-%d")
|
||||
if isinstance(value, date):
|
||||
return value.strftime("%Y-%m-%d")
|
||||
return str(value)
|
||||
|
||||
|
||||
def fmt_datetime(value: datetime | str | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
return format_datetime(value)
|
||||
return str(value)
|
||||
|
||||
|
||||
def vehicle_to_read(row: DbVehicle) -> VehicleRead:
|
||||
return VehicleRead(
|
||||
id=row.id,
|
||||
vehicleNo=row.vehicle_no,
|
||||
remarks=row.remark,
|
||||
)
|
||||
|
||||
|
||||
def plan_to_read(row: DbPurchasePlan, *, executed_quantity: float | None = None) -> PurchasePlanRead:
|
||||
return PurchasePlanRead(
|
||||
id=row.id,
|
||||
planNo=row.plan_no,
|
||||
planDate=fmt_date(row.plan_date) or "",
|
||||
startDate=fmt_date(row.start_date),
|
||||
endDate=fmt_date(row.end_date),
|
||||
businessType=row.business_type,
|
||||
productId=row.product_id,
|
||||
product=row.product,
|
||||
specification=row.specification,
|
||||
plannedQuantity=row.planned_quantity,
|
||||
vehicleNo=row.vehicle_no or "",
|
||||
customerName=row.customer_name,
|
||||
remarks=row.remark,
|
||||
executionStatus=row.execution_status,
|
||||
executedAt=fmt_datetime(row.executed_at),
|
||||
lastWeighingSlipId=row.last_weighing_slip_id,
|
||||
executedQuantity=(
|
||||
executed_quantity if executed_quantity is not None else (row.executed_quantity or 0)
|
||||
),
|
||||
autoGenerateEnabled=row.auto_generate_enabled or 0,
|
||||
autoGenerateEndMonth=row.auto_generate_end_month,
|
||||
autoGenerateLastMonth=row.auto_generate_last_month,
|
||||
isDeleted=row.del_flag,
|
||||
createdAt=format_datetime(row.create_time) or "",
|
||||
)
|
||||
|
||||
|
||||
def plan_to_read_with_fill_link(
|
||||
row: DbPurchasePlan,
|
||||
*,
|
||||
executed_quantity: float | None = None,
|
||||
fill_link_inventory_id: str | None = None,
|
||||
) -> PurchasePlanWithFillLink:
|
||||
base = plan_to_read(row, executed_quantity=executed_quantity)
|
||||
return PurchasePlanWithFillLink(
|
||||
**base.model_dump(by_alias=False),
|
||||
fill_link_inventory_id=fill_link_inventory_id,
|
||||
)
|
||||
|
||||
|
||||
def is_empty_inventory_date(value: date | None) -> bool:
|
||||
return value is None or value == EMPTY_INVENTORY_DATE
|
||||
|
||||
|
||||
def inventory_date_to_api(value: date | None) -> str:
|
||||
if is_empty_inventory_date(value):
|
||||
return ""
|
||||
return fmt_date(value) or ""
|
||||
|
||||
|
||||
def inventory_to_read(
|
||||
row: DbInboundOutbound,
|
||||
*,
|
||||
weighing: DbWeighingSlip | None = None,
|
||||
) -> InboundOutboundRead:
|
||||
gross_weight = None
|
||||
tare_weight = None
|
||||
deduction = None
|
||||
net_weight = None
|
||||
gross_weight_time = None
|
||||
tare_weight_time = None
|
||||
has_weighing_slip = False
|
||||
if weighing:
|
||||
gross_weight = weighing.gross_weight
|
||||
tare_weight = weighing.tare_weight
|
||||
deduction = weighing.deduction
|
||||
net_weight = weighing.net_weight
|
||||
gross_weight_time = fmt_datetime(weighing.gross_weight_time)
|
||||
tare_time = fmt_datetime(weighing.tare_weight_time)
|
||||
tare_weight_time = tare_time if tare_time and tare_time.strip() else None
|
||||
has_weighing_slip = True
|
||||
|
||||
return InboundOutboundRead(
|
||||
id=row.id,
|
||||
date=inventory_date_to_api(row.date),
|
||||
partnerCompany=row.partner_company,
|
||||
businessType=row.business_type,
|
||||
productId=row.product_id,
|
||||
product=row.product,
|
||||
specification=row.specification,
|
||||
quantity=row.quantity,
|
||||
planNo=row.plan_no,
|
||||
batchNo=row.batch_no,
|
||||
vehicleNo=row.vehicle_no,
|
||||
status=row.status,
|
||||
remarks=row.remark,
|
||||
purchasePlanId=row.purchase_plan_id,
|
||||
isDeleted=row.del_flag,
|
||||
createdAt=format_datetime(row.create_time) or "",
|
||||
grossWeight=gross_weight,
|
||||
tareWeight=tare_weight,
|
||||
deduction=deduction,
|
||||
netWeight=net_weight,
|
||||
grossWeightTime=gross_weight_time,
|
||||
tareWeightTime=tare_weight_time,
|
||||
hasWeighingSlip=has_weighing_slip,
|
||||
)
|
||||
|
||||
|
||||
def weighing_to_read(row: DbWeighingSlip, *, inventory_date: str | None = None) -> WeighingSlipRead:
|
||||
return WeighingSlipRead(
|
||||
id=row.id,
|
||||
vehicleNo=row.vehicle_no,
|
||||
planNo=row.plan_no,
|
||||
productId=row.product_id,
|
||||
cargoName=row.cargo_name,
|
||||
specification=row.specification,
|
||||
businessType=row.business_type,
|
||||
batchNo=row.batch_no,
|
||||
supplier=row.supplier,
|
||||
consignee=row.consignee,
|
||||
grossWeight=row.gross_weight,
|
||||
grossWeightTime=fmt_datetime(row.gross_weight_time) or "",
|
||||
tareWeight=row.tare_weight,
|
||||
tareWeightTime=fmt_datetime(row.tare_weight_time) or "",
|
||||
deduction=row.deduction,
|
||||
netWeight=row.net_weight,
|
||||
plannedQuantity=row.planned_quantity,
|
||||
executedQuantity=row.executed_quantity,
|
||||
purchasePlanId=row.purchase_plan_id,
|
||||
inboundOutboundId=row.inbound_outbound_id,
|
||||
weighingPoint=row.weighing_point,
|
||||
remarks=row.remark,
|
||||
currentQuantity=row.current_quantity,
|
||||
isVoided=row.is_voided,
|
||||
createdAt=format_datetime(row.create_time) or "",
|
||||
inventoryDate=inventory_date,
|
||||
)
|
||||
|
||||
|
||||
def datetime_end_of_day(value: date) -> datetime:
|
||||
return datetime.combine(value, time(23, 59, 59))
|
||||
|
||||
|
||||
def parse_date_str(value: str | None) -> date | None:
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return datetime.strptime(value[:10], "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def parse_datetime_str(value: str | None) -> datetime | None:
|
||||
if not value:
|
||||
return None
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"):
|
||||
try:
|
||||
return datetime.strptime(value[:19] if fmt != "%Y-%m-%d" else value[:10], fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
@@ -0,0 +1,14 @@
|
||||
"""地磅计划辅助函数。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def generate_plan_no() -> str:
|
||||
"""生成计划号:SX-YYYYMMDDHHMMSS001"""
|
||||
now = datetime.now()
|
||||
return f"SX-{now.strftime('%Y%m%d%H%M%S')}001"
|
||||
|
||||
|
||||
def default_execution_status(business_type: str) -> str:
|
||||
return "待入库" if business_type == "入库" else "待出库"
|
||||
@@ -0,0 +1,66 @@
|
||||
"""地磅模块 — CRM 产品解析与快照写入。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.crud.crm.product import get_product
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProductSnapshot:
|
||||
product_id: str
|
||||
product_name: str
|
||||
specification: str | None
|
||||
|
||||
|
||||
def resolve_crm_product(db: Session, tenant_no: str, product_id: str) -> ProductSnapshot:
|
||||
"""校验 CRM 产品并返回名称/规格快照。新建单据 product_id 必填。"""
|
||||
pid = (product_id or "").strip()
|
||||
if not pid:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请选择 CRM 产品",
|
||||
)
|
||||
|
||||
row = get_product(db, pid)
|
||||
if row is None or row.tenant_no != tenant_no:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="产品不存在或不可用",
|
||||
)
|
||||
if row.status != "on":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="产品已下架,请重新选择",
|
||||
)
|
||||
|
||||
name = (row.product_name or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="产品名称无效",
|
||||
)
|
||||
|
||||
model = (row.model or "").strip() or None
|
||||
return ProductSnapshot(product_id=row.id, product_name=name, specification=model)
|
||||
|
||||
|
||||
def apply_product_snapshot_to_plan(plan, snapshot: ProductSnapshot) -> None:
|
||||
plan.product_id = snapshot.product_id
|
||||
plan.product = snapshot.product_name
|
||||
plan.specification = snapshot.specification
|
||||
|
||||
|
||||
def apply_product_snapshot_to_inventory(row, snapshot: ProductSnapshot) -> None:
|
||||
row.product_id = snapshot.product_id
|
||||
row.product = snapshot.product_name
|
||||
row.specification = snapshot.specification
|
||||
|
||||
|
||||
def apply_product_snapshot_to_weighing(row, snapshot: ProductSnapshot) -> None:
|
||||
row.product_id = snapshot.product_id
|
||||
row.cargo_name = snapshot.product_name
|
||||
row.specification = snapshot.specification
|
||||
@@ -0,0 +1,124 @@
|
||||
"""地磅单联动业务逻辑 — 迁移自 simple-dibang-xiaoshou weighing._process_weighing_business_logic。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbWeighingSlip
|
||||
|
||||
|
||||
def _has_weight_time(value: datetime | str | None) -> bool:
|
||||
if value is None:
|
||||
return False
|
||||
if isinstance(value, datetime):
|
||||
return True
|
||||
return bool(str(value).strip())
|
||||
|
||||
|
||||
def process_weighing_business_logic(db: Session, weighing: DbWeighingSlip) -> None:
|
||||
"""
|
||||
处理地磅单的业务逻辑:
|
||||
1. 更新计划单的已执行量(单位:kg)
|
||||
2. 更新出入库单的状态
|
||||
3. 重新计算地磅单的 executed_quantity(按创建时间顺序累计)
|
||||
"""
|
||||
plan_ids_to_process: set[str] = set()
|
||||
inventory_ids_to_process: set[str] = set()
|
||||
|
||||
if weighing.purchase_plan_id:
|
||||
plan_ids_to_process.add(weighing.purchase_plan_id)
|
||||
|
||||
if weighing.inbound_outbound_id:
|
||||
inventory_ids_to_process.add(weighing.inbound_outbound_id)
|
||||
|
||||
if weighing.plan_no:
|
||||
plans = (
|
||||
db.query(DbPurchasePlan)
|
||||
.filter(
|
||||
DbPurchasePlan.plan_no == weighing.plan_no,
|
||||
DbPurchasePlan.del_flag == 0,
|
||||
DbPurchasePlan.tenant_no == weighing.tenant_no,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for plan in plans:
|
||||
plan_ids_to_process.add(plan.id)
|
||||
|
||||
for plan_id in plan_ids_to_process:
|
||||
plan = db.query(DbPurchasePlan).filter(DbPurchasePlan.id == plan_id).one_or_none()
|
||||
if not plan:
|
||||
continue
|
||||
|
||||
plan_weighings = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
DbWeighingSlip.tenant_no == plan.tenant_no,
|
||||
or_(
|
||||
DbWeighingSlip.plan_no == plan.plan_no,
|
||||
DbWeighingSlip.purchase_plan_id == plan.id,
|
||||
),
|
||||
)
|
||||
.order_by(DbWeighingSlip.create_time.asc())
|
||||
.all()
|
||||
)
|
||||
|
||||
total_executed = sum(w.net_weight for w in plan_weighings if w.net_weight)
|
||||
plan.executed_quantity = total_executed
|
||||
|
||||
planned_quantity_kg = plan.planned_quantity * 1000
|
||||
if total_executed >= planned_quantity_kg:
|
||||
plan.execution_status = "已入库" if plan.business_type == "入库" else "已出库"
|
||||
elif total_executed > 0:
|
||||
plan.execution_status = "入库中" if plan.business_type == "入库" else "出库中"
|
||||
else:
|
||||
plan.execution_status = "待入库" if plan.business_type == "入库" else "待出库"
|
||||
|
||||
db.commit()
|
||||
db.refresh(plan)
|
||||
|
||||
cumulative_executed = 0.0
|
||||
for slip in plan_weighings:
|
||||
cumulative_executed += slip.net_weight if slip.net_weight else 0.0
|
||||
slip.executed_quantity = cumulative_executed
|
||||
db.commit()
|
||||
|
||||
for inventory_id in inventory_ids_to_process:
|
||||
inventory = (
|
||||
db.query(DbInboundOutbound)
|
||||
.filter(DbInboundOutbound.id == inventory_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if not inventory:
|
||||
continue
|
||||
|
||||
related_weighings = (
|
||||
db.query(DbWeighingSlip)
|
||||
.filter(
|
||||
DbWeighingSlip.inbound_outbound_id == inventory_id,
|
||||
DbWeighingSlip.is_voided == 0,
|
||||
DbWeighingSlip.del_flag == 0,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
all_complete = (
|
||||
len(related_weighings) > 0
|
||||
and all(
|
||||
_has_weight_time(w.gross_weight_time) and _has_weight_time(w.tare_weight_time)
|
||||
for w in related_weighings
|
||||
)
|
||||
)
|
||||
|
||||
if all_complete:
|
||||
inventory.status = "已完成"
|
||||
else:
|
||||
inventory.status = (
|
||||
"待入库" if inventory.business_type == "入库" else "待出库"
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.refresh(inventory)
|
||||
@@ -20,6 +20,7 @@ def init_scheduled_tasks(get_db_session):
|
||||
remind_customer_recycle,
|
||||
warn_customer_recycle,
|
||||
)
|
||||
from app.tasks.db_auto_plan import auto_generate_monthly_plans_job
|
||||
|
||||
scheduler.add_job(
|
||||
check_customer_protect_time,
|
||||
@@ -27,6 +28,12 @@ def init_scheduled_tasks(get_db_session):
|
||||
id="check_customer_protect_time",
|
||||
replace_existing=True,
|
||||
)
|
||||
scheduler.add_job(
|
||||
auto_generate_monthly_plans_job,
|
||||
"cron", day=1, hour=1, minute=30,
|
||||
id="db_auto_generate_monthly_plans",
|
||||
replace_existing=True,
|
||||
)
|
||||
scheduler.add_job(
|
||||
remind_customer_recycle,
|
||||
"cron", hour=9, minute=0,
|
||||
@@ -47,6 +54,7 @@ def init_scheduled_tasks(get_db_session):
|
||||
)
|
||||
logger.info(
|
||||
"已注册定时任务: check_customer_protect_time(01:00), "
|
||||
"db_auto_generate_monthly_plans(每月1日01:30), "
|
||||
"pool_recovery_job(02:00), remind_customer_recycle(09:00), "
|
||||
"warn_customer_recycle(10:00)"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
"""地磅计划按月自动生成定时任务"""
|
||||
import logging
|
||||
|
||||
from app.core.database import SessionLocal
|
||||
from app.services.db.auto_generate_plans import run_auto_generate_monthly_plans
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def auto_generate_monthly_plans_job():
|
||||
"""每月 1 日凌晨执行:按上月计划模板生成本月计划。"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
logger.info("======== 开始执行地磅计划按月自动生成任务 ========")
|
||||
result = run_auto_generate_monthly_plans(db, force=False)
|
||||
logger.info(
|
||||
"======== 地磅计划按月自动生成任务完成:created=%s, month=%s, skipped=%s ========",
|
||||
result.get("created_count"),
|
||||
result.get("current_month"),
|
||||
result.get("skipped_reason"),
|
||||
)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
logger.exception("地磅计划按月自动生成任务异常")
|
||||
finally:
|
||||
db.close()
|
||||
Reference in new issue
Block a user