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));