All files / src/features/year-summary summary.ts

99.54% Statements 217/218
95.34% Branches 164/172
100% Functions 69/69
99.5% Lines 201/202

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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739                                                                          8x     8x                                                                 8x                                                                                                                                                                                                                                 8x     8x 2076x                   17436x 1244x   17574x     8x       15707x   8x 3624x       302x 3624x 3624x                     751x 751x 751x   792x                 2416x     12569x       2076x       21140x 1170x 1170x         1514x   1134x                                       302x 12953x 5534x 302x 1510x   302x 1510x   302x 12544x 12544x   302x   302x 2877x 1524x       302x 2650x 1320x   1330x 1330x 1x   1329x       302x 302x 302x 302x 302x 3564x 3564x 3564x 3564x 3564x 4261x 4261x 2869x 1392x 36x 1356x 1338x   18x             3564x 24x   3564x 3564x 5484x 5484x             3564x 3577x   3564x 332x       302x 1510x         302x 302x   302x                                                                                   302x 302x 7x 6x             302x   302x 302x 302x 298x   4x       4x 4x       8x 1226x               302x 302x 302x 597x                           16x 16x 16x 16x 35x 2x 16x 18x   18x 18x 1x   17x 17x 1x 1x     16x     16x                   302x 302x 302x 302x 302x 302x 16x 16x   18x 18x   18x 18x     2x 16x 16x 16x                       16x         16x       286x 286x                               5x 240x                             115x   176x     115x 166x                     325x 325x 325x 325x 325x 480x   480x 480x           421x 421x     325x                   302x 302x 302x     302x     597x   597x   597x         2754x       302x   2465x     302x 302x 2465x 1323x     1322x       302x                 302x         302x                           302x 21x   281x 281x 281x 148x   296x 281x 429x 278x   151x 151x 151x                       70x   126x   100x 70x 14x   56x 56x 160x               8x 8x 8x         63x 63x 63x 7x   56x    
// A flat's year for one user as form 7H rows (openspec: year-summary). Pure
// functions over plain values: nothing is stored, and the rules of each
// feature are reused, not copied (a month's charges, usage charges, the
// category's row, the split on a date). Every amount counts at the user's
// share on the date that decides it (the cash basis); a part is rounded to
// the cent once, from the exact sum.
import { sharesOn, type Period } from '#/features/ownership/periods'
import {
  chargeMonth,
  type ChargePeriods,
} from '#/features/recurring-amounts/charges'
import {
  monthOf,
  type Month,
  type Period as ValuePeriod,
} from '#/features/recurring-amounts/periods'
import {
  rentMonths,
  tenancyMonths,
  type PaymentOfMonth,
} from '#/features/rent-payments/status'
import type { FeeOfTenancy } from '#/features/tenancies/fees'
import { CATEGORIES, type SummaryRow } from '#/features/transactions/categories'
import {
  usageChargesOfMonth,
  type OccupiedTenancy,
  type UsageChargePeriods,
} from '#/features/usage-charges/months'
import { USAGE_CHARGE_ROWS } from '#/features/usage-charges/rows'
import { addDays, type IsoDate } from '#/lib/dates'
import type {
  ChargeKind,
  FinancingTreatment,
  TransactionCategory,
} from '#/server/db/schema'
 
/** The form 7H rows the summary fills, in the order of the form. */
export const ROW_NUMBERS = ['2.1', '2.2', '2.3', '2.4', '2.5'] as const
export type RowNumber = (typeof ROW_NUMBERS)[number]
 
const ROW_OF: Record<SummaryRow | 'capital_charges', RowNumber> = {
  gross_rent: '2.1',
  maintenance_and_water: '2.2',
  capital_charges: '2.3',
  annual_repairs: '2.4',
  other_costs: '2.5',
}
 
/** What a part of a row is made of. */
export type PartKey =
  | 'rent_payments'
  | TransactionCategory
  | 'maintenance_charge'
  | 'financing_charge'
  // Damage kept from deposits: a negative part of row 2.4.
  | 'deposit_damage'
  // A usage charge of the kind, as the housing company charges it.
  | ChargeKind
 
export interface Part {
  key: PartKey
  /** The user's share of the part, cents. */
  cents: number
}
 
export interface Row {
  /** In the order of `PART_ORDER`, leaving out parts of 0. */
  parts: Part[]
  /** The parts' sum, cents. */
  cents: number
}
 
/** The order parts are shown in. */
const PART_ORDER: readonly PartKey[] = [
  'rent_payments',
  'tenant_water_charge',
  'other_income',
  'maintenance_charge',
  'water',
  'water_charge',
  'financing_charge',
  'annual_repair',
  'deposit_damage',
  'insurance',
  'other_expense',
  'parking',
  'sauna',
  'other',
]
 
/** An entry as the summary reads it. */
export interface SummaryEntry {
  category: TransactionCategory
  /** Positive, cents. */
  amount: number
  date: IsoDate
}
 
/** A tenancy as the summary reads it. */
export interface SummaryTenancy {
  id: string
  tenantName: string
  validFrom: IsoDate
  /** The last day; null while an open-ended tenancy runs. */
  validTo: IsoDate | null
  dueDay: number
  /** Occupants periods, for per-occupant usage charges and fees. */
  occupants: readonly ValuePeriod<number>[]
  /** The tenancy's rent periods and tenant fees, for the rent status. */
  rent: readonly ValuePeriod<number>[]
  fees: readonly FeeOfTenancy[]
}
 
/** A deposit return as the summary reads it. */
export interface SummaryDepositReturn {
  returnedOn: IsoDate
  /** Kept for damage, cents. */
  damageKept: number
}
 
/** The housing company details stored on the flat. */
export interface HousingCompany {
  name: string | null
  businessId: string | null
  apartmentNumber: string | null
}
 
/** One flat's data for a year, as plain values. */
export interface FlatYearInput {
  propertyId: string
  housingCompany: HousingCompany
  /** The flat's ownership periods, with everyone's shares. */
  ownership: readonly Period[]
  tenancies: readonly SummaryTenancy[]
  /** Payments of any month and date; the summary picks by payment date. */
  payments: readonly PaymentOfMonth[]
  /** The year's entries. */
  entries: readonly SummaryEntry[]
  charges: ChargePeriods
  treatments: readonly ValuePeriod<FinancingTreatment>[]
  usageCharges: readonly UsageChargePeriods[]
  /** The deposit returns of the flat's tenancies; the summary picks the year's. */
  depositReturns?: readonly SummaryDepositReturn[]
}
 
/** The flat details form 7H asks for. */
export interface FlatDetails extends HousingCompany {
  tenant: { kind: 'one'; name: string } | { kind: 'several' } | { kind: 'none' }
  /** Null without a tenancy in the year. */
  period: { from: IsoDate; to: IsoDate } | null
  /**
   * Every tenancy of the year in start order, each clipped to the year and to
   * today. Used by the PDF; `tenant` and `period` are what the screen shows.
   */
  tenancies: { name: string; from: IsoDate; to: IsoDate }[]
  /** Basis points at the end of the user's period in the year. */
  share: number
}
 
export type HousingDetail = 'name' | 'business_id' | 'apartment_number'
 
/** What needs checking in the year; no check changes a figure. */
export type Check =
  | { kind: 'unpaid'; months: Month[] }
  | { kind: 'partly_paid'; months: { month: Month; missing: number }[] }
  | { kind: 'vacant'; months: Month[] }
  | { kind: 'details_missing'; missing: HousingDetail[] }
  | { kind: 'treatment_not_set'; months: Month[] }
  | { kind: 'share_changed'; on: IsoDate; from: number; to: number }
 
export interface FlatYear {
  propertyId: string
  /** The user had a share above 0 on some day of the year, up to today. */
  included: boolean
  rows: Record<RowNumber, Row>
  /** Counted in no row, at the user's share (cents). */
  outside: {
    capitalised: number
    improvements: number
    /** Damage compensation over the year's annual repairs. */
    depositDamage: number
  }
  details: FlatDetails
  checks: Check[]
}
 
const WHOLE = 10000
 
/** Rounds `sum of amount × basis points` to cents, halves up. */
const toCents = (weighted: number): number =>
  Math.floor((2 * weighted + WHOLE) / (2 * WHOLE))
 
/** Whose figures: one owner's share, or the whole flat at every owner's share. */
export type Holder = { kind: 'owner'; userId: string } | { kind: 'whole' }
 
/** The holder's basis points in a period: one owner's, or all owners' summed. */
export function holderShare(
  period: { shares: readonly { userId: string; share: number }[] },
  holder: Holder,
): number {
  if (holder.kind === 'whole') {
    return period.shares.reduce((sum, s) => sum + s.share, 0)
  }
  return period.shares.find((s) => s.userId === holder.userId)?.share ?? 0
}
 
const shareOn = (
  ownership: readonly Period[],
  holder: Holder,
  date: IsoDate,
): number => holderShare({ shares: sharesOn(ownership, date) }, holder)
 
const monthIn = (year: number, index: number): Month =>
  `${String(year)}-${String(index + 1).padStart(2, '0')}`
 
/** The months of the year counted: up to the current one in the current year. */
export function monthsCounted(year: number, today: IsoDate): Month[] {
  const current = monthOf(today)
  return Array.from({ length: 12 }, (_, i) => monthIn(year, i)).filter(
    (month) => month <= current,
  )
}
 
/** Whether the user had a share above 0 on some day of the year up to today. */
export function ownedInYear(
  ownership: readonly Period[],
  holder: Holder,
  year: number,
  today: IsoDate,
): boolean {
  const first = `${String(year)}-01-01`
  const last = `${String(year)}-12-31` < today ? `${String(year)}-12-31` : today
  return ownership.some(
    (period) =>
      holderShare(period, holder) > 0 &&
      period.validFrom <= last &&
      (period.validTo === null || period.validTo >= first) &&
      first <= last,
  )
}
 
/** Sums of amount × basis points by part, then rounded once each. */
class Weighted {
  private readonly sums = new Map<PartKey, number>()
 
  add(key: PartKey, cents: number, basisPoints: number) {
    this.sums.set(key, (this.sums.get(key) ?? 0) + cents * basisPoints)
  }
 
  cents(key: PartKey): number {
    return toCents(this.sums.get(key) ?? 0)
  }
 
  parts(keys: readonly PartKey[]): Part[] {
    return PART_ORDER.filter((key) => keys.includes(key)).flatMap((key) => {
      const cents = this.cents(key)
      return cents > 0 ? [{ key, cents }] : []
    })
  }
}
 
const rowOf = (parts: Part[]): Row => ({
  parts,
  cents: parts.reduce((sum, part) => sum + part.cents, 0),
})
 
export interface FlatYearOptions {
  /**
   * Show only the tenancies of the days the holder had a share above 0, for a
   * former owner (openspec: year-summary, "Tenants in a former owner's
   * period"). Without it the flat details cover the whole year.
   */
  ownDaysOnly?: boolean
}
 
/** The flat's year for the user, on `today` (in Finland). */
export function flatYear(
  input: FlatYearInput,
  holder: Holder,
  year: number,
  today: IsoDate,
  options: FlatYearOptions = {},
): FlatYear {
  const { ownership } = input
  const shareAt = (date: IsoDate) => shareOn(ownership, holder, date)
  const inYear = (date: IsoDate) => date.startsWith(`${String(year)}-`)
  const byRow = new Map<RowNumber, Weighted>(
    ROW_NUMBERS.map((row) => [row, new Weighted()]),
  )
  const rowKeys = new Map<RowNumber, Set<PartKey>>(
    ROW_NUMBERS.map((row) => [row, new Set()]),
  )
  const add = (row: RowNumber, key: PartKey, cents: number, date: IsoDate) => {
    byRow.get(row)?.add(key, cents, shareAt(date))
    rowKeys.get(row)?.add(key)
  }
  const outside = { capitalised: new Weighted(), improvements: new Weighted() }
 
  for (const payment of input.payments) {
    if (inYear(payment.paidOn)) {
      add('2.1', 'rent_payments', payment.amount, payment.paidOn)
    }
  }
 
  for (const entry of input.entries) {
    if (!inYear(entry.date)) {
      continue
    }
    const row = CATEGORIES[entry.category].row
    if (row === null) {
      outside.improvements.add('improvement', entry.amount, shareAt(entry.date))
    } else {
      add(ROW_OF[row], entry.category, entry.amount, entry.date)
    }
  }
 
  const occupied: readonly OccupiedTenancy[] = input.tenancies
  const months = monthsCounted(year, today)
  const treatmentNotSet: Month[] = []
  const vacant: Month[] = []
  for (const month of months) {
    const first = `${month}-01`
    const charged = chargeMonth(input.charges, input.treatments, month)
    let monthCents = 0
    let untreated = false
    for (const charge of charged?.charges ?? []) {
      monthCents += charge.cents
      if (charge.kind === 'maintenance_charge') {
        add('2.2', 'maintenance_charge', charge.cents, first)
      } else if (charged?.treatment === undefined) {
        untreated ||= charge.cents > 0
      } else if (charged.treatment === 'income_recognised') {
        add('2.3', 'financing_charge', charge.cents, first)
      } else {
        outside.capitalised.add(
          'financing_charge',
          charge.cents,
          shareAt(first),
        )
      }
    }
    if (untreated && shareAt(first) > 0) {
      treatmentNotSet.push(month)
    }
    const used = usageChargesOfMonth(input.usageCharges, occupied, month)
    for (const charge of used?.charges ?? []) {
      monthCents += charge.cents
      add(
        ROW_OF[USAGE_CHARGE_ROWS[charge.kind]],
        charge.kind,
        charge.cents,
        first,
      )
    }
    const ran = input.tenancies.some(
      (tenancy) => tenancyMonths(tenancy, month, month).length > 0,
    )
    if (!ran && monthCents > 0 && shareAt(first) > 0) {
      vacant.push(month)
    }
  }
 
  const rows = Object.fromEntries(
    ROW_NUMBERS.map((row) => [
      row,
      rowOf(byRow.get(row)?.parts([...(rowKeys.get(row) ?? [])]) ?? []),
    ]),
  ) as Record<RowNumber, Row>
  const damage = depositDamage(input, shareAt, inYear, rows['2.4'])
  rows['2.4'] = damage.row
 
  return {
    propertyId: input.propertyId,
    included: ownedInYear(ownership, holder, year, today),
    rows,
    outside: {
      capitalised: outside.capitalised.cents('financing_charge'),
      improvements: outside.improvements.cents('improvement'),
      depositDamage: damage.surplus,
    },
    details: flatDetails(
      input,
      holder,
      year,
      today,
      options.ownDaysOnly === true,
    ),
    checks: [
      ...paymentChecks(input, holder, months, today),
      ...(vacant.length > 0
        ? [{ kind: 'vacant' as const, months: vacant }]
        : []),
      ...detailsChecks(input.housingCompany),
      ...(treatmentNotSet.length > 0
        ? [{ kind: 'treatment_not_set' as const, months: treatmentNotSet }]
        : []),
      ...shareChanges(ownership, holder, year, today),
    ],
  }
}
 
/**
 * Damage kept from the deposits returned in the year, at the user's share on
 * each return date, rounded once: a negative part of row 2.4, at most the
 * annual repairs part so the row stays at 0 or above. What is over goes
 * outside form 7H as tax-free compensation.
 */
function depositDamage(
  input: FlatYearInput,
  shareAt: (date: IsoDate) => number,
  inYear: (date: IsoDate) => boolean,
  row: Row,
): { row: Row; surplus: number } {
  const kept = new Weighted()
  for (const depositReturn of input.depositReturns ?? []) {
    if (inYear(depositReturn.returnedOn)) {
      kept.add(
        'deposit_damage',
        depositReturn.damageKept,
        shareAt(depositReturn.returnedOn),
      )
    }
  }
  const compensation = kept.cents('deposit_damage')
  const repairs =
    row.parts.find((part) => part.key === 'annual_repair')?.cents ?? 0
  const applied = Math.min(compensation, repairs)
  if (applied === 0) {
    return { row, surplus: compensation }
  }
  const parts = [
    ...row.parts,
    { key: 'deposit_damage' as const, cents: -applied },
  ]
  parts.sort((a, b) => PART_ORDER.indexOf(a.key) - PART_ORDER.indexOf(b.key))
  return { row: rowOf(parts), surplus: compensation - applied }
}
 
/** The last day of the year counted: 31 December, or today in the current year. */
const lastDayCounted = (year: number, today: IsoDate): IsoDate =>
  `${String(year)}-12-31` < today ? `${String(year)}-12-31` : today
 
/** The tenancies that ran on some day of the year counted. */
function tenanciesInYear(
  tenancies: readonly SummaryTenancy[],
  year: number,
  today: IsoDate,
): SummaryTenancy[] {
  const first = `${String(year)}-01-01`
  const last = lastDayCounted(year, today)
  return tenancies.filter(
    (t) => t.validFrom <= last && (t.validTo === null || t.validTo >= first),
  )
}
 
/**
 * The runs of days of the year counted on which the holder had a share above
 * 0, in date order. Periods that follow each other are one run.
 */
function holderSpans(
  ownership: readonly Period[],
  holder: Holder,
  year: number,
  today: IsoDate,
): { from: IsoDate; to: IsoDate }[] {
  const first = `${String(year)}-01-01`
  const last = lastDayCounted(year, today)
  const spans: { from: IsoDate; to: IsoDate }[] = []
  const owned = ownership
    .filter((period) => holderShare(period, holder) > 0)
    .sort((a, b) => a.validFrom.localeCompare(b.validFrom))
  for (const period of owned) {
    const from = period.validFrom > first ? period.validFrom : first
    const to =
      period.validTo !== null && period.validTo < last ? period.validTo : last
    if (from > to) {
      continue
    }
    const previous = spans.at(-1)
    if (previous !== undefined && addDays(previous.to, 1) >= from) {
      Eif (to > previous.to) {
        previous.to = to
      }
    } else {
      spans.push({ from, to })
    }
  }
  return spans
}
 
function flatDetails(
  input: FlatYearInput,
  holder: Holder,
  year: number,
  today: IsoDate,
  ownDaysOnly: boolean,
): FlatDetails {
  const ran = tenanciesInYear(input.tenancies, year, today)
  const first = `${String(year)}-01-01`
  const end = `${String(year)}-12-31`
  const last = lastDayCounted(year, today)
  const share = shareAtEnd(input.ownership, holder, year, today)
  if (ownDaysOnly) {
    const spans = holderSpans(input.ownership, holder, year, today)
    const pieces = ran
      .flatMap((t) =>
        spans.flatMap((span) => {
          const from = t.validFrom > span.from ? t.validFrom : span.from
          const to =
            t.validTo !== null && t.validTo < span.to ? t.validTo : span.to
          return from <= to ? [{ id: t.id, name: t.tenantName, from, to }] : []
        }),
      )
      .sort((a, b) => a.from.localeCompare(b.from))
    const tenants = new Set(pieces.map((piece) => piece.id))
    const only = tenants.size === 1 ? pieces[0] : undefined
    return {
      ...input.housingCompany,
      tenant: only
        ? { kind: 'one', name: only.name }
        : tenants.size > 1
          ? { kind: 'several' }
          : { kind: 'none' },
      period:
        pieces.length > 0
          ? {
              from: pieces[0]?.from ?? first,
              to: pieces.reduce(
                (latest, p) => (p.to > latest ? p.to : latest),
                first,
              ),
            }
          : null,
      tenancies: pieces.map(({ name, from, to }) => ({ name, from, to })),
      share,
    }
  }
  const only = ran.length === 1 ? ran[0] : undefined
  return {
    ...input.housingCompany,
    tenant: only
      ? { kind: 'one', name: only.tenantName }
      : ran.length > 1
        ? { kind: 'several' }
        : { kind: 'none' },
    period: only
      ? {
          from: only.validFrom > first ? only.validFrom : first,
          to: only.validTo !== null && only.validTo < end ? only.validTo : end,
        }
      : ran.length > 1
        ? { from: first, to: end }
        : null,
    tenancies: [...ran]
      .sort((a, b) => a.validFrom.localeCompare(b.validFrom))
      .map((t) => ({
        name: t.tenantName,
        from: t.validFrom > first ? t.validFrom : first,
        to: t.validTo !== null && t.validTo < last ? t.validTo : last,
      })),
    share,
  }
}
 
/** Everyone who had a share above 0 on some day of the year up to today. */
export function ownersInYear(
  ownership: readonly Period[],
  year: number,
  today: IsoDate,
): string[] {
  const ids = new Set(
    ownership.flatMap((period) =>
      period.shares.filter((s) => s.share > 0).map((s) => s.userId),
    ),
  )
  return [...ids].filter((userId) =>
    ownedInYear(ownership, { kind: 'owner', userId }, year, today),
  )
}
 
/** The holder's share at the end of their ownership in the year. */
export function shareAtEnd(
  ownership: readonly Period[],
  holder: Holder,
  year: number,
  today: IsoDate,
): number {
  const first = `${String(year)}-01-01`
  const last = lastDayCounted(year, today)
  let share = 0
  let end = ''
  for (const period of ownership) {
    const own = holderShare(period, holder)
    const periodEnd =
      period.validTo !== null && period.validTo < last ? period.validTo : last
    if (
      own > 0 &&
      period.validFrom <= last &&
      periodEnd >= first &&
      periodEnd > end
    ) {
      end = periodEnd
      share = own
    }
  }
  return share
}
 
/** Months not fully paid, in months the user had a share on the first day. */
function paymentChecks(
  input: FlatYearInput,
  holder: Holder,
  months: readonly Month[],
  today: IsoDate,
): Check[] {
  const from = months.at(0)
  const to = months.at(-1)
  Iif (from === undefined || to === undefined) {
    return []
  }
  const rows = rentMonths({
    tenancies: input.tenancies,
    payments: input.payments,
    rents: Object.fromEntries(input.tenancies.map((t) => [t.id, t.rent])),
    occupants: Object.fromEntries(
      input.tenancies.map((t) => [t.id, t.occupants]),
    ),
    fees: Object.fromEntries(input.tenancies.map((t) => [t.id, t.fees])),
    from,
    to,
    today,
  })
    .filter((row) => shareOn(input.ownership, holder, `${row.month}-01`) > 0)
    .reverse()
  // Two tenancies in one month give two rows: name the month once, and a
  // partly paid month with what is missing from both.
  const unpaid = [
    ...new Set(
      rows.filter((row) => row.status.kind === 'late').map((row) => row.month),
    ),
  ]
  const missing = new Map<Month, number>()
  for (const row of rows) {
    if (row.status.kind === 'partly_paid') {
      missing.set(row.month, (missing.get(row.month) ?? 0) + row.status.missing)
    }
  }
  const partly = [...missing].map(([month, cents]) => ({
    month,
    missing: cents,
  }))
  return [
    ...(unpaid.length > 0 ? [{ kind: 'unpaid' as const, months: unpaid }] : []),
    ...(partly.length > 0
      ? [{ kind: 'partly_paid' as const, months: partly }]
      : []),
  ]
}
 
function detailsChecks(company: HousingCompany): Check[] {
  const missing: HousingDetail[] = [
    ...(company.name ? [] : ['name' as const]),
    ...(company.businessId ? [] : ['business_id' as const]),
    ...(company.apartmentNumber ? [] : ['apartment_number' as const]),
  ]
  return missing.length > 0 ? [{ kind: 'details_missing', missing }] : []
}
 
/**
 * The days in the year the user's share changed: bought, sold or split
 * anew. A share starting during the year changes from 0.
 */
function shareChanges(
  ownership: readonly Period[],
  holder: Holder,
  year: number,
  today: IsoDate,
): Check[] {
  // The sum of all owners' shares doesn't change when owners do.
  if (holder.kind === 'whole') {
    return []
  }
  const first = `${String(year)}-01-01`
  const last = lastDayCounted(year, today)
  const sorted = [...ownership].sort((a, b) =>
    a.validFrom.localeCompare(b.validFrom),
  )
  const own = (period: Period) => holderShare(period, holder)
  return sorted.flatMap((period, i) => {
    if (period.validFrom <= first || period.validFrom > last) {
      return []
    }
    const from = i === 0 ? 0 : own(sorted[i - 1])
    const to = own(period)
    return from !== to
      ? [{ kind: 'share_changed' as const, on: period.validFrom, from, to }]
      : []
  })
}
 
/** The years from the holder's first share above 0 up to the current year. */
export function summaryYears(
  ownership: readonly Period[],
  holder: Holder,
  today: IsoDate,
): number[] {
  const starts = ownership
    .filter(
      (period) => period.validFrom <= today && holderShare(period, holder) > 0,
    )
    .map((period) => Number(period.validFrom.slice(0, 4)))
  if (starts.length === 0) {
    return []
  }
  const current = Number(today.slice(0, 4))
  const first = Math.min(...starts)
  return Array.from({ length: current - first + 1 }, (_, i) => first + i)
}
 
/**
 * The year to open: the previous year from January to June, the current year
 * from July, within the years; before the first of them, the first.
 */
export function openingYear(years: readonly number[], today: IsoDate): number {
  const current = Number(today.slice(0, 4))
  const wanted = Number(today.slice(5, 7)) <= 6 ? current - 1 : current
  return clampYear(years, wanted)
}
 
/** The nearest year in the range; the year itself when there is no range. */
export function clampYear(years: readonly number[], year: number): number {
  const first = years.at(0)
  const last = years.at(-1)
  if (first === undefined || last === undefined) {
    return year
  }
  return Math.min(Math.max(year, first), last)
}