diff --git a/src/pages/accounting/components/ARAgingPropertyReport.tsx b/src/pages/accounting/components/ARAgingPropertyReport.tsx index 99073a2..8c05b2e 100644 --- a/src/pages/accounting/components/ARAgingPropertyReport.tsx +++ b/src/pages/accounting/components/ARAgingPropertyReport.tsx @@ -83,8 +83,13 @@ export function ARAgingPropertyReport({ companyId, companyName, logoUrl, to: pro for (const u of units) unitById.set(u.id, u); const ownerById = new Map(); for (const o of owners) ownerById.set(o.id, o); + // Show the CURRENT owner per unit (active + primary first) so a unit's + // balance is never labeled with a moved-out owner. Funds attach to the unit. const ownerByUnit = new Map(); - for (const o of owners) if (o.unit_id && !ownerByUnit.has(o.unit_id)) ownerByUnit.set(o.unit_id, o); + const ownerRank = (o: OwnerInfo) => + ((o.status == null || o.status === "active") ? 0 : 2) + (o.is_primary ? 0 : 1); + for (const o of [...owners].sort((a, b) => ownerRank(a) - ownerRank(b))) + if (o.unit_id && !ownerByUnit.has(o.unit_id)) ownerByUnit.set(o.unit_id, o); // Latest collection status per unit (rows came back newest-first) const collByUnit = new Map(); diff --git a/src/pages/accounting/lib/ownerLedger.ts b/src/pages/accounting/lib/ownerLedger.ts index 38e1e6b..5590ea4 100644 --- a/src/pages/accounting/lib/ownerLedger.ts +++ b/src/pages/accounting/lib/ownerLedger.ts @@ -29,6 +29,8 @@ export type OwnerInfo = { first_name: string | null; last_name: string | null; unit_id: string | null; + status: string | null; + is_primary: boolean | null; }; export async function fetchAssociationId(companyId: string): Promise { @@ -73,7 +75,7 @@ export async function fetchOwnerLedger(associationId: string, asOf: string): Pro export async function fetchUnitsAndOwners(associationId: string): Promise<{ units: UnitInfo[]; owners: OwnerInfo[] }> { const [unitsRes, ownersRes] = await Promise.all([ supabase.from("units").select("id, unit_number, address, account_number").eq("association_id", associationId), - supabase.from("owners").select("id, first_name, last_name, unit_id").eq("association_id", associationId), + supabase.from("owners").select("id, first_name, last_name, unit_id, status, is_primary").eq("association_id", associationId), ]); return { units: (unitsRes.data ?? []) as UnitInfo[],