mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
67 lines
1.4 KiB
TypeScript
67 lines
1.4 KiB
TypeScript
import {
|
|
buildDateOnlyStringFromPeriodDay,
|
|
formatDateOnlyLabel,
|
|
} from "@/lib/utils/date";
|
|
|
|
type FinancialStatusLabelInput = {
|
|
isSettled: boolean;
|
|
dueDate: string | null;
|
|
paidAt: string | null;
|
|
paidPrefix?: string;
|
|
duePrefix?: string;
|
|
};
|
|
|
|
type FinancialDueDateInfo = {
|
|
label: string;
|
|
date: string | null;
|
|
};
|
|
|
|
export function formatFinancialDateLabel(
|
|
value: string | null,
|
|
prefix?: string,
|
|
options?: Intl.DateTimeFormatOptions,
|
|
): string | null {
|
|
return formatDateOnlyLabel(value, prefix, options);
|
|
}
|
|
|
|
export function buildFinancialStatusLabel({
|
|
isSettled,
|
|
dueDate,
|
|
paidAt,
|
|
paidPrefix = "Pago em",
|
|
duePrefix = "Vence em",
|
|
}: FinancialStatusLabelInput): string | null {
|
|
if (isSettled) {
|
|
return formatFinancialDateLabel(paidAt, paidPrefix);
|
|
}
|
|
|
|
return formatFinancialDateLabel(dueDate, duePrefix);
|
|
}
|
|
|
|
export function buildDueDateInfoFromPeriodDay(
|
|
period: string,
|
|
dueDay: string,
|
|
options?: {
|
|
prefix?: string;
|
|
fallbackPrefix?: string;
|
|
},
|
|
): FinancialDueDateInfo {
|
|
const prefix = options?.prefix ?? "Vence em";
|
|
const fallbackPrefix = options?.fallbackPrefix ?? "Vence dia";
|
|
const dueDate = buildDateOnlyStringFromPeriodDay(period, dueDay);
|
|
|
|
if (!dueDate) {
|
|
return {
|
|
label: `${fallbackPrefix} ${dueDay}`,
|
|
date: null,
|
|
};
|
|
}
|
|
|
|
return {
|
|
label:
|
|
formatFinancialDateLabel(dueDate, prefix) ??
|
|
`${fallbackPrefix} ${dueDay}`,
|
|
date: dueDate,
|
|
};
|
|
}
|