mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 19:01:47 +00:00
fix(financeiro): alinhar saldo, métricas e relatórios
This commit is contained in:
@@ -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,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user