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 | 3x 3x 11x 11x 3x 3x 3x 6x 7x 6x 5x 6x 6x 11x 11x 3x 10x 6x 5x 2x 3x 5x 5x 2x 3x 27x 27x 3x 3x 3x 3x 3x 3x 3x 6x 3x 3x 6x 5x 3x 5x | // A flat's rent payments by year (Design canvas: Vuokranmaksut; openspec:
// rent-payments, "The rent payments page"): the rent received in the year by
// payment date, the months with their statuses, newest first, and a warning
// for the months not fully paid.
import Box from '@mui/material/Box'
import IconButton from '@mui/material/IconButton'
import Link from '@mui/material/Link'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { createLink } from '@tanstack/react-router'
import { YearChoice } from '#/components/YearChoice'
import { monthOf } from '#/features/recurring-amounts/periods'
import type { Tenancy } from '#/features/tenancies/tenancy.repository.server'
import {
formatDayMonth,
formatMoney,
formatMonth,
formatMonthName,
} from '#/lib/format'
import { m } from '#/paraglide/messages.js'
import { colors, radii } from '#/theme/tokens'
import { statusText } from './labels'
import type { RentPayment } from './rent-payment.repository.server'
import {
monthsNotFullyPaid,
rentMonths,
tenancyMonths,
type RentMonth,
type FeesByTenancy,
type OccupantsByTenancy,
type RentsByTenancy,
} from './status'
import { StatusChip } from './StatusChip'
export interface RentPaymentsPageView {
today: string
/** Newest first. */
tenancies: Tenancy[]
rents: RentsByTenancy
occupants: OccupantsByTenancy
fees: FeesByTenancy
payments: RentPayment[]
}
interface Props {
view: RentPaymentsPageView
propertyId: string
address: string
/** The year shown; the current year when not chosen. */
year: number | undefined
onBack: () => void
onYear: (year: number) => void
}
const card = {
bgcolor: 'background.paper',
border: `1px solid ${colors.cardBorder}`,
borderRadius: `${String(radii.card)}px`,
} as const
const RowLink = createLink(Link)
function Icon(props: { path: string; size?: number }) {
const size = props.size ?? 24
return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.4"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<path d={props.path} />
</svg>
)
}
const BACK = 'M15 5 L8 12 L15 19'
const NEXT = 'M9 5 L16 12 L9 19'
const ALERT = 'M12 3 L22 20 H2 Z M12 10 V14 M12 17 V17.01'
/**
* The years to choose from: those in which a tenancy ran or rent was paid
* (rent paid in advance can fall before the tenancy), newest first.
*/
export function rentYears(
tenancies: readonly Tenancy[],
payments: readonly Pick<RentPayment, 'paidOn'>[],
today: string,
) {
const current = Number(today.slice(0, 4))
const paidIn = new Set(payments.map((p) => Number(p.paidOn.slice(0, 4))))
const first = Math.min(
current,
...tenancies.map((t) => Number(t.validFrom.slice(0, 4))),
...paidIn,
)
const years: number[] = []
for (let year = current; year >= first; year--) {
const to = year === current ? monthOf(today) : `${String(year)}-12`
if (
paidIn.has(year) ||
tenancies.some((t) => tenancyMonths(t, `${String(year)}-01`, to).length)
) {
years.push(year)
}
}
return years
}
/** The short status a row of the list shows. */
function shortStatus(row: RentMonth): string {
switch (row.status.kind) {
case 'paid':
return m.rent_short_paid()
case 'partly_paid':
return m.rent_short_partly_paid()
case 'late':
return m.rent_short_late()
case 'due':
case 'nothing_to_pay':
return statusText(row.status)
case 'rent_not_set':
return m.rent_short_rent_not_set()
}
}
/** The line under a row's month. */
function rowDetail(row: RentMonth): string {
const { status } = row
switch (status.kind) {
case 'paid':
return m.rent_row_paid({
amount: formatMoney(row.paid),
date: formatDayMonth(status.on),
})
case 'partly_paid':
return m.rent_row_partly_paid({
paid: formatMoney(row.paid),
expected: formatMoney(row.expected ?? 0),
missing: formatMoney(status.missing),
})
case 'late':
return m.rent_row_late({ date: formatDayMonth(row.dueDate) })
case 'due':
return m.rent_row_due({ date: formatDayMonth(row.dueDate) })
case 'nothing_to_pay':
return m.rent_row_nothing_to_pay({ amount: formatMoney(0) })
case 'rent_not_set':
return m.rent_row_rent_not_set()
}
}
/** "Lokakuu 2026, marraskuu 2026" style list of months. */
export function monthList(rows: readonly RentMonth[]): string {
return [...rows]
.reverse()
.map((row) =>
formatMonth(Number(row.month.slice(0, 4)), Number(row.month.slice(5, 7))),
)
.join(', ')
}
export function RentPaymentsPage(props: Props) {
const { view } = props
const years = rentYears(view.tenancies, view.payments, view.today)
const currentYear = Number(view.today.slice(0, 4))
const year = props.year ?? currentYear
const data = {
tenancies: view.tenancies,
rents: view.rents,
occupants: view.occupants,
fees: view.fees,
payments: view.payments,
today: view.today,
}
const rows = rentMonths({
...data,
from: `${String(year)}-01`,
to: year === currentYear ? monthOf(view.today) : `${String(year)}-12`,
})
const received = view.payments
.filter((p) => p.paidOn.startsWith(`${String(year)}-`))
.reduce((sum, p) => sum + p.amount, 0)
const unpaid = monthsNotFullyPaid(data).filter((row) =>
row.month.startsWith(`${String(year)}-`),
)
const showTenant = new Set(rows.map((row) => row.tenancy.id)).size > 1
return (
<Stack sx={{ minHeight: '100dvh' }}>
<Box
component="header"
sx={{
display: 'grid',
gridTemplateColumns: '44px 1fr 44px',
alignItems: 'center',
px: 1.5,
pt: 2,
pb: 0.5,
}}
>
<IconButton aria-label={m.amounts_back()} onClick={props.onBack}>
<Icon path={BACK} />
</IconButton>
<Stack sx={{ alignItems: 'center', minWidth: 0 }}>
<Typography variant="h3" component="h1" sx={{ fontSize: 18 }}>
{m.rent_payments_title()}
</Typography>
<Typography variant="body2" color="text.secondary" noWrap>
{props.address}
</Typography>
</Stack>
</Box>
<Stack
component="main"
sx={{ flex: 1, gap: 1.5, px: 2.5, pt: 1.25, pb: 3 }}
>
{years.length === 0 ? (
<Typography
sx={{
...card,
px: 2.5,
py: 3,
textAlign: 'center',
fontWeight: 800,
}}
>
{m.rent_payments_empty()}
</Typography>
) : (
<>
<YearChoice
label={m.rent_payments_year()}
years={years}
year={year}
onYear={props.onYear}
/>
<Stack
component="section"
aria-labelledby="received-title"
sx={{ ...card, px: 2, py: 1.75, gap: 0.5 }}
>
<Typography
id="received-title"
component="h2"
variant="body2"
sx={{ fontWeight: 800, color: 'text.secondary' }}
>
{m.rent_payments_received({ year: String(year) })}
</Typography>
<Typography sx={{ fontSize: 26, fontWeight: 900 }}>
{formatMoney(received)}
</Typography>
<Typography variant="body2" color="text.secondary">
{m.rent_payments_received_note()}
</Typography>
</Stack>
{unpaid.length > 0 ? (
<Stack
role="note"
direction="row"
sx={{
gap: 1.25,
alignItems: 'flex-start',
bgcolor: colors.yellowTint,
borderRadius: `${String(radii.tile)}px`,
px: 1.75,
py: 1.5,
}}
>
<Box sx={{ display: 'flex', flexShrink: 0, mt: 0.25 }}>
<Icon path={ALERT} size={20} />
</Box>
<Typography sx={{ fontSize: 13, fontWeight: 800 }}>
{m.rent_payments_unpaid({
count: unpaid.length,
months: monthList(unpaid),
})}
</Typography>
</Stack>
) : null}
{rows.length > 0 ? (
<Box
component="ul"
aria-label={m.rent_payments_months()}
sx={{
...card,
m: 0,
p: 0,
listStyle: 'none',
overflow: 'hidden',
}}
>
{rows.map((row, i) => (
<Box
key={`${row.month}-${row.tenancy.id}`}
component="li"
sx={{
borderTop:
i === 0 ? 'none' : `1px solid ${colors.divider}`,
}}
>
<RowLink
to="/properties/$propertyId/rent/$tenancyId/$month"
params={{
propertyId: props.propertyId,
tenancyId: row.tenancy.id,
month: row.month,
}}
search={{ year }}
underline="none"
sx={{
display: 'flex',
alignItems: 'center',
gap: 1.5,
minHeight: 60,
pl: 2,
pr: 1.5,
py: 0.75,
color: 'text.primary',
}}
>
<Stack sx={{ flex: 1, minWidth: 0 }}>
<Typography sx={{ fontSize: 15, fontWeight: 900 }}>
{formatMonthName(row.month)}
</Typography>
<Typography variant="body2" color="text.secondary">
{showTenant
? `${row.tenancy.tenantName} ยท ${rowDetail(row)}`
: rowDetail(row)}
</Typography>
</Stack>
<StatusChip status={row.status} text={shortStatus(row)} />
<Icon path={NEXT} size={18} />
</RowLink>
</Box>
))}
</Box>
) : null}
</>
)}
</Stack>
</Stack>
)
}
|