fix(financeiro): alinhar saldo, métricas e relatórios

This commit is contained in:
Felipe Coutinho
2026-04-03 18:10:43 +00:00
parent acaf9d5c27
commit 549a5bdba1
32 changed files with 960 additions and 118 deletions

View File

@@ -1,4 +1,4 @@
import { and, asc, eq, gte, inArray, lte, ne, sum } from "drizzle-orm";
import { and, asc, eq, gte, inArray, lte, sum } from "drizzle-orm";
import { financialAccounts, transactions } from "@/db/schema";
import type { DashboardCardMetrics } from "@/features/dashboard/dashboard-metrics-queries";
import type {
@@ -9,6 +9,7 @@ import {
buildDashboardAdminFilters,
excludeAutoInvoiceEntries,
excludeInitialBalanceWhenConfigured,
excludeTransactionsFromExcludedAccounts,
} from "@/features/dashboard/transaction-filters";
import { db } from "@/shared/lib/db";
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
@@ -30,6 +31,7 @@ const TRANSACTION_TYPE_TRANSFER = "Transferência";
type PeriodTotals = {
receitas: number;
despesas: number;
transferAdjustment: number;
balanco: number;
};
@@ -37,6 +39,7 @@ type PeriodSummaryRow = {
period: string | null;
transactionType: string;
totalAmount: string | number | null;
accountExcludeFromBalance: boolean | null;
};
export type DashboardPeriodOverview = {
@@ -47,6 +50,7 @@ export type DashboardPeriodOverview = {
const createEmptyTotals = (): PeriodTotals => ({
receitas: 0,
despesas: 0,
transferAdjustment: 0,
balanco: 0,
});
@@ -106,6 +110,7 @@ export async function fetchDashboardPeriodOverview(
period: transactions.period,
transactionType: transactions.transactionType,
totalAmount: sum(transactions.amount).as("total"),
accountExcludeFromBalance: financialAccounts.excludeFromBalance,
})
.from(transactions)
.leftJoin(
@@ -120,13 +125,18 @@ export async function fetchDashboardPeriodOverview(
inArray(transactions.transactionType, [
TRANSACTION_TYPE_INCOME,
TRANSACTION_TYPE_EXPENSE,
TRANSACTION_TYPE_TRANSFER,
]),
ne(transactions.transactionType, TRANSACTION_TYPE_TRANSFER),
excludeAutoInvoiceEntries(),
excludeInitialBalanceWhenConfigured(),
excludeTransactionsFromExcludedAccounts(),
),
)
.groupBy(transactions.period, transactions.transactionType)
.groupBy(
transactions.period,
transactions.transactionType,
financialAccounts.excludeFromBalance,
)
.orderBy(
asc(transactions.period),
asc(transactions.transactionType),
@@ -146,6 +156,11 @@ export async function fetchDashboardPeriodOverview(
totals.receitas += total;
} else if (row.transactionType === TRANSACTION_TYPE_EXPENSE) {
totals.despesas += Math.abs(total);
} else if (
row.transactionType === TRANSACTION_TYPE_TRANSFER &&
row.accountExcludeFromBalance === false
) {
totals.transferAdjustment += total;
}
}
@@ -164,7 +179,8 @@ export async function fetchDashboardPeriodOverview(
for (const key of periodRange) {
const totals = ensurePeriodTotals(periodTotals, key);
totals.balanco = totals.receitas - totals.despesas;
totals.balanco =
totals.receitas - totals.despesas + totals.transferAdjustment;
runningForecast += totals.balanco;
forecastByPeriod.set(key, runningForecast);
}
@@ -179,7 +195,7 @@ export async function fetchDashboardPeriodOverview(
monthLabel: formatPeriodMonthShort(chartPeriod).toLowerCase(),
income: entry.receitas,
expense: entry.despesas,
balance: entry.receitas - entry.despesas,
balance: entry.balanco,
};
});