diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 8b37f5f..c241532 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -1005,6 +1005,12 @@ export type MessageKey = | "payments.showLedger" | "payments.hideLedger" | "payments.plan" + | "payments.student" + | "payments.username" + | "payments.allStudents" + | "payments.allPlans" + | "payments.inDebt" + | "payments.settled" | "terms.title" | "terms.subtitle" | "terms.create" @@ -2254,6 +2260,12 @@ const en: Dict = { "payments.showLedger": "Show account activity", "payments.hideLedger": "Hide account activity", "payments.plan": "Plan", + "payments.student": "Student", + "payments.username": "Username", + "payments.allStudents": "All students", + "payments.allPlans": "All plans", + "payments.inDebt": "In debt", + "payments.settled": "Settled", "terms.title": "Academic terms", "terms.subtitle": "Manage academic terms and assign courses to each one.", "terms.create": "Create term", @@ -3429,6 +3441,12 @@ const tr: Dict = { "payments.showLedger": "Hesap hareketlerini göster", "payments.hideLedger": "Hesap hareketlerini gizle", "payments.plan": "Plan", + "payments.student": "Öğrenci", + "payments.username": "Kullanıcı adı", + "payments.allStudents": "Tüm öğrenciler", + "payments.allPlans": "Tüm planlar", + "payments.inDebt": "Borçlu", + "payments.settled": "Borçsuz", "terms.title": "Akademik dönemler", "terms.subtitle": "Akademik dönemleri yönetin ve dersleri ilgili dönemlere atayın.", "terms.create": "Dönem oluştur", diff --git a/src/pages/payment-statement-page.tsx b/src/pages/payment-statement-page.tsx index 1601c47..a8d7366 100644 --- a/src/pages/payment-statement-page.tsx +++ b/src/pages/payment-statement-page.tsx @@ -125,7 +125,7 @@ function StatementContent() {

{t("payments.balance")}

{formatTry(summary().balance, moneyLocale())}

-
+
}> void refetch()} /> diff --git a/src/pages/payments-page.tsx b/src/pages/payments-page.tsx index e3f94a9..5fc424c 100644 --- a/src/pages/payments-page.tsx +++ b/src/pages/payments-page.tsx @@ -2,6 +2,7 @@ import { For, Index, Show, Suspense, createMemo, createResource, createSignal } import type { ColumnDef } from "@tanstack/solid-table"; import { deletePaymentPlanById, + getPaymentBalanceByUserId, getPaymentLedgerByUserId, getPaymentPlans, getPaymentStatementByUserId, @@ -17,7 +18,8 @@ import { type PaymentLine, type StatementEntry, } from "@/api/payments"; -import { formatApiError } from "@/api/client"; +import { getUsers } from "@/api/users"; +import { formatApiError, type User } from "@/api/client"; import { RouteGuard } from "@/components/layout/route-guard"; import { PageHeader } from "@/components/layout/page-header"; import { Alert } from "@/components/ui/alert"; @@ -28,7 +30,7 @@ import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; import { DatePicker } from "@/components/ui/date-picker"; import { EmptyState } from "@/components/ui/empty-state"; import { ErrorAlert } from "@/components/ui/error-alert"; -import { IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; +import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; @@ -85,7 +87,85 @@ function PaymentsContent() { const [pending, setPending] = createSignal(false); // ============================ Collection ============================ - const [student, setStudent] = createSignal(""); + const [selectedStudent, setSelectedStudent] = createSignal(null); + const student = () => selectedStudent()?.id ?? ""; + const studentLabel = (user: User) => `${user.name ?? ""} ${user.surname ?? ""}`.trim() || user.username; + + const [students, { refetch: refetchStudents }] = createResource(async () => + (await getUsers({ limit: 500 })).items.filter((user) => user.role === "student"), + ); + const studentList = () => students() ?? []; + + // Filter the student list by fee plan (via that plan's assignment roster). + const [planFilter, setPlanFilter] = createSignal(""); + const [filterAssignments] = createResource( + () => planFilter() || null, + (planId) => getPlanAssignments(planId, { limit: 500 }), + ); + const filteredStudents = createMemo(() => { + const all = studentList(); + if (!planFilter()) return all; + const ids = new Set((filterAssignments()?.items ?? []).map((row) => row.student.id)); + return all.filter((user) => ids.has(user.id)); + }); + + // Per-student balance for the "in debt / settled" column. No bulk endpoint + // exists, so this is one call per listed student — bounded to a plan-sized + // list. ponytail: N+1 balance fetch, capped at 200; add a bulk + // /payments/balances endpoint if whole-school unfiltered debt is needed. + const BALANCE_FETCH_CAP = 200; + const [balances] = createResource( + () => { + const ids = filteredStudents().map((user) => user.id); + return ids.length > 0 && ids.length <= BALANCE_FETCH_CAP ? ids : null; + }, + async (ids) => { + const pairs = await Promise.all( + ids.map(async (id) => [id, (await getPaymentBalanceByUserId(id)).balance_minor] as const), + ); + return Object.fromEntries(pairs) as Record; + }, + ); + const balanceOf = (id: string) => balances()?.[id]; + + const studentColumns = createMemo[]>(() => [ + { + id: "name", + header: t("payments.student"), + cell: (cell) => {studentLabel(cell.row.original)}, + }, + { + accessorKey: "username", + header: t("payments.username"), + cell: (cell) => @{cell.row.original.username}, + }, + { + id: "debt", + header: t("payments.status"), + accessorFn: (user) => balanceOf(user.id) ?? 0, + cell: (cell) => { + const bal = balanceOf(cell.row.original.id); + if (bal === undefined) return —; + const inDebt = bal < 0; + return ( + + + {inDebt ? t("payments.inDebt") : t("payments.settled")} + + ); + }, + }, + { + id: "balance", + header: t("payments.balance"), + accessorFn: (user) => balanceOf(user.id) ?? 0, + cell: (cell) => { + const bal = balanceOf(cell.row.original.id); + return {bal === undefined ? "—" : formatTry(bal, moneyLocale())}; + }, + }, + ]); + const [statement, { refetch: refetchStatement }] = createResource( () => student() || null, (userId) => getPaymentStatementByUserId(userId, { limit: 500 }), @@ -377,11 +457,48 @@ function PaymentsContent() { {/* ---------------- Collection ---------------- */} -
- -
- - + + }> + + void refetchStudents()} /> + + 0} fallback={}> + setSelectedStudent(user)} + searchPredicate={(user, q) => `${studentLabel(user)} ${user.username} ${user.email ?? ""}`.toLowerCase().includes(q.toLowerCase())} + filterPlaceholder={t("payments.selectStudent")} + title={t("payments.selectStudent")} + filters={ + + } + tableClass="min-w-160" + enableSorting + enablePagination + pageSize={STATEMENT_PAGE_SIZE} + /> + + +
+ } + > + {(current) => (<> +
+
+

{studentLabel(current())}

+

@{current().username}

+
+ +

{t("payments.totalDebt")}

{formatTry(summary().billed, moneyLocale())}

{t("payments.collected")}

{formatTry(summary().collected, moneyLocale())}

@@ -389,7 +506,7 @@ function PaymentsContent() {

{t("payments.overdueCount")}

0 }}>{summary().overdue}

-
+
}> void refetchStatement()} /> @@ -446,6 +563,7 @@ function PaymentsContent() {
+ )} @@ -457,13 +575,13 @@ function PaymentsContent() { {t("payments.createPlan")} -
+
}> void refetchPlans()} /> 0} fallback={}> - + { setPlanFilter(plan.id); setTab("collect"); }} tableClass="min-w-160" filterColumn="name" enablePagination pageSize={PLAN_PAGE_SIZE} />