All files / src/features/tenancies fees.ts

100% Statements 15/15
100% Branches 19/19
100% Functions 5/5
100% Lines 15/15

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                                                                  2737x                             1395x 1395x 2x   1393x                                                             393x 82x       71x     393x 323x   70x 70x 94x 97x   94x          
// A tenancy's occupants and tenant fees in a month (openspec: tenancies,
// "Occupants of a tenancy", "Tenant fees are recorded as periods"). Pure: no
// database access. Prorating by days is the rent payments' business
// (`rent-payments/status.ts`); these are the full monthly amounts.
import type { Month, Period } from '#/features/recurring-amounts/periods'
import type { IsoDate } from '#/lib/dates'
import type { ChargeBasis, ChargeKind } from '#/server/db/schema'
import { valueInTenancyMonth, type RentDates } from './rent'
 
/** What the fee calculations need of a tenancy. */
export type FeeTenancy = Pick<RentDates, 'validFrom'>
 
/** What the fee calculations need of a fee. */
export interface FeeAmounts {
  basis: ChargeBasis
  /** Cents per month or per occupant per month, oldest first. */
  amounts: readonly Period<number>[]
}
 
/** A tenant fee as the rent calculations and the pages read it. */
export interface FeeOfTenancy extends FeeAmounts {
  id: string
  kind: ChargeKind
  /** Set for the kind `other`. */
  description: string | null
}
 
/** The persons living in the flat in `month`; undefined without a period. */
export function occupantsInMonth(
  occupants: readonly Period<number>[],
  tenancy: FeeTenancy,
  month: Month,
): number | undefined {
  return valueInTenancyMonth(occupants, tenancy, month)
}
 
/**
 * A fee's full monthly amount in `month`: the amount of its period on the
 * month's first day (or the tenancy's start day in the month it starts),
 * times the occupants for a per-occupant fee. Undefined when no amount is in
 * effect. A tenancy without occupants counts one.
 */
export function feeInMonth(
  fee: FeeAmounts,
  occupants: readonly Period<number>[],
  tenancy: FeeTenancy,
  month: Month,
): number | undefined {
  const amount = valueInTenancyMonth(fee.amounts, tenancy, month)
  if (amount === undefined) {
    return undefined
  }
  return fee.basis === 'per_occupant'
    ? amount * (occupantsInMonth(occupants, tenancy, month) ?? 1)
    : amount
}
 
/** A tenancy with its tenant fees, for offering them to the next one. */
export interface TenancyWithFees {
  id: string
  validFrom: IsoDate
  /** The last day; null while an open-ended tenancy runs. */
  validTo: IsoDate | null
  fees: readonly FeeOfTenancy[]
}
 
/** A fee offered to the next tenancy, with the amount it carries over. */
export interface OfferedFee {
  fee: FeeOfTenancy
  /** Cents: the amount in force on the previous tenancy's last day. */
  amount: number
}
 
/**
 * The fees of the tenancy that starts last before `start` that aren't ended
 * on its last day (openspec: tenancies, "Occupants and fees of a new
 * tenancy"), each with the amount it carries over.
 */
export function offeredFees(
  tenancies: readonly TenancyWithFees[],
  start: IsoDate,
): OfferedFee[] {
  let previous: TenancyWithFees | undefined
  for (const tenancy of tenancies) {
    if (
      tenancy.validFrom < start &&
      (previous === undefined || tenancy.validFrom > previous.validFrom)
    ) {
      previous = tenancy
    }
  }
  if (previous === undefined) {
    return []
  }
  const lastDay = previous.validTo
  return previous.fees.flatMap((fee) => {
    const inForce = fee.amounts
      .filter((p) => lastDay === null || p.validFrom <= lastDay)
      .at(-1)
    return inForce !== undefined && inForce.value > 0
      ? [{ fee, amount: inForce.value }]
      : []
  })
}