💳 payments collect tab — student roster with debt status & plan filter
Browse files at this commitc39779552d38f83572d80cebe1c92e9b62ba5928Parent: 531cdb0- feat
- 💳 payments collect tab — student roster with debt status & plan filter
3 changed files +148 -12
- src/i18n/messages.ts +18 -0
- src/pages/payment-statement-page.tsx +1 -1
- src/pages/payments-page.tsx +129 -11
src/i18n/messages.tsmodified+18 -0
| @@ -1005,6 +1005,12 @@ export type MessageKey = | ||
| 1005 | 1005 | | "payments.showLedger" |
| 1006 | 1006 | | "payments.hideLedger" |
| 1007 | 1007 | | "payments.plan" |
| 1008 | + | "payments.student" | |
| 1009 | + | "payments.username" | |
| 1010 | + | "payments.allStudents" | |
| 1011 | + | "payments.allPlans" | |
| 1012 | + | "payments.inDebt" | |
| 1013 | + | "payments.settled" | |
| 1008 | 1014 | | "terms.title" |
| 1009 | 1015 | | "terms.subtitle" |
| 1010 | 1016 | | "terms.create" |
| @@ -2254,6 +2260,12 @@ const en: Dict = { | ||
| 2254 | 2260 | "payments.showLedger": "Show account activity", |
| 2255 | 2261 | "payments.hideLedger": "Hide account activity", |
| 2256 | 2262 | "payments.plan": "Plan", |
| 2263 | + "payments.student": "Student", | |
| 2264 | + "payments.username": "Username", | |
| 2265 | + "payments.allStudents": "All students", | |
| 2266 | + "payments.allPlans": "All plans", | |
| 2267 | + "payments.inDebt": "In debt", | |
| 2268 | + "payments.settled": "Settled", | |
| 2257 | 2269 | "terms.title": "Academic terms", |
| 2258 | 2270 | "terms.subtitle": "Manage academic terms and assign courses to each one.", |
| 2259 | 2271 | "terms.create": "Create term", |
| @@ -3429,6 +3441,12 @@ const tr: Dict = { | ||
| 3429 | 3441 | "payments.showLedger": "Hesap hareketlerini göster", |
| 3430 | 3442 | "payments.hideLedger": "Hesap hareketlerini gizle", |
| 3431 | 3443 | "payments.plan": "Plan", |
| 3444 | + "payments.student": "Öğrenci", | |
| 3445 | + "payments.username": "Kullanıcı adı", | |
| 3446 | + "payments.allStudents": "Tüm öğrenciler", | |
| 3447 | + "payments.allPlans": "Tüm planlar", | |
| 3448 | + "payments.inDebt": "Borçlu", | |
| 3449 | + "payments.settled": "Borçsuz", | |
| 3432 | 3450 | "terms.title": "Akademik dönemler", |
| 3433 | 3451 | "terms.subtitle": "Akademik dönemleri yönetin ve dersleri ilgili dönemlere atayın.", |
| 3434 | 3452 | "terms.create": "Dönem oluştur", |
src/pages/payment-statement-page.tsxmodified+1 -1
| @@ -125,7 +125,7 @@ function StatementContent() { | ||
| 125 | 125 | <div class="detail-metric-card"><p class="text-xs uppercase text-muted-foreground">{t("payments.balance")}</p><p class="mt-1 text-2xl font-semibold tabular-nums" classList={{ "text-destructive": summary().balance < 0 }}>{formatTry(summary().balance, moneyLocale())}</p></div> |
| 126 | 126 | </div> |
| 127 | 127 | |
| 128 | - <section class="data-shell space-y-4 border-violet-500/15 bg-violet-500/2.5 p-4"> | |
| 128 | + <section class="space-y-4"> | |
| 129 | 129 | <Suspense fallback={<DataTableSkeleton columns={6} rows={6} />}> |
| 130 | 130 | <Show when={statement.error}> |
| 131 | 131 | <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetch()} /> |
src/pages/payments-page.tsxmodified+129 -11
| @@ -2,6 +2,7 @@ import { For, Index, Show, Suspense, createMemo, createResource, createSignal } | ||
| 2 | 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | 3 | import { |
| 4 | 4 | deletePaymentPlanById, |
| 5 | + getPaymentBalanceByUserId, | |
| 5 | 6 | getPaymentLedgerByUserId, |
| 6 | 7 | getPaymentPlans, |
| 7 | 8 | getPaymentStatementByUserId, |
| @@ -17,7 +18,8 @@ import { | ||
| 17 | 18 | type PaymentLine, |
| 18 | 19 | type StatementEntry, |
| 19 | 20 | } from "@/api/payments"; |
| 20 | -import { formatApiError } from "@/api/client"; | |
| 21 | +import { getUsers } from "@/api/users"; | |
| 22 | +import { formatApiError, type User } from "@/api/client"; | |
| 21 | 23 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 22 | 24 | import { PageHeader } from "@/components/layout/page-header"; |
| 23 | 25 | import { Alert } from "@/components/ui/alert"; |
| @@ -28,7 +30,7 @@ import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; | ||
| 28 | 30 | import { DatePicker } from "@/components/ui/date-picker"; |
| 29 | 31 | import { EmptyState } from "@/components/ui/empty-state"; |
| 30 | 32 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 31 | -import { IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; | |
| 33 | +import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; | |
| 32 | 34 | import { Input } from "@/components/ui/input"; |
| 33 | 35 | import { Label } from "@/components/ui/label"; |
| 34 | 36 | import { Select } from "@/components/ui/select"; |
| @@ -85,7 +87,85 @@ function PaymentsContent() { | ||
| 85 | 87 | const [pending, setPending] = createSignal(false); |
| 86 | 88 | |
| 87 | 89 | // ============================ Collection ============================ |
| 88 | - const [student, setStudent] = createSignal(""); | |
| 90 | + const [selectedStudent, setSelectedStudent] = createSignal<User | null>(null); | |
| 91 | + const student = () => selectedStudent()?.id ?? ""; | |
| 92 | + const studentLabel = (user: User) => `${user.name ?? ""} ${user.surname ?? ""}`.trim() || user.username; | |
| 93 | + | |
| 94 | + const [students, { refetch: refetchStudents }] = createResource(async () => | |
| 95 | + (await getUsers({ limit: 500 })).items.filter((user) => user.role === "student"), | |
| 96 | + ); | |
| 97 | + const studentList = () => students() ?? []; | |
| 98 | + | |
| 99 | + // Filter the student list by fee plan (via that plan's assignment roster). | |
| 100 | + const [planFilter, setPlanFilter] = createSignal(""); | |
| 101 | + const [filterAssignments] = createResource( | |
| 102 | + () => planFilter() || null, | |
| 103 | + (planId) => getPlanAssignments(planId, { limit: 500 }), | |
| 104 | + ); | |
| 105 | + const filteredStudents = createMemo(() => { | |
| 106 | + const all = studentList(); | |
| 107 | + if (!planFilter()) return all; | |
| 108 | + const ids = new Set((filterAssignments()?.items ?? []).map((row) => row.student.id)); | |
| 109 | + return all.filter((user) => ids.has(user.id)); | |
| 110 | + }); | |
| 111 | + | |
| 112 | + // Per-student balance for the "in debt / settled" column. No bulk endpoint | |
| 113 | + // exists, so this is one call per listed student — bounded to a plan-sized | |
| 114 | + // list. ponytail: N+1 balance fetch, capped at 200; add a bulk | |
| 115 | + // /payments/balances endpoint if whole-school unfiltered debt is needed. | |
| 116 | + const BALANCE_FETCH_CAP = 200; | |
| 117 | + const [balances] = createResource( | |
| 118 | + () => { | |
| 119 | + const ids = filteredStudents().map((user) => user.id); | |
| 120 | + return ids.length > 0 && ids.length <= BALANCE_FETCH_CAP ? ids : null; | |
| 121 | + }, | |
| 122 | + async (ids) => { | |
| 123 | + const pairs = await Promise.all( | |
| 124 | + ids.map(async (id) => [id, (await getPaymentBalanceByUserId(id)).balance_minor] as const), | |
| 125 | + ); | |
| 126 | + return Object.fromEntries(pairs) as Record<string, number>; | |
| 127 | + }, | |
| 128 | + ); | |
| 129 | + const balanceOf = (id: string) => balances()?.[id]; | |
| 130 | + | |
| 131 | + const studentColumns = createMemo<ColumnDef<User>[]>(() => [ | |
| 132 | + { | |
| 133 | + id: "name", | |
| 134 | + header: t("payments.student"), | |
| 135 | + cell: (cell) => <span class="font-medium">{studentLabel(cell.row.original)}</span>, | |
| 136 | + }, | |
| 137 | + { | |
| 138 | + accessorKey: "username", | |
| 139 | + header: t("payments.username"), | |
| 140 | + cell: (cell) => <span class="text-sm text-muted-foreground">@{cell.row.original.username}</span>, | |
| 141 | + }, | |
| 142 | + { | |
| 143 | + id: "debt", | |
| 144 | + header: t("payments.status"), | |
| 145 | + accessorFn: (user) => balanceOf(user.id) ?? 0, | |
| 146 | + cell: (cell) => { | |
| 147 | + const bal = balanceOf(cell.row.original.id); | |
| 148 | + if (bal === undefined) return <span class="text-sm text-muted-foreground">—</span>; | |
| 149 | + const inDebt = bal < 0; | |
| 150 | + return ( | |
| 151 | + <Badge variant="outline" class={inDebt ? "border-red-500/40 bg-red-500/10 text-red-700 dark:text-red-300" : "border-emerald-500/40 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"}> | |
| 152 | + <span class={`mr-1.5 inline-block h-1.5 w-1.5 rounded-full ${inDebt ? "bg-red-500" : "bg-emerald-500"}`} /> | |
| 153 | + {inDebt ? t("payments.inDebt") : t("payments.settled")} | |
| 154 | + </Badge> | |
| 155 | + ); | |
| 156 | + }, | |
| 157 | + }, | |
| 158 | + { | |
| 159 | + id: "balance", | |
| 160 | + header: t("payments.balance"), | |
| 161 | + accessorFn: (user) => balanceOf(user.id) ?? 0, | |
| 162 | + cell: (cell) => { | |
| 163 | + const bal = balanceOf(cell.row.original.id); | |
| 164 | + return <span class="tabular-nums" classList={{ "text-destructive": (bal ?? 0) < 0 }}>{bal === undefined ? "—" : formatTry(bal, moneyLocale())}</span>; | |
| 165 | + }, | |
| 166 | + }, | |
| 167 | + ]); | |
| 168 | + | |
| 89 | 169 | const [statement, { refetch: refetchStatement }] = createResource( |
| 90 | 170 | () => student() || null, |
| 91 | 171 | (userId) => getPaymentStatementByUserId(userId, { limit: 500 }), |
| @@ -377,11 +457,48 @@ function PaymentsContent() { | ||
| 377 | 457 | |
| 378 | 458 | {/* ---------------- Collection ---------------- */} |
| 379 | 459 | <TabsContent value="collect" class="space-y-5"> |
| 380 | - <div class="data-shell max-w-md p-4"> | |
| 381 | - <UserSearchSelect id="collect-student" role="student" value={student()} onChange={setStudent} label={t("payments.selectStudent")} placeholder={t("payments.selectStudent")} /> | |
| 382 | - </div> | |
| 383 | - | |
| 384 | - <Show when={student()}> | |
| 460 | + <Show | |
| 461 | + when={selectedStudent()} | |
| 462 | + fallback={ | |
| 463 | + <section class="data-shell space-y-4 border-sky-500/15 bg-sky-500/2.5 p-4"> | |
| 464 | + <Suspense fallback={<DataTableSkeleton columns={4} rows={8} />}> | |
| 465 | + <Show when={students.error}> | |
| 466 | + <ErrorAlert message={formatApiError(students.error)} onRetry={() => void refetchStudents()} /> | |
| 467 | + </Show> | |
| 468 | + <Show when={studentList().length > 0} fallback={<EmptyState title={t("payments.selectStudent")} />}> | |
| 469 | + <DataTable | |
| 470 | + columns={studentColumns()} | |
| 471 | + data={filteredStudents()} | |
| 472 | + onRowClick={(user) => setSelectedStudent(user)} | |
| 473 | + searchPredicate={(user, q) => `${studentLabel(user)} ${user.username} ${user.email ?? ""}`.toLowerCase().includes(q.toLowerCase())} | |
| 474 | + filterPlaceholder={t("payments.selectStudent")} | |
| 475 | + title={t("payments.selectStudent")} | |
| 476 | + filters={ | |
| 477 | + <Select value={planFilter()} onChange={(e) => setPlanFilter(e.currentTarget.value)} wrapperClass="w-56"> | |
| 478 | + <option value="">{t("payments.allPlans")}</option> | |
| 479 | + <For each={planList()}>{(plan) => <option value={plan.id}>{plan.name}</option>}</For> | |
| 480 | + </Select> | |
| 481 | + } | |
| 482 | + tableClass="min-w-160" | |
| 483 | + enableSorting | |
| 484 | + enablePagination | |
| 485 | + pageSize={STATEMENT_PAGE_SIZE} | |
| 486 | + /> | |
| 487 | + </Show> | |
| 488 | + </Suspense> | |
| 489 | + </section> | |
| 490 | + } | |
| 491 | + > | |
| 492 | + {(current) => (<> | |
| 493 | + <div class="flex items-center justify-between gap-3"> | |
| 494 | + <div> | |
| 495 | + <h2 class="text-lg font-semibold">{studentLabel(current())}</h2> | |
| 496 | + <p class="text-sm text-muted-foreground">@{current().username}</p> | |
| 497 | + </div> | |
| 498 | + <Button variant="outline" size="sm" class="rounded-lg" onClick={() => setSelectedStudent(null)}> | |
| 499 | + <IconChevronLeft class="h-4 w-4" />{t("payments.allStudents")} | |
| 500 | + </Button> | |
| 501 | + </div> | |
| 385 | 502 | <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4"> |
| 386 | 503 | <div class="detail-metric-card"><p class="text-xs uppercase text-muted-foreground">{t("payments.totalDebt")}</p><p class="mt-1 font-semibold tabular-nums">{formatTry(summary().billed, moneyLocale())}</p></div> |
| 387 | 504 | <div class="detail-metric-card"><p class="text-xs uppercase text-muted-foreground">{t("payments.collected")}</p><p class="mt-1 font-semibold tabular-nums">{formatTry(summary().collected, moneyLocale())}</p></div> |
| @@ -389,7 +506,7 @@ function PaymentsContent() { | ||
| 389 | 506 | <div class="detail-metric-card"><p class="text-xs uppercase text-muted-foreground">{t("payments.overdueCount")}</p><p class="mt-1 font-semibold tabular-nums" classList={{ "text-destructive": summary().overdue > 0 }}>{summary().overdue}</p></div> |
| 390 | 507 | </div> |
| 391 | 508 | |
| 392 | - <section class="data-shell space-y-4 border-violet-500/15 bg-violet-500/2.5 p-4"> | |
| 509 | + <section class="data-shell space-y-4 border-sky-500/15 bg-sky-500/2.5 p-4"> | |
| 393 | 510 | <Suspense fallback={<DataTableSkeleton columns={7} rows={5} />}> |
| 394 | 511 | <Show when={statement.error}> |
| 395 | 512 | <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetchStatement()} /> |
| @@ -446,6 +563,7 @@ function PaymentsContent() { | ||
| 446 | 563 | </section> |
| 447 | 564 | </Show> |
| 448 | 565 | </div> |
| 566 | + </>)} | |
| 449 | 567 | </Show> |
| 450 | 568 | </TabsContent> |
| 451 | 569 | |
| @@ -457,13 +575,13 @@ function PaymentsContent() { | ||
| 457 | 575 | {t("payments.createPlan")} |
| 458 | 576 | </Button> |
| 459 | 577 | </div> |
| 460 | - <section class="data-shell space-y-4 border-violet-500/15 bg-violet-500/2.5 p-4"> | |
| 578 | + <section class="data-shell space-y-4 border-sky-500/15 bg-sky-500/2.5 p-4"> | |
| 461 | 579 | <Suspense fallback={<DataTableSkeleton columns={4} rows={6} />}> |
| 462 | 580 | <Show when={plans.error}> |
| 463 | 581 | <ErrorAlert message={formatApiError(plans.error)} onRetry={() => void refetchPlans()} /> |
| 464 | 582 | </Show> |
| 465 | 583 | <Show when={planList().length > 0} fallback={<EmptyState title={t("payments.empty")} />}> |
| 466 | - <DataTable columns={planColumns()} data={planList()} tableClass="min-w-160" filterColumn="name" enablePagination pageSize={PLAN_PAGE_SIZE} /> | |
| 584 | + <DataTable columns={planColumns()} data={planList()} onRowClick={(plan) => { setPlanFilter(plan.id); setTab("collect"); }} tableClass="min-w-160" filterColumn="name" enablePagination pageSize={PLAN_PAGE_SIZE} /> | |
| 467 | 585 | </Show> |
| 468 | 586 | </Suspense> |
| 469 | 587 | </section> |