diff --git a/src/components/cases/collections-tab.tsx b/src/components/cases/collections-tab.tsx index a874af3..da56bed 100644 --- a/src/components/cases/collections-tab.tsx +++ b/src/components/cases/collections-tab.tsx @@ -645,6 +645,33 @@ export function CollectionDetail({ .maybeSingle(); const firmName = (firm as any)?.company_name || ""; + // Look up a linked Homeowner-type contact on this case to pull its + // property_address (preferred over the homeowner record's address). + let contactPropertyAddress: string | null = null; + try { + const { data: linkedContacts } = await supabase + .from("case_contacts") + .select("contact:contacts(name, email, contact_type, property_address)") + .eq("case_id", collection.case_id); + const homeownerContacts = (linkedContacts || []) + .map((r: any) => r.contact) + .filter((c: any) => c && c.contact_type === "homeowner"); + // Prefer match by email or name to the collection's homeowner + const hoFullName = `${ho?.first_name ?? ""} ${ho?.last_name ?? ""}`.trim().toLowerCase(); + const hoEmail = (ho?.email || "").toLowerCase(); + const matched = + homeownerContacts.find( + (c: any) => + (hoEmail && (c.email || "").toLowerCase() === hoEmail) || + (hoFullName && (c.name || "").toLowerCase() === hoFullName), + ) || homeownerContacts[0]; + if (matched?.property_address) { + contactPropertyAddress = matched.property_address; + } + } catch { + // ignore — fall back to homeowner record address + } + const doc = new jsPDF({ orientation: "landscape", unit: "pt", format: "letter" }); const pageWidth = doc.internal.pageSize.getWidth(); const pageHeight = doc.internal.pageSize.getHeight(); @@ -673,8 +700,16 @@ export function CollectionDetail({ doc.text("PROPERTY ADDRESS:", margin, 88); doc.setFont("helvetica", "normal"); const addrLines: string[] = []; - if (ho?.address) addrLines.push(ho.address); - if (ho?.unit_number) addrLines.push(`Unit ${ho.unit_number}`); + if (contactPropertyAddress) { + // Split multi-line property address into lines + contactPropertyAddress.split(/\r?\n/).forEach((l) => { + const t = l.trim(); + if (t) addrLines.push(t); + }); + } else { + if (ho?.address) addrLines.push(ho.address); + if (ho?.unit_number) addrLines.push(`Unit ${ho.unit_number}`); + } if (addrLines.length === 0) addrLines.push("N/A"); addrLines.forEach((line, i) => doc.text(line, margin, 102 + i * 12)); diff --git a/src/components/contacts/contact-form-dialog.tsx b/src/components/contacts/contact-form-dialog.tsx index 2fb28eb..7d698b9 100644 --- a/src/components/contacts/contact-form-dialog.tsx +++ b/src/components/contacts/contact-form-dialog.tsx @@ -40,6 +40,7 @@ export interface ContactRecord { postal_code?: string | null; notes?: string | null; contact_type?: string | null; + property_address?: string | null; } export function ContactFormDialog({ @@ -92,6 +93,7 @@ export function ContactFormDialog({ postal_code: form.postal_code || null, notes: form.notes || null, contact_type: form.contact_type || "other", + property_address: form.property_address || null, }; let saved: { id: string; name: string } | null = null; if (contact?.id) { @@ -185,6 +187,19 @@ export function ContactFormDialog({ set("postal_code", e.target.value)} /> + {form.contact_type === "homeowner" && ( +
+ Used as the property address on collection ledger statements. +
+