Budget spend: simple DEBIT+/CREDIT- net formula, consistent with type flip

This commit is contained in:
2026-04-21 10:52:39 -04:00
parent 1a984d1eac
commit a9c12b94e1
2 changed files with 2 additions and 16 deletions

View File

@@ -27,14 +27,7 @@ export default async function BudgetsPage({ searchParams }: { searchParams: Sear
}), }),
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
SELECT t."budgetId", SELECT t."budgetId",
COALESCE(SUM( COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total
CASE
WHEN a.type = 'CREDIT_CARD' AND t.type = 'CREDIT' THEN t."amountCents"
WHEN a.type = 'CREDIT_CARD' AND t.type = 'DEBIT' THEN -t."amountCents"
WHEN t.type = 'DEBIT' THEN t."amountCents"
ELSE -t."amountCents"
END
), 0)::bigint AS total
FROM "Transaction" t FROM "Transaction" t
JOIN "Account" a ON t."accountId" = a.id JOIN "Account" a ON t."accountId" = a.id
WHERE a."userId" = ${userId} WHERE a."userId" = ${userId}

View File

@@ -42,14 +42,7 @@ export default async function DashboardPage({ searchParams }: { searchParams: Se
}), }),
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
SELECT t."budgetId", SELECT t."budgetId",
COALESCE(SUM( COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total
CASE
WHEN a.type = 'CREDIT_CARD' AND t.type = 'CREDIT' THEN t."amountCents"
WHEN a.type = 'CREDIT_CARD' AND t.type = 'DEBIT' THEN -t."amountCents"
WHEN t.type = 'DEBIT' THEN t."amountCents"
ELSE -t."amountCents"
END
), 0)::bigint AS total
FROM "Transaction" t FROM "Transaction" t
JOIN "Account" a ON t."accountId" = a.id JOIN "Account" a ON t."accountId" = a.id
WHERE a."userId" = ${userId} WHERE a."userId" = ${userId}