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 | 7721x 7698x 7721x 11213x 11213x 453x 10760x 10760x 12x 4286x 3928x 3928x 3928x 3928x 3928x 5633x 5633x 53x 5580x 5580x 7x 5580x 3928x 1030x 2898x 5580x 30x 30x 30x 30x 358x 358x 30x 328x 328x 284x 44x 44x 84x 30x 8x 8x 44x 6x 6x 6x 6x 3x 3x 3x 5x | // A flat's usage charges month by month and by year (openspec: usage-charges,
// "Usage charges of a month"). Pure functions over the periods and the
// tenancies' occupants: nothing is stored. A per-occupant charge multiplies
// by the occupants of the tenancy running on the month's first day, or one
// while the flat is vacant. Amounts are not divided by days.
import {
monthOf,
periodOn,
valueInMonth,
type Month,
type Period,
} from '#/features/recurring-amounts/periods'
import type { IsoDate } from '#/lib/dates'
import type { ChargeBasis, ChargeKind } from '#/server/db/schema'
/** A usage charge with its amount periods, oldest first. */
export interface UsageChargePeriods {
id: string
kind: ChargeKind
basis: ChargeBasis
/** Set for the kind `other`. */
description: string | null
periods: readonly Period<number>[]
}
/** A tenancy's dates and occupants, for the per-occupant amounts. */
export interface OccupiedTenancy {
validFrom: IsoDate
/** The last day; null while an open-ended tenancy runs. */
validTo: IsoDate | null
occupants: readonly Period<number>[]
}
/** One usage charge in a month. */
export interface MonthUsageCharge {
id: string
kind: ChargeKind
description: string | null
basis: ChargeBasis
/** The month's amount: the period's, times the occupants per occupant. */
cents: number
/** The occupants counted, for a per-occupant charge; else null. */
persons: number | null
/** The period's amount: per occupant for a per-occupant charge. */
perUnit: number
/** The month before's amount, when both are above 0 and differ. */
previous?: number
}
export interface UsageChargeMonth {
month: Month
charges: MonthUsageCharge[]
total: number
}
export interface UsageChargeYear {
year: number
/** Newest first, only months with a usage charge. */
months: UsageChargeMonth[]
/** Per usage charge, leaving out sums of 0, in the charges' order. */
sums: {
id: string
kind: ChargeKind
description: string | null
cents: number
}[]
total: number
}
/**
* The occupants on `day`: those of the tenancy running then, or one when no
* tenancy runs (a vacant flat).
*/
export function occupantsOn(
tenancies: readonly OccupiedTenancy[],
day: IsoDate,
): number {
const running = tenancies.find(
(t) => t.validFrom <= day && (t.validTo === null || day <= t.validTo),
)
return running ? (periodOn(running.occupants, day)?.value ?? 1) : 1
}
function amountIn(
charge: UsageChargePeriods,
tenancies: readonly OccupiedTenancy[],
month: Month,
): Omit<MonthUsageCharge, 'previous'> | undefined {
const perUnit = valueInMonth(charge.periods, month)
if (perUnit === undefined) {
return undefined
}
const persons =
charge.basis === 'per_occupant'
? occupantsOn(tenancies, `${month}-01`)
: null
return {
id: charge.id,
kind: charge.kind,
description: charge.description,
basis: charge.basis,
perUnit,
persons,
cents: perUnit * (persons ?? 1),
}
}
const monthIn = (year: number, index: number): Month =>
`${String(year)}-${String(index + 1).padStart(2, '0')}`
function monthBefore(month: Month): Month {
const [year, m] = month.split('-').map(Number) as [number, number]
return m === 1 ? monthIn(year - 1, 11) : monthIn(year, m - 2)
}
/** A month's usage charges, or undefined when none has an amount. */
export function usageChargesOfMonth(
charges: readonly UsageChargePeriods[],
tenancies: readonly OccupiedTenancy[],
month: Month,
): UsageChargeMonth | undefined {
const before = monthBefore(month)
const rows: MonthUsageCharge[] = []
for (const charge of charges) {
const row: MonthUsageCharge | undefined = amountIn(charge, tenancies, month)
if (row === undefined) {
continue
}
const previous = amountIn(charge, tenancies, before)?.cents
if (
previous !== undefined &&
previous > 0 &&
row.cents > 0 &&
previous !== row.cents
) {
row.previous = previous
}
rows.push(row)
}
if (rows.length === 0) {
return undefined
}
return {
month,
charges: rows,
total: rows.reduce((sum, row) => sum + row.cents, 0),
}
}
/** A year's usage charges, up to the current month in the current year. */
export function usageChargesOfYear(
charges: readonly UsageChargePeriods[],
tenancies: readonly OccupiedTenancy[],
year: number,
today: IsoDate,
): UsageChargeYear {
const current = monthOf(today)
const months: UsageChargeMonth[] = []
const sums = new Map<string, number>()
for (let i = 0; i < 12; i++) {
const month = monthIn(year, i)
if (month > current) {
break
}
const row = usageChargesOfMonth(charges, tenancies, month)
if (!row) {
continue
}
months.unshift(row)
for (const charge of row.charges) {
sums.set(charge.id, (sums.get(charge.id) ?? 0) + charge.cents)
}
}
return {
year,
months,
sums: charges.flatMap((charge) => {
const cents = sums.get(charge.id) ?? 0
return cents > 0
? [
{
id: charge.id,
kind: charge.kind,
description: charge.description,
cents,
},
]
: []
}),
total: months.reduce((sum, row) => sum + row.total, 0),
}
}
/**
* The years with usage charges, newest first: the current year and every
* earlier one from the first period's year. Empty when no usage charge has an
* amount in any month up to the current one.
*/
export function usageChargeYears(
charges: readonly UsageChargePeriods[],
today: IsoDate,
): number[] {
const first = charges
.flatMap((charge) => charge.periods.map((p) => p.validFrom))
.filter((from) => from <= today)
.sort()
.at(0)
if (first === undefined) {
return []
}
const current = Number(today.slice(0, 4))
const from = Number(first.slice(0, 4))
return Array.from({ length: current - from + 1 }, (_, i) => current - i)
}
|