order-number.ts
Language TypeScript1.7 KB37 lines
/*** Customer-facing order numbers.** Plinth stores a bare integer `order_ref`. What everyone at Meridian actually says out* loud, on the phone, in Bitrix, on a carrier claim, is the display number:** MD + two-digit year of the order + order_ref* 39582 placed in 2026 -> MD2639582** The dashboard must show the display number. A designer handed "39582" has to* translate it before they can search for it, and a number nobody can paste into* Plinth is decoration rather than information.** The year comes from when the order was placed, which in Plinth is* `closed_on` despite the name (see projection.ts). Without a date* there is no correct prefix, so this returns the bare id rather than guessing* the current year: a wrong order number is worse than an obviously partial one,* because it looks searchable and silently is not.** "MD" is hardcoded, and that is correct. Meridian runs a single* storefront, so there is no second prefix to choose between. If another one* ever opens, this is the function to revisit, and it will need a storefront* field on `orders.fetch` rather than a guess.*//** "MD2639582", or "39582" when the placed date is unknown. */export function displayOrderNumber(orderId: number, placedAt: string | null): string {if (!placedAt) return String(orderId)const year = new Date(placedAt).getUTCFullYear()if (!Number.isFinite(year)) return String(orderId)return `MD${String(year % 100).padStart(2, '0')}${orderId}`}/** True when the number is the real, pasteable one rather than a bare id. */export function isDisplayOrderNumber(value: string): boolean {return /^[A-Z]{2}\d{2}\d+$/.test(value.trim().toUpperCase())}