forge
Explore
Log in

Don't you dare go Hollow

💳 payments collect tab — student roster with debt status & plan filter

Browse files at this commit

c39779552d38f83572d80cebe1c92e9b62ba5928Burak BoduroğluParent: 531cdb0
  • feat
    • 💳 payments collect tab — student roster with debt status & plan filter
Show full message
feat: 💳 payments collect tab — student roster with debt status & plan filter
- replace the lone student search box with a sortable student DataTable
  (name, username, in-debt/settled badge, balance); row click opens the
  student's collection detail with a back-to-roster action
- fee-plan filter dropdown; clicking a plan row jumps to the collect tab
  pre-filtered to that plan's assignment roster
- per-student balance fetched for the listed set (capped at 200) to show
  and sort by debt status
- drop the tinted card wrappers behind the payments/statement tables
src/i18n/messages.tsmodified+18 -0
@@ -1005,6 +1005,12 @@ export type MessageKey =
10051005 | "payments.showLedger"
10061006 | "payments.hideLedger"
10071007 | "payments.plan"
1008+ | "payments.student"
1009+ | "payments.username"
1010+ | "payments.allStudents"
1011+ | "payments.allPlans"
1012+ | "payments.inDebt"
1013+ | "payments.settled"
10081014 | "terms.title"
10091015 | "terms.subtitle"
10101016 | "terms.create"
@@ -2254,6 +2260,12 @@ const en: Dict = {
22542260 "payments.showLedger": "Show account activity",
22552261 "payments.hideLedger": "Hide account activity",
22562262 "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",
22572269 "terms.title": "Academic terms",
22582270 "terms.subtitle": "Manage academic terms and assign courses to each one.",
22592271 "terms.create": "Create term",
@@ -3429,6 +3441,12 @@ const tr: Dict = {
34293441 "payments.showLedger": "Hesap hareketlerini göster",
34303442 "payments.hideLedger": "Hesap hareketlerini gizle",
34313443 "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",
34323450 "terms.title": "Akademik dönemler",
34333451 "terms.subtitle": "Akademik dönemleri yönetin ve dersleri ilgili dönemlere atayın.",
34343452 "terms.create": "Dönem oluştur",
src/pages/payment-statement-page.tsxmodified+1 -1
@@ -125,7 +125,7 @@ function StatementContent() {
125125 <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>
126126 </div>
127127
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">
129129 <Suspense fallback={<DataTableSkeleton columns={6} rows={6} />}>
130130 <Show when={statement.error}>
131131 <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 }
22 import type { ColumnDef } from "@tanstack/solid-table";
33 import {
44 deletePaymentPlanById,
5+ getPaymentBalanceByUserId,
56 getPaymentLedgerByUserId,
67 getPaymentPlans,
78 getPaymentStatementByUserId,
@@ -17,7 +18,8 @@ import {
1718 type PaymentLine,
1819 type StatementEntry,
1920 } from "@/api/payments";
20-import { formatApiError } from "@/api/client";
21+import { getUsers } from "@/api/users";
22+import { formatApiError, type User } from "@/api/client";
2123 import { RouteGuard } from "@/components/layout/route-guard";
2224 import { PageHeader } from "@/components/layout/page-header";
2325 import { Alert } from "@/components/ui/alert";
@@ -28,7 +30,7 @@ import { DataTable, DataTableSkeleton } from "@/components/ui/data-table";
2830 import { DatePicker } from "@/components/ui/date-picker";
2931 import { EmptyState } from "@/components/ui/empty-state";
3032 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";
3234 import { Input } from "@/components/ui/input";
3335 import { Label } from "@/components/ui/label";
3436 import { Select } from "@/components/ui/select";
@@ -85,7 +87,85 @@ function PaymentsContent() {
8587 const [pending, setPending] = createSignal(false);
8688
8789 // ============================ 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+
89169 const [statement, { refetch: refetchStatement }] = createResource(
90170 () => student() || null,
91171 (userId) => getPaymentStatementByUserId(userId, { limit: 500 }),
@@ -377,11 +457,48 @@ function PaymentsContent() {
377457
378458 {/* ---------------- Collection ---------------- */}
379459 <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>
385502 <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
386503 <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>
387504 <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() {
389506 <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>
390507 </div>
391508
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">
393510 <Suspense fallback={<DataTableSkeleton columns={7} rows={5} />}>
394511 <Show when={statement.error}>
395512 <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetchStatement()} />
@@ -446,6 +563,7 @@ function PaymentsContent() {
446563 </section>
447564 </Show>
448565 </div>
566+ </>)}
449567 </Show>
450568 </TabsContent>
451569
@@ -457,13 +575,13 @@ function PaymentsContent() {
457575 {t("payments.createPlan")}
458576 </Button>
459577 </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">
461579 <Suspense fallback={<DataTableSkeleton columns={4} rows={6} />}>
462580 <Show when={plans.error}>
463581 <ErrorAlert message={formatApiError(plans.error)} onRetry={() => void refetchPlans()} />
464582 </Show>
465583 <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} />
467585 </Show>
468586 </Suspense>
469587 </section>