fix: derivar período da fatura ao abrir dialog com cartão pré-preenchido
Quando o lancamento-dialog abre com cartaoId já definido (ex: vindo de pré-lançamentos), agora roda deriveCreditCardPeriod() na inicialização para calcular o período correto da fatura automaticamente, sem precisar trocar o cartão e voltar. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -35,6 +35,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
applyFieldDependencies,
|
applyFieldDependencies,
|
||||||
buildLancamentoInitialState,
|
buildLancamentoInitialState,
|
||||||
|
deriveCreditCardPeriod,
|
||||||
} from "@/lib/lancamentos/form-helpers";
|
} from "@/lib/lancamentos/form-helpers";
|
||||||
import { BasicFieldsSection } from "./basic-fields-section";
|
import { BasicFieldsSection } from "./basic-fields-section";
|
||||||
import { BoletoFieldsSection } from "./boleto-fields-section";
|
import { BoletoFieldsSection } from "./boleto-fields-section";
|
||||||
@@ -98,22 +99,40 @@ export function LancamentoDialog({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (dialogOpen) {
|
if (dialogOpen) {
|
||||||
setFormState(
|
const initial = buildLancamentoInitialState(
|
||||||
buildLancamentoInitialState(
|
lancamento,
|
||||||
lancamento,
|
defaultPagadorId,
|
||||||
defaultPagadorId,
|
defaultPeriod,
|
||||||
defaultPeriod,
|
{
|
||||||
{
|
defaultCartaoId,
|
||||||
defaultCartaoId,
|
defaultPaymentMethod,
|
||||||
defaultPaymentMethod,
|
defaultPurchaseDate,
|
||||||
defaultPurchaseDate,
|
defaultName,
|
||||||
defaultName,
|
defaultAmount,
|
||||||
defaultAmount,
|
defaultTransactionType,
|
||||||
defaultTransactionType,
|
isImporting,
|
||||||
isImporting,
|
},
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Derive credit card period on open when cartaoId is pre-filled
|
||||||
|
if (
|
||||||
|
initial.paymentMethod === "Cartão de crédito" &&
|
||||||
|
initial.cartaoId &&
|
||||||
|
initial.purchaseDate
|
||||||
|
) {
|
||||||
|
const card = cartaoOptions.find(
|
||||||
|
(opt) => opt.value === initial.cartaoId,
|
||||||
|
);
|
||||||
|
if (card?.closingDay) {
|
||||||
|
initial.period = deriveCreditCardPeriod(
|
||||||
|
initial.purchaseDate,
|
||||||
|
card.closingDay,
|
||||||
|
card.dueDay,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setFormState(initial);
|
||||||
setErrorMessage(null);
|
setErrorMessage(null);
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
@@ -128,6 +147,7 @@ export function LancamentoDialog({
|
|||||||
defaultAmount,
|
defaultAmount,
|
||||||
defaultTransactionType,
|
defaultTransactionType,
|
||||||
isImporting,
|
isImporting,
|
||||||
|
cartaoOptions,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const primaryPagador = formState.pagadorId;
|
const primaryPagador = formState.pagadorId;
|
||||||
|
|||||||
Reference in New Issue
Block a user