Merge branch 'dev' into picture-feedback

This commit is contained in:
HTEWJ\26070045 committed 2026-10-29 11:04:59 +08:00
commit 34ad40778b
180 files changed
+13262 -5333

No files matched your search

@@ -4,11 +4,13 @@ backend/app/services/contract_payment/ledger_focus.py
用途:合同台账汇总卡口径——即将到期、逾期未付、未到期(7日外)、本月应付,均按合同计数。
依赖:ContractPaymentContract、ContractPaymentNode、业务时区 Asia/Shanghai。
逻辑:由 list/summary API 调用;即将到期/逾期/未到期互斥,本月应付可与这三项重叠。行级「待补充」不进汇总卡。
金额和比例都为 0 的阶段不参与逾期、即将到期和本月应付。
"""
from __future__ import annotations
from collections import defaultdict
from decimal import Decimal, InvalidOperation
from datetime import date, datetime
from typing import Any, Iterable
from zoneinfo import ZoneInfo
@@ -58,6 +60,37 @@ def node_due_reference(due_date: date | None, anchor_date: date | None) -> date
return due_date or anchor_date
def stage_has_nothing_to_pay(*, status: str | None, percentage: object, amount: object) -> bool:
"""
判断付款阶段是否没有应付金额。
核心逻辑:无此阶段,或比例与金额都是 0(空值按 0)。已付阶段不按 0 处理。
与前台 isNothingToPay 一致,避免 0 元验收款、质保款把合同标成已逾期。
Args:
status: 节点状态。
percentage: 付款比例,可为 Decimal、数字或字符串。
amount: 应付金额,空值视为 0。
Returns:
无需付款时为 True。
"""
normalized = str(status or "").strip().lower()
if normalized == "not_applicable":
return True
if normalized == "paid":
return False
return _is_zero(percentage) and _is_zero(amount)
def _is_zero(value: object) -> bool:
"""空值和 0 都视为没有金额或比例;无法解析的值不当成 0。"""
if value is None or value == "":
return True
try:
return Decimal(str(value)) == 0
except (InvalidOperation, ValueError):
return False
def classify_ledger_status(
*,
contract_status: str | None,
@@ -69,11 +102,11 @@ def classify_ledger_status(
按合同聚合出台账状态,与列表「状态」列一致。
核心逻辑:非激活或待确认 → 待补充(仅行状态,不进汇总卡);无进行中节点则按是否已付分为已完成/待补充;
进行中节点若已过到期日 → 逾期,若在今天起 7 天内 → 即将到期,否则进行中。
金额和比例都为 0 的阶段不参与判断。其余进行中节点若已过到期日 → 逾期,若在今天起 7 天内 → 即将到期,否则进行中。
Args:
contract_status: 合同主状态。
parse_status: 解析状态。
nodes: 轻量节点 dict,含 status / due_date / anchor_date / paid_at。
nodes: 轻量节点 dict,含 status / due_date / anchor_date / paid_at / percentage / amount。
today: 业务日。
Returns:
overdue / due / active / completed / needs_completion。
@@ -83,7 +116,14 @@ def classify_ledger_status(
return "needs_completion"
if not node_list:
return "needs_completion"
active = [node for node in node_list if str(node.get("status") or "").strip().lower() == "active"]
active = [
node for node in node_list
if str(node.get("status") or "").strip().lower() == "active" and not stage_has_nothing_to_pay(
status=node.get("status"),
percentage=node.get("percentage"),
amount=node.get("amount"),
)
]
if not active:
has_paid = any(
str(node.get("status") or "").strip().lower() == "paid" or bool(node.get("paid_at"))
@@ -106,7 +146,7 @@ def has_unpaid_due_this_month(nodes: Iterable[dict[str, Any]], today: date) -> b
"""
合同是否有未付节点的到期日落在本自然月。
核心逻辑:只看 status=active 的节点;月份按 today 所在自然月(含 today 所在月的逾期节点)。
核心逻辑:只看 status=active 且仍有应付金额的节点;月份按 today 所在自然月(含 today 所在月的逾期节点)。
Args:
nodes: 轻量节点 dict。
today: 业务日,用于确定自然月。
@@ -118,6 +158,8 @@ def has_unpaid_due_this_month(nodes: Iterable[dict[str, Any]], today: date) -> b
for node in nodes:
if str(node.get("status") or "").strip().lower() != "active":
continue
if stage_has_nothing_to_pay(status=node.get("status"), percentage=node.get("percentage"), amount=node.get("amount")):
continue
ref = node_due_reference(node.get("due_date"), node.get("anchor_date"))
if ref and month_start <= ref < month_end:
return True
@@ -131,6 +173,8 @@ def _node_payload(node: ContractPaymentNode) -> dict[str, Any]:
"due_date": node.due_date,
"anchor_date": node.anchor_date,
"paid_at": (node.evidence_json or {}).get("paid_at"),
"percentage": node.percentage,
"amount": node.amount,
}