diff --git a/src/components/collections/payment-plans-panel.tsx b/src/components/collections/payment-plans-panel.tsx index cef9093..33bff81 100644 --- a/src/components/collections/payment-plans-panel.tsx +++ b/src/components/collections/payment-plans-panel.tsx @@ -65,7 +65,13 @@ function addInterval(dateStr: string, frequency: Frequency, n: number) { return d.toISOString().slice(0, 10); } -export function PaymentPlansPanel({ collectionId }: { collectionId: string }) { +export function PaymentPlansPanel({ + collectionId, + caseId, +}: { + collectionId: string; + caseId?: string | null; +}) { const { user } = useAuth(); const [plans, setPlans] = useState([]); const [installments, setInstallments] = useState>({}); @@ -119,6 +125,25 @@ export function PaymentPlansPanel({ collectionId }: { collectionId: string }) { else load(); }; + const updatePaidAmount = async (id: string, value: string) => { + const num = value === "" ? null : parseFloat(value) || 0; + setInstallments((prev) => { + const next: Record = {}; + Object.entries(prev).forEach(([k, list]) => { + next[k] = list.map((x) => (x.id === id ? { ...x, paid_amount: num } : x)); + }); + return next; + }); + const { error } = await supabase + .from("payment_plan_installments") + .update({ paid_amount: num }) + .eq("id", id); + if (error) { + toast.error("Could not save", { description: error.message }); + load(); + } + }; + const updatePlanStatus = async (planId: string, status: PlanStatus) => { const { error } = await supabase .from("payment_plans") @@ -220,8 +245,21 @@ export function PaymentPlansPanel({ collectionId }: { collectionId: string }) { {overdue && " ยท overdue"} -
- {formatCurrency(i.amount)} +
+ {i.paid ? ( + updatePaidAmount(i.id, e.target.value)} + className="h-7 w-[100px] text-sm text-right tabular-nums" + title="Amount paid" + /> + ) : ( +
+ {formatCurrency(i.amount)} +
+ )}
); @@ -243,6 +281,7 @@ export function PaymentPlansPanel({ collectionId }: { collectionId: string }) { open={createOpen} onOpenChange={setCreateOpen} collectionId={collectionId} + caseId={caseId ?? null} userId={user?.id} onSaved={load} /> @@ -264,12 +303,14 @@ function CreatePlanDialog({ open, onOpenChange, collectionId, + caseId, userId, onSaved, }: { open: boolean; onOpenChange: (v: boolean) => void; collectionId: string; + caseId?: string | null; userId?: string; onSaved: () => void; }) { @@ -307,6 +348,7 @@ function CreatePlanDialog({ .from("payment_plans") .insert({ collection_id: collectionId, + case_id: caseId ?? null, name: name || null, total_amount: t, down_payment: parseFloat(down) || 0, diff --git a/src/routes/collections.$collectionId.tsx b/src/routes/collections.$collectionId.tsx index 5d0fcc4..351d5cb 100644 --- a/src/routes/collections.$collectionId.tsx +++ b/src/routes/collections.$collectionId.tsx @@ -598,7 +598,7 @@ function CollectionDetailRoute() { {/* Payment plans */} - + {/* Ledger */}