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 =
1005 | "payments.showLedger"1005 | "payments.showLedger"
1006 | "payments.hideLedger"1006 | "payments.hideLedger"
1007 | "payments.plan"1007 | "payments.plan"
1008+ | "payments.student"
1009+ | "payments.username"
1010+ | "payments.allStudents"
1011+ | "payments.allPlans"
1012+ | "payments.inDebt"
1013+ | "payments.settled"
1008 | "terms.title"1014 | "terms.title"
1009 | "terms.subtitle"1015 | "terms.subtitle"
1010 | "terms.create"1016 | "terms.create"
@@ -2254,6 +2260,12 @@ const en: Dict = {
2254 "payments.showLedger": "Show account activity",2260 "payments.showLedger": "Show account activity",
2255 "payments.hideLedger": "Hide account activity",2261 "payments.hideLedger": "Hide account activity",
2256 "payments.plan": "Plan",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 "terms.title": "Academic terms",2269 "terms.title": "Academic terms",
2258 "terms.subtitle": "Manage academic terms and assign courses to each one.",2270 "terms.subtitle": "Manage academic terms and assign courses to each one.",
2259 "terms.create": "Create term",2271 "terms.create": "Create term",
@@ -3429,6 +3441,12 @@ const tr: Dict = {
3429 "payments.showLedger": "Hesap hareketlerini göster",3441 "payments.showLedger": "Hesap hareketlerini göster",
3430 "payments.hideLedger": "Hesap hareketlerini gizle",3442 "payments.hideLedger": "Hesap hareketlerini gizle",
3431 "payments.plan": "Plan",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 "terms.title": "Akademik dönemler",3450 "terms.title": "Akademik dönemler",
3433 "terms.subtitle": "Akademik dönemleri yönetin ve dersleri ilgili dönemlere atayın.",3451 "terms.subtitle": "Akademik dönemleri yönetin ve dersleri ilgili dönemlere atayın.",
3434 "terms.create": "Dönem oluştur",3452 "terms.create": "Dönem oluştur",
src/pages/payment-statement-page.tsxmodified+1 -1
@@ -125,7 +125,7 @@ function StatementContent() {
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>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 </div>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 <Suspense fallback={<DataTableSkeleton columns={6} rows={6} />}>129 <Suspense fallback={<DataTableSkeleton columns={6} rows={6} />}>
130 <Show when={statement.error}>130 <Show when={statement.error}>
131 <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetch()} />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 import type { ColumnDef } from "@tanstack/solid-table";2 import type { ColumnDef } from "@tanstack/solid-table";
3 import {3 import {
4 deletePaymentPlanById,4 deletePaymentPlanById,
5+ getPaymentBalanceByUserId,
5 getPaymentLedgerByUserId,6 getPaymentLedgerByUserId,
6 getPaymentPlans,7 getPaymentPlans,
7 getPaymentStatementByUserId,8 getPaymentStatementByUserId,
@@ -17,7 +18,8 @@ import {
17 type PaymentLine,18 type PaymentLine,
18 type StatementEntry,19 type StatementEntry,
19 } from "@/api/payments";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 import { RouteGuard } from "@/components/layout/route-guard";23 import { RouteGuard } from "@/components/layout/route-guard";
22 import { PageHeader } from "@/components/layout/page-header";24 import { PageHeader } from "@/components/layout/page-header";
23 import { Alert } from "@/components/ui/alert";25 import { Alert } from "@/components/ui/alert";
@@ -28,7 +30,7 @@ import { DataTable, DataTableSkeleton } from "@/components/ui/data-table";
28 import { DatePicker } from "@/components/ui/date-picker";30 import { DatePicker } from "@/components/ui/date-picker";
29 import { EmptyState } from "@/components/ui/empty-state";31 import { EmptyState } from "@/components/ui/empty-state";
30 import { ErrorAlert } from "@/components/ui/error-alert";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 import { Input } from "@/components/ui/input";34 import { Input } from "@/components/ui/input";
33 import { Label } from "@/components/ui/label";35 import { Label } from "@/components/ui/label";
34 import { Select } from "@/components/ui/select";36 import { Select } from "@/components/ui/select";
@@ -85,7 +87,85 @@ function PaymentsContent() {
85 const [pending, setPending] = createSignal(false);87 const [pending, setPending] = createSignal(false);
86 88
87 // ============================ Collection ============================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 const [statement, { refetch: refetchStatement }] = createResource(169 const [statement, { refetch: refetchStatement }] = createResource(
90 () => student() || null,170 () => student() || null,
91 (userId) => getPaymentStatementByUserId(userId, { limit: 500 }),171 (userId) => getPaymentStatementByUserId(userId, { limit: 500 }),
@@ -377,11 +457,48 @@ function PaymentsContent() {
377 457
378 {/* ---------------- Collection ---------------- */}458 {/* ---------------- Collection ---------------- */}
379 <TabsContent value="collect" class="space-y-5">459 <TabsContent value="collect" class="space-y-5">
380- <div class="data-shell max-w-md p-4">460+ <Show
381- <UserSearchSelect id="collect-student" role="student" value={student()} onChange={setStudent} label={t("payments.selectStudent")} placeholder={t("payments.selectStudent")} />461+ when={selectedStudent()}
382- </div>462+ fallback={
383-463+ <section class="data-shell space-y-4 border-sky-500/15 bg-sky-500/2.5 p-4">
384- <Show when={student()}>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 <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">502 <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
386 <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>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 <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>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 <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>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 </div>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 <Suspense fallback={<DataTableSkeleton columns={7} rows={5} />}>510 <Suspense fallback={<DataTableSkeleton columns={7} rows={5} />}>
394 <Show when={statement.error}>511 <Show when={statement.error}>
395 <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetchStatement()} />512 <ErrorAlert message={formatApiError(statement.error)} onRetry={() => void refetchStatement()} />
@@ -446,6 +563,7 @@ function PaymentsContent() {
446 </section>563 </section>
447 </Show>564 </Show>
448 </div>565 </div>
566+ </>)}
449 </Show>567 </Show>
450 </TabsContent>568 </TabsContent>
451 569
@@ -457,13 +575,13 @@ function PaymentsContent() {
457 {t("payments.createPlan")}575 {t("payments.createPlan")}
458 </Button>576 </Button>
459 </div>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 <Suspense fallback={<DataTableSkeleton columns={4} rows={6} />}>579 <Suspense fallback={<DataTableSkeleton columns={4} rows={6} />}>
462 <Show when={plans.error}>580 <Show when={plans.error}>
463 <ErrorAlert message={formatApiError(plans.error)} onRetry={() => void refetchPlans()} />581 <ErrorAlert message={formatApiError(plans.error)} onRetry={() => void refetchPlans()} />
464 </Show>582 </Show>
465 <Show when={planList().length > 0} fallback={<EmptyState title={t("payments.empty")} />}>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 </Show>585 </Show>
468 </Suspense>586 </Suspense>
469 </section>587 </section>