diff --git a/backend/app/api/budget.py b/backend/app/api/budget.py index 05e51ec4..d179b227 100644 --- a/backend/app/api/budget.py +++ b/backend/app/api/budget.py @@ -258,6 +258,7 @@ def auto_decompose_budget( last_year = year - 1 values = db.query(KPIValue).filter( KPIValue.kpi_id == kid, + actual_only_clause(), # 实测区口径:预估行禁作预算派生基数(kpi_value_scope) KPIValue.period.like(f"{last_year}-%"), KPIValue.actual_value.isnot(None), ).order_by(KPIValue.period.asc()).all() @@ -331,6 +332,7 @@ def auto_decompose_budget( last_year = year - 1 values = db.query(KPIValue).filter( KPIValue.kpi_id == kpi_id, + actual_only_clause(), # 实测区口径:预估行禁作预算派生基数(kpi_value_scope) KPIValue.period.like(f"{last_year}-%"), KPIValue.actual_value.isnot(None), ).order_by(KPIValue.period.asc()).all() diff --git a/backend/app/api/customer_dashboard.py b/backend/app/api/customer_dashboard.py index ac8da0c7..f611e0f6 100644 --- a/backend/app/api/customer_dashboard.py +++ b/backend/app/api/customer_dashboard.py @@ -9,6 +9,7 @@ from app.deps import get_entity_id from app.auth_middleware import require_auth, require_role from app.models import KPIDefinition, KPIValue, KPIAlert, User from app.utils.alert_entity_scope import month_period_clause +from app.utils.kpi_value_scope import actual_only_clause import logging logger = logging.getLogger("cma.customer") @@ -73,7 +74,7 @@ def list_customer_kpis( latest_val = sum(v.actual_value for v in values if v.actual_value) if values else None latest = type('obj', (object,), {"actual_value": latest_val, "period": f"{months[0]}~{months[-1]}"})() if latest_val else None elif period == "year": - values = base_query.filter(KPIValue.period.like(f"{period_str[:4]}%")).all() + values = base_query.filter(actual_only_clause(), KPIValue.period.like(f"{period_str[:4]}%")).all() latest_val = sum(v.actual_value for v in values if v.actual_value) if values else None latest = type('obj', (object,), {"actual_value": latest_val, "period": period_str[:4]})() if latest_val else None elif period == "custom" and start_date and end_date: diff --git a/backend/app/utils/cash_forecast_engine.py b/backend/app/utils/cash_forecast_engine.py index d8dddbb5..e56c3c6c 100644 --- a/backend/app/utils/cash_forecast_engine.py +++ b/backend/app/utils/cash_forecast_engine.py @@ -4,6 +4,7 @@ import os from datetime import datetime, timedelta from typing import Optional, TYPE_CHECKING from sqlalchemy.orm import Session +from app.utils.kpi_value_scope import actual_only_clause import math import random @@ -202,6 +203,7 @@ def get_entity_kpi_history(entity_id: int, kpi_code: str, db: Session, limit_mon return [] values = db.query(KPIValue).filter( KPIValue.kpi_id == kpi.id, + actual_only_clause(), # 实测区口径:预估行禁进历史/趋势序列(kpi_value_scope) KPIValue.actual_value.isnot(None), ).order_by(KPIValue.period.desc()).limit(limit_months).all() return values @@ -256,6 +258,7 @@ def get_current_cash_balance(db: Session, entity_id: int) -> Optional[float]: if kpi: latest_cash = db.query(KPIValue).filter( KPIValue.kpi_id == kpi.id, + actual_only_clause(), # 实测区口径:预估行禁作现金余额依据(kpi_value_scope) KPIValue.actual_value.isnot(None), ).order_by(KPIValue.period.desc()).first() if latest_cash and latest_cash.actual_value is not None: @@ -467,6 +470,7 @@ def calculate_accuracy(entity_id: int, db: Session) -> list: actuals = db.query(KPIValue).filter( KPIValue.kpi_id == cash_kpi.id, + actual_only_clause(), # 实测区口径:预估行禁进实际值序列(预测输入)(kpi_value_scope) KPIValue.actual_value.isnot(None), ).order_by(KPIValue.period.desc()).limit(12).all()