382 lines
14 KiB
Python
382 lines
14 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 BxReimburseApply, BxWorkflowDef
|
|
from app.schemas.bx.common import ApiResponse, PagedData
|
|
from app.schemas.bx.reimburse import (
|
|
ReimburseApplyCreate,
|
|
ReimburseApplyOut,
|
|
ReimburseApplyUpdate,
|
|
ReimburseApprovalCreate,
|
|
ReimburseApprovalOut,
|
|
ReimburseApproverPendingStatsOut,
|
|
ReimburseMyStatsOut,
|
|
)
|
|
from app.services.bx.expense_item import ExpenseItemService
|
|
from app.services.bx.reimburse import ReimburseService
|
|
from app.utils.bx_codegen import generate_apply_code, generate_title
|
|
from app.utils.bx_date_range import parse_create_time_day_bounds
|
|
|
|
router = APIRouter(prefix="/reimburses", tags=["报销管理"])
|
|
|
|
|
|
def _tenant(db: Session, user: BxUserContext) -> str:
|
|
return get_tenant_no(db, user.to_tenant_user_dict())
|
|
|
|
|
|
def _build_details_data(db: Session, tenant_no: str, body_details) -> List[dict]:
|
|
details_data: List[dict] = []
|
|
for d in body_details:
|
|
item = ExpenseItemService.get_by_id(db, d.expense_item_id, tenant_no)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"费用项目 {d.expense_item_id} 不存在",
|
|
)
|
|
details_data.append(
|
|
{
|
|
"expense_id": d.expense_item_id,
|
|
"expense_name": item.item_name,
|
|
"amount": d.amount,
|
|
"remark": d.remark,
|
|
}
|
|
)
|
|
return details_data
|
|
|
|
|
|
@router.get("/generate-title", response_model=ApiResponse[str])
|
|
def get_generated_title(
|
|
reimburseDate: Optional[str] = None,
|
|
excludeApplyId: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
exclude_id = str(excludeApplyId).strip() if excludeApplyId else None
|
|
title = generate_title(db, user.name, tenant_no, reimburseDate, exclude_id)
|
|
return ApiResponse(data=title)
|
|
|
|
|
|
@router.get("/", response_model=ApiResponse[PagedData[ReimburseApplyOut]])
|
|
def list_reimburses(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
status: Optional[str] = None,
|
|
keyword: str = "",
|
|
expense_id: Optional[str] = None,
|
|
applicantId: Optional[str] = None,
|
|
applyDate: 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(BxReimburseApply.status == status)
|
|
if keyword:
|
|
filters.append(
|
|
or_(
|
|
BxReimburseApply.apply_code.contains(keyword),
|
|
BxReimburseApply.applicant_name.contains(keyword),
|
|
)
|
|
)
|
|
if expense_id:
|
|
filters.append(BxReimburseApply.expense_id == str(expense_id).strip())
|
|
if applicantId:
|
|
filters.append(BxReimburseApply.applicant_id == str(applicantId).strip())
|
|
bounds = parse_create_time_day_bounds(applyDate)
|
|
if bounds:
|
|
d0, d1 = bounds
|
|
filters.append(BxReimburseApply.create_time >= d0)
|
|
filters.append(BxReimburseApply.create_time < d1)
|
|
rows, total = ReimburseService.get_paged(db, tenant_no, page=page, page_size=pageSize, filters=filters)
|
|
out_rows = ReimburseService.out_rows_with_attachment_counts(db, tenant_no, list(rows))
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/my", response_model=ApiResponse[PagedData[ReimburseApplyOut]])
|
|
def my_reimburses(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
status: Optional[str] = None,
|
|
keyword: str = "",
|
|
applyDate: Optional[str] = None,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
filters = [
|
|
or_(
|
|
BxReimburseApply.applicant_id == user.id,
|
|
BxReimburseApply.create_by == user.id,
|
|
)
|
|
]
|
|
if status:
|
|
filters.append(BxReimburseApply.status == status)
|
|
if keyword:
|
|
filters.append(
|
|
or_(
|
|
BxReimburseApply.apply_code.contains(keyword),
|
|
BxReimburseApply.applicant_name.contains(keyword),
|
|
)
|
|
)
|
|
bounds = parse_create_time_day_bounds(applyDate)
|
|
if bounds:
|
|
d0, d1 = bounds
|
|
filters.append(BxReimburseApply.create_time >= d0)
|
|
filters.append(BxReimburseApply.create_time < d1)
|
|
rows, total = ReimburseService.get_paged(db, tenant_no, page=page, page_size=pageSize, filters=filters)
|
|
out_rows = ReimburseService.out_rows_with_attachment_counts(db, tenant_no, list(rows))
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/my/stats", response_model=ApiResponse[ReimburseMyStatsOut])
|
|
def my_reimburse_stats(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = ReimburseService.my_apply_stats(db, user.id, tenant_no)
|
|
return ApiResponse(data=ReimburseMyStatsOut.model_validate(data))
|
|
|
|
|
|
@router.get("/stats/status-counts", response_model=ApiResponse[dict])
|
|
def reimburse_status_counts(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
return ApiResponse(data=ReimburseService.global_status_counts(db, tenant_no))
|
|
|
|
|
|
@router.get("/pending-approval", response_model=ApiResponse[PagedData[ReimburseApplyOut]])
|
|
def pending_approval(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
keyword: str = "",
|
|
applicantId: Optional[str] = None,
|
|
applyDate: 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 = ReimburseService.pending_approval(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
keyword=keyword,
|
|
applicant_id=applicant_id,
|
|
apply_date=applyDate,
|
|
)
|
|
out_rows = ReimburseService.out_rows_with_attachment_counts(db, tenant_no, list(rows))
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/pending-approval/stats", response_model=ApiResponse[ReimburseApproverPendingStatsOut])
|
|
def pending_approval_stats(
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
data = ReimburseService.pending_approval_stats(db, user, tenant_no)
|
|
return ApiResponse(data=ReimburseApproverPendingStatsOut.model_validate(data))
|
|
|
|
|
|
@router.get("/approved-by-me", response_model=ApiResponse[PagedData[ReimburseApplyOut]])
|
|
def approved_by_me(
|
|
page: int = 1,
|
|
pageSize: int = 10,
|
|
keyword: str = "",
|
|
applicantId: Optional[str] = None,
|
|
applyDate: 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 = ReimburseService.approved_by_me(
|
|
db,
|
|
user,
|
|
tenant_no,
|
|
page=page,
|
|
page_size=pageSize,
|
|
keyword=keyword,
|
|
applicant_id=applicant_id,
|
|
apply_date=applyDate,
|
|
)
|
|
out_rows = ReimburseService.out_rows_with_attachment_counts(db, tenant_no, list(rows))
|
|
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
|
|
|
|
|
@router.get("/{reimburse_id}", response_model=ApiResponse[ReimburseApplyOut])
|
|
def get_reimburse(
|
|
reimburse_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
reimburse = ReimburseService.get_by_id(db, reimburse_id, tenant_no)
|
|
if not reimburse:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
out = ReimburseService.build_apply_out_with_details(db, tenant_no, reimburse)
|
|
return ApiResponse(data=out)
|
|
|
|
|
|
@router.get("/{reimburse_id}/approvals", response_model=ApiResponse[List[ReimburseApprovalOut]])
|
|
def get_approvals(
|
|
reimburse_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not ReimburseService.get_by_id(db, reimburse_id, tenant_no):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
approvals = ReimburseService.get_approvals(db, reimburse_id, tenant_no)
|
|
return ApiResponse(data=[ReimburseApprovalOut.model_validate(a) for a in approvals])
|
|
|
|
|
|
@router.get("/{reimburse_id}/approval-history", response_model=ApiResponse[List[ReimburseApprovalOut]])
|
|
def get_approval_history(
|
|
reimburse_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not ReimburseService.get_by_id(db, reimburse_id, tenant_no):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
history = ReimburseService.get_approval_history(db, reimburse_id, tenant_no)
|
|
return ApiResponse(data=[ReimburseApprovalOut.model_validate(a) for a in history])
|
|
|
|
|
|
@router.post("/", response_model=ApiResponse[ReimburseApplyOut])
|
|
def create_reimburse(
|
|
body: ReimburseApplyCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
details_data = _build_details_data(db, tenant_no, body.details)
|
|
data = {
|
|
"apply_code": generate_apply_code(db, "BX", tenant_no),
|
|
"status": "draft",
|
|
"remark": body.remark,
|
|
"reimburse_date": body.reimburse_date,
|
|
}
|
|
if body.title:
|
|
data["title"] = body.title
|
|
wf = (
|
|
db.query(BxWorkflowDef)
|
|
.filter(
|
|
BxWorkflowDef.biz_type == "reimburse",
|
|
BxWorkflowDef.tenant_no == tenant_no,
|
|
BxWorkflowDef.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if wf:
|
|
data["wf_id"] = wf.id
|
|
reimburse = ReimburseService.create_with_details(db, data, details_data, user=user)
|
|
db.commit()
|
|
db.refresh(reimburse)
|
|
out = ReimburseService.build_apply_out_with_details(db, tenant_no, reimburse)
|
|
return ApiResponse(data=out)
|
|
|
|
|
|
@router.put("/{reimburse_id}", response_model=ApiResponse[ReimburseApplyOut])
|
|
def update_reimburse(
|
|
reimburse_id: str,
|
|
body: ReimburseApplyUpdate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
details_data = None
|
|
if body.details is not None:
|
|
details_data = _build_details_data(db, tenant_no, body.details)
|
|
main_data = {}
|
|
if body.remark is not None:
|
|
main_data["remark"] = body.remark
|
|
if body.reimburse_date is not None:
|
|
main_data["reimburse_date"] = body.reimburse_date
|
|
if body.title is not None:
|
|
main_data["title"] = body.title
|
|
reimburse = ReimburseService.update_with_details(
|
|
db, reimburse_id, main_data, details_data, user=user
|
|
)
|
|
if not reimburse:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
db.commit()
|
|
db.refresh(reimburse)
|
|
out = ReimburseService.build_apply_out_with_details(db, tenant_no, reimburse)
|
|
return ApiResponse(data=out)
|
|
|
|
|
|
@router.post("/{reimburse_id}/submit", response_model=ApiResponse[ReimburseApplyOut])
|
|
def submit_reimburse(
|
|
reimburse_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
reimburse = ReimburseService.submit(db, reimburse_id, user)
|
|
if not reimburse:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
db.commit()
|
|
db.refresh(reimburse)
|
|
return ApiResponse(data=ReimburseService.out_one_with_attachment_counts(db, tenant_no, reimburse))
|
|
|
|
|
|
@router.post("/{reimburse_id}/approve", response_model=ApiResponse[ReimburseApplyOut])
|
|
def approve_reimburse(
|
|
reimburse_id: str,
|
|
body: ReimburseApprovalCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
reimburse = ReimburseService.approve(db, reimburse_id, user, body.approval_opinion or "")
|
|
if not reimburse:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在或无权审批")
|
|
db.commit()
|
|
db.refresh(reimburse)
|
|
return ApiResponse(data=ReimburseService.out_one_with_attachment_counts(db, tenant_no, reimburse))
|
|
|
|
|
|
@router.post("/{reimburse_id}/reject", response_model=ApiResponse[ReimburseApplyOut])
|
|
def reject_reimburse(
|
|
reimburse_id: str,
|
|
body: ReimburseApprovalCreate,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
reimburse = ReimburseService.reject(db, reimburse_id, user, body.approval_opinion or "")
|
|
if not reimburse:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在或无权审批")
|
|
db.commit()
|
|
db.refresh(reimburse)
|
|
return ApiResponse(data=ReimburseService.out_one_with_attachment_counts(db, tenant_no, reimburse))
|
|
|
|
|
|
@router.delete("/{reimburse_id}", response_model=ApiResponse[None])
|
|
def delete_reimburse(
|
|
reimburse_id: str,
|
|
db: Session = Depends(get_db),
|
|
user: BxUserContext = Depends(get_bx_current_user),
|
|
):
|
|
tenant_no = _tenant(db, user)
|
|
if not ReimburseService.get_by_id(db, reimburse_id, tenant_no):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="报销单不存在")
|
|
ReimburseService.soft_delete(db, reimburse_id, user=user)
|
|
db.commit()
|
|
return ApiResponse(data=None)
|