UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

146 lines (144 loc) • 10.5 kB
import { Temporal } from "../../internal/temporal.js"; import { plainDateOf } from "../../internal/inputChecks.js"; import { isObject } from "../../internal/isObject.js"; // src/intermodal/calculate/billingTimeline.ts /** * Lay out the deadline chain around a demurrage or detention invoice: the last date it may be * issued, the last date it may be disputed, and the last date the dispute must be resolved. * * A billing regime or service contract can set up to three windows around an invoice: one to * issue it, one to dispute it, and one to resolve the dispute. The number of days in each is the * caller's fact; GMT carries none of them. The arithmetic is this function's: each deadline is a * date, counted in calendar days from a date, with the anchor as day zero and the deadline day * itself inside the window. * * - **Day zero is the anchor and the deadline is `anchor + days`** on the ISO 8601 calendar * (`Temporal.PlainDate.add`); a date is "by the deadline" when * `Temporal.PlainDate.compare(date, deadline) <= 0`. That is this function's stated contract, * not a reading of any rule; a regime that counts differently passes a different number. * - **Deadlines are dates, never instants.** Reduce an instant to the billing party's local date * first with `convertUtcToPlainDate(instant, { timeZone })`; this function does not guess a * zone. `anchorOn` is whatever date the caller counts from, such as the last charged date * (`chargeableDays(...).chargedDates.at(-1)`) or, for a party re-billing a charge it was * itself billed, the issuance date of the invoice it received. * - **Windows have no defaults.** Each is a safe integer of at least `0`; a missing, negative or * non-integer window returns `null`, because a silently defaulted window is a wrong deadline. * - **The chain fills in as its dates exist.** With only `anchorOn` the result is a forecast: * `invoiceDeadline` is set and every other field is `null`. An invoice date sets * `issuedByDeadline` and `disputeDeadline`; a request date sets `requestedByDeadline` and * `resolutionDeadline`. An explicit `undefined` is an omission; any other value must be a * valid ISO date. * - **The booleans compare dates and say nothing else.** Whether a charge is payable, whether a * dispute must be heard, and any consequence are the consumer's. GMT computes dates, not * liability. * - **`agreedResolutionOn` replaces the computed `resolutionDeadline`** when given. It must be a * valid date on or after `requestReceivedOn`, else `null`; it is validated even when no * request exists, but cannot act without one. * - **A request cannot precede the invoice it disputes.** `requestReceivedOn` without * `invoiceIssuedOn`, or earlier than it, is a data error and returns `null`. An invoice earlier * than the anchor is allowed: an invoice may be issued while charges still accrue. * - Every emitted date is canonical bare ISO (`Temporal.PlainDate#toString()`), so an input * annotation such as `[u-ca=iso8601]` does not reach the output, as `addDate` behaves. * - Returns `null` when `dates` or `windows` is not an object, `anchorOn` or a given optional * date is not a valid ISO date, a window is missing or not a safe integer of at least `0`, a * request exists without an invoice or before it, `agreedResolutionOn` is before the request, * or a deadline would leave Temporal's range. * * @param dates The anchor date, and the invoice and request dates once they exist * @param windows The three calendar-day windows, none defaulted, and an agreed resolution date * @returns { invoiceDeadline, issuedByDeadline, disputeDeadline, requestedByDeadline, resolutionDeadline }, or null on invalid input * * @example billingTimeline({ anchorOn: "2026-03-01" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2026-03-31", issuedByDeadline: null, disputeDeadline: null, requestedByDeadline: null, resolutionDeadline: null } (no invoice yet: a forecast) * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-03-31" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2026-03-31", issuedByDeadline: true, disputeDeadline: "2026-04-30", requestedByDeadline: null, resolutionDeadline: null } (day 30 is the last day by the deadline) * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-04-01" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2026-03-31", issuedByDeadline: false, disputeDeadline: "2026-05-01", requestedByDeadline: null, resolutionDeadline: null } (day 31) * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-03-20", requestReceivedOn: "2026-04-19" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2026-03-31", issuedByDeadline: true, disputeDeadline: "2026-04-19", requestedByDeadline: true, resolutionDeadline: "2026-05-19" } * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-03-20", requestReceivedOn: "2026-04-20" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30, agreedResolutionOn: "2026-06-01" }) // { invoiceDeadline: "2026-03-31", issuedByDeadline: true, disputeDeadline: "2026-04-19", requestedByDeadline: false, resolutionDeadline: "2026-06-01" } (an agreed date replaces the computed one) * @example billingTimeline({ anchorOn: "2026-03-10", invoiceIssuedOn: "2026-04-05" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2026-04-09", issuedByDeadline: true, disputeDeadline: "2026-05-05", requestedByDeadline: null, resolutionDeadline: null } (a re-bill anchored on the invoice it received, not on the charge) * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-03-05", requestReceivedOn: "2026-03-18" }, { issueDays: 14, disputeDays: 14, resolutionDays: 45 }) // { invoiceDeadline: "2026-03-15", issuedByDeadline: true, disputeDeadline: "2026-03-19", requestedByDeadline: true, resolutionDeadline: "2026-05-02" } (a service contract's own windows) * @example billingTimeline({ anchorOn: "2028-01-30" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // { invoiceDeadline: "2028-02-29", issuedByDeadline: null, disputeDeadline: null, requestedByDeadline: null, resolutionDeadline: null } (calendar days across a leap day) * @example billingTimeline({ anchorOn: "2026-03-01", invoiceIssuedOn: "2026-03-31" }, { disputeDays: 30, resolutionDays: 30 }) // null (issueDays has no default) * @example billingTimeline({ anchorOn: "2026-03-01", requestReceivedOn: "2026-04-19" }, { issueDays: 30, disputeDays: 30, resolutionDays: 30 }) // null (a request cannot precede the invoice it disputes) */ function billingTimeline(dates, windows) { try { return layOutDeadlines(dates, windows); } catch { return null; } } /** A window is a whole number of calendar days, zero or more. */ function isWindow(value) { return typeof value === "number" && Number.isSafeInteger(value) && value >= 0; } /** An optional date is absent (`undefined`) or a valid ISO date; anything else is invalid. */ function optionalDate(value) { if (value === void 0) return; return plainDateOf(value); } /** `date + days` on the ISO calendar, canonical. Throws `RangeError` past Temporal's range. */ function plusDays(date, days) { return date.add({ days }); } /** Whether `date` is on or before `deadline`. */ function byDeadline(date, deadline) { return Temporal.PlainDate.compare(date, deadline) <= 0; } /** The dates, each a valid ISO date, with no request before its invoice; `null` otherwise. */ function readDates(dates) { const anchorOn = plainDateOf(dates.anchorOn); if (anchorOn === null) return null; const invoiceIssuedOn = optionalDate(dates.invoiceIssuedOn); const requestReceivedOn = optionalDate(dates.requestReceivedOn); if (invoiceIssuedOn === null || requestReceivedOn === null) return null; if (requestReceivedOn !== void 0 && (invoiceIssuedOn === void 0 || !byDeadline(invoiceIssuedOn, requestReceivedOn))) return null; return { anchorOn, invoiceIssuedOn, requestReceivedOn }; } /** The three windows, each present and valid, and the agreed date if given; `null` otherwise. */ function readWindows(windows) { const { issueDays, disputeDays, resolutionDays } = windows; if (!isWindow(issueDays) || !isWindow(disputeDays) || !isWindow(resolutionDays)) return null; const agreedResolutionOn = optionalDate(windows.agreedResolutionOn); if (agreedResolutionOn === null) return null; return { issueDays, disputeDays, resolutionDays, agreedResolutionOn }; } /** The agreed date when given, else `requestReceivedOn + resolutionDays`; `null` with no request. */ function resolutionDeadline(requestReceivedOn, windows) { if (requestReceivedOn === void 0) return null; return windows.agreedResolutionOn === void 0 ? plusDays(requestReceivedOn, windows.resolutionDays) : windows.agreedResolutionOn; } /** Whether `date` is on or before `deadline`; `null` until both exist. */ function onOrBefore(date, deadline) { return date === void 0 || deadline === null ? null : byDeadline(date, deadline); } /** An agreed resolution date cannot precede the request it resolves. */ function agreedAfterRequest(requestReceivedOn, agreedResolutionOn) { return requestReceivedOn === void 0 || agreedResolutionOn === void 0 || byDeadline(requestReceivedOn, agreedResolutionOn); } /** `billingTimeline` without the guard; every read of its arguments happens here. */ function layOutDeadlines(dates, windows) { if (!isObject(dates) || !isObject(windows)) return null; const read = readDates(dates); const counts = readWindows(windows); if (read === null || counts === null || !agreedAfterRequest(read.requestReceivedOn, counts.agreedResolutionOn)) return null; const { anchorOn, invoiceIssuedOn, requestReceivedOn } = read; const invoiceDeadline = plusDays(anchorOn, counts.issueDays); const disputeDeadline = invoiceIssuedOn === void 0 ? null : plusDays(invoiceIssuedOn, counts.disputeDays); const resolution = resolutionDeadline(requestReceivedOn, counts); return { invoiceDeadline: invoiceDeadline.toString(), issuedByDeadline: onOrBefore(invoiceIssuedOn, invoiceDeadline), disputeDeadline: disputeDeadline === null ? null : disputeDeadline.toString(), requestedByDeadline: onOrBefore(requestReceivedOn, disputeDeadline), resolutionDeadline: resolution === null ? null : resolution.toString() }; } export { billingTimeline };