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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 7752x 7752x 3613x 29x 29x 3x 26x 3x 23x 2x 21x 8x 13x 13x 5x 8x 58x 58x 2x 56x 3x 53x 2x 51x 4x 47x 31x 405x 88x 72x 405x 43x 43x 43x 5x 38x 22x 16x 22x 22x 92x 92x 18x 19x 48x | // A tenancy's rent as periods (openspec: tenancies, "The rent of a tenancy",
// "Changing a tenancy's rent", "Rent changes stay inside their tenancy").
// The first period starts on the tenancy's start day, later ones on the
// first of a month; the latest has no end, bounded by the tenancy's dates.
// Pure: no database access.
import {
monthOf,
periodOn,
type Month,
type Period,
} from '#/features/recurring-amounts/periods'
import type { IsoDate } from '#/lib/dates'
/** The dates of a tenancy that its rent depends on. */
export interface RentDates {
validFrom: IsoDate
/** The last day; null while an open-ended tenancy runs. */
validTo: IsoDate | null
}
/**
* A month's value of a tenancy's periods (rent, occupants, a fee's amount):
* the period on the month's first day, or on the tenancy's start day in the
* month it starts. Undefined when none is in effect.
*/
export function valueInTenancyMonth<T>(
periods: readonly Period<T>[],
tenancy: Pick<RentDates, 'validFrom'>,
month: Month,
): T | undefined {
const day =
monthOf(tenancy.validFrom) === month ? tenancy.validFrom : `${month}-01`
return periodOn(periods, day)?.value
}
/** A month's rent; undefined when no rent is in effect. */
export function rentInMonth(
rents: readonly Period<number>[],
tenancy: Pick<RentDates, 'validFrom'>,
month: Month,
): number | undefined {
return valueInTenancyMonth(rents, tenancy, month)
}
/**
* The tenancy's rent in effect `today`, or its first rent when it starts
* later; undefined without one.
*/
export function rentToday(
rents: readonly Period<number>[],
tenancy: Pick<RentDates, 'validFrom'>,
today: IsoDate,
): number | undefined {
return periodOn(rents, today < tenancy.validFrom ? tenancy.validFrom : today)
?.value
}
export type RentDateProblem =
| 'amount_date_not_month_start'
| 'rent_date_before_tenancy'
| 'rent_date_after_tenancy'
| 'amount_date_too_early'
/**
* A new rent starts on the first of a month or the tenancy's start day,
* inside the tenancy, and not before the latest period's start. On the
* start of any of its periods (`starts`, in order) it corrects that
* period. Before the first period's start no rent is in effect (a migrated
* tenancy), so a date there fills the months up to the first period
* (`fillsBeforeFirst`).
*/
export function validateRentDate(
from: IsoDate,
limits: { tenancy: RentDates; starts: readonly IsoDate[] },
): RentDateProblem | undefined {
const { tenancy, starts } = limits
if (!from.endsWith('-01') && from !== tenancy.validFrom) {
return 'amount_date_not_month_start'
}
if (from < tenancy.validFrom) {
return 'rent_date_before_tenancy'
}
if (tenancy.validTo !== null && from > tenancy.validTo) {
return 'rent_date_after_tenancy'
}
if (fillsBeforeFirst(from, starts.at(0)) || starts.includes(from)) {
return undefined
}
const latestStart = starts.at(-1)
if (latestStart !== undefined && from < latestStart) {
return 'amount_date_too_early'
}
return undefined
}
/**
* The date of a new occupants or fee amount period: the first of a month or
* the tenancy's start day, inside the tenancy, and not before the latest
* period's start (`latestStart`, none for a fee's first amount). On that
* start itself it corrects the latest period. Unlike the rent, a date before
* the first period is never a fill: a fee may start mid-tenancy.
*/
export function validateTenancyPeriodDate(
from: IsoDate,
limits: { tenancy: RentDates; latestStart: IsoDate | undefined },
): RentDateProblem | undefined {
const { tenancy, latestStart } = limits
if (!from.endsWith('-01') && from !== tenancy.validFrom) {
return 'amount_date_not_month_start'
}
if (from < tenancy.validFrom) {
return 'rent_date_before_tenancy'
}
if (tenancy.validTo !== null && from > tenancy.validTo) {
return 'rent_date_after_tenancy'
}
if (latestStart !== undefined && from < latestStart) {
return 'amount_date_too_early'
}
return undefined
}
/**
* A rent dated before the first period's start adds a period from that
* date to the day before the first period, instead of a change.
*/
export function fillsBeforeFirst(
from: IsoDate,
firstStart: IsoDate | undefined,
): firstStart is IsoDate {
return firstStart !== undefined && from < firstStart
}
/**
* The rent the new tenant form suggests: the latest rent of the flat's
* tenancy that starts last before `start`, or none.
*/
export function suggestedRent(
tenancies: readonly {
validFrom: IsoDate
rents: readonly Period<number>[]
}[],
start: IsoDate,
): number | undefined {
let previous: (typeof tenancies)[number] | undefined
for (const tenancy of tenancies) {
if (
tenancy.validFrom < start &&
(previous === undefined || tenancy.validFrom > previous.validFrom)
) {
previous = tenancy
}
}
return previous?.rents.at(-1)?.value
}
/**
* Where a corrected start leaves the first period of a tenancy's rent,
* occupants or a fee: moved to the new start when it started on the old one
* or the new start is after it, else where it is (`move` null). A start on
* or after the second period's start is refused.
*/
export function firstPeriodMove<T>(
periods: readonly Period<T>[],
oldStart: IsoDate,
newStart: IsoDate,
): { ok: true; move: { from: IsoDate; to: IsoDate } | null } | { ok: false } {
const first = periods.at(0)
const second = periods.at(1)
if (second !== undefined && newStart >= second.validFrom) {
return { ok: false }
}
if (
first === undefined ||
first.validFrom === newStart ||
(first.validFrom !== oldStart && newStart < first.validFrom)
) {
return { ok: true, move: null }
}
return { ok: true, move: { from: first.validFrom, to: newStart } }
}
/** `firstPeriodMove` for the rent, with the rent's reason. */
export function firstRentMove(
rents: readonly Period<number>[],
oldStart: IsoDate,
newStart: IsoDate,
):
| { ok: true; move: { from: IsoDate; to: IsoDate } | null }
| { ok: false; reason: 'tenancy_start_after_rent_change' } {
const moved = firstPeriodMove(rents, oldStart, newStart)
return moved.ok
? moved
: { ok: false, reason: 'tenancy_start_after_rent_change' }
}
/** A last day before the latest period's start would leave it outside. */
export function periodAfterEnd<T>(
periods: readonly Period<T>[],
lastDay: IsoDate | null,
): boolean {
const latest = periods.at(-1)
return lastDay !== null && latest !== undefined && latest.validFrom > lastDay
}
/**
* A fee changes after `lastDay`: a period with an amount above 0 starts
* after it. A 0 € period there only records that the fee ends with the
* tenancy, so it doesn't count.
*/
export function feeChangesAfterEnd(
amounts: readonly Period<number>[],
lastDay: IsoDate | null,
): boolean {
return (
lastDay !== null &&
amounts.some((p) => p.validFrom > lastDay && p.value !== 0)
)
}
/** `periodAfterEnd` for the rent. */
export function rentAfterEnd(
rents: readonly Period<number>[],
lastDay: IsoDate | null,
): boolean {
return periodAfterEnd(rents, lastDay)
}
|