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:
@@ -6,8 +6,42 @@ export class ProjectsService {
|
||||
constructor(private tenant: TenantService) {}
|
||||
|
||||
async findAll() {
|
||||
// Return all active projects ordered by name
|
||||
return this.tenant.query('SELECT * FROM projects WHERE is_active = true ORDER BY name');
|
||||
// Return all active projects with dynamically computed reserve fund balance
|
||||
// The total reserve fund balance (from reserve EQUITY accounts = fund balance) is distributed
|
||||
// proportionally across reserve projects based on their estimated_cost
|
||||
return this.tenant.query(`
|
||||
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.*,
|
||||
CASE
|
||||
WHEN p.fund_source = 'reserve' AND p.estimated_cost > 0 AND rtc.total > 0 THEN
|
||||
LEAST(ROUND((rb.total * (p.estimated_cost / rtc.total)) / p.estimated_cost * 100, 2), 100)
|
||||
ELSE p.funded_percentage
|
||||
END as funded_percentage,
|
||||
CASE
|
||||
WHEN p.fund_source = 'reserve' AND rtc.total > 0 THEN
|
||||
ROUND(rb.total * (p.estimated_cost / rtc.total), 2)
|
||||
ELSE p.current_fund_balance
|
||||
END as current_fund_balance
|
||||
FROM projects p
|
||||
CROSS JOIN reserve_balance rb
|
||||
CROSS JOIN reserve_total_cost rtc
|
||||
WHERE p.is_active = true
|
||||
ORDER BY p.name
|
||||
`);
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
@@ -18,10 +52,39 @@ export class ProjectsService {
|
||||
|
||||
async findForPlanning() {
|
||||
// Only return projects that have target_year set (for the Capital Planning kanban)
|
||||
// Uses the same dynamic reserve fund balance computation as findAll()
|
||||
return this.tenant.query(`
|
||||
SELECT * FROM projects
|
||||
WHERE is_active = true AND target_year IS NOT NULL
|
||||
ORDER BY target_year, target_month NULLS LAST, priority
|
||||
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.*,
|
||||
CASE
|
||||
WHEN p.fund_source = 'reserve' AND p.estimated_cost > 0 AND rtc.total > 0 THEN
|
||||
LEAST(ROUND((rb.total * (p.estimated_cost / rtc.total)) / p.estimated_cost * 100, 2), 100)
|
||||
ELSE p.funded_percentage
|
||||
END as funded_percentage,
|
||||
CASE
|
||||
WHEN p.fund_source = 'reserve' AND rtc.total > 0 THEN
|
||||
ROUND(rb.total * (p.estimated_cost / rtc.total), 2)
|
||||
ELSE p.current_fund_balance
|
||||
END as current_fund_balance
|
||||
FROM projects p
|
||||
CROSS JOIN reserve_balance rb
|
||||
CROSS JOIN reserve_total_cost rtc
|
||||
WHERE p.is_active = true AND p.target_year IS NOT NULL
|
||||
ORDER BY p.target_year, p.target_month NULLS LAST, p.priority
|
||||
`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user