mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
- botão "Ajustar fatura" na página da fatura abre dialog com input do valor real e preview da diferença; action faz upsert/delete idempotente do lançamento de ajuste - opção "Reembolso" no dropdown de ações de despesas à vista cria receita espelhada no extrato ou fatura correta, vinculada ao lançamento original - botão "Ajustar saldo" no extrato da conta compara saldo real informado e gera lançamento de ajuste por (accountId, period) via upsert/delete idempotente - constantes INVOICE_ADJUSTMENT_NAME, ACCOUNT_BALANCE_ADJUSTMENT_NAME, REFUND_NOTE_PREFIX e buildRefundNote() centralizadas em shared/lib/accounts/constants.ts - extrato agora contabiliza transferências internas em Entradas e Saídas corretamente Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
209 lines
5.9 KiB
TypeScript
209 lines
5.9 KiB
TypeScript
import { RiPencilLine } from "@remixicon/react";
|
|
import { notFound } from "next/navigation";
|
|
import { connection } from "next/server";
|
|
import { AccountDialog } from "@/features/accounts/components/account-dialog";
|
|
import { AccountStatementCard } from "@/features/accounts/components/account-statement-card";
|
|
import { AdjustBalanceDialog } from "@/features/accounts/components/adjust-balance-dialog";
|
|
import type { Account } from "@/features/accounts/components/types";
|
|
import {
|
|
fetchAccountData,
|
|
fetchAccountLancamentosPage,
|
|
fetchAccountSummary,
|
|
} from "@/features/accounts/statement-queries";
|
|
import { fetchUserPreferences } from "@/features/settings/queries";
|
|
import { TransactionsPage as LancamentosSection } from "@/features/transactions/components/page/transactions-page";
|
|
import {
|
|
buildOptionSets,
|
|
buildSluggedFilters,
|
|
buildSlugMaps,
|
|
buildTransactionWhere,
|
|
extractTransactionSearchFilters,
|
|
getSingleParam,
|
|
mapTransactionsData,
|
|
type ResolvedSearchParams,
|
|
resolveTransactionPagination,
|
|
} from "@/features/transactions/page-helpers";
|
|
import {
|
|
fetchRecentEstablishments,
|
|
fetchTransactionFilterSources,
|
|
} from "@/features/transactions/queries";
|
|
import MonthNavigation from "@/shared/components/month-picker/month-navigation";
|
|
import { Button } from "@/shared/components/ui/button";
|
|
import { getUserId } from "@/shared/lib/auth/server";
|
|
import { loadLogoOptions } from "@/shared/lib/logo/options";
|
|
import { parsePeriodParam } from "@/shared/utils/period";
|
|
|
|
type PageSearchParams = Promise<ResolvedSearchParams>;
|
|
|
|
type PageProps = {
|
|
params: Promise<{ accountId: string }>;
|
|
searchParams?: PageSearchParams;
|
|
};
|
|
|
|
const capitalize = (value: string) =>
|
|
value.length > 0 ? value[0]?.toUpperCase().concat(value.slice(1)) : value;
|
|
|
|
export default async function Page({ params, searchParams }: PageProps) {
|
|
await connection();
|
|
const { accountId } = await params;
|
|
const userId = await getUserId();
|
|
const resolvedSearchParams = searchParams ? await searchParams : undefined;
|
|
|
|
const periodoParamRaw = getSingleParam(resolvedSearchParams, "periodo");
|
|
const {
|
|
period: selectedPeriod,
|
|
monthName,
|
|
year,
|
|
} = parsePeriodParam(periodoParamRaw);
|
|
|
|
const searchFilters = extractTransactionSearchFilters(resolvedSearchParams);
|
|
const pagination = resolveTransactionPagination(resolvedSearchParams);
|
|
|
|
const account = await fetchAccountData(userId, accountId);
|
|
|
|
if (!account) {
|
|
notFound();
|
|
}
|
|
|
|
const [
|
|
filterSources,
|
|
logoOptions,
|
|
accountSummary,
|
|
estabelecimentos,
|
|
userPreferences,
|
|
] = await Promise.all([
|
|
fetchTransactionFilterSources(userId),
|
|
loadLogoOptions(),
|
|
fetchAccountSummary(userId, accountId, selectedPeriod),
|
|
fetchRecentEstablishments(userId),
|
|
fetchUserPreferences(userId),
|
|
]);
|
|
const sluggedFilters = buildSluggedFilters(filterSources);
|
|
const slugMaps = buildSlugMaps(sluggedFilters);
|
|
|
|
const filters = buildTransactionWhere({
|
|
userId,
|
|
period: selectedPeriod,
|
|
filters: searchFilters,
|
|
slugMaps,
|
|
accountId: account.id,
|
|
});
|
|
|
|
const transactionsPage = await fetchAccountLancamentosPage(
|
|
filters,
|
|
pagination,
|
|
);
|
|
|
|
const transactionData = mapTransactionsData(transactionsPage.rows);
|
|
|
|
const { openingBalance, currentBalance, totalIncomes, totalExpenses } =
|
|
accountSummary;
|
|
|
|
const periodLabel = `${capitalize(monthName)} de ${year}`;
|
|
|
|
const accountDialogData: Account = {
|
|
id: account.id,
|
|
name: account.name,
|
|
accountType: account.accountType,
|
|
status: account.status,
|
|
note: account.note,
|
|
logo: account.logo,
|
|
initialBalance: Number(account.initialBalance ?? 0),
|
|
balance: currentBalance,
|
|
};
|
|
|
|
const {
|
|
payerOptions,
|
|
splitPayerOptions,
|
|
defaultPayerId,
|
|
accountOptions,
|
|
cardOptions,
|
|
categoryOptions,
|
|
payerFilterOptions,
|
|
categoryFilterOptions,
|
|
accountCardFilterOptions,
|
|
} = buildOptionSets({
|
|
...sluggedFilters,
|
|
payerRows: filterSources.payerRows,
|
|
limitContaId: account.id,
|
|
});
|
|
|
|
return (
|
|
<main className="flex flex-col gap-6">
|
|
<MonthNavigation />
|
|
|
|
<AccountStatementCard
|
|
accountName={account.name}
|
|
accountType={account.accountType}
|
|
status={account.status}
|
|
periodLabel={periodLabel}
|
|
openingBalance={openingBalance}
|
|
currentBalance={currentBalance}
|
|
totalIncomes={totalIncomes}
|
|
totalExpenses={totalExpenses}
|
|
logo={account.logo}
|
|
balanceAdjustment={
|
|
<AdjustBalanceDialog
|
|
accountId={account.id}
|
|
period={selectedPeriod}
|
|
currentBalance={currentBalance}
|
|
/>
|
|
}
|
|
actions={
|
|
<AccountDialog
|
|
mode="update"
|
|
account={accountDialogData}
|
|
logoOptions={logoOptions}
|
|
trigger={
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="text-muted-foreground hover:text-foreground"
|
|
aria-label="Editar conta"
|
|
>
|
|
<RiPencilLine className="size-4" />
|
|
</Button>
|
|
}
|
|
/>
|
|
}
|
|
/>
|
|
|
|
<section className="flex flex-col gap-4">
|
|
<LancamentosSection
|
|
currentUserId={userId}
|
|
transactions={transactionData}
|
|
payerOptions={payerOptions}
|
|
splitPayerOptions={splitPayerOptions}
|
|
defaultPayerId={defaultPayerId}
|
|
accountOptions={accountOptions}
|
|
cardOptions={cardOptions}
|
|
categoryOptions={categoryOptions}
|
|
payerFilterOptions={payerFilterOptions}
|
|
categoryFilterOptions={categoryFilterOptions}
|
|
accountCardFilterOptions={accountCardFilterOptions}
|
|
selectedPeriod={selectedPeriod}
|
|
estabelecimentos={estabelecimentos}
|
|
pagination={{
|
|
page: transactionsPage.page,
|
|
pageSize: transactionsPage.pageSize,
|
|
totalItems: transactionsPage.totalItems,
|
|
totalPages: transactionsPage.totalPages,
|
|
}}
|
|
exportContext={{
|
|
source: "account-statement",
|
|
period: selectedPeriod,
|
|
filters: searchFilters,
|
|
accountId: account.id,
|
|
settledOnly: true,
|
|
}}
|
|
allowCreate={false}
|
|
noteAsColumn={userPreferences?.statementNoteAsColumn ?? false}
|
|
columnOrder={userPreferences?.transactionsColumnOrder ?? null}
|
|
attachmentMaxSizeMb={userPreferences?.attachmentMaxSizeMb ?? 50}
|
|
/>
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|