diff --git a/backend/app/services/bx/reimburse_list_filters.py b/backend/app/services/bx/reimburse_list_filters.py new file mode 100644 index 000000000..ad74bee1e --- /dev/null +++ b/backend/app/services/bx/reimburse_list_filters.py @@ -0,0 +1,130 @@ +"""报销申请列表列头/导出共用过滤条件拼装。""" +from __future__ import annotations + +from decimal import Decimal, InvalidOperation +from typing import List, Optional + +from sqlalchemy import or_ +from sqlalchemy.orm import Session + +from app.models.bx_models import BxReimburseApply +from app.services.bx.reimburse import ReimburseService +from app.utils.bx_date_range import append_create_time_filters + +_AMOUNT_OPS = frozenset({"between", "eq", "gt", "gte", "lt", "lte"}) + + +def _parse_decimal(raw: Optional[str]) -> Optional[Decimal]: + text = str(raw or "").strip() + if not text: + return None + try: + return Decimal(text) + except (InvalidOperation, ValueError): + return None + + +def build_reimburse_column_filters( + *, + db: Optional[Session], + tenant_no: str, + keyword: str = "", + status: Optional[str] = None, + status_in: Optional[str] = None, + apply_code: Optional[str] = None, + title: Optional[str] = None, + expense_name: Optional[str] = None, + expense_id: Optional[str] = None, + applicant_id: Optional[str] = None, + current_node: Optional[str] = None, + amount_op: Optional[str] = None, + amount: Optional[str] = None, + amount_min: Optional[str] = None, + amount_max: Optional[str] = None, + apply_date: Optional[str] = None, + apply_date_from: Optional[str] = None, + apply_date_to: Optional[str] = None, + ids: Optional[List[str]] = None, +) -> list: + filters: list = [] + + statuses: List[str] = [] + if status_in and str(status_in).strip(): + statuses = [s.strip() for s in str(status_in).split(",") if s.strip()] + elif status and str(status).strip(): + statuses = [str(status).strip()] + if statuses: + filters.append(BxReimburseApply.status.in_(statuses)) + + kw = (keyword or "").strip() + if kw: + filters.append( + or_( + BxReimburseApply.apply_code.contains(kw), + BxReimburseApply.applicant_name.contains(kw), + BxReimburseApply.title.contains(kw), + ) + ) + + code = (apply_code or "").strip() + if code: + filters.append(BxReimburseApply.apply_code.contains(code)) + + title_kw = (title or "").strip() + if title_kw: + filters.append(BxReimburseApply.title.contains(title_kw)) + + node_kw = (current_node or "").strip() + if node_kw: + filters.append(BxReimburseApply.current_node.contains(node_kw)) + + if expense_id and str(expense_id).strip(): + filters.append(BxReimburseApply.expense_id == str(expense_id).strip()) + + if db is not None: + expense_filter = ReimburseService.build_expense_name_condition(db, tenant_no, expense_name) + if expense_filter is not None: + filters.append(expense_filter) + else: + name_kw = (expense_name or "").strip() + if name_kw: + filters.append(BxReimburseApply.expense_name.contains(name_kw)) + + if applicant_id and str(applicant_id).strip(): + filters.append(BxReimburseApply.applicant_id == str(applicant_id).strip()) + + if ids: + filters.append(BxReimburseApply.id.in_([str(i) for i in ids])) + + op = (amount_op or "").strip() or "between" + if op in _AMOUNT_OPS: + if op == "between": + dmin = _parse_decimal(amount_min) + dmax = _parse_decimal(amount_max) + if dmin is not None: + filters.append(BxReimburseApply.amount >= dmin) + if dmax is not None: + filters.append(BxReimburseApply.amount <= dmax) + else: + dval = _parse_decimal(amount) + if dval is not None: + col = BxReimburseApply.amount + if op == "eq": + filters.append(col == dval) + elif op == "gt": + filters.append(col > dval) + elif op == "gte": + filters.append(col >= dval) + elif op == "lt": + filters.append(col < dval) + elif op == "lte": + filters.append(col <= dval) + + append_create_time_filters( + filters, + BxReimburseApply, + apply_date=apply_date, + apply_date_from=apply_date_from, + apply_date_to=apply_date_to, + ) + return filters diff --git a/backend/tests/test_bx_reimburse_list_filters.py b/backend/tests/test_bx_reimburse_list_filters.py new file mode 100644 index 000000000..e0515a30a --- /dev/null +++ b/backend/tests/test_bx_reimburse_list_filters.py @@ -0,0 +1,74 @@ +"""报销申请列表列头/导出共用过滤条件拼装单测。""" +from __future__ import annotations + +from sqlalchemy.dialects import mysql + +from app.services.bx.reimburse_list_filters import build_reimburse_column_filters + + +def _compile(clause) -> str: + return str( + clause.compile(dialect=mysql.dialect(), compile_kwargs={"literal_binds": True}) + ).lower() + + +def test_status_in_adds_in_clause(): + filters = build_reimburse_column_filters( + db=None, + tenant_no="shanxia2.0", + status_in="draft,pending", + ) + assert len(filters) == 1 + sql = _compile(filters[0]) + assert "status" in sql + assert "in (" in sql + assert "draft" in sql + assert "pending" in sql + + +def test_apply_code_contains(): + filters = build_reimburse_column_filters( + db=None, + tenant_no="shanxia2.0", + apply_code="BXZ", + ) + assert len(filters) == 1 + sql = _compile(filters[0]) + assert "apply_code" in sql + assert "bxz" in sql + + +def test_amount_between_min_only(): + filters = build_reimburse_column_filters( + db=None, + tenant_no="shanxia2.0", + amount_op="between", + amount_min="100", + ) + assert len(filters) == 1 + sql = _compile(filters[0]) + assert "amount" in sql + assert ">=" in sql + assert "100" in sql + + +def test_amount_eq(): + filters = build_reimburse_column_filters( + db=None, + tenant_no="shanxia2.0", + amount_op="eq", + amount="500", + ) + assert len(filters) == 1 + sql = _compile(filters[0]) + assert "amount" in sql + assert "=" in sql + assert "500" in sql + + +def test_empty_params_yield_no_filters(): + filters = build_reimburse_column_filters( + db=None, + tenant_no="shanxia2.0", + ) + assert filters == []