Files
events-hub/backend/src/utils/billing/dates.js
T
joshuaandClaude Opus 5.5 6fce0fe90e Billing: invoices, approval, receipts, statements, licence changes (1.7.0)
- Invoices drafted 14 days before each licence's invoice date and emailed
  to the alert email for approval; approved ones go out on their date from
  08:00 SA time. Drafts can be edited, recalculated, sent early, put back
  to draft or voided; one-off invoices too.
- Annual tier review from the 12-month event-income band; approving a tier
  change emails a notice of licence change, applied when the invoice goes out.
  Notices can also be sent by hand.
- Founding churches (35% off as its own line), payments with receipts,
  statements for any dates.
- Every billing email is styled HTML with a PDF (invoice / tax invoice,
  receipt, statement, licence change) and logged per customer.
- Settings -> Billing: business and bank details, VAT (off: plain invoices;
  on: VAT-inclusive tax invoices), timing and numbering.
- Migration 20261103111645_invoicing; pdfkit 0.17.2; tests/billing.test.js;
  scripts/sample-billing-pdfs.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:42:45 +02:00

80 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Calendar dates and money for billing. Date-only columns (@db.Date) come back as JavaScript
* Dates at UTC midnight, so dates here are UTC-midnight Dates and 'YYYY-MM-DD' strings. "Today"
* is today in South Africa.
*/
const TZ = 'Africa/Johannesburg';
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
/** 'YYYY-MM-DD' of a date-only value (a UTC-midnight Date or a string). */
function ymd(d) {
if (!d) return null;
if (typeof d === 'string') return d.slice(0, 10);
return d.toISOString().slice(0, 10);
}
/** A date-only value as a UTC-midnight Date. Throws on nonsense. */
function day(v) {
const s = ymd(v instanceof Date ? v : String(v || ''));
if (!s || !/^\d{4}-\d{2}-\d{2}$/.test(s)) throw Object.assign(new Error('Not a valid date.'), { status: 400 });
const d = new Date(`${s}T00:00:00Z`);
if (Number.isNaN(d.getTime()) || ymd(d) !== s) throw Object.assign(new Error('Not a valid date.'), { status: 400 });
return d;
}
/** Today's date in South Africa, as a UTC-midnight Date. */
function today(now = new Date()) {
return day(new Intl.DateTimeFormat('en-CA', { timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit' }).format(now));
}
/** The hour (0–23) in South Africa. */
function saHour(now = new Date()) {
return Number(new Intl.DateTimeFormat('en-GB', { timeZone: TZ, hour: '2-digit', hourCycle: 'h23' }).format(now));
}
const addDays = (d, n) => new Date(day(d).getTime() + n * 864e5);
/**
* `n` months after `d`, on `anchorDay` (the billing day) or the month's last day when the month
* is shorter: 31 Jan + 1 month on day 31 → 28/29 Feb; then + 1 → 31 Mar.
*/
function addMonths(d, n, anchorDay = day(d).getUTCDate()) {
const base = day(d);
const y = base.getUTCFullYear();
const m = base.getUTCMonth() + n;
const last = new Date(Date.UTC(y, m + 1, 0)).getUTCDate();
return new Date(Date.UTC(y, m, Math.min(anchorDay, last)));
}
/** "1 December 2026". */
function longDate(d) {
if (!d) return '';
const x = day(d);
return `${x.getUTCDate()} ${MONTHS[x.getUTCMonth()]} ${x.getUTCFullYear()}`;
}
/** "15 Dec 2026". */
function shortDate(d) {
if (!d) return '';
const x = day(d);
return `${x.getUTCDate()} ${MONTHS[x.getUTCMonth()].slice(0, 3)} ${x.getUTCFullYear()}`;
}
/** "December 2026" (a monthly period), or "1 December 2026 – 30 November 2027". */
function periodLabel(start, end) {
const s = day(start);
const e = day(end);
const wholeMonth = s.getUTCDate() === 1 && ymd(addDays(e, 1)) === ymd(addMonths(s, 1, 1));
return wholeMonth ? `${MONTHS[s.getUTCMonth()]} ${s.getUTCFullYear()}` : `${longDate(s)} – ${longDate(e)}`;
}
/** R1 350.00 (cents in, South African style with a space between thousands). */
function money(cents) {
const neg = cents < 0;
const [r, c] = (Math.abs(Math.round(cents)) / 100).toFixed(2).split('.');
return `${neg ? '-' : ''}R${r.replace(/\B(?=(\d{3})+(?!\d))/g, ' ')}.${c}`;
}
module.exports = { TZ, ymd, day, today, saHour, addDays, addMonths, longDate, shortDate, periodLabel, money };