409 lines
13 KiB
Python
409 lines
13 KiB
Python
"""车辆充值审批:待办、通过、驳回、发放登记。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.services.wechat_approval_notices import sync_approval_notifications
|
|
from app.services.wechat_completion_notices import sync_completion_notification
|
|
|
|
from app.services.pl_vehicle_fuel_card_service import attach_recharge_list_bindings
|
|
from app.core.db_auth import DbUserContext
|
|
from app.core.utils import generate_uuid
|
|
from app.models.bx_models import BxWorkflowNode
|
|
from app.models.pl_models import (
|
|
PlVehicleRechargeApply,
|
|
PlVehicleRechargeApproval,
|
|
PlVehicleRechargeRecord,
|
|
)
|
|
from app.schemas.pl.vehicle_recharge import (
|
|
VehicleRechargeApplyRead,
|
|
VehicleRechargeRecordCreate,
|
|
)
|
|
from app.services.pl.vehicle_recharge_list_filters import (
|
|
build_recharge_apply_column_filters,
|
|
)
|
|
from app.services.pl_vehicle_recharge_service import (
|
|
apply_to_read,
|
|
generate_recharge_order_no,
|
|
_workflow_nodes,
|
|
)
|
|
|
|
|
|
def _get_apply_for_approval(
|
|
db: Session, tenant_no: str, apply_id: str
|
|
) -> PlVehicleRechargeApply:
|
|
row = (
|
|
db.query(PlVehicleRechargeApply)
|
|
.filter(
|
|
PlVehicleRechargeApply.id == apply_id,
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
.populate_existing()
|
|
.with_for_update()
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="申请单不存在")
|
|
return row
|
|
|
|
|
|
def _current_node(
|
|
db: Session,
|
|
apply: PlVehicleRechargeApply,
|
|
user: DbUserContext,
|
|
tenant_no: str,
|
|
) -> BxWorkflowNode:
|
|
if apply.status != "in_approval" or not apply.current_node:
|
|
raise HTTPException(status_code=400, detail="申请单不在审批中")
|
|
node = (
|
|
db.query(BxWorkflowNode)
|
|
.filter(
|
|
BxWorkflowNode.id == apply.current_node,
|
|
BxWorkflowNode.tenant_no == tenant_no,
|
|
BxWorkflowNode.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not node:
|
|
raise HTTPException(status_code=400, detail="当前审批节点不存在或已删除")
|
|
if node.approver_id != user.id:
|
|
raise HTTPException(status_code=403, detail="无权审批当前节点")
|
|
return node
|
|
|
|
|
|
def _append_approval(
|
|
db: Session,
|
|
*,
|
|
apply: PlVehicleRechargeApply,
|
|
node: BxWorkflowNode,
|
|
user: DbUserContext,
|
|
tenant_no: str,
|
|
status: str,
|
|
opinion: Optional[str],
|
|
now: datetime,
|
|
) -> PlVehicleRechargeApproval:
|
|
row = PlVehicleRechargeApproval(
|
|
id=generate_uuid(),
|
|
apply_id=apply.id,
|
|
approval_round=apply.approval_round or 1,
|
|
node_id=node.id,
|
|
node_name=node.node_name,
|
|
approver_id=user.id,
|
|
approver_name=user.name or "",
|
|
approval_status=status,
|
|
approval_opinion=(opinion or "").strip() or None,
|
|
approval_time=now,
|
|
tenant_no=tenant_no,
|
|
create_by=user.id,
|
|
create_dept_id=user.dept_id or "",
|
|
create_time=now,
|
|
update_by=user.id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
return row
|
|
|
|
|
|
def list_pending(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
apply_no: Optional[str] = None,
|
|
license_plate: Optional[str] = None,
|
|
recharge_type: Optional[str] = None,
|
|
recharge_type_in: Optional[str] = None,
|
|
status: Optional[str] = None,
|
|
status_in: Optional[str] = None,
|
|
apply_date_from=None,
|
|
apply_date_to=None,
|
|
applicant_name: Optional[str] = None,
|
|
remark: Optional[str] = None,
|
|
current_node_name: Optional[str] = None,
|
|
odometer_km_op: Optional[str] = None,
|
|
odometer_km: Optional[str] = None,
|
|
odometer_km_min: Optional[str] = None,
|
|
odometer_km_max: Optional[str] = None,
|
|
create_time_from=None,
|
|
create_time_to=None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> tuple[list[VehicleRechargeApplyRead], int]:
|
|
query = (
|
|
db.query(PlVehicleRechargeApply)
|
|
.join(
|
|
BxWorkflowNode,
|
|
BxWorkflowNode.id == PlVehicleRechargeApply.current_node,
|
|
)
|
|
.filter(
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
PlVehicleRechargeApply.status == "in_approval",
|
|
BxWorkflowNode.tenant_no == tenant_no,
|
|
BxWorkflowNode.del_flag == 0,
|
|
BxWorkflowNode.approver_id == user.id,
|
|
)
|
|
)
|
|
for cond in build_recharge_apply_column_filters(
|
|
apply_no=apply_no,
|
|
license_plate=license_plate,
|
|
recharge_type=recharge_type,
|
|
recharge_type_in=recharge_type_in,
|
|
status=status,
|
|
status_in=status_in,
|
|
apply_date_from=apply_date_from,
|
|
apply_date_to=apply_date_to,
|
|
applicant_name=applicant_name,
|
|
remark=remark,
|
|
current_node_name=current_node_name,
|
|
odometer_km_op=odometer_km_op,
|
|
odometer_km=odometer_km,
|
|
odometer_km_min=odometer_km_min,
|
|
odometer_km_max=odometer_km_max,
|
|
create_time_from=create_time_from,
|
|
create_time_to=create_time_to,
|
|
):
|
|
query = query.filter(cond)
|
|
# 待办按当前节点审批人授权,不做 create_by 数据范围过滤
|
|
total = query.count()
|
|
page = max(1, page)
|
|
page_size = min(max(1, page_size), 500)
|
|
rows = (
|
|
query.order_by(PlVehicleRechargeApply.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return attach_recharge_list_bindings(db, tenant_no, [apply_to_read(row, db, include_detail=True) for row in rows]), total
|
|
|
|
|
|
def list_done(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
apply_no: Optional[str] = None,
|
|
license_plate: Optional[str] = None,
|
|
recharge_type: Optional[str] = None,
|
|
recharge_type_in: Optional[str] = None,
|
|
status: Optional[str] = None,
|
|
status_in: Optional[str] = None,
|
|
apply_date_from=None,
|
|
apply_date_to=None,
|
|
applicant_name: Optional[str] = None,
|
|
remark: Optional[str] = None,
|
|
current_node_name: Optional[str] = None,
|
|
odometer_km_op: Optional[str] = None,
|
|
odometer_km: Optional[str] = None,
|
|
odometer_km_min: Optional[str] = None,
|
|
odometer_km_max: Optional[str] = None,
|
|
create_time_from=None,
|
|
create_time_to=None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> tuple[list[VehicleRechargeApplyRead], int]:
|
|
apply_ids = [
|
|
r[0]
|
|
for r in (
|
|
db.query(PlVehicleRechargeApproval.apply_id)
|
|
.filter(
|
|
PlVehicleRechargeApproval.tenant_no == tenant_no,
|
|
PlVehicleRechargeApproval.del_flag == 0,
|
|
PlVehicleRechargeApproval.approver_id == user.id,
|
|
)
|
|
.distinct()
|
|
.all()
|
|
)
|
|
]
|
|
if not apply_ids:
|
|
return [], 0
|
|
query = db.query(PlVehicleRechargeApply).filter(
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
PlVehicleRechargeApply.id.in_(apply_ids),
|
|
)
|
|
for cond in build_recharge_apply_column_filters(
|
|
apply_no=apply_no,
|
|
license_plate=license_plate,
|
|
recharge_type=recharge_type,
|
|
recharge_type_in=recharge_type_in,
|
|
status=status,
|
|
status_in=status_in,
|
|
apply_date_from=apply_date_from,
|
|
apply_date_to=apply_date_to,
|
|
applicant_name=applicant_name,
|
|
remark=remark,
|
|
current_node_name=current_node_name,
|
|
odometer_km_op=odometer_km_op,
|
|
odometer_km=odometer_km,
|
|
odometer_km_min=odometer_km_min,
|
|
odometer_km_max=odometer_km_max,
|
|
create_time_from=create_time_from,
|
|
create_time_to=create_time_to,
|
|
):
|
|
query = query.filter(cond)
|
|
total = query.count()
|
|
page = max(1, page)
|
|
page_size = min(max(1, page_size), 500)
|
|
rows = (
|
|
query.order_by(PlVehicleRechargeApply.update_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return attach_recharge_list_bindings(db, tenant_no, [apply_to_read(row, db, include_detail=True) for row in rows]), total
|
|
|
|
|
|
def approve_apply(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
apply_id: str,
|
|
*,
|
|
opinion: Optional[str] = None,
|
|
) -> VehicleRechargeApplyRead:
|
|
apply = _get_apply_for_approval(db, tenant_no, apply_id)
|
|
node = _current_node(db, apply, user, tenant_no)
|
|
action = (node.action_type or "approve").strip() or "approve"
|
|
if action != "approve":
|
|
raise HTTPException(status_code=400, detail="当前节点请使用发放登记")
|
|
nodes = _workflow_nodes(db, apply.wf_id, tenant_no)
|
|
idx = next((i for i, n in enumerate(nodes) if n.id == node.id), None)
|
|
if idx is None:
|
|
raise HTTPException(status_code=400, detail="当前审批节点不在流程中")
|
|
if idx >= len(nodes) - 1:
|
|
raise HTTPException(status_code=400, detail="末节点须为发放登记,不能仅通过")
|
|
now = datetime.now()
|
|
_append_approval(
|
|
db,
|
|
apply=apply,
|
|
node=node,
|
|
user=user,
|
|
tenant_no=tenant_no,
|
|
status="approved",
|
|
opinion=opinion,
|
|
now=now,
|
|
)
|
|
apply.current_node = nodes[idx + 1].id
|
|
apply.update_by = user.id
|
|
apply.update_time = now
|
|
sync_approval_notifications(
|
|
db, "pl_recharge", apply_id, tenant_no, actor_id=user.id
|
|
)
|
|
db.commit()
|
|
db.refresh(apply)
|
|
return apply_to_read(apply, db, include_detail=True)
|
|
|
|
|
|
def reject_apply(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
apply_id: str,
|
|
*,
|
|
opinion: Optional[str] = None,
|
|
) -> VehicleRechargeApplyRead:
|
|
apply = _get_apply_for_approval(db, tenant_no, apply_id)
|
|
node = _current_node(db, apply, user, tenant_no)
|
|
opinion_text = (opinion or "").strip()
|
|
if not opinion_text:
|
|
raise HTTPException(status_code=400, detail="驳回须填写意见")
|
|
now = datetime.now()
|
|
_append_approval(
|
|
db,
|
|
apply=apply,
|
|
node=node,
|
|
user=user,
|
|
tenant_no=tenant_no,
|
|
status="rejected",
|
|
opinion=opinion_text,
|
|
now=now,
|
|
)
|
|
apply.status = "rejected"
|
|
apply.current_node = None
|
|
apply.update_by = user.id
|
|
apply.update_time = now
|
|
sync_approval_notifications(
|
|
db, "pl_recharge", apply_id, tenant_no, actor_id=user.id
|
|
)
|
|
db.commit()
|
|
db.refresh(apply)
|
|
return apply_to_read(apply, db, include_detail=True)
|
|
|
|
|
|
def disburse_apply(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
apply_id: str,
|
|
payload: VehicleRechargeRecordCreate,
|
|
) -> VehicleRechargeApplyRead:
|
|
apply = _get_apply_for_approval(db, tenant_no, apply_id)
|
|
node = _current_node(db, apply, user, tenant_no)
|
|
action = (node.action_type or "approve").strip() or "approve"
|
|
if action != "disburse":
|
|
raise HTTPException(status_code=400, detail="当前节点不是发放节点")
|
|
if payload.recharge_type != apply.recharge_type:
|
|
raise HTTPException(status_code=400, detail="充值类型与申请单不一致")
|
|
existing = (
|
|
db.query(PlVehicleRechargeRecord.id)
|
|
.filter(
|
|
PlVehicleRechargeRecord.apply_id == apply.id,
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if existing:
|
|
raise HTTPException(status_code=400, detail="该申请单已有发放记录")
|
|
|
|
now = datetime.now()
|
|
record = PlVehicleRechargeRecord(
|
|
id=generate_uuid(),
|
|
recharge_order_no=generate_recharge_order_no(db),
|
|
recharge_date=payload.recharge_date,
|
|
recharge_type=apply.recharge_type,
|
|
card_no=(payload.card_no or "").strip() or None,
|
|
gas_station=(payload.gas_station or "").strip() or None,
|
|
amount=payload.amount,
|
|
attachment_urls=payload.attachment_urls,
|
|
apply_id=apply.id,
|
|
license_plate=apply.license_plate,
|
|
tenant_no=tenant_no,
|
|
remark=payload.remark,
|
|
create_by=user.id,
|
|
create_dept_id=user.dept_id or "",
|
|
create_time=now,
|
|
update_by=user.id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(record)
|
|
_append_approval(
|
|
db,
|
|
apply=apply,
|
|
node=node,
|
|
user=user,
|
|
tenant_no=tenant_no,
|
|
status="disbursed",
|
|
opinion=payload.remark,
|
|
now=now,
|
|
)
|
|
apply.status = "disbursed"
|
|
apply.current_node = None
|
|
apply.update_by = user.id
|
|
apply.update_time = now
|
|
sync_completion_notification(
|
|
db, "pl_recharge", apply_id, tenant_no, actor_id=user.id,
|
|
event_id=generate_uuid(), occurred_at=now,
|
|
)
|
|
db.commit()
|
|
db.refresh(apply)
|
|
return apply_to_read(apply, db, include_detail=True)
|