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 229 230 231 232 233 234 235 236 237 238 239 | 11x 4577x 1840x 64x 11x 5364x 4073x 4073x 4073x 4073x 4073x 4300x 4073x 4073x 5700x 5700x 1045x 4655x 4655x 4655x 4317x 6x 4655x 1621x 1621x 3x 1621x 3x 4655x 4073x 936x 3137x 4655x 46x 552x 552x 46x 46x 46x 46x 509x 509x 301x 208x 208x 394x 165x 165x 229x 229x 229x 229x 46x 21x 63x 63x 46x 46x 208x 41x 41x 492x 696x 41x 12x 26x 26x 12x 12x 3x 9x 9x 18x | // A flat's charges month by month and by year (openspec: recurring-amounts,
// "Charges of a month"): the maintenance charge and each financing charge.
// Pure functions over the periods: nothing is stored, so a corrected period
// corrects every month computed from it.
import type { IsoDate } from '#/lib/dates'
import type { FinancingTreatment } from '#/server/db/schema'
import { monthOf, valueInMonth, type Month, type Period } from './periods'
/** The periods a year's charges are computed from. The rent is not a charge. */
export interface ChargePeriods {
maintenance: readonly Period<number>[]
/** The flat's financing charges, in the order they are shown. */
financing: readonly {
id: string
name: string | null
periods: readonly Period<number>[]
}[]
}
/** A charge's treatment for the year's sums: `not_set` without one. */
export type TreatmentKey = FinancingTreatment | 'not_set'
export const TREATMENT_KEYS: readonly TreatmentKey[] = [
'income_recognised',
'capitalised',
'not_set',
]
/** One charge in a month: the maintenance charge or a financing charge. */
export interface MonthCharge {
/** `maintenance` or the financing charge's id. */
key: string
kind: 'maintenance_charge' | 'financing_charge'
/** A financing charge's name; null for the unnamed one and maintenance. */
name: string | null
cents: number
/** The month before's amount, when both are above 0 and differ. */
previous?: number
/** A financing charge's first month, after a month with charges. */
started?: true
/** A financing charge's first month at 0 after one above it. */
ended?: true
}
export interface ChargeMonth {
month: Month
/** The charges set in the month, maintenance first. */
charges: MonthCharge[]
/** The financing charges' treatment on the month's first day. */
treatment: FinancingTreatment | undefined
total: number
}
/** A financing charge's sum in a year under one treatment. */
export interface FinancingLine {
chargeId: string
name: string | null
treatment: TreatmentKey
cents: number
}
export interface ChargeYear {
year: number
/** Newest first, only months with a charge set. */
months: ChargeMonth[]
maintenance: number
/** Per financing charge and treatment, leaving out sums of 0. */
financing: FinancingLine[]
total: number
/** The whole year with the periods as they are now; current year before December only. */
estimate: number | undefined
}
interface Sequence {
key: string
kind: MonthCharge['kind']
name: string | null
periods: readonly Period<number>[]
}
function sequencesOf(periods: ChargePeriods): Sequence[] {
return [
{
key: 'maintenance',
kind: 'maintenance_charge',
name: null,
periods: periods.maintenance,
},
...periods.financing.map((charge) => ({
key: charge.id,
kind: 'financing_charge' as const,
name: charge.name,
periods: charge.periods,
})),
]
}
const yearOf = (date: IsoDate): number => Number(date.slice(0, 4))
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 charges, or undefined when none is set. */
export function chargeMonth(
periods: ChargePeriods,
treatments: readonly Period<FinancingTreatment>[],
month: Month,
): ChargeMonth | undefined {
const sequences = sequencesOf(periods)
const before = monthBefore(month)
const anyBefore = sequences.some(
(sequence) => valueInMonth(sequence.periods, before) !== undefined,
)
const charges: MonthCharge[] = []
for (const { key, kind, name, periods: own } of sequences) {
const cents = valueInMonth(own, month)
if (cents === undefined) {
continue
}
const previous = valueInMonth(own, before)
const charge: MonthCharge = { key, kind, name, cents }
if (previous !== undefined && previous > 0 && cents > 0) {
if (previous !== cents) {
charge.previous = previous
}
}
if (kind === 'financing_charge') {
const first = own.at(0)
if (first && monthOf(first.validFrom) === month && anyBefore) {
charge.started = true
}
if (cents === 0 && previous !== undefined && previous > 0) {
charge.ended = true
}
}
charges.push(charge)
}
if (charges.length === 0) {
return undefined
}
return {
month,
charges,
treatment: valueInMonth(treatments, month),
total: charges.reduce((sum, charge) => sum + charge.cents, 0),
}
}
/** The months of a year to count: up to the current month in the current year. */
function monthsOf(year: number, today: IsoDate): Month[] {
const current = monthOf(today)
return Array.from({ length: 12 }, (_, i) => monthIn(year, i)).filter(
(month) => month <= current,
)
}
/** A year's charges, newest month first. */
export function chargesOfYear(
periods: ChargePeriods,
treatments: readonly Period<FinancingTreatment>[],
year: number,
today: IsoDate,
): ChargeYear {
const months: ChargeMonth[] = []
let maintenance = 0
// Per charge id, then treatment.
const sums = new Map<string, Partial<Record<TreatmentKey, number>>>()
for (const month of monthsOf(year, today)) {
const row = chargeMonth(periods, treatments, month)
if (!row) {
continue
}
months.unshift(row)
for (const charge of row.charges) {
if (charge.kind === 'maintenance_charge') {
maintenance += charge.cents
continue
}
const key: TreatmentKey = row.treatment ?? 'not_set'
const sum = sums.get(charge.key) ?? {}
sum[key] = (sum[key] ?? 0) + charge.cents
sums.set(charge.key, sum)
}
}
const financing = periods.financing.flatMap((charge) =>
TREATMENT_KEYS.flatMap((treatment) => {
const cents = sums.get(charge.id)?.[treatment] ?? 0
return cents > 0
? [{ chargeId: charge.id, name: charge.name, treatment, cents }]
: []
}),
)
const isCurrent = year === yearOf(today) && monthOf(today) < monthIn(year, 11)
return {
year,
months,
maintenance,
financing,
total: months.reduce((sum, row) => sum + row.total, 0),
estimate: isCurrent ? wholeYear(periods, year) : undefined,
}
}
function wholeYear(periods: ChargePeriods, year: number): number {
let total = 0
for (let i = 0; i < 12; i++) {
for (const sequence of sequencesOf(periods)) {
total += valueInMonth(sequence.periods, monthIn(year, i)) ?? 0
}
}
return total
}
/**
* The years with charges, newest first: the current year and every earlier
* one from the first charge period's year. Empty when no charge is set in
* any month up to the current one.
*/
export function chargeYears(periods: ChargePeriods, today: IsoDate): number[] {
const starts = sequencesOf(periods)
.flatMap((sequence) => sequence.periods.map((p) => p.validFrom))
.filter((from) => from <= today)
.sort()
const first = starts.at(0)
if (first === undefined) {
return []
}
const current = yearOf(today)
return Array.from(
{ length: current - yearOf(first) + 1 },
(_, i) => current - i,
)
}
|