diff --git a/crud.py b/crud.py index c4c413d..805e415 100644 --- a/crud.py +++ b/crud.py @@ -243,12 +243,17 @@ async def get_client_balance_summary(client_id: str, as_of_time: Optional[dateti params ) - # Get total payments made (only those created before the cutoff time) + # Get total payments made (only those with ATM transaction time before the cutoff) + # Use transaction_time instead of created_at for temporal accuracy + payment_time_filter = "" + if as_of_time is not None: + payment_time_filter = "AND transaction_time <= :as_of_time" + total_payments_result = await db.fetchone( f""" SELECT COALESCE(SUM(amount_fiat), 0) as total FROM satoshimachine.dca_payments - WHERE client_id = :client_id AND status = 'confirmed' {time_filter} + WHERE client_id = :client_id AND status = 'confirmed' {payment_time_filter} """, params ) @@ -262,7 +267,7 @@ async def get_client_balance_summary(client_id: str, as_of_time: Optional[dateti from lnbits.core.services import logger # Verify timezone consistency for temporal filtering tz_info = "UTC" if as_of_time.tzinfo == timezone.utc else f"TZ: {as_of_time.tzinfo}" - logger.info(f"Client {client_id[:8]}... balance as of {as_of_time} ({tz_info}): {total_deposits - total_payments} centavos remaining") + logger.info(f"Client {client_id[:8]}... balance as of {as_of_time} ({tz_info}): deposits.confirmed_at <= cutoff, payments.transaction_time <= cutoff → {total_deposits - total_payments} centavos remaining") return ClientBalanceSummary( client_id=client_id,