fix(boletos): diferencia pagamentos e recebimentos

This commit is contained in:
Felipe Coutinho
2026-05-31 15:18:55 -03:00
parent 35abe1b0bf
commit 99bc049cf4
10 changed files with 130 additions and 58 deletions

View File

@@ -81,6 +81,8 @@ const renderLancamento = (
const renderBoleto = (event: Extract<CalendarEvent, { type: "boleto" }>) => { const renderBoleto = (event: Extract<CalendarEvent, { type: "boleto" }>) => {
const isPaid = Boolean(event.transaction.isSettled); const isPaid = Boolean(event.transaction.isSettled);
const isIncome = event.transaction.transactionType === "Receita";
const settlementLabel = isIncome ? "Recebido" : "Pago";
const dueDateLabel = formatFinancialDateLabel( const dueDateLabel = formatFinancialDateLabel(
event.transaction.dueDate, event.transaction.dueDate,
"Vence em", "Vence em",
@@ -89,7 +91,7 @@ const renderBoleto = (event: Extract<CalendarEvent, { type: "boleto" }>) => {
const paymentDateLabel = isPaid const paymentDateLabel = isPaid
? formatFinancialDateLabel( ? formatFinancialDateLabel(
event.transaction.boletoPaymentDate, event.transaction.boletoPaymentDate,
"Pago em", `${settlementLabel} em`,
DATE_FORMAT, DATE_FORMAT,
) )
: null; : null;
@@ -109,7 +111,9 @@ const renderBoleto = (event: Extract<CalendarEvent, { type: "boleto" }>) => {
<span className="text-success">{paymentDateLabel}</span> <span className="text-success">{paymentDateLabel}</span>
)} )}
</div> </div>
<Badge variant="outline">{isPaid ? "Pago" : "Pendente"}</Badge> <Badge variant="outline">
{isPaid ? settlementLabel : "Pendente"}
</Badge>
</div> </div>
<MoneyValues <MoneyValues
className="font-medium whitespace-nowrap" className="font-medium whitespace-nowrap"

View File

@@ -1,18 +1,28 @@
import type { DashboardBill } from "@/features/dashboard/bills/bills-queries"; import type { DashboardBill } from "@/features/dashboard/bills/bills-queries";
import type { PaymentDialogState } from "@/features/dashboard/payments/use-payment-dialog-controller"; import type { PaymentDialogState } from "@/features/dashboard/payments/use-payment-dialog-controller";
import { getBusinessDateString, isDateOnlyPast } from "@/shared/utils/date"; import {
getBusinessDateString,
isDateOnlyPast,
parseUtcDateString,
toDateOnlyString,
} from "@/shared/utils/date";
import { import {
buildFinancialStatusLabel, buildFinancialStatusLabel,
buildRelativeFinancialStatusLabel, buildRelativeFinancialStatusLabel,
formatFinancialDateLabel, formatFinancialDateLabel,
formatRelativeFinancialDateLabel,
} from "@/shared/utils/financial-dates"; } from "@/shared/utils/financial-dates";
export type BillDialogState = PaymentDialogState; export type BillDialogState = PaymentDialogState;
type BillStatusDateItem = Pick< type BillStatusDateItem = Pick<
DashboardBill, DashboardBill,
"dueDate" | "boletoPaymentDate" | "isSettled" "dueDate" | "boletoPaymentDate" | "isSettled" | "transactionType"
>; >;
export const isIncomeBill = (bill: Pick<DashboardBill, "transactionType">) => {
return bill.transactionType === "Receita";
};
export const formatBillDateLabel = (value: string | null, prefix?: string) => { export const formatBillDateLabel = (value: string | null, prefix?: string) => {
return formatFinancialDateLabel(value, prefix); return formatFinancialDateLabel(value, prefix);
}; };
@@ -22,10 +32,15 @@ export const buildBillStatusLabel = (bill: BillStatusDateItem) => {
isSettled: bill.isSettled, isSettled: bill.isSettled,
dueDate: bill.dueDate, dueDate: bill.dueDate,
paidAt: bill.boletoPaymentDate, paidAt: bill.boletoPaymentDate,
paidPrefix: isIncomeBill(bill) ? "Recebido em" : "Pago em",
}); });
}; };
export const buildBillWidgetStatusLabel = (bill: BillStatusDateItem) => { export const buildBillWidgetStatusLabel = (bill: BillStatusDateItem) => {
if (bill.isSettled && isIncomeBill(bill)) {
return formatRelativeFinancialDateLabel(bill.boletoPaymentDate, "received");
}
return buildRelativeFinancialStatusLabel({ return buildRelativeFinancialStatusLabel({
isSettled: bill.isSettled, isSettled: bill.isSettled,
dueDate: bill.dueDate, dueDate: bill.dueDate,
@@ -43,6 +58,34 @@ export const isBillOverdue = (bill: DashboardBill) => {
return isDateOnlyPast(bill.dueDate); return isDateOnlyPast(bill.dueDate);
}; };
export const formatBillWidgetOverdueLabel = (
bill: Pick<DashboardBill, "dueDate" | "isSettled" | "transactionType">,
): string | null => {
if (bill.isSettled) {
return null;
}
const dueDateValue = toDateOnlyString(bill.dueDate);
const todayValue = getBusinessDateString();
if (!dueDateValue || dueDateValue >= todayValue) {
return null;
}
const dueDate = parseUtcDateString(dueDateValue);
const today = parseUtcDateString(todayValue);
if (!dueDate || !today) {
return null;
}
const overdueDays = Math.round(
(today.getTime() - dueDate.getTime()) / (24 * 60 * 60 * 1000),
);
const overdueLabel = isIncomeBill(bill) ? "Atrasada" : "Atrasado";
return overdueDays === 1
? `${overdueLabel} · venceu ontem`
: `${overdueLabel} · venceu há ${overdueDays} dias`;
};
export const getBillStatusBadgeVariant = ( export const getBillStatusBadgeVariant = (
statusLabel: string, statusLabel: string,
): "success" | "info" => { ): "success" | "info" => {

View File

@@ -1,9 +1,11 @@
import { RiCheckboxCircleFill, RiExternalLinkLine } from "@remixicon/react"; import { RiCheckboxCircleFill } from "@remixicon/react";
import Link from "next/link"; import Link from "next/link";
import { import {
buildBillStatusLabel, buildBillStatusLabel,
buildBillWidgetStatusLabel, buildBillWidgetStatusLabel,
formatBillWidgetOverdueLabel,
isBillOverdue, isBillOverdue,
isIncomeBill,
} from "@/features/dashboard/bills/bills-helpers"; } from "@/features/dashboard/bills/bills-helpers";
import type { DashboardBill } from "@/features/dashboard/bills/bills-queries"; import type { DashboardBill } from "@/features/dashboard/bills/bills-queries";
import { EstablishmentLogo } from "@/shared/components/entity-avatar"; import { EstablishmentLogo } from "@/shared/components/entity-avatar";
@@ -36,8 +38,10 @@ export function BillListItem({ bill, period, onPay }: BillListItemProps) {
const statusLabel = buildBillWidgetStatusLabel(bill); const statusLabel = buildBillWidgetStatusLabel(bill);
const absoluteStatusLabel = buildBillStatusLabel(bill); const absoluteStatusLabel = buildBillStatusLabel(bill);
const overdue = isBillOverdue(bill); const overdue = isBillOverdue(bill);
const income = isIncomeBill(bill);
const overdueLabel = formatBillWidgetOverdueLabel(bill);
const statusTooltipLabel = const statusTooltipLabel =
statusLabel && statusLabel !== absoluteStatusLabel overdueLabel || (statusLabel && statusLabel !== absoluteStatusLabel)
? absoluteStatusLabel ? absoluteStatusLabel
: null; : null;
const href = buildTransactionsHref(bill.name, period); const href = buildTransactionsHref(bill.name, period);
@@ -53,10 +57,6 @@ export function BillListItem({ bill, period, onPay }: BillListItemProps) {
className="inline-flex max-w-full items-center gap-1 text-sm font-medium text-foreground underline-offset-2 hover:text-primary hover:underline" className="inline-flex max-w-full items-center gap-1 text-sm font-medium text-foreground underline-offset-2 hover:text-primary hover:underline"
> >
<span className="truncate">{bill.name}</span> <span className="truncate">{bill.name}</span>
<RiExternalLinkLine
className="size-3 shrink-0 text-muted-foreground"
aria-hidden
/>
</Link> </Link>
<div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground"> <div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
{statusLabel ? ( {statusLabel ? (
@@ -67,9 +67,10 @@ export function BillListItem({ bill, period, onPay }: BillListItemProps) {
className={cn( className={cn(
"cursor-help rounded-full py-0.5", "cursor-help rounded-full py-0.5",
bill.isSettled && "text-success font-semibold", bill.isSettled && "text-success font-semibold",
overdue && "text-destructive font-semibold",
)} )}
> >
{statusLabel} {overdueLabel ?? statusLabel}
</span> </span>
</TooltipTrigger> </TooltipTrigger>
<TooltipContent side="top"> <TooltipContent side="top">
@@ -81,9 +82,10 @@ export function BillListItem({ bill, period, onPay }: BillListItemProps) {
className={cn( className={cn(
"rounded-full py-0.5", "rounded-full py-0.5",
bill.isSettled && "text-success font-semibold", bill.isSettled && "text-success font-semibold",
overdue && "text-destructive font-semibold",
)} )}
> >
{statusLabel} {overdueLabel ?? statusLabel}
</span> </span>
) )
) : null} ) : null}
@@ -93,29 +95,35 @@ export function BillListItem({ bill, period, onPay }: BillListItemProps) {
<div className="flex shrink-0 flex-col items-end"> <div className="flex shrink-0 flex-col items-end">
<MoneyValues className="font-medium" amount={bill.amount} /> <MoneyValues className="font-medium" amount={bill.amount} />
<Button {bill.isSettled ? (
type="button" <span className="flex h-7 items-center gap-0.5 text-xs font-medium text-success">
size="sm" <RiCheckboxCircleFill className="size-3.5" />{" "}
variant="link" {income ? "Recebido" : "Pago"}
className="h-auto p-0 disabled:opacity-100" </span>
disabled={bill.isSettled} ) : (
onClick={() => onPay(bill.id)} <Button
> type="button"
{bill.isSettled ? ( size="sm"
<span className="flex items-center gap-0.5 text-success"> variant="link"
<RiCheckboxCircleFill className="size-3.5" /> Pago className="-mr-1.5 h-7 px-1.5 py-0"
</span> onClick={() => onPay(bill.id)}
) : overdue ? ( >
<span className="overdue-blink"> {overdue ? (
<span className="overdue-blink-primary text-destructive"> <span className="overdue-blink">
Atrasado <span className="overdue-blink-primary text-destructive">
{income ? "Atrasada" : "Atrasado"}
</span>
<span className="overdue-blink-secondary">
{income ? "Receber" : "Pagar"}
</span>
</span> </span>
<span className="overdue-blink-secondary">Pagar</span> ) : income ? (
</span> "Receber"
) : ( ) : (
"Pagar" "Pagar"
)} )}
</Button> </Button>
)}
</div> </div>
</li> </li>
); );

View File

@@ -7,6 +7,7 @@ import {
type BillDialogState, type BillDialogState,
formatBillDateLabel, formatBillDateLabel,
getBillStatusBadgeVariant, getBillStatusBadgeVariant,
isIncomeBill,
} from "@/features/dashboard/bills/bills-helpers"; } from "@/features/dashboard/bills/bills-helpers";
import type { import type {
BillPaymentAccountOption, BillPaymentAccountOption,
@@ -66,11 +67,13 @@ export function BillPaymentDialog({
onConfirm, onConfirm,
}: BillPaymentDialogProps) { }: BillPaymentDialogProps) {
const isProcessing = modalState === "processing" || isPending; const isProcessing = modalState === "processing" || isPending;
const income = bill ? isIncomeBill(bill) : false;
const settlementLabel = income ? "Recebido" : "Pago";
const dueLabel = bill const dueLabel = bill
? formatBillDateLabel(bill.dueDate, "Vencimento:") ? formatBillDateLabel(bill.dueDate, "Vencimento:")
: null; : null;
const paidLabel = bill const paidLabel = bill
? formatBillDateLabel(bill.boletoPaymentDate, "Pago em:") ? formatBillDateLabel(bill.boletoPaymentDate, `${settlementLabel} em:`)
: null; : null;
const isBillPending = bill ? !bill.isSettled : false; const isBillPending = bill ? !bill.isSettled : false;
const paymentDateValue = paymentDate.toISOString().split("T")[0] ?? ""; const paymentDateValue = paymentDate.toISOString().split("T")[0] ?? "";
@@ -103,8 +106,8 @@ export function BillPaymentDialog({
> >
{modalState === "success" ? ( {modalState === "success" ? (
<PaymentSuccess <PaymentSuccess
title="Pagamento registrado!" title={income ? "Recebimento registrado!" : "Pagamento registrado!"}
description="Atualizamos o status do boleto para pago. Em instantes ele aparecerá como baixado no histórico." description={`Atualizamos o status do boleto para ${income ? "recebido" : "pago"}. Em instantes ele aparecerá como baixado no histórico.`}
onClose={onClose} onClose={onClose}
/> />
) : ( ) : (
@@ -112,10 +115,12 @@ export function BillPaymentDialog({
<DialogHeader> <DialogHeader>
<div className="mb-1 flex items-center gap-3"> <div className="mb-1 flex items-center gap-3">
<div> <div>
<DialogTitle>Confirmar pagamento</DialogTitle> <DialogTitle>
{income ? "Confirmar recebimento" : "Confirmar pagamento"}
</DialogTitle>
<DialogDescription className="mt-1 text-xs"> <DialogDescription className="mt-1 text-xs">
{isBillPending {isBillPending
? "Escolha a conta de origem e a data em que o boleto foi pago." ? `Escolha a conta de ${income ? "destino" : "origem"} e a data em que o boleto foi ${income ? "recebido" : "pago"}.`
: "Boleto"} : "Boleto"}
</DialogDescription> </DialogDescription>
</div> </div>
@@ -158,12 +163,15 @@ export function BillPaymentDialog({
<div className="flex items-center gap-1.5 text-muted-foreground"> <div className="flex items-center gap-1.5 text-muted-foreground">
<RiCalendarLine className="size-3.5" /> <RiCalendarLine className="size-3.5" />
<span className="text-xs font-medium uppercase"> <span className="text-xs font-medium uppercase">
{bill.isSettled ? "Pago em" : "Vencimento"} {bill.isSettled
? `${settlementLabel} em`
: "Vencimento"}
</span> </span>
</div> </div>
<p className="font-semibold"> <p className="font-semibold">
{bill.isSettled {bill.isSettled
? (paidLabel?.replace("Pago em: ", "") ?? "—") ? (paidLabel?.replace(`${settlementLabel} em: `, "") ??
"—")
: (dueLabel?.replace("Vencimento: ", "") ?? "—")} : (dueLabel?.replace("Vencimento: ", "") ?? "—")}
</p> </p>
</Card> </Card>
@@ -175,7 +183,7 @@ export function BillPaymentDialog({
<div className="space-y-3"> <div className="space-y-3">
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="bill-widget-payment-account"> <Label htmlFor="bill-widget-payment-account">
Conta de pagamento Conta de {income ? "recebimento" : "pagamento"}
</Label> </Label>
<Select <Select
value={paymentAccountId} value={paymentAccountId}
@@ -212,7 +220,7 @@ export function BillPaymentDialog({
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="bill-widget-payment-date"> <Label htmlFor="bill-widget-payment-date">
Data do pagamento Data do {income ? "recebimento" : "pagamento"}
</Label> </Label>
<DatePicker <DatePicker
id="bill-widget-payment-date" id="bill-widget-payment-date"
@@ -231,8 +239,8 @@ export function BillPaymentDialog({
<span className="text-sm text-muted-foreground"> <span className="text-sm text-muted-foreground">
Status atual Status atual
</span> </span>
<Badge variant={getBillStatusBadgeVariant("Pago")}> <Badge variant={getBillStatusBadgeVariant(settlementLabel)}>
Pago {settlementLabel}
</Badge> </Badge>
</div> </div>
)} )}

View File

@@ -15,7 +15,7 @@ export function BillsList({ bills, period, onPay }: BillsListProps) {
<WidgetEmptyState <WidgetEmptyState
icon={<RiBarcodeFill className="size-6 text-muted-foreground" />} icon={<RiBarcodeFill className="size-6 text-muted-foreground" />}
title="Nenhum boleto cadastrado para o período selecionado" title="Nenhum boleto cadastrado para o período selecionado"
description="Cadastre boletos para monitorar os pagamentos aqui." description="Cadastre boletos para monitorar os vencimentos aqui."
/> />
); );
} }

View File

@@ -41,9 +41,7 @@ export function BillsWidgetView({
}: BillsWidgetViewProps) { }: BillsWidgetViewProps) {
return ( return (
<> <>
<div className="flex flex-col gap-4"> <BillsList bills={bills} period={period} onPay={onOpenPaymentDialog} />
<BillsList bills={bills} period={period} onPay={onOpenPaymentDialog} />
</div>
<BillPaymentDialog <BillPaymentDialog
bill={selectedBill} bill={selectedBill}

View File

@@ -30,7 +30,7 @@ export function PayerBoletoCard({ items }: PayerBoletoCardProps) {
<WidgetEmptyState <WidgetEmptyState
icon={<RiBarcodeLine className="size-6 text-muted-foreground" />} icon={<RiBarcodeLine className="size-6 text-muted-foreground" />}
title="Nenhum boleto cadastrado para o período" title="Nenhum boleto cadastrado para o período"
description="Quando houver despesas registradas com boleto, elas aparecerão aqui." description="Quando houver lançamentos registrados com boleto, eles aparecerão aqui."
/> />
</CardContent> </CardContent>
); );

View File

@@ -608,7 +608,12 @@ export async function toggleTransactionSettlementAction(
const data = toggleSettlementSchema.parse(input); const data = toggleSettlementSchema.parse(input);
const existing = await db.query.transactions.findFirst({ const existing = await db.query.transactions.findFirst({
columns: { id: true, paymentMethod: true, accountId: true }, columns: {
id: true,
paymentMethod: true,
accountId: true,
transactionType: true,
},
where: and( where: and(
eq(transactions.id, data.id), eq(transactions.id, data.id),
eq(transactions.userId, user.id), eq(transactions.userId, user.id),
@@ -627,6 +632,7 @@ export async function toggleTransactionSettlementAction(
} }
const isBoleto = existing.paymentMethod === "Boleto"; const isBoleto = existing.paymentMethod === "Boleto";
const isIncomeBill = isBoleto && existing.transactionType === "Receita";
const customPaymentDate = const customPaymentDate =
isBoleto && data.value && data.paymentDate isBoleto && data.value && data.paymentDate
? parseLocalDateString(data.paymentDate) ? parseLocalDateString(data.paymentDate)
@@ -652,7 +658,7 @@ export async function toggleTransactionSettlementAction(
if (!paymentAccount) { if (!paymentAccount) {
return { return {
success: false, success: false,
error: "Conta de pagamento não encontrada.", error: `Conta de ${isIncomeBill ? "recebimento" : "pagamento"} não encontrada.`,
}; };
} }
} }
@@ -682,8 +688,8 @@ export async function toggleTransactionSettlementAction(
return { return {
success: true, success: true,
message: data.value message: data.value
? "Lançamento marcado como pago." ? `Lançamento marcado como ${isIncomeBill ? "recebido" : "pago"}.`
: "Pagamento desfeito com sucesso.", : `${isIncomeBill ? "Recebimento" : "Pagamento"} desfeito com sucesso.`,
}; };
} catch (error) { } catch (error) {
return handleActionError(error); return handleActionError(error);

View File

@@ -63,6 +63,7 @@ export type PayerBoletoItem = {
dueDate: string | null; dueDate: string | null;
boletoPaymentDate: string | null; boletoPaymentDate: string | null;
isSettled: boolean; isSettled: boolean;
transactionType: string;
}; };
export type PayerPaymentStatusData = { export type PayerPaymentStatusData = {
@@ -322,6 +323,7 @@ export async function fetchPayerBoletoItems({
dueDate: transactions.dueDate, dueDate: transactions.dueDate,
boletoPaymentDate: transactions.boletoPaymentDate, boletoPaymentDate: transactions.boletoPaymentDate,
isSettled: transactions.isSettled, isSettled: transactions.isSettled,
transactionType: transactions.transactionType,
}) })
.from(transactions) .from(transactions)
.leftJoin( .leftJoin(
@@ -350,6 +352,7 @@ export async function fetchPayerBoletoItems({
dueDate: toDateOnlyString(row.dueDate), dueDate: toDateOnlyString(row.dueDate),
boletoPaymentDate: toDateOnlyString(row.boletoPaymentDate), boletoPaymentDate: toDateOnlyString(row.boletoPaymentDate),
isSettled: Boolean(row.isSettled), isSettled: Boolean(row.isSettled),
transactionType: row.transactionType,
}); });
} }

View File

@@ -19,7 +19,7 @@ type FinancialDueDateInfo = {
date: string | null; date: string | null;
}; };
type RelativeFinancialDateContext = "due" | "paid"; type RelativeFinancialDateContext = "due" | "paid" | "received";
export function formatFinancialDateLabel( export function formatFinancialDateLabel(
value: string | null, value: string | null,
@@ -75,15 +75,17 @@ export function formatRelativeFinancialDateLabel(
return formatFinancialDateLabel(normalizedValue, "Vence em"); return formatFinancialDateLabel(normalizedValue, "Vence em");
} }
const settlementLabel = context === "received" ? "Recebido" : "Pago";
if (normalizedValue === referenceDate) { if (normalizedValue === referenceDate) {
return "Pago hoje"; return `${settlementLabel} hoje`;
} }
if (normalizedValue === yesterday) { if (normalizedValue === yesterday) {
return "Pago ontem"; return `${settlementLabel} ontem`;
} }
return formatFinancialDateLabel(normalizedValue, "Pago em"); return formatFinancialDateLabel(normalizedValue, `${settlementLabel} em`);
} }
export function buildFinancialStatusLabel({ export function buildFinancialStatusLabel({