import { Show, Suspense, createEffect, createMemo, createSignal } from "solid-js"; import { createResource } from "@/lib/create-resource"; import type { ColumnDef } from "@tanstack/solid-table"; import { getMyPaymentBalance, getMyPaymentStatement, getPaymentBalanceByUserId, getPaymentStatementByUserId, type StatementEntry, } from "@/api/payments"; import { getMyStudents } from "@/api/parents"; import { formatApiError } from "@/api/client"; import { RouteGuard } from "@/components/layout/route-guard"; import { Badge } from "@/components/ui/badge"; import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; import { DetailField } from "@/components/ui/detail-field"; import { ErrorAlert } from "@/components/ui/error-alert"; import { IconEye } from "@/components/ui/icons"; import { Label } from "@/components/ui/label"; import { SearchableSelect } from "@/components/ui/searchable-select"; import { SidePanel } from "@/components/ui/side-panel"; import { TableRowActions } from "@/components/ui/table-row-actions"; import { formatDate } from "@/lib/format"; import { formatTry } from "@/lib/meals"; import { sortStatementEntries, statementStatus } from "@/lib/payments"; import { personLabel } from "@/lib/person"; import { useAuth } from "@/stores/auth-context"; import { usePreferences, useT } from "@/stores/preferences-context"; const STATEMENT_PAGE_SIZE = 10; export default function PaymentStatementPage() { return ( ); } function StatementContent() { const auth = useAuth(); const t = useT(); const { locale } = usePreferences(); const moneyLocale = () => (locale() === "tr" ? "tr-TR" : "en-US"); const isParent = () => auth.user()!.role === "parent"; const [children] = createResource(() => (isParent() ? getMyStudents({ limit: 100 }) : null)); const [selectedChild, setSelectedChild] = createSignal(""); createEffect(() => { if (isParent() && !selectedChild() && children()?.items[0]) setSelectedChild(children()!.items[0].id); }); // Parent reads a child by id; everyone else reads their own record. const targetId = () => (isParent() ? selectedChild() : ""); const [statement, { refetch }] = createResource( () => (isParent() ? targetId() || null : "me"), (key) => (key === "me" ? getMyPaymentStatement({ limit: 100 }) : getPaymentStatementByUserId(key, { limit: 100 })), ); const [balance] = createResource( () => (isParent() ? targetId() || null : "me"), (key) => (key === "me" ? getMyPaymentBalance() : getPaymentBalanceByUserId(key)), ); const entries = () => statement()?.entries.items ?? []; const [viewEntry, setViewEntry] = createSignal(null); const summary = createMemo(() => { const rows = entries(); return { billed: rows.filter((row) => !row.reversed).reduce((sum, row) => sum + row.amount_minor, 0), collected: rows.reduce((sum, row) => sum + row.credited_minor, 0), balance: statement()?.balance_minor ?? balance()?.balance_minor ?? 0, }; }); const columns = createMemo[]>(() => [ { id: "plan", header: t("payments.planName"), cell: (cell) => {cell.row.original.plan_name ?? "—"}, }, { accessorKey: "due_at", header: t("payments.due"), cell: (cell) => {cell.row.original.due_at == null ? "—" : formatDate(cell.row.original.due_at, locale())}, }, { accessorKey: "outstanding_minor", header: t("payments.outstanding"), cell: (cell) => {formatTry(cell.row.original.outstanding_minor, moneyLocale())}, }, { id: "status", header: t("payments.status"), cell: (cell) => { const status = statementStatus(cell.row.original); return {t(status.key)}; }, }, { id: "actions", header: t("common.actions"), meta: { headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", cellClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", }, cell: (cell) => ( , onSelect: () => setViewEntry(cell.row.original), }]} /> ), }, ]); return (
({ value: child.id, label: personLabel(child) }))} />

{t("payments.collected")}

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

0 ? Math.max(0, Math.min(100, Math.round((summary().collected / summary().billed) * 100))) : 0}%` }} />

{t("payments.totalDebt")}

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

{t("payments.balance")}

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

}> void refetch()} />
{ if (!open) setViewEntry(null); }} title={viewEntry()?.plan_name ?? t("payments.plan")} description={viewEntry()?.due_at == null ? "—" : formatDate(viewEntry()!.due_at, locale())} > {(entry) => (
)}
); }