|
|
@@ -4,6 +4,14 @@ from fastapi import APIRouter, Depends, HTTPException, 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
|
|
|
@@ -38,6 +46,57 @@ def _fill_offset_amounts(db: Session, tenant_no: str, rows, out_rows: List[Advan
|
|
|
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 _advance_list_filters(body: BxApplyListExportBody) -> list:
|
|
|
+ filters = []
|
|
|
+ if body.status:
|
|
|
+ filters.append(BxAdvanceApply.status == body.status)
|
|
|
+ if body.keyword:
|
|
|
+ filters.append(
|
|
|
+ or_(
|
|
|
+ BxAdvanceApply.apply_code.contains(body.keyword),
|
|
|
+ BxAdvanceApply.applicant_name.contains(body.keyword),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ if body.applicant_id:
|
|
|
+ filters.append(BxAdvanceApply.applicant_id == str(body.applicant_id).strip())
|
|
|
+ if body.ids:
|
|
|
+ filters.append(BxAdvanceApply.id.in_([str(i) for i in body.ids]))
|
|
|
+ append_create_time_filters(
|
|
|
+ filters,
|
|
|
+ BxAdvanceApply,
|
|
|
+ apply_date=body.apply_date,
|
|
|
+ apply_date_from=body.apply_date_from,
|
|
|
+ apply_date_to=body.apply_date_to,
|
|
|
+ )
|
|
|
+ return filters
|
|
|
+
|
|
|
+
|
|
|
+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(
|
|
|
page: int = 1,
|
|
|
@@ -207,6 +266,140 @@ def approved_by_me(
|
|
|
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)
|
|
|
+ filters = _advance_list_filters(body)
|
|
|
+ rows, _ = AdvanceService.get_paged(
|
|
|
+ db, tenant_no, page=1, page_size=EXPORT_MAX_ROWS, filters=filters, user=user
|
|
|
+ )
|
|
|
+ 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)
|
|
|
+ filters = [
|
|
|
+ or_(
|
|
|
+ BxAdvanceApply.applicant_id == user.id,
|
|
|
+ BxAdvanceApply.create_by == user.id,
|
|
|
+ )
|
|
|
+ ]
|
|
|
+ filters.extend(_advance_list_filters(body))
|
|
|
+ rows, _ = AdvanceService.get_paged(
|
|
|
+ db, tenant_no, page=1, page_size=EXPORT_MAX_ROWS, filters=filters, user=user
|
|
|
+ )
|
|
|
+ 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)
|
|
|
+ applicant_id = str(body.applicant_id).strip() if body.applicant_id else None
|
|
|
+ rows, _ = AdvanceService.pending_approval(
|
|
|
+ db,
|
|
|
+ user,
|
|
|
+ tenant_no,
|
|
|
+ page=1,
|
|
|
+ page_size=EXPORT_MAX_ROWS,
|
|
|
+ keyword=body.keyword or "",
|
|
|
+ applicant_id=applicant_id,
|
|
|
+ apply_date=body.apply_date,
|
|
|
+ apply_date_from=body.apply_date_from,
|
|
|
+ apply_date_to=body.apply_date_to,
|
|
|
+ )
|
|
|
+ if body.ids:
|
|
|
+ id_set = {str(i) for i in body.ids}
|
|
|
+ rows = [r for r in rows if str(r.id) in id_set]
|
|
|
+ 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)
|
|
|
+ applicant_id = str(body.applicant_id).strip() if body.applicant_id else None
|
|
|
+ rows, _ = AdvanceService.approved_by_me(
|
|
|
+ db,
|
|
|
+ user,
|
|
|
+ tenant_no,
|
|
|
+ page=1,
|
|
|
+ page_size=EXPORT_MAX_ROWS,
|
|
|
+ keyword=body.keyword or "",
|
|
|
+ applicant_id=applicant_id,
|
|
|
+ apply_date=body.apply_date,
|
|
|
+ apply_date_from=body.apply_date_from,
|
|
|
+ apply_date_to=body.apply_date_to,
|
|
|
+ )
|
|
|
+ if body.ids:
|
|
|
+ id_set = {str(i) for i in body.ids}
|
|
|
+ rows = [r for r in rows if str(r.id) in id_set]
|
|
|
+ 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,
|