const BANGKOK_OFFSET = "+07:00"; export function commissionIncomeMonth(value: string | string[] | undefined, now = new Date()) { const currentParts = new Intl.DateTimeFormat("en-US", { timeZone: "Asia/Bangkok", year: "numeric", month: "2-digit", }).formatToParts(now); const current = `${currentParts.find((part) => part.type === "year")!.value}-${currentParts.find((part) => part.type === "month")!.value}`; const month = typeof value === "string" && /^(20\d{2}|[3-9]\d{3})-(0[1-9]|1[0-2])$/.test(value) ? value : current; const [year, number] = month.split("-").map(Number); const next = number === 12 ? `${year + 1}-01` : `${year}-${String(number + 1).padStart(2, "0")}`; const previous = number === 1 ? `${year - 1}-12` : `${year}-${String(number - 1).padStart(2, "0")}`; return { month, year, number, previous, next, from: new Date(`${month}-01T00:00:00${BANGKOK_OFFSET}`), to: new Date(`${next}-01T00:00:00${BANGKOK_OFFSET}`), days: new Date(Date.UTC(year, number, 0)).getUTCDate(), leadingDays: (new Date(Date.UTC(year, number - 1, 1)).getUTCDay() + 6) % 7, }; } export function commissionIncomeDay(date: Date) { return Number(new Intl.DateTimeFormat("en-US", { timeZone: "Asia/Bangkok", day: "numeric", }).format(date)); } export function commissionIncomeShares(amountBaht: number) { return { buzzSatang: amountBaht * 90, gunshinSatang: amountBaht * 10 }; }