支持具备 changeApplicant 权限时代填申请人及发票/余额联动;系统/CRM/BX/PL 主数据与下拉选项改为仅租户隔离,避免「仅本人」导致空选项。 Co-authored-by: Cursor <cursoragent@cursor.com>
362 lines
13 KiB
Python
362 lines
13 KiB
Python
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import Session
|
|
|
|
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.expense_item import ExpenseItemService
|
|
from app.utils.bx_codegen import generate_apply_code
|
|
from app.utils.bx_date_range import append_create_time_filters
|
|
|
|
router = APIRouter(prefix="/advances", tags=["备用金管理"])
|
|
|
|
|
|
def _tenant(db: Session, user: BxUserContext) -> str:
|
|
return get_tenant_no(db, user.to_tenant_user_dict())
|
|
|
|
|
|
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)
|
|
|
|
|
|
@router.get("/", response_model=ApiResponse[PagedData[AdvanceApplyOut]])
|
|
def list_advances(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
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)
|
|
filters = []
|
|
if status:
|
|
filters.append(BxAdvanceApply.status == status)
|
|
if keyword:
|
|
filters.append(
|
|
or_(
|
|
BxAdvanceApply.apply_code.contains(keyword),
|
|
BxAdvanceApply.applicant_name.contains(keyword),
|
|
)
|
|
)
|
|
if applicantId:
|
|
filters.append(BxAdvanceApply.applicant_id == str(applicantId).strip())
|
|
append_create_time_filters(
|
|
filters,
|
|
BxAdvanceApply,
|
|
apply_date=applyDate,
|
|
apply_date_from=applyDateFrom,
|
|
apply_date_to=applyDateTo,
|
|
)
|
|
rows, total = AdvanceService.get_paged(db, tenant_no, page=page, page_size=pageSize, filters=filters, user=user)
|
|
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(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
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)
|
|
filters = [
|
|
or_(
|
|
BxAdvanceApply.applicant_id == user.id,
|
|
BxAdvanceApply.create_by == user.id,
|
|
)
|
|
]
|
|
if status:
|
|
filters.append(BxAdvanceApply.status == status)
|
|
if keyword:
|
|
filters.append(
|
|
or_(
|
|
BxAdvanceApply.apply_code.contains(keyword),
|
|
BxAdvanceApply.applicant_name.contains(keyword),
|
|
)
|
|
)
|
|
append_create_time_filters(
|
|
filters,
|
|
BxAdvanceApply,
|
|
apply_date=applyDate,
|
|
apply_date_from=applyDateFrom,
|
|
apply_date_to=applyDateTo,
|
|
)
|
|
rows, total = AdvanceService.get_paged(db, tenant_no, page=page, page_size=pageSize, filters=filters, user=user)
|
|
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(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
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)
|
|
applicant_id = str(applicantId).strip() if applicantId else None
|
|
rows, total = AdvanceService.pending_approval(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
keyword=keyword,
|
|
applicant_id=applicant_id,
|
|
apply_date=applyDate,
|
|
apply_date_from=applyDateFrom,
|
|
apply_date_to=applyDateTo,
|
|
)
|
|
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(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
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)
|
|
applicant_id = str(applicantId).strip() if applicantId else None
|
|
rows, total = AdvanceService.approved_by_me(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
keyword=keyword,
|
|
applicant_id=applicant_id,
|
|
apply_date=applyDate,
|
|
apply_date_from=applyDateFrom,
|
|
apply_date_to=applyDateTo,
|
|
)
|
|
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("/{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["apply_code"] = generate_apply_code(db, "BYJ", tenant_no)
|
|
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.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)
|