feat(bx): add list Excel export for reimbursement module pages
Wire ListExportDropdown on advance/reimburse/payment/expense/view/wallet lists, with matching POST export endpoints. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fe15a130cb
commit
dbba755fd8
16 files changed
+901
-11
No files matched your search
@@ -8,6 +8,14 @@ from fastapi.responses import StreamingResponse
|
||||
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
|
||||
@@ -42,6 +50,54 @@ def _tenant(db: Session, user: BxUserContext) -> str:
|
||||
return get_tenant_no(db, user.to_tenant_user_dict())
|
||||
|
||||
|
||||
REIMBURSE_EXPORT_COLUMNS = [
|
||||
ExportColumn(title="申请单号", field="applyCode"),
|
||||
ExportColumn(title="申请时间", field="applyTime"),
|
||||
ExportColumn(title="申请人", field="applicantName"),
|
||||
ExportColumn(title="报销主题", field="title"),
|
||||
ExportColumn(title="费用科目", field="expenseName"),
|
||||
ExportColumn(title="金额(元)", field="amount"),
|
||||
ExportColumn(title="状态", field="status"),
|
||||
ExportColumn(title="当前节点", field="currentNode"),
|
||||
]
|
||||
|
||||
|
||||
def _reimburse_list_filters(body: BxApplyListExportBody, db: Session, tenant_no: str) -> list:
|
||||
filters = []
|
||||
if body.status:
|
||||
filters.append(BxReimburseApply.status == body.status)
|
||||
if body.keyword:
|
||||
filters.append(
|
||||
or_(
|
||||
BxReimburseApply.apply_code.contains(body.keyword),
|
||||
BxReimburseApply.applicant_name.contains(body.keyword),
|
||||
BxReimburseApply.title.contains(body.keyword),
|
||||
)
|
||||
)
|
||||
if body.expense_id:
|
||||
filters.append(BxReimburseApply.expense_id == str(body.expense_id).strip())
|
||||
expense_filter = ReimburseService.build_expense_name_condition(db, tenant_no, body.expense_name)
|
||||
if expense_filter is not None:
|
||||
filters.append(expense_filter)
|
||||
if body.applicant_id:
|
||||
filters.append(BxReimburseApply.applicant_id == str(body.applicant_id).strip())
|
||||
if body.ids:
|
||||
filters.append(BxReimburseApply.id.in_([str(i) for i in body.ids]))
|
||||
append_create_time_filters(
|
||||
filters,
|
||||
BxReimburseApply,
|
||||
apply_date=body.apply_date,
|
||||
apply_date_from=body.apply_date_from,
|
||||
apply_date_to=body.apply_date_to,
|
||||
)
|
||||
return filters
|
||||
|
||||
|
||||
def _export_reimburse_rows(db: Session, tenant_no: str, rows) -> list:
|
||||
out_rows = ReimburseService.out_rows_with_attachment_counts(db, tenant_no, list(rows))
|
||||
return [dump_export_row(r) for r in out_rows]
|
||||
|
||||
|
||||
def _build_details_data(
|
||||
db: Session,
|
||||
tenant_no: str,
|
||||
@@ -327,6 +383,139 @@ def approved_by_me(
|
||||
return ApiResponse(data=PagedData(rows=out_rows, total=total, page=page, page_size=pageSize))
|
||||
|
||||
|
||||
|
||||
@router.post("/export")
|
||||
def export_reimburses(
|
||||
body: BxApplyListExportBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: BxUserContext = Depends(get_bx_current_user),
|
||||
):
|
||||
try:
|
||||
tenant_no = _tenant(db, user)
|
||||
filters = _reimburse_list_filters(body, db, tenant_no)
|
||||
rows, _ = ReimburseService.get_paged(
|
||||
db, tenant_no, page=1, page_size=EXPORT_MAX_ROWS, filters=filters, user=user
|
||||
)
|
||||
return respond_xlsx(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
sheet_title="报销申请",
|
||||
columns=REIMBURSE_EXPORT_COLUMNS,
|
||||
rows=_export_reimburse_rows(db, tenant_no, 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_reimburses(
|
||||
body: BxApplyListExportBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: BxUserContext = Depends(get_bx_current_user),
|
||||
):
|
||||
try:
|
||||
tenant_no = _tenant(db, user)
|
||||
filters = [
|
||||
or_(
|
||||
BxReimburseApply.applicant_id == user.id,
|
||||
BxReimburseApply.create_by == user.id,
|
||||
)
|
||||
]
|
||||
filters.extend(_reimburse_list_filters(body, db, tenant_no))
|
||||
rows, _ = ReimburseService.get_paged(
|
||||
db, tenant_no, page=1, page_size=EXPORT_MAX_ROWS, filters=filters, user=user
|
||||
)
|
||||
return respond_xlsx(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
sheet_title="我的报销",
|
||||
columns=REIMBURSE_EXPORT_COLUMNS,
|
||||
rows=_export_reimburse_rows(db, tenant_no, 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_reimburses(
|
||||
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, _ = ReimburseService.pending_approval(
|
||||
db,
|
||||
user,
|
||||
tenant_no,
|
||||
page=1,
|
||||
page_size=EXPORT_MAX_ROWS,
|
||||
keyword=body.keyword or "",
|
||||
applicant_id=applicant_id,
|
||||
expense_name=body.expense_name,
|
||||
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]
|
||||
return respond_xlsx(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
sheet_title="报销待审批",
|
||||
columns=REIMBURSE_EXPORT_COLUMNS,
|
||||
rows=_export_reimburse_rows(db, tenant_no, 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_reimburses(
|
||||
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, _ = ReimburseService.approved_by_me(
|
||||
db,
|
||||
user,
|
||||
tenant_no,
|
||||
page=1,
|
||||
page_size=EXPORT_MAX_ROWS,
|
||||
keyword=body.keyword or "",
|
||||
applicant_id=applicant_id,
|
||||
expense_name=body.expense_name,
|
||||
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]
|
||||
return respond_xlsx(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
sheet_title="报销已审批",
|
||||
columns=REIMBURSE_EXPORT_COLUMNS,
|
||||
rows=_export_reimburse_rows(db, tenant_no, rows),
|
||||
filename_stem="报销已审批",
|
||||
requested_columns=body.columns,
|
||||
)
|
||||
except ExportValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
|
||||
|
||||
@router.get("/{reimburse_id}", response_model=ApiResponse[ReimburseApplyOut])
|
||||
def get_reimburse(
|
||||
reimburse_id: str,
|
||||
|
||||
Reference in new issue
Block a user