643 lines
23 KiB
Python
643 lines
23 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status, Query
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.bx.export_common import (
|
|
EXPORT_MAX_ROWS,
|
|
ExportColumn,
|
|
ExportValidationError,
|
|
BxApplyListExportBody,
|
|
dump_export_row,
|
|
respond_xlsx,
|
|
)
|
|
from app.core.bx_auth import BxUserContext, get_bx_current_user
|
|
from app.core.database import get_db
|
|
from app.core.tenant_context import get_tenant_no
|
|
from app.models.bx_models import BxAdvanceApply, BxWorkflowDef
|
|
from app.schemas.bx.advance import (
|
|
AdvanceApplyCreate,
|
|
AdvanceApplyOut,
|
|
AdvanceApplyUpdate,
|
|
AdvanceApprovalCreate,
|
|
AdvanceApprovalOut,
|
|
AdvanceApproverPendingStatsOut,
|
|
AdvanceMyStatsOut,
|
|
)
|
|
from app.schemas.bx.common import ApiResponse, PagedData
|
|
from app.services.bx.advance import AdvanceService
|
|
from app.services.bx.advance_list_filters import build_advance_column_filters
|
|
from app.services.bx.expense_item import ExpenseItemService
|
|
from app.services.bx.list_sort import order_advance, parse_sort_params
|
|
|
|
router = APIRouter(prefix="/advances", tags=["备用金管理"])
|
|
|
|
|
|
def _tenant(db: Session, user: BxUserContext) -> str:
|
|
return get_tenant_no(db, user.to_tenant_user_dict())
|
|
|
|
|
|
def _advance_order(params: dict):
|
|
sf, so = parse_sort_params(params)
|
|
return order_advance(sf, so)
|
|
|
|
def _fill_offset_amounts(db: Session, tenant_no: str, rows, out_rows: List[AdvanceApplyOut]) -> None:
|
|
if not out_rows:
|
|
return
|
|
offset_map = AdvanceService.offset_amount_map(db, tenant_no, [r.id for r in rows])
|
|
for out_row in out_rows:
|
|
out_row.offset_amount = offset_map.get(out_row.id, 0.0)
|
|
|
|
|
|
ADVANCE_EXPORT_COLUMNS = [
|
|
ExportColumn(title="申请单号", field="applyCode"),
|
|
ExportColumn(title="申请时间", field="applyTime"),
|
|
ExportColumn(title="申请人", field="applicantName"),
|
|
ExportColumn(title="申请原因", field="reason"),
|
|
ExportColumn(title="申请金额(元)", field="amount"),
|
|
ExportColumn(title="实际发放金额(元)", field="actualAmount"),
|
|
ExportColumn(title="冲抵总金额(元)", field="offsetAmount"),
|
|
ExportColumn(title="状态", field="status"),
|
|
ExportColumn(title="当前节点", field="currentNode"),
|
|
]
|
|
|
|
|
|
def _merge_query(request: Request, **known) -> dict:
|
|
out = {k: v for k, v in known.items() if v is not None and v != ""}
|
|
for k, v in request.query_params.multi_items():
|
|
if k in ("page", "pageSize"):
|
|
continue
|
|
if k not in out and v not in (None, ""):
|
|
out[k] = v
|
|
return out
|
|
|
|
|
|
def _param_get(params: dict, *keys: str) -> Any:
|
|
for key in keys:
|
|
if key in params and params[key] not in (None, ""):
|
|
return params[key]
|
|
return None
|
|
|
|
|
|
def _filters_from_params(params: dict, *, tenant_no: str = "") -> list:
|
|
ids = _param_get(params, "ids")
|
|
if isinstance(ids, str):
|
|
ids = [s.strip() for s in ids.split(",") if s.strip()]
|
|
return build_advance_column_filters(
|
|
tenant_no=tenant_no,
|
|
keyword=str(_param_get(params, "keyword") or ""),
|
|
status=_param_get(params, "status"),
|
|
status_in=_param_get(params, "statusIn", "status_in"),
|
|
apply_code=_param_get(params, "applyCode", "apply_code"),
|
|
reason=_param_get(params, "reason"),
|
|
applicant_id=_param_get(params, "applicantId", "applicant_id"),
|
|
applicant_name=_param_get(params, "applicantName", "applicant_name"),
|
|
current_node=_param_get(params, "currentNode", "current_node"),
|
|
issue_by=_param_get(params, "issueBy", "issue_by"),
|
|
issue_user_id=_param_get(params, "issueUserId", "issue_user_id"),
|
|
issue_user_name=_param_get(params, "issueUserName", "issue_user_name"),
|
|
amount_op=_param_get(params, "amountOp", "amount_op"),
|
|
amount=_param_get(params, "amount"),
|
|
amount_min=_param_get(params, "amountMin", "amount_min"),
|
|
amount_max=_param_get(params, "amountMax", "amount_max"),
|
|
actual_amount_op=_param_get(params, "actualAmountOp", "actual_amount_op"),
|
|
actual_amount=_param_get(params, "actualAmount", "actual_amount"),
|
|
actual_amount_min=_param_get(params, "actualAmountMin", "actual_amount_min"),
|
|
actual_amount_max=_param_get(params, "actualAmountMax", "actual_amount_max"),
|
|
offset_amount_op=_param_get(params, "offsetAmountOp", "offset_amount_op"),
|
|
offset_amount=_param_get(params, "offsetAmount", "offset_amount"),
|
|
offset_amount_min=_param_get(params, "offsetAmountMin", "offset_amount_min"),
|
|
offset_amount_max=_param_get(params, "offsetAmountMax", "offset_amount_max"),
|
|
apply_date=_param_get(params, "applyDate", "apply_date"),
|
|
apply_date_from=_param_get(params, "applyDateFrom", "apply_date_from"),
|
|
apply_date_to=_param_get(params, "applyDateTo", "apply_date_to"),
|
|
issue_time_from=_param_get(params, "issueTimeFrom", "issue_time_from"),
|
|
issue_time_to=_param_get(params, "issueTimeTo", "issue_time_to"),
|
|
ids=ids if isinstance(ids, list) else None,
|
|
)
|
|
|
|
|
|
def _body_to_params(body: BxApplyListExportBody) -> dict:
|
|
data = body.model_dump(by_alias=True, exclude_none=True)
|
|
extra = getattr(body, "model_extra", None) or {}
|
|
for key, value in extra.items():
|
|
if key not in data and value not in (None, ""):
|
|
data[key] = value
|
|
return data
|
|
|
|
|
|
def _advance_list_filters(body: BxApplyListExportBody, *, tenant_no: str = "") -> list:
|
|
return _filters_from_params(_body_to_params(body), tenant_no=tenant_no)
|
|
|
|
|
|
def _export_advance_rows(
|
|
db: Session,
|
|
tenant_no: str,
|
|
rows,
|
|
*,
|
|
with_offset: bool = True,
|
|
) -> List[dict]:
|
|
out_rows = [AdvanceApplyOut.model_validate(r) for r in rows]
|
|
if with_offset:
|
|
_fill_offset_amounts(db, tenant_no, rows, out_rows)
|
|
return [dump_export_row(r) for r in out_rows]
|
|
|
|
|
|
@router.get("/", response_model=ApiResponse[PagedData[AdvanceApplyOut]])
|
|
def list_advances(
|
|
request: Request,
|
|
page: int = 1,
|
|
pageSize: int = Query(10, ge=1, le=500),
|
|
status: Optional[str] = None,
|
|
keyword: str = "",
|
|
applicantId: Optional[str] = None,
|
|
applyDate: Optional[str] = None,
|
|
applyDateFrom: Optional[str] = None,
|
|
applyDateTo: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
params = _merge_query(
|
|
request,
|
|
status=status,
|
|
keyword=keyword or None,
|
|
applicantId=applicantId,
|
|
applyDate=applyDate,
|
|
applyDateFrom=applyDateFrom,
|
|
applyDateTo=applyDateTo,
|
|
)
|
|
filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, total = AdvanceService.get_paged(
|
|
db,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
filters=filters,
|
|
user=user,
|
|
order_by=_advance_order(params),
|
|
)
|
|
out_rows = [AdvanceApplyOut.model_validate(r) for r in rows]
|
|
_fill_offset_amounts(db, tenant_no, rows, out_rows)
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/my", response_model=ApiResponse[PagedData[AdvanceApplyOut]])
|
|
def my_advances(
|
|
request: Request,
|
|
page: int = 1,
|
|
pageSize: int = Query(10, ge=1, le=500),
|
|
status: Optional[str] = None,
|
|
keyword: str = "",
|
|
applyDate: Optional[str] = None,
|
|
applyDateFrom: Optional[str] = None,
|
|
applyDateTo: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
params = _merge_query(
|
|
request,
|
|
status=status,
|
|
keyword=keyword or None,
|
|
applyDate=applyDate,
|
|
applyDateFrom=applyDateFrom,
|
|
applyDateTo=applyDateTo,
|
|
)
|
|
filters = [
|
|
or_(
|
|
BxAdvanceApply.applicant_id == user.id,
|
|
BxAdvanceApply.create_by == user.id,
|
|
)
|
|
]
|
|
filters.extend(_filters_from_params(params, tenant_no=tenant_no))
|
|
rows, total = AdvanceService.get_paged(
|
|
db,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
filters=filters,
|
|
user=user,
|
|
order_by=_advance_order(params),
|
|
)
|
|
out_rows = [AdvanceApplyOut.model_validate(r) for r in rows]
|
|
_fill_offset_amounts(db, tenant_no, rows, out_rows)
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/my/stats", response_model=ApiResponse[AdvanceMyStatsOut])
|
|
def my_advance_stats(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = AdvanceService.my_apply_stats(db, user, tenant_no)
|
|
return ApiResponse(data=AdvanceMyStatsOut.model_validate(data))
|
|
|
|
|
|
@router.get("/stats/status-counts", response_model=ApiResponse[dict])
|
|
def advance_status_counts(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
return ApiResponse(data=AdvanceService.global_status_counts(db, tenant_no, user=user))
|
|
|
|
|
|
@router.get("/pending-approval", response_model=ApiResponse[PagedData[AdvanceApplyOut]])
|
|
def pending_approval(
|
|
request: Request,
|
|
page: int = 1,
|
|
pageSize: int = Query(10, ge=1, le=500),
|
|
keyword: str = "",
|
|
applicantId: Optional[str] = None,
|
|
applyDate: Optional[str] = None,
|
|
applyDateFrom: Optional[str] = None,
|
|
applyDateTo: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
params = _merge_query(
|
|
request,
|
|
keyword=keyword or None,
|
|
applicantId=applicantId,
|
|
applyDate=applyDate,
|
|
applyDateFrom=applyDateFrom,
|
|
applyDateTo=applyDateTo,
|
|
)
|
|
# Tab 语义固定 pending;不把 status 列参传入 builder
|
|
params.pop("status", None)
|
|
params.pop("statusIn", None)
|
|
params.pop("status_in", None)
|
|
extra_filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, total = AdvanceService.pending_approval(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
extra_filters=extra_filters,
|
|
order_by=_advance_order(params),
|
|
)
|
|
out_rows = [AdvanceApplyOut.model_validate(r) for r in rows]
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/pending-approval/stats", response_model=ApiResponse[AdvanceApproverPendingStatsOut])
|
|
def advance_pending_approval_stats(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = AdvanceService.pending_approval_stats(db, user, tenant_no)
|
|
return ApiResponse(data=AdvanceApproverPendingStatsOut.model_validate(data))
|
|
|
|
|
|
@router.get("/approved-by-me", response_model=ApiResponse[PagedData[AdvanceApplyOut]])
|
|
def approved_by_me(
|
|
request: Request,
|
|
page: int = 1,
|
|
pageSize: int = Query(10, ge=1, le=500),
|
|
keyword: str = "",
|
|
applicantId: Optional[str] = None,
|
|
applyDate: Optional[str] = None,
|
|
applyDateFrom: Optional[str] = None,
|
|
applyDateTo: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
params = _merge_query(
|
|
request,
|
|
keyword=keyword or None,
|
|
applicantId=applicantId,
|
|
applyDate=applyDate,
|
|
applyDateFrom=applyDateFrom,
|
|
applyDateTo=applyDateTo,
|
|
)
|
|
extra_filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, total = AdvanceService.approved_by_me(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
extra_filters=extra_filters,
|
|
order_by=_advance_order(params),
|
|
)
|
|
out_rows = [AdvanceApplyOut.model_validate(r) for r in rows]
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.post("/export")
|
|
def export_advances(
|
|
body: BxApplyListExportBody,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
try:
|
|
tenant_no = _tenant(db, user)
|
|
params = _body_to_params(body)
|
|
filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, _ = AdvanceService.get_paged(
|
|
db,
|
|
tenant_no,
|
|
page=1,
|
|
page_size=EXPORT_MAX_ROWS,
|
|
filters=filters,
|
|
user=user,
|
|
order_by=_advance_order(params),
|
|
)
|
|
export_rows = _export_advance_rows(db, tenant_no, rows)
|
|
return respond_xlsx(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
sheet_title="备用金申请",
|
|
columns=ADVANCE_EXPORT_COLUMNS,
|
|
rows=export_rows,
|
|
filename_stem="备用金申请",
|
|
requested_columns=body.columns,
|
|
)
|
|
except ExportValidationError as e:
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
|
|
|
|
|
@router.post("/my/export")
|
|
def export_my_advances(
|
|
body: BxApplyListExportBody,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
try:
|
|
tenant_no = _tenant(db, user)
|
|
params = _body_to_params(body)
|
|
filters = [
|
|
or_(
|
|
BxAdvanceApply.applicant_id == user.id,
|
|
BxAdvanceApply.create_by == user.id,
|
|
)
|
|
]
|
|
filters.extend(_filters_from_params(params, tenant_no=tenant_no))
|
|
rows, _ = AdvanceService.get_paged(
|
|
db,
|
|
tenant_no,
|
|
page=1,
|
|
page_size=EXPORT_MAX_ROWS,
|
|
filters=filters,
|
|
user=user,
|
|
order_by=_advance_order(params),
|
|
)
|
|
export_rows = _export_advance_rows(db, tenant_no, rows)
|
|
return respond_xlsx(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
sheet_title="我的备用金",
|
|
columns=ADVANCE_EXPORT_COLUMNS,
|
|
rows=export_rows,
|
|
filename_stem="我的备用金申请",
|
|
requested_columns=body.columns,
|
|
)
|
|
except ExportValidationError as e:
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
|
|
|
|
|
@router.post("/pending-approval/export")
|
|
def export_pending_approval_advances(
|
|
body: BxApplyListExportBody,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
try:
|
|
tenant_no = _tenant(db, user)
|
|
params = _body_to_params(body)
|
|
params.pop("status", None)
|
|
params.pop("statusIn", None)
|
|
params.pop("status_in", None)
|
|
extra_filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, _ = AdvanceService.pending_approval(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=1,
|
|
page_size=EXPORT_MAX_ROWS,
|
|
extra_filters=extra_filters,
|
|
order_by=_advance_order(params),
|
|
)
|
|
export_rows = _export_advance_rows(db, tenant_no, rows, with_offset=False)
|
|
return respond_xlsx(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
sheet_title="备用金待审批",
|
|
columns=ADVANCE_EXPORT_COLUMNS,
|
|
rows=export_rows,
|
|
filename_stem="备用金待审批",
|
|
requested_columns=body.columns,
|
|
)
|
|
except ExportValidationError as e:
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
|
|
|
|
|
@router.post("/approved-by-me/export")
|
|
def export_approved_by_me_advances(
|
|
body: BxApplyListExportBody,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
try:
|
|
tenant_no = _tenant(db, user)
|
|
params = _body_to_params(body)
|
|
extra_filters = _filters_from_params(params, tenant_no=tenant_no)
|
|
rows, _ = AdvanceService.approved_by_me(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=1,
|
|
page_size=EXPORT_MAX_ROWS,
|
|
extra_filters=extra_filters,
|
|
order_by=_advance_order(params),
|
|
)
|
|
export_rows = _export_advance_rows(db, tenant_no, rows, with_offset=False)
|
|
return respond_xlsx(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
sheet_title="备用金已审批",
|
|
columns=ADVANCE_EXPORT_COLUMNS,
|
|
rows=export_rows,
|
|
filename_stem="备用金已审批",
|
|
requested_columns=body.columns,
|
|
)
|
|
except ExportValidationError as e:
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
|
|
|
|
|
@router.get("/{advance_id}", response_model=ApiResponse[AdvanceApplyOut])
|
|
def get_advance(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
advance = AdvanceService.get_by_id(db, advance_id, tenant_no, user=user)
|
|
if not advance:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
out = AdvanceApplyOut.model_validate(advance)
|
|
offset_map = AdvanceService.offset_amount_map(db, tenant_no, [advance_id])
|
|
out.offset_amount = offset_map.get(advance_id, 0.0)
|
|
return ApiResponse(data=out)
|
|
|
|
|
|
@router.get("/{advance_id}/approvals", response_model=ApiResponse[List[AdvanceApprovalOut]])
|
|
def get_approvals(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not AdvanceService.get_by_id(db, advance_id, tenant_no, user=user):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
approvals = AdvanceService.get_approvals(db, advance_id, tenant_no, user=user)
|
|
return ApiResponse(data=[AdvanceApprovalOut.model_validate(a) for a in approvals])
|
|
|
|
|
|
@router.get("/{advance_id}/approval-history", response_model=ApiResponse[List[AdvanceApprovalOut]])
|
|
def get_approval_history(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not AdvanceService.get_by_id(db, advance_id, tenant_no, user=user):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
history = AdvanceService.get_approval_history(db, advance_id, tenant_no, user=user)
|
|
return ApiResponse(data=[AdvanceApprovalOut.model_validate(a) for a in history])
|
|
|
|
|
|
@router.post("/", response_model=ApiResponse[AdvanceApplyOut])
|
|
def create_advance(
|
|
body: AdvanceApplyCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = body.model_dump(exclude_unset=True)
|
|
eid = data.get("expense_item_id")
|
|
if eid:
|
|
if not ExpenseItemService.get_by_id(db, eid, tenant_no, user=user, skip_scope=True):
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"费用项目 {eid} 不存在")
|
|
data["applicant_id"] = user.id
|
|
data["applicant_name"] = user.name
|
|
data["status"] = "draft"
|
|
wf = (
|
|
db.query(BxWorkflowDef)
|
|
.filter(
|
|
BxWorkflowDef.biz_type == "advance",
|
|
BxWorkflowDef.tenant_no == tenant_no,
|
|
BxWorkflowDef.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if wf:
|
|
data["wf_id"] = wf.id
|
|
advance = AdvanceService.create(db, data, user=user)
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.put("/{advance_id}", response_model=ApiResponse[AdvanceApplyOut])
|
|
def update_advance(
|
|
advance_id: str,
|
|
body: AdvanceApplyUpdate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = body.model_dump(exclude_unset=True)
|
|
if data.get("expense_item_id"):
|
|
eid = data["expense_item_id"]
|
|
if not ExpenseItemService.get_by_id(db, eid, tenant_no, user=user, skip_scope=True):
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"费用项目 {eid} 不存在")
|
|
advance = AdvanceService.update(db, advance_id, data, user=user)
|
|
if not advance:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.post("/{advance_id}/submit", response_model=ApiResponse[AdvanceApplyOut])
|
|
def submit_advance(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
advance = AdvanceService.submit(db, advance_id, user)
|
|
if not advance:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.post("/{advance_id}/approve", response_model=ApiResponse[AdvanceApplyOut])
|
|
def approve_advance(
|
|
advance_id: str,
|
|
body: AdvanceApprovalCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
advance = AdvanceService.approve(db, advance_id, user, body.approval_opinion or "")
|
|
if not advance:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在或无权审批")
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.post("/{advance_id}/reject", response_model=ApiResponse[AdvanceApplyOut])
|
|
def reject_advance(
|
|
advance_id: str,
|
|
body: AdvanceApprovalCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
advance = AdvanceService.reject(db, advance_id, user, body.approval_opinion or "")
|
|
if not advance:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在或无权审批")
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.post("/{advance_id}/withdraw", response_model=ApiResponse[AdvanceApplyOut])
|
|
def withdraw_advance(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
advance = AdvanceService.withdraw(db, advance_id, user)
|
|
if not advance:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="备用金申请不存在、非本人申请或当前状态不可撤回",
|
|
)
|
|
db.commit()
|
|
db.refresh(advance)
|
|
return ApiResponse(data=AdvanceApplyOut.model_validate(advance))
|
|
|
|
|
|
@router.delete("/{advance_id}", response_model=ApiResponse[None])
|
|
def delete_advance(
|
|
advance_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not AdvanceService.get_by_id(db, advance_id, tenant_no, user=user):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="备用金申请不存在")
|
|
AdvanceService.soft_delete(db, advance_id, user=user)
|
|
db.commit()
|
|
return ApiResponse(data=None)
|