💳 custom payment method and multi-student plan assignment
Browse files at this commitd95087e2cb2e2423cd2cb81748e102a6441a56b4Parent: 5574c54- feat
- 💳 custom payment method and multi-student plan assignment
2 changed files +53 -9
src/components/users/user-search-select.tsxmodified+2 -0
| @@ -27,6 +27,7 @@ export function UserSearchSelect(props: { | |||
| 27 | id: string; | 27 | id: string; |
| 28 | value: string; | 28 | value: string; |
| 29 | onChange: (value: string) => void; | 29 | onChange: (value: string) => void; |
| 30 | + onSelect?: (user: PersonRef | null) => void; | ||
| 30 | label?: string; | 31 | label?: string; |
| 31 | excludeIds?: string[]; | 32 | excludeIds?: string[]; |
| 32 | placeholder?: string; | 33 | placeholder?: string; |
| @@ -121,6 +122,7 @@ export function UserSearchSelect(props: { | |||
| 121 | onChange={(user) => { | 122 | onChange={(user) => { |
| 122 | setSelected(user); | 123 | setSelected(user); |
| 123 | props.onChange(user?.id ?? ""); | 124 | props.onChange(user?.id ?? ""); |
| 125 | + props.onSelect?.(user ?? null); | ||
| 124 | }} | 126 | }} |
| 125 | onInputChange={(value) => { | 127 | onInputChange={(value) => { |
| 126 | setQuery(value); | 128 | setQuery(value); |
src/pages/payments-page.tsxmodified+51 -9
| @@ -51,6 +51,8 @@ import { usePreferences, useT } from "@/stores/preferences-context"; | |||
| 51 | const PLAN_PAGE_SIZE = 10; | 51 | const PLAN_PAGE_SIZE = 10; |
| 52 | const STATEMENT_PAGE_SIZE = 15; | 52 | const STATEMENT_PAGE_SIZE = 15; |
| 53 | const STUDENT_PAGE_SIZE = 10; | 53 | const STUDENT_PAGE_SIZE = 10; |
| 54 | +const MAX_PLAN_ASSIGNMENT_STUDENTS = 200; | ||
| 55 | +const CUSTOM_PAYMENT_METHOD = "__custom__"; | ||
| 54 | 56 | ||
| 55 | function dateInputFromMs(ms: number): string { | 57 | function dateInputFromMs(ms: number): string { |
| 56 | const date = new Date(ms); | 58 | const date = new Date(ms); |
| @@ -261,12 +263,14 @@ function PaymentsContent() { | |||
| 261 | const [viewEntry, setViewEntry] = createSignal<StatementEntry | null>(null); | 263 | const [viewEntry, setViewEntry] = createSignal<StatementEntry | null>(null); |
| 262 | const [collectAmount, setCollectAmount] = createSignal(""); | 264 | const [collectAmount, setCollectAmount] = createSignal(""); |
| 263 | const [collectMethod, setCollectMethod] = createSignal(""); | 265 | const [collectMethod, setCollectMethod] = createSignal(""); |
| 266 | + const [collectCustomMethod, setCollectCustomMethod] = createSignal(""); | ||
| 264 | const [collectNote, setCollectNote] = createSignal(""); | 267 | const [collectNote, setCollectNote] = createSignal(""); |
| 265 | 268 | ||
| 266 | const openCollect = (entry: StatementEntry) => { | 269 | const openCollect = (entry: StatementEntry) => { |
| 267 | setCollectEntry(entry); | 270 | setCollectEntry(entry); |
| 268 | setCollectAmount(entry.outstanding_minor > 0 ? String(entry.outstanding_minor / 100) : ""); | 271 | setCollectAmount(entry.outstanding_minor > 0 ? String(entry.outstanding_minor / 100) : ""); |
| 269 | setCollectMethod(t("payments.methodCash")); | 272 | setCollectMethod(t("payments.methodCash")); |
| 273 | + setCollectCustomMethod(""); | ||
| 270 | setCollectNote(""); | 274 | setCollectNote(""); |
| 271 | setError(""); | 275 | setError(""); |
| 272 | }; | 276 | }; |
| @@ -279,13 +283,18 @@ function PaymentsContent() { | |||
| 279 | setError(t("payments.amountTry")); | 283 | setError(t("payments.amountTry")); |
| 280 | return; | 284 | return; |
| 281 | } | 285 | } |
| 286 | + const method = collectMethod() === CUSTOM_PAYMENT_METHOD ? collectCustomMethod().trim() : collectMethod().trim(); | ||
| 287 | + if (collectMethod() === CUSTOM_PAYMENT_METHOD && !method) { | ||
| 288 | + setError(t("payments.methodRequired")); | ||
| 289 | + return; | ||
| 290 | + } | ||
| 282 | setPending(true); | 291 | setPending(true); |
| 283 | setError(""); | 292 | setError(""); |
| 284 | try { | 293 | try { |
| 285 | await postPaymentCredit({ | 294 | await postPaymentCredit({ |
| 286 | charge_id: entry.charge_id, | 295 | charge_id: entry.charge_id, |
| 287 | amount_minor, | 296 | amount_minor, |
| 288 | - method: collectMethod().trim() || undefined, | 297 | + method: method || undefined, |
| 289 | note: collectNote().trim() || undefined, | 298 | note: collectNote().trim() || undefined, |
| 290 | request_key: crypto.randomUUID(), | 299 | request_key: crypto.randomUUID(), |
| 291 | }); | 300 | }); |
| @@ -393,6 +402,8 @@ function PaymentsContent() { | |||
| 393 | const [assignPlan, setAssignPlan] = createSignal<FeePlan | null>(null); | 402 | const [assignPlan, setAssignPlan] = createSignal<FeePlan | null>(null); |
| 394 | const [viewPlan, setViewPlan] = createSignal<FeePlan | null>(null); | 403 | const [viewPlan, setViewPlan] = createSignal<FeePlan | null>(null); |
| 395 | const [assignStudent, setAssignStudent] = createSignal(""); | 404 | const [assignStudent, setAssignStudent] = createSignal(""); |
| 405 | + const [assignCandidate, setAssignCandidate] = createSignal<PersonRef | null>(null); | ||
| 406 | + const [assignStudents, setAssignStudents] = createSignal<PersonRef[]>([]); | ||
| 396 | const [assignments, { refetch: refetchAssignments }] = createResource( | 407 | const [assignments, { refetch: refetchAssignments }] = createResource( |
| 397 | () => assignPlan()?.id ?? viewPlan()?.id ?? null, | 408 | () => assignPlan()?.id ?? viewPlan()?.id ?? null, |
| 398 | (planId) => getPlanAssignments(planId, { limit: 200 }), | 409 | (planId) => getPlanAssignments(planId, { limit: 200 }), |
| @@ -485,20 +496,33 @@ function PaymentsContent() { | |||
| 485 | const openAssign = (plan: FeePlan) => { | 496 | const openAssign = (plan: FeePlan) => { |
| 486 | setAssignPlan(plan); | 497 | setAssignPlan(plan); |
| 487 | setAssignStudent(""); | 498 | setAssignStudent(""); |
| 499 | + setAssignCandidate(null); | ||
| 500 | + setAssignStudents([]); | ||
| 488 | setError(""); | 501 | setError(""); |
| 489 | }; | 502 | }; |
| 503 | + const addAssignStudent = () => { | ||
| 504 | + const candidate = assignCandidate(); | ||
| 505 | + if (!candidate || assignStudents().length >= MAX_PLAN_ASSIGNMENT_STUDENTS || assignStudents().some((student) => student.id === candidate.id)) return; | ||
| 506 | + setAssignStudents([...assignStudents(), candidate]); | ||
| 507 | + setAssignStudent(""); | ||
| 508 | + setAssignCandidate(null); | ||
| 509 | + }; | ||
| 510 | + const removeAssignStudent = (id: string) => setAssignStudents(assignStudents().filter((student) => student.id !== id)); | ||
| 490 | const submitAssign = async () => { | 511 | const submitAssign = async () => { |
| 491 | const plan = assignPlan(); | 512 | const plan = assignPlan(); |
| 492 | - const target = assignStudent(); | 513 | + const students = assignStudents(); |
| 493 | - if (!plan || !target) return; | 514 | + if (!plan || students.length === 0) return; |
| 494 | setPending(true); | 515 | setPending(true); |
| 495 | setError(""); | 516 | setError(""); |
| 496 | try { | 517 | try { |
| 497 | - const outcomes = await postPlanAssignment(plan.id, { student_ids: [target] }); | 518 | + const outcomes = await postPlanAssignment(plan.id, { student_ids: students.map((student) => student.id) }); |
| 498 | - const outcome = outcomes[0]; | 519 | + const assigned = outcomes.filter((outcome) => outcome.status === "assigned").length; |
| 499 | - const key = outcome?.status === "assigned" ? "payments.outcomeAssigned" : outcome?.status === "already_assigned" ? "payments.outcomeAlready" : "payments.outcomeRejected"; | 520 | + const alreadyAssigned = outcomes.filter((outcome) => outcome.status === "already_assigned").length; |
| 500 | - setFlash(t(key as never)); | 521 | + const rejected = outcomes.length - assigned - alreadyAssigned; |
| 522 | + setFlash(t("payments.assignmentSummary", { assigned, alreadyAssigned, rejected })); | ||
| 523 | + setAssignStudents([]); | ||
| 501 | setAssignStudent(""); | 524 | setAssignStudent(""); |
| 525 | + setAssignCandidate(null); | ||
| 502 | await refetchAssignments(); | 526 | await refetchAssignments(); |
| 503 | } catch (err) { | 527 | } catch (err) { |
| 504 | setError(formatApiError(err)); | 528 | setError(formatApiError(err)); |
| @@ -770,8 +794,15 @@ function PaymentsContent() { | |||
| 770 | <Label for="collect-method">{t("payments.method")}</Label> | 794 | <Label for="collect-method">{t("payments.method")}</Label> |
| 771 | <Select id="collect-method" value={collectMethod()} onChange={(e) => setCollectMethod(e.currentTarget.value)}> | 795 | <Select id="collect-method" value={collectMethod()} onChange={(e) => setCollectMethod(e.currentTarget.value)}> |
| 772 | <For each={PAYMENT_METHOD_KEYS}>{(key) => <option value={t(key)}>{t(key)}</option>}</For> | 796 | <For each={PAYMENT_METHOD_KEYS}>{(key) => <option value={t(key)}>{t(key)}</option>}</For> |
| 797 | + <option value={CUSTOM_PAYMENT_METHOD}>{t("payments.methodOther")}</option> | ||
| 773 | </Select> | 798 | </Select> |
| 774 | </div> | 799 | </div> |
| 800 | + <Show when={collectMethod() === CUSTOM_PAYMENT_METHOD}> | ||
| 801 | + <div class="space-y-1.5"> | ||
| 802 | + <Label for="collect-custom-method">{t("payments.methodOther")}</Label> | ||
| 803 | + <Input id="collect-custom-method" maxlength={50} required value={collectCustomMethod()} onInput={(e) => setCollectCustomMethod(e.currentTarget.value)} /> | ||
| 804 | + </div> | ||
| 805 | + </Show> | ||
| 775 | <div class="space-y-1.5"> | 806 | <div class="space-y-1.5"> |
| 776 | <Label for="collect-note">{t("payments.note")}</Label> | 807 | <Label for="collect-note">{t("payments.note")}</Label> |
| 777 | <Textarea id="collect-note" maxlength={500} value={collectNote()} onInput={(e) => setCollectNote(e.currentTarget.value)} /> | 808 | <Textarea id="collect-note" maxlength={500} value={collectNote()} onInput={(e) => setCollectNote(e.currentTarget.value)} /> |
| @@ -865,10 +896,21 @@ function PaymentsContent() { | |||
| 865 | <p class="text-sm text-muted-foreground">{t("payments.assignHelp")}</p> | 896 | <p class="text-sm text-muted-foreground">{t("payments.assignHelp")}</p> |
| 866 | <div class="flex flex-col gap-2 sm:flex-row sm:items-end"> | 897 | <div class="flex flex-col gap-2 sm:flex-row sm:items-end"> |
| 867 | <div class="min-w-0 flex-1"> | 898 | <div class="min-w-0 flex-1"> |
| 868 | - <UserSearchSelect id="assign-student" role="student" value={assignStudent()} onChange={setAssignStudent} label={t("payments.selectStudent")} placeholder={t("payments.selectStudent")} /> | 899 | + <UserSearchSelect id="assign-student" role="student" value={assignStudent()} onChange={setAssignStudent} onSelect={setAssignCandidate} excludeIds={assignStudents().map((student) => student.id)} label={t("payments.selectStudent")} placeholder={t("payments.selectStudent")} /> |
| 869 | </div> | 900 | </div> |
| 870 | - <Button disabled={!assignStudent() || pending()} onClick={() => void submitAssign()}>{t("payments.assign")}</Button> | 901 | + <Button type="button" variant="outline" disabled={!assignCandidate() || assignStudents().length >= MAX_PLAN_ASSIGNMENT_STUDENTS || pending()} onClick={addAssignStudent}>{t("payments.addStudent")}</Button> |
| 871 | </div> | 902 | </div> |
| 903 | + <Show when={assignStudents().length > 0}> | ||
| 904 | + <div class="space-y-2"> | ||
| 905 | + <Label>{t("payments.selectedStudents", { count: assignStudents().length })}</Label> | ||
| 906 | + <div class="flex flex-wrap gap-2"> | ||
| 907 | + <For each={assignStudents()}> | ||
| 908 | + {(student) => <Badge variant="secondary" class="gap-1.5 py-1 pr-1">{personLabel(student)}<Button type="button" variant="ghost" size="sm" class="h-5 w-5 rounded-md p-0" onClick={() => removeAssignStudent(student.id)} aria-label={t("common.remove")}><IconTrash class="h-3.5 w-3.5" /></Button></Badge>} | ||
| 909 | + </For> | ||
| 910 | + </div> | ||
| 911 | + <Button disabled={pending()} onClick={() => void submitAssign()}>{t("payments.assignSelected")}</Button> | ||
| 912 | + </div> | ||
| 913 | + </Show> | ||
| 872 | <div class="space-y-2"> | 914 | <div class="space-y-2"> |
| 873 | <Label>{t("payments.assignments")}</Label> | 915 | <Label>{t("payments.assignments")}</Label> |
| 874 | <div class="divide-y divide-border/60"> | 916 | <div class="divide-y divide-border/60"> |