Fix reserve fund balance, dynamic project funding, year-end report, and unit form
- Dashboard reserve fund KPI now uses reserve equity accounts (fund balance position) instead of asset accounts, correctly showing the total reserve fund balance regardless of how users categorize their reserve accounts - Projects findAll() and findForPlanning() dynamically compute funded_percentage and current_fund_balance from reserve equity account balances via CTE, distributing the total reserve balance proportionally across projects - Year-end summary reserve status now queries unified projects table instead of deprecated reserve_components table - Remove standalone Monthly Assessment field from Units form — assessment amount is now inherited from the selected assessment group Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -373,14 +373,40 @@ export class ReportsService {
|
||||
WHERE EXTRACT(YEAR FROM invoice_date) = $1
|
||||
`, [year]);
|
||||
|
||||
// Reserve fund status
|
||||
// Reserve fund status from unified projects table
|
||||
// Uses dynamic reserve balance computation (reserve equity accounts = fund balance)
|
||||
const reserveStatus = await this.tenant.query(`
|
||||
SELECT name, current_fund_balance, replacement_cost,
|
||||
CASE WHEN replacement_cost > 0
|
||||
THEN ROUND((current_fund_balance / replacement_cost * 100)::numeric, 1)
|
||||
ELSE 0 END as percent_funded
|
||||
FROM reserve_components
|
||||
ORDER BY name
|
||||
WITH reserve_balance AS (
|
||||
SELECT COALESCE(SUM(sub.balance), 0) as total FROM (
|
||||
SELECT COALESCE(SUM(jel.credit), 0) - COALESCE(SUM(jel.debit), 0) as balance
|
||||
FROM accounts a
|
||||
LEFT JOIN journal_entry_lines jel ON jel.account_id = a.id
|
||||
LEFT JOIN journal_entries je ON je.id = jel.journal_entry_id AND je.is_posted = true AND je.is_void = false
|
||||
WHERE a.fund_type = 'reserve' AND a.account_type = 'equity' AND a.is_active = true
|
||||
GROUP BY a.id
|
||||
) sub
|
||||
),
|
||||
reserve_total_cost AS (
|
||||
SELECT COALESCE(SUM(estimated_cost), 0) as total
|
||||
FROM projects
|
||||
WHERE is_active = true AND fund_source = 'reserve' AND estimated_cost > 0
|
||||
)
|
||||
SELECT p.name,
|
||||
CASE
|
||||
WHEN rtc.total > 0 THEN ROUND(rb.total * (p.estimated_cost / rtc.total), 2)
|
||||
ELSE 0
|
||||
END as current_fund_balance,
|
||||
p.estimated_cost as replacement_cost,
|
||||
CASE
|
||||
WHEN p.estimated_cost > 0 AND rtc.total > 0 THEN
|
||||
LEAST(ROUND((rb.total * (p.estimated_cost / rtc.total)) / p.estimated_cost * 100, 1), 100)
|
||||
ELSE 0
|
||||
END as percent_funded
|
||||
FROM projects p
|
||||
CROSS JOIN reserve_balance rb
|
||||
CROSS JOIN reserve_total_cost rtc
|
||||
WHERE p.is_active = true AND p.fund_source = 'reserve'
|
||||
ORDER BY p.name
|
||||
`);
|
||||
|
||||
return {
|
||||
@@ -414,23 +440,17 @@ export class ReportsService {
|
||||
FROM invoices WHERE status NOT IN ('paid', 'void', 'written_off')
|
||||
`);
|
||||
|
||||
// Reserve fund balance: computed from journal entries on reserve fund_type accounts
|
||||
// credit - debit for equity/liability/income accounts (reserve equity + reserve income - reserve expenses)
|
||||
// Reserve fund balance: use the reserve equity accounts (fund balance accounts like 3100)
|
||||
// The equity accounts track the total reserve fund position via double-entry bookkeeping
|
||||
// This is the standard HOA approach — every reserve contribution/expenditure flows through equity
|
||||
const reserves = await this.tenant.query(`
|
||||
SELECT COALESCE(SUM(sub.balance), 0) as total FROM (
|
||||
SELECT
|
||||
CASE
|
||||
WHEN a.account_type IN ('asset')
|
||||
THEN COALESCE(SUM(jel.debit), 0) - COALESCE(SUM(jel.credit), 0)
|
||||
WHEN a.account_type IN ('expense')
|
||||
THEN -(COALESCE(SUM(jel.debit), 0) - COALESCE(SUM(jel.credit), 0))
|
||||
ELSE COALESCE(SUM(jel.credit), 0) - COALESCE(SUM(jel.debit), 0)
|
||||
END as balance
|
||||
SELECT COALESCE(SUM(jel.credit), 0) - COALESCE(SUM(jel.debit), 0) as balance
|
||||
FROM accounts a
|
||||
LEFT JOIN journal_entry_lines jel ON jel.account_id = a.id
|
||||
LEFT JOIN journal_entries je ON je.id = jel.journal_entry_id AND je.is_posted = true AND je.is_void = false
|
||||
WHERE a.fund_type = 'reserve' AND a.account_type IN ('asset') AND a.is_active = true
|
||||
GROUP BY a.id, a.account_type
|
||||
WHERE a.fund_type = 'reserve' AND a.account_type = 'equity' AND a.is_active = true
|
||||
GROUP BY a.id
|
||||
) sub
|
||||
`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user