|
@@ -213,6 +213,10 @@ router.get('/deductible-summary/:property_id', async (req, res) => {
|
|
|
const currentYear = new Date().getFullYear();
|
|
const currentYear = new Date().getFullYear();
|
|
|
const startYear = currentYear - 10;
|
|
const startYear = currentYear - 10;
|
|
|
|
|
|
|
|
|
|
+ // Fetch 20 years of data to compute amortization for the full 11-year window
|
|
|
|
|
+ const amortYears = 10;
|
|
|
|
|
+ const fetchStartYear = startYear - (amortYears - 1);
|
|
|
|
|
+
|
|
|
const rows = (await pool.query(`
|
|
const rows = (await pool.query(`
|
|
|
SELECT year, SUM(amount) as total, COUNT(*) as count
|
|
SELECT year, SUM(amount) as total, COUNT(*) as count
|
|
|
FROM charges
|
|
FROM charges
|
|
@@ -220,17 +224,32 @@ router.get('/deductible-summary/:property_id', async (req, res) => {
|
|
|
AND year >= $3 AND year <= $4
|
|
AND year >= $3 AND year <= $4
|
|
|
GROUP BY year
|
|
GROUP BY year
|
|
|
ORDER BY year ASC
|
|
ORDER BY year ASC
|
|
|
- `, [property_id, req.userId, startYear, currentYear])).rows;
|
|
|
|
|
|
|
+ `, [property_id, req.userId, fetchStartYear, currentYear])).rows;
|
|
|
|
|
|
|
|
const byYear = Object.fromEntries(rows.map(r => [r.year, { total: round2(parseFloat(r.total)), count: parseInt(r.count) }]));
|
|
const byYear = Object.fromEntries(rows.map(r => [r.year, { total: round2(parseFloat(r.total)), count: parseInt(r.count) }]));
|
|
|
|
|
+
|
|
|
|
|
+ // Build result for the 11-year display window
|
|
|
const years = [];
|
|
const years = [];
|
|
|
for (let y = startYear; y <= currentYear; y++) {
|
|
for (let y = startYear; y <= currentYear; y++) {
|
|
|
- years.push({ year: y, total: byYear[y]?.total || 0, count: byYear[y]?.count || 0 });
|
|
|
|
|
|
|
+ const total = byYear[y]?.total || 0;
|
|
|
|
|
+ const count = byYear[y]?.count || 0;
|
|
|
|
|
+
|
|
|
|
|
+ // Amortization: sum of (investment of year Y-i) / 10, for i = 0..9
|
|
|
|
|
+ let amortization = 0;
|
|
|
|
|
+ for (let i = 0; i < amortYears; i++) {
|
|
|
|
|
+ const pastYear = y - i;
|
|
|
|
|
+ const pastTotal = byYear[pastYear]?.total || 0;
|
|
|
|
|
+ amortization += pastTotal / amortYears;
|
|
|
|
|
+ }
|
|
|
|
|
+ amortization = round2(amortization);
|
|
|
|
|
+
|
|
|
|
|
+ years.push({ year: y, total, count, amortization });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const grandTotal = round2(years.reduce((s, y) => s + y.total, 0));
|
|
const grandTotal = round2(years.reduce((s, y) => s + y.total, 0));
|
|
|
|
|
+ const grandAmortization = round2(years.reduce((s, y) => s + y.amortization, 0));
|
|
|
|
|
|
|
|
- res.json({ property: prop, start_year: startYear, end_year: currentYear, years, grand_total: grandTotal });
|
|
|
|
|
|
|
+ res.json({ property: prop, start_year: startYear, end_year: currentYear, years, grand_total: grandTotal, grand_amortization: grandAmortization });
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// Annual bilan for a property
|
|
// Annual bilan for a property
|