Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 18365x 30199x 345x 345x 73x 3x 70x 5x 65x 3x 62x 46026x 40979x 30199x 3x 36x 35x 44x 14x 14x 14x 14x 38x 38x 38x 20x 20x 20x 343x 116x 343x 38x 102x 102x | // Rent, charges and the financing charge treatment as monthly periods
// (openspec: recurring-amounts). Pure functions: no database access. Every
// period starts on the first of a month, and a month's value is the one in
// effect on its first day, so the month is the only unit: no proration.
import type { IsoDate } from '#/lib/dates'
/** One period of a value: an amount in cents, or a treatment. */
export interface Period<T> {
validFrom: IsoDate
/** Null for the current, open-ended period. */
validTo: IsoDate | null
value: T
}
/** A calendar month as `YYYY-MM`. */
export type Month = string
export const monthOf = (date: IsoDate): Month => date.slice(0, 7)
const firstDay = (month: Month): IsoDate => `${month}-01`
/** The month after `month`: `2025-12` → `2026-01`. */
export function nextMonth(month: Month): Month {
const [year, m] = month.split('-').map(Number) as [number, number]
return m === 12
? `${String(year + 1)}-01`
: `${String(year)}-${String(m + 1).padStart(2, '0')}`
}
export type AmountDateProblem =
| 'amount_date_not_month_start'
| 'amount_date_before_ownership'
| 'amount_date_too_early'
/**
* A new amount or treatment starts on the first of a month, not before the
* month the flat's ownership starts in, and not before the latest period's
* start. On that start itself it corrects the latest period.
*/
export function validateAmountDate(
from: IsoDate,
limits: { ownershipStart: IsoDate; latestStart: IsoDate | undefined },
): AmountDateProblem | undefined {
if (!from.endsWith('-01')) {
return 'amount_date_not_month_start'
}
if (monthOf(from) < monthOf(limits.ownershipStart)) {
return 'amount_date_before_ownership'
}
if (limits.latestStart !== undefined && from < limits.latestStart) {
return 'amount_date_too_early'
}
return undefined
}
/** The period containing a date, if any. */
export function periodOn<T>(
periods: readonly Period<T>[],
date: IsoDate,
): Period<T> | undefined {
return periods.find(
(p) => p.validFrom <= date && (p.validTo === null || date <= p.validTo),
)
}
/** A month's value: the one in effect on its first day. */
export function valueInMonth<T>(
periods: readonly Period<T>[],
month: Month,
): T | undefined {
return periodOn(periods, firstDay(month))?.value
}
/** Each month's amount of a year, January first; undefined when unset. */
export function monthlyAmounts(
periods: readonly Period<number>[],
year: number,
): (number | undefined)[] {
return Array.from({ length: 12 }, (_, i) =>
valueInMonth(periods, `${String(year)}-${String(i + 1).padStart(2, '0')}`),
)
}
/** What the page shows: the period in effect today and the next one. */
export function currentAndUpcoming<T>(
periods: readonly Period<T>[],
today: IsoDate,
): { current: Period<T> | undefined; upcoming: Period<T> | undefined } {
return {
current: periodOn(periods, today),
upcoming: periods.find((p) => p.validFrom > today),
}
}
export interface YearEffect {
/** Per month, in cents; negative for a decrease. */
difference: number
/** Of the current amount; undefined when it is zero. */
percent: number | undefined
year: number
/** Months from the change to the end of its year. */
months: number
/** `difference` times `months`. */
total: number
}
/** How a new amount from `from` changes the rest of that year. */
export function yearEffect(
currentCents: number,
newCents: number,
from: IsoDate,
): YearEffect {
const [year, month] = from.split('-').map(Number) as [number, number]
const difference = newCents - currentCents
const months = 13 - month
return {
difference,
percent: currentCents === 0 ? undefined : (difference / currentCents) * 100,
year,
months,
total: difference * months,
}
}
/**
* The financing charges added to the acquisition cost so far: over all the
* flat's financing charges, the amount of every month up to and including
* today's whose treatment on its first day is capitalised.
*/
export function accumulatedCapitalised(
charges: readonly { periods: readonly Period<number>[] }[],
treatments: readonly Period<'income_recognised' | 'capitalised'>[],
today: IsoDate,
): number {
let total = 0
const last = monthOf(today)
for (const { periods } of charges) {
const first = periods.at(0)
Iif (first === undefined) {
continue
}
for (let month = monthOf(first.validFrom); month <= last;) {
if (valueInMonth(treatments, month) === 'capitalised') {
total += valueInMonth(periods, month) ?? 0
}
month = nextMonth(month)
}
}
return total
}
/**
* A charge has ended when its amount today is 0 and no later period starts
* (spec: "Ending a financing charge").
*/
export function isEnded(
periods: readonly Period<number>[],
today: IsoDate,
): boolean {
const current = periodOn(periods, today)
return current?.value === 0 && current === periods.at(-1)
}
|