"""催缴纳入规则(纯函数)""" from __future__ import annotations from datetime import date, timedelta from decimal import Decimal from typing import Any, Dict, Iterable, Mapping, Optional, Tuple from app.services.sample_payment_period_summary import summarize_period def _dec(v: Any) -> Decimal: if v is None or v == "": return Decimal("0") return Decimal(str(v)) def chase_period_money( *, due_amount: Any, transfers: Iterable[Mapping[str, Any]], ) -> Tuple[Decimal, Decimal]: """已缴 = 仅 yjf;欠缴 = max(应缴 − 已缴, 0)。""" summary: Dict[str, Any] = summarize_period( due_amount=due_amount, transfers=transfers ) return summary["displayPaidAmount"], summary["arrearsAmount"] def is_chase_candidate(*, due_date: date, today: date, arrears: Any) -> bool: """欠缴>0,且应缴日已逾期或落在 [today, today+30]。""" if _dec(arrears) <= 0: return False if due_date < today: return True return today <= due_date <= today + timedelta(days=30) def parse_due_date_ymd(raw: Any) -> Optional[date]: value = str(raw or "") if len(value) == 8 and value.isdigit(): normalized = value elif ( len(value) == 10 and value[4] == "-" and value[7] == "-" and value[:4].isdigit() and value[5:7].isdigit() and value[8:].isdigit() ): normalized = value.replace("-", "") else: return None try: return date( int(normalized[:4]), int(normalized[4:6]), int(normalized[6:8]), ) except ValueError: return None