diff --git a/src/routes/clients.$clientId.tsx b/src/routes/clients.$clientId.tsx index 5ae32ad..387b579 100644 --- a/src/routes/clients.$clientId.tsx +++ b/src/routes/clients.$clientId.tsx @@ -36,8 +36,24 @@ function ClientDetail() { supabase.from("cases").select("id, case_number, title, status, opened_at").eq("client_id", clientId).order("opened_at", { ascending: false }), ]); if (ce) toast.error("Failed to load client", { description: ce.message }); + const caseIds = (cs ?? []).map((x: any) => x.id); + let totals: Record = {}; + if (caseIds.length) { + const { data: te } = await supabase + .from("time_entries") + .select("case_id, hours, hourly_rate, billable") + .in("case_id", caseIds) + .eq("billable", true); + (te ?? []).forEach((t: any) => { + const h = Number(t.hours) || 0; + const r = Number(t.hourly_rate) || 0; + if (!totals[t.case_id]) totals[t.case_id] = { hours: 0, amount: 0 }; + totals[t.case_id].hours += h; + totals[t.case_id].amount += h * r; + }); + } setClient(c); - setCases(cs ?? []); + setCases((cs ?? []).map((x: any) => ({ ...x, billed: totals[x.id] ?? { hours: 0, amount: 0 } }))); setLoading(false); };