fix: compound inflation, budget planner CSV import, simplify budget manager
- Fix compound inflation: use Math.pow(1 + rate/100, yearsGap) instead of flat rate so multi-year gaps (e.g., 2026→2029) compound annually - Budget Planner: add CSV import flow + Download Template button; show proper empty state when no base budget exists with Create/Import options - Budget Manager: remove CSV import, Download Template, and Save buttons; redirect users to Budget Planner when no budget exists for selected year - Fix getAvailableYears to return null latestBudgetYear when no budgets exist and include current year in year selector for fresh tenants Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Controller, Get, Post, Put, Delete, Body, Param, Query, Req, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Put, Delete, Body, Param, Query, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { AllowViewer } from '../../common/decorators/allow-viewer.decorator';
|
||||
@@ -170,6 +171,28 @@ export class BoardPlanningController {
|
||||
return this.budgetPlanning.advanceStatus(parseInt(year, 10), dto.status, req.user.sub);
|
||||
}
|
||||
|
||||
@Post('budget-plans/:year/import')
|
||||
importBudgetPlanLines(
|
||||
@Param('year') year: string,
|
||||
@Body() lines: any[],
|
||||
@Req() req: any,
|
||||
) {
|
||||
return this.budgetPlanning.importLines(parseInt(year, 10), lines, req.user.sub);
|
||||
}
|
||||
|
||||
@Get('budget-plans/:year/template')
|
||||
async getBudgetPlanTemplate(
|
||||
@Param('year') year: string,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
const csv = await this.budgetPlanning.getTemplate(parseInt(year, 10));
|
||||
res.set({
|
||||
'Content-Type': 'text/csv',
|
||||
'Content-Disposition': `attachment; filename="budget_template_${year}.csv"`,
|
||||
});
|
||||
res.send(csv);
|
||||
}
|
||||
|
||||
@Delete('budget-plans/:year')
|
||||
deleteBudgetPlan(@Param('year') year: string) {
|
||||
return this.budgetPlanning.deletePlan(parseInt(year, 10));
|
||||
|
||||
@@ -40,7 +40,9 @@ export class BudgetPlanningService {
|
||||
const result = await this.tenant.query(
|
||||
'SELECT MAX(fiscal_year) as max_year FROM budgets',
|
||||
);
|
||||
const latestBudgetYear = result[0]?.max_year || new Date().getFullYear();
|
||||
const rawMaxYear = result[0]?.max_year;
|
||||
const latestBudgetYear = rawMaxYear || null; // null means no budgets exist at all
|
||||
const baseYear = rawMaxYear || new Date().getFullYear();
|
||||
|
||||
// Also find years that already have plans
|
||||
const existingPlans = await this.tenant.query(
|
||||
@@ -51,10 +53,11 @@ export class BudgetPlanningService {
|
||||
status: p.status,
|
||||
}));
|
||||
|
||||
// Return next 5 years after latest budget, marking which have plans
|
||||
// Return next 5 years (or current year + 4 if no budgets exist)
|
||||
const years = [];
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
const yr = latestBudgetYear + i;
|
||||
const startOffset = rawMaxYear ? 1 : 0; // include current year if no budgets exist
|
||||
for (let i = startOffset; i <= startOffset + 4; i++) {
|
||||
const yr = baseYear + i;
|
||||
const existing = planYears.find((p: any) => p.year === yr);
|
||||
years.push({
|
||||
year: yr,
|
||||
@@ -83,12 +86,12 @@ export class BudgetPlanningService {
|
||||
const plan = rows[0];
|
||||
|
||||
// Generate inflated lines from base year
|
||||
await this.generateLines(plan.id, baseYear, inflationRate);
|
||||
await this.generateLines(plan.id, baseYear, inflationRate, fiscalYear);
|
||||
|
||||
return this.getPlan(fiscalYear);
|
||||
}
|
||||
|
||||
async generateLines(planId: string, baseYear: number, inflationRate: number) {
|
||||
async generateLines(planId: string, baseYear: number, inflationRate: number, fiscalYear: number) {
|
||||
// Delete existing non-manually-adjusted lines (or all if fresh)
|
||||
await this.tenant.query(
|
||||
'DELETE FROM budget_plan_lines WHERE budget_plan_id = $1 AND is_manually_adjusted = false',
|
||||
@@ -115,7 +118,9 @@ export class BudgetPlanningService {
|
||||
|
||||
if (!baseLines.length) return;
|
||||
|
||||
const multiplier = 1 + inflationRate / 100;
|
||||
// Compound inflation: (1 + rate/100)^yearsGap
|
||||
const yearsGap = Math.max(1, fiscalYear - baseYear);
|
||||
const multiplier = Math.pow(1 + inflationRate / 100, yearsGap);
|
||||
|
||||
// Get existing manually-adjusted lines to avoid duplicates
|
||||
const manualLines = await this.tenant.query(
|
||||
@@ -185,7 +190,7 @@ export class BudgetPlanningService {
|
||||
);
|
||||
|
||||
// Re-generate only non-manually-adjusted lines
|
||||
await this.generateLines(plan.id, plan.base_year, inflationRate);
|
||||
await this.generateLines(plan.id, plan.base_year, inflationRate, fiscalYear);
|
||||
|
||||
return this.getPlan(fiscalYear);
|
||||
}
|
||||
@@ -253,6 +258,139 @@ export class BudgetPlanningService {
|
||||
);
|
||||
}
|
||||
|
||||
async importLines(fiscalYear: number, lines: any[], userId: string) {
|
||||
// Ensure plan exists (create if needed)
|
||||
let plans = await this.tenant.query(
|
||||
'SELECT * FROM budget_plans WHERE fiscal_year = $1', [fiscalYear],
|
||||
);
|
||||
if (!plans.length) {
|
||||
await this.tenant.query(
|
||||
`INSERT INTO budget_plans (fiscal_year, base_year, inflation_rate, created_by)
|
||||
VALUES ($1, $1, 0, $2) RETURNING *`,
|
||||
[fiscalYear, userId],
|
||||
);
|
||||
plans = await this.tenant.query(
|
||||
'SELECT * FROM budget_plans WHERE fiscal_year = $1', [fiscalYear],
|
||||
);
|
||||
}
|
||||
const plan = plans[0];
|
||||
const errors: string[] = [];
|
||||
const created: string[] = [];
|
||||
let imported = 0;
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const accountNumber = String(line.accountNumber || line.account_number || '').trim();
|
||||
const accountName = String(line.accountName || line.account_name || '').trim();
|
||||
if (!accountNumber) {
|
||||
errors.push(`Row ${i + 1}: missing account_number`);
|
||||
continue;
|
||||
}
|
||||
|
||||
let accounts = await this.tenant.query(
|
||||
`SELECT id, fund_type, account_type FROM accounts WHERE account_number = $1 AND is_active = true`,
|
||||
[accountNumber],
|
||||
);
|
||||
|
||||
// Auto-create account if not found
|
||||
if ((!accounts || accounts.length === 0) && accountName) {
|
||||
const accountType = this.inferAccountType(accountNumber, accountName);
|
||||
const fundType = this.inferFundType(accountNumber, accountName);
|
||||
await this.tenant.query(
|
||||
`INSERT INTO accounts (account_number, name, account_type, fund_type, is_system)
|
||||
VALUES ($1, $2, $3, $4, false)`,
|
||||
[accountNumber, accountName, accountType, fundType],
|
||||
);
|
||||
accounts = await this.tenant.query(
|
||||
`SELECT id, fund_type, account_type FROM accounts WHERE account_number = $1 AND is_active = true`,
|
||||
[accountNumber],
|
||||
);
|
||||
created.push(`${accountNumber} - ${accountName} (${accountType}/${fundType})`);
|
||||
}
|
||||
|
||||
if (!accounts || accounts.length === 0) {
|
||||
errors.push(`Row ${i + 1}: account "${accountNumber}" not found`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const account = accounts[0];
|
||||
const fundType = line.fund_type || account.fund_type || 'operating';
|
||||
const monthValues = monthCols.map((m) => {
|
||||
const key = m === 'dec_amt' ? 'dec' : m;
|
||||
return this.parseCurrency(line[key] ?? line[m] ?? 0);
|
||||
});
|
||||
|
||||
await this.tenant.query(
|
||||
`INSERT INTO budget_plan_lines (budget_plan_id, account_id, fund_type,
|
||||
jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec_amt, is_manually_adjusted)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, true)
|
||||
ON CONFLICT (budget_plan_id, account_id, fund_type)
|
||||
DO UPDATE SET jan=$4, feb=$5, mar=$6, apr=$7, may=$8, jun=$9,
|
||||
jul=$10, aug=$11, sep=$12, oct=$13, nov=$14, dec_amt=$15,
|
||||
is_manually_adjusted=true`,
|
||||
[plan.id, account.id, fundType, ...monthValues],
|
||||
);
|
||||
imported++;
|
||||
}
|
||||
|
||||
return { imported, errors, created, plan: await this.getPlan(fiscalYear) };
|
||||
}
|
||||
|
||||
async getTemplate(fiscalYear: number): Promise<string> {
|
||||
const rows = await this.tenant.query(
|
||||
`SELECT a.account_number, a.name as account_name,
|
||||
COALESCE(b.jan, 0) as jan, COALESCE(b.feb, 0) as feb,
|
||||
COALESCE(b.mar, 0) as mar, COALESCE(b.apr, 0) as apr,
|
||||
COALESCE(b.may, 0) as may, COALESCE(b.jun, 0) as jun,
|
||||
COALESCE(b.jul, 0) as jul, COALESCE(b.aug, 0) as aug,
|
||||
COALESCE(b.sep, 0) as sep, COALESCE(b.oct, 0) as oct,
|
||||
COALESCE(b.nov, 0) as nov, COALESCE(b.dec_amt, 0) as dec
|
||||
FROM accounts a
|
||||
LEFT JOIN budgets b ON b.account_id = a.id AND b.fiscal_year = $1
|
||||
WHERE a.is_active = true
|
||||
AND a.account_type IN ('income', 'expense')
|
||||
ORDER BY a.account_number`,
|
||||
[fiscalYear],
|
||||
);
|
||||
|
||||
const header = 'account_number,account_name,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec';
|
||||
const csvLines = rows.map((r: any) => {
|
||||
const name = String(r.account_name).includes(',') ? `"${r.account_name}"` : r.account_name;
|
||||
return [r.account_number, name, r.jan, r.feb, r.mar, r.apr, r.may, r.jun, r.jul, r.aug, r.sep, r.oct, r.nov, r.dec].join(',');
|
||||
});
|
||||
return [header, ...csvLines].join('\n');
|
||||
}
|
||||
|
||||
private parseCurrency(val: string | number | undefined | null): number {
|
||||
if (val === undefined || val === null) return 0;
|
||||
if (typeof val === 'number') return val;
|
||||
let s = String(val).trim();
|
||||
if (!s || s === '-' || s === '$-' || s === '$ -') return 0;
|
||||
const isNegative = s.includes('(') && s.includes(')');
|
||||
s = s.replace(/[$,\s()]/g, '');
|
||||
if (!s || s === '-') return 0;
|
||||
const num = parseFloat(s);
|
||||
if (isNaN(num)) return 0;
|
||||
return isNegative ? -num : num;
|
||||
}
|
||||
|
||||
private inferAccountType(accountNumber: string, accountName: string): string {
|
||||
const prefix = parseInt(accountNumber.split('-')[0].trim(), 10);
|
||||
if (isNaN(prefix)) return 'expense';
|
||||
const nameUpper = (accountName || '').toUpperCase();
|
||||
if (prefix >= 3000 && prefix < 4000) return 'income';
|
||||
if (nameUpper.includes('INCOME') || nameUpper.includes('REVENUE') || nameUpper.includes('ASSESSMENT')) return 'income';
|
||||
return 'expense';
|
||||
}
|
||||
|
||||
private inferFundType(accountNumber: string, accountName: string): string {
|
||||
const prefix = parseInt(accountNumber.split('-')[0].trim(), 10);
|
||||
const nameUpper = (accountName || '').toUpperCase();
|
||||
if (nameUpper.includes('RESERVE')) return 'reserve';
|
||||
if (prefix >= 7000 && prefix < 8000) return 'reserve';
|
||||
return 'operating';
|
||||
}
|
||||
|
||||
async deletePlan(fiscalYear: number) {
|
||||
const plans = await this.tenant.query(
|
||||
'SELECT * FROM budget_plans WHERE fiscal_year = $1', [fiscalYear],
|
||||
|
||||
Reference in New Issue
Block a user