2026-07-03 21:59:16 -05:00
|
|
|
export type Schedule = 'monthly' | 'weekly' | 'biweekly' | 'quarterly' | 'annual';
|
|
|
|
|
|
|
|
|
|
export const BILLING_SCHEDULE_OPTIONS: [Schedule, string][] = [
|
2026-05-30 21:20:51 -05:00
|
|
|
['monthly', 'Monthly'],
|
|
|
|
|
['weekly', 'Weekly'],
|
|
|
|
|
['biweekly', 'Biweekly'],
|
|
|
|
|
['quarterly', 'Quarterly'],
|
|
|
|
|
['annual', 'Annual'],
|
|
|
|
|
];
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
const LABELS: Record<string, string> = Object.fromEntries(BILLING_SCHEDULE_OPTIONS);
|
|
|
|
|
|
|
|
|
|
export interface ScheduleBill {
|
|
|
|
|
cycle_type?: string | null;
|
|
|
|
|
billing_cycle?: string | null;
|
|
|
|
|
}
|
2026-05-30 21:20:51 -05:00
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function scheduleFromBillingCycle(billingCycle: string | null | undefined): Schedule {
|
2026-05-30 21:20:51 -05:00
|
|
|
const value = String(billingCycle || '').toLowerCase();
|
|
|
|
|
if (value === 'quarterly') return 'quarterly';
|
|
|
|
|
if (value === 'annually' || value === 'annual') return 'annual';
|
|
|
|
|
return 'monthly';
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function normalizeSchedule(value: string | null | undefined, fallback = 'monthly'): string {
|
2026-05-30 21:20:51 -05:00
|
|
|
if (!value) return fallback;
|
|
|
|
|
const normalized = String(value).toLowerCase();
|
|
|
|
|
return LABELS[normalized] ? normalized : fallback;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function scheduleValue(bill: ScheduleBill = {}): string {
|
2026-05-30 21:20:51 -05:00
|
|
|
const cycleType = normalizeSchedule(bill.cycle_type, '');
|
|
|
|
|
const billingCycle = String(bill.billing_cycle || '').toLowerCase();
|
|
|
|
|
|
|
|
|
|
if (cycleType === 'monthly' && ['quarterly', 'annually', 'annual'].includes(billingCycle)) {
|
|
|
|
|
return scheduleFromBillingCycle(billingCycle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cycleType || scheduleFromBillingCycle(billingCycle);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function scheduleLabel(valueOrBill: string | ScheduleBill | null | undefined): string {
|
2026-05-30 21:20:51 -05:00
|
|
|
const value = valueOrBill && typeof valueOrBill === 'object'
|
|
|
|
|
? scheduleValue(valueOrBill)
|
|
|
|
|
: normalizeSchedule(valueOrBill);
|
|
|
|
|
return LABELS[value] || 'Monthly';
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function billingCycleForSchedule(schedule: string | null | undefined): string {
|
2026-05-30 21:20:51 -05:00
|
|
|
const value = normalizeSchedule(schedule);
|
|
|
|
|
if (value === 'quarterly') return 'quarterly';
|
|
|
|
|
if (value === 'annual') return 'annually';
|
|
|
|
|
if (value === 'weekly' || value === 'biweekly') return 'irregular';
|
|
|
|
|
return 'monthly';
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:59:16 -05:00
|
|
|
export function defaultCycleDayForSchedule(schedule: string | null | undefined): string {
|
2026-05-30 21:20:51 -05:00
|
|
|
const value = normalizeSchedule(schedule);
|
|
|
|
|
return value === 'weekly' || value === 'biweekly' ? 'monday' : '1';
|
|
|
|
|
}
|