BillTracker/client/lib/billingSchedule.js

54 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

export const BILLING_SCHEDULE_OPTIONS = [
['monthly', 'Monthly'],
['weekly', 'Weekly'],
['biweekly', 'Biweekly'],
['quarterly', 'Quarterly'],
['annual', 'Annual'],
];
const LABELS = Object.fromEntries(BILLING_SCHEDULE_OPTIONS);
export function scheduleFromBillingCycle(billingCycle) {
const value = String(billingCycle || '').toLowerCase();
if (value === 'quarterly') return 'quarterly';
if (value === 'annually' || value === 'annual') return 'annual';
return 'monthly';
}
export function normalizeSchedule(value, fallback = 'monthly') {
if (!value) return fallback;
const normalized = String(value).toLowerCase();
return LABELS[normalized] ? normalized : fallback;
}
export function scheduleValue(bill = {}) {
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);
}
export function scheduleLabel(valueOrBill) {
const value = valueOrBill && typeof valueOrBill === 'object'
? scheduleValue(valueOrBill)
: normalizeSchedule(valueOrBill);
return LABELS[value] || 'Monthly';
}
export function billingCycleForSchedule(schedule) {
const value = normalizeSchedule(schedule);
if (value === 'quarterly') return 'quarterly';
if (value === 'annual') return 'annually';
if (value === 'weekly' || value === 'biweekly') return 'irregular';
return 'monthly';
}
export function defaultCycleDayForSchedule(schedule) {
const value = normalizeSchedule(schedule);
return value === 'weekly' || value === 'biweekly' ? 'monday' : '1';
}