Bladeren bron

ajout du tableau d'amortissements

jeremy 4 maanden geleden
bovenliggende
commit
cf3562e172
2 gewijzigde bestanden met toevoegingen van 30 en 6 verwijderingen
  1. 22 3
      backend/src/routes/charges.js
  2. 8 3
      frontend/src/pages/Charges.jsx

+ 22 - 3
backend/src/routes/charges.js

@@ -213,6 +213,10 @@ router.get('/deductible-summary/:property_id', async (req, res) => {
   const currentYear = new Date().getFullYear();
   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(`
     SELECT year, SUM(amount) as total, COUNT(*) as count
     FROM charges
@@ -220,17 +224,32 @@ router.get('/deductible-summary/:property_id', async (req, res) => {
       AND year >= $3 AND year <= $4
     GROUP BY year
     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) }]));
+
+  // Build result for the 11-year display window
   const years = [];
   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 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

+ 8 - 3
frontend/src/pages/Charges.jsx

@@ -437,7 +437,7 @@ export default function Charges() {
             <div className="flex items-center gap-2">
               <span className="text-lg">💼</span>
               <div>
-                <h3 className="text-sm font-semibold text-purple-800">Charges déductibles des impôts — 10 ans</h3>
+                <h3 className="text-sm font-semibold text-purple-800">Charges déductibles des impôts — 11 ans</h3>
                 <p className="text-xs text-purple-500">
                   Total cumulé : <span className="font-bold">{fmt(deductibleSummary.grand_total)}</span>
                 </p>
@@ -451,7 +451,8 @@ export default function Charges() {
                 <thead>
                   <tr className="bg-purple-50 border-b border-purple-100">
                     <th className="text-left px-4 py-2 text-purple-700 font-semibold">Année</th>
-                    <th className="text-right px-4 py-2 text-purple-700 font-semibold">Montant déductible</th>
+                    <th className="text-right px-4 py-2 text-purple-700 font-semibold">Investissement</th>
+                    <th className="text-right px-4 py-2 text-purple-700 font-semibold">Amortissement /10 ans</th>
                     <th className="text-right px-4 py-2 text-purple-700 font-semibold">Nb factures</th>
                   </tr>
                 </thead>
@@ -462,6 +463,9 @@ export default function Charges() {
                       <td className={`px-4 py-2 text-right ${y.total > 0 ? 'text-purple-700' : 'text-gray-300'}`}>
                         {y.total > 0 ? fmt(y.total) : '—'}
                       </td>
+                      <td className={`px-4 py-2 text-right ${y.amortization > 0 ? 'text-blue-700 font-medium' : 'text-gray-300'}`}>
+                        {y.amortization > 0 ? fmt(y.amortization) : '—'}
+                      </td>
                       <td className={`px-4 py-2 text-right ${y.count > 0 ? 'text-gray-600' : 'text-gray-300'}`}>
                         {y.count > 0 ? y.count : '—'}
                       </td>
@@ -470,8 +474,9 @@ export default function Charges() {
                 </tbody>
                 <tfoot className="bg-purple-50 border-t-2 border-purple-200">
                   <tr>
-                    <td className="px-4 py-2 font-bold text-purple-800">Total (10 ans)</td>
+                    <td className="px-4 py-2 font-bold text-purple-800">Total (11 ans)</td>
                     <td className="px-4 py-2 text-right font-bold text-purple-800">{fmt(deductibleSummary.grand_total)}</td>
+                    <td className="px-4 py-2 text-right font-bold text-blue-800">{fmt(deductibleSummary.grand_amortization)}</td>
                     <td className="px-4 py-2 text-right font-bold text-gray-600">
                       {deductibleSummary.years.reduce((s, y) => s + y.count, 0)}
                     </td>