Refine client balance summary logic: Updated the get_client_balance_summary function to filter payments based on transaction_time for improved temporal accuracy. Enhanced logging to clarify balance calculations with respect to the cutoff time, ensuring better transparency in reported balances.

This commit is contained in:
padreug 2025-07-05 16:41:41 +02:00
parent 59e4d53215
commit 5988a25983

11
crud.py
View file

@ -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,