Monday, 16 February 2026

Update capex

function readComputeLicenceTotals() {

  console.clear(); // Clear console

  

  const table = document.querySelector('table') || document.getElementById('yourTableId');

  console.log('🔍 Table:', table ? '✅ Found' : '❌ Missing');

  

  if (!table || table.rows.length === 0) return {compute: 0, licence: 0, grand: 0};

  

  // DEBUG: Show ALL headers + first row

  const headers = Array.from(table.rows[0].cells).map((cell, i) => 

    `Col${i}: "${cell.textContent.trim()} | ${cell.innerHTML.substring(0,50)}..."`

  );

  console.table(['Headers:', headers.join('

')]);

  

  const firstRowCells = table.rows[1]?.cells;

  if (firstRowCells) {

    console.table(['Sample Row:', Array.from(firstRowCells).map((cell, i) => 

      `Col${i}: "${cell.textContent.trim()} | HTML: ${cell.innerHTML.substring(0,30)}..."`

    )]);

  }

  

  // UNIVERSAL: Find columns by ANY mention of compute/licence

  let computeCol = -1, licenceCol = -1;

  for (let col = 0; col < table.rows[0].cells.length; col++) {

    const headerText = table.rows[0].cells[col].textContent.toLowerCase();

    const headerHTML = table.rows[0].cells[col].innerHTML.toLowerCase();

    

    if (headerText.includes('compute') || headerHTML.includes('compute')) computeCol = col;

    if (headerText.includes('licence') || headerText.includes('license') || headerHTML.includes('licence')) licenceCol = col;

  }

  

  console.log(`📊 Compute Col: ${computeCol} | Licence Col: ${licenceCol}`);

  

  if (computeCol === -1 || licenceCol === -1) {

    console.error('❌ Columns not found! Check headers above.');

    return {compute: 0, licence: 0, grand: 0};

  }

  

  // READ ALL ROWS - Handles text, inputs, spans, autopopulated

  let computeTotal = 0, licenceTotal = 0;

  for (let r = 1; r < table.rows.length; r++) { // Skip header

    const row = table.rows[r];

    

    // COMPUTE CELL - Try all methods

    if (row.cells[computeCol]) {

      const cell = row.cells[computeCol];

      let val = 0;

      

      // Method 1: Direct text

      val = parseFloat(cell.textContent.replace(/[^d.,-]/g, '').replace(/,/g, '')) || 0;

      

      // Method 2: Input value (autopopulated inputs)

      const input = cell.querySelector('input[type="number"], input[type="text"]');

      if (input) val = parseFloat(input.value) || val;

      

      // Method 3: Span/div autopopulated

      const span = cell.querySelector('span, div');

      if (span && !input) val = parseFloat(span.textContent.replace(/[^d.,-]/g, '').replace(/,/g, '')) || val;

      

      computeTotal += val;

      console.log(`Row ${r} Compute: "${cell.innerHTML}" → ${val}`);

    }

    

    // LICENCE CELL - Same logic

    if (row.cells[licenceCol]) {

      const cell = row.cells[licenceCol];

      let val = 0;

      

      val = parseFloat(cell.textContent.replace(/[^d.,-]/g, '').replace(/,/g, '')) || 0;

      const input = cell.querySelector('input');

      if (input) val = parseFloat(input.value) || val;

      const span = cell.querySelector('span, div');

      if (span && !input) val = parseFloat(span.textContent.replace(/[^d.,-]/g, '').replace(/,/g, '')) || val;

      

      licenceTotal += val;

      console.log(`Row ${r} Licence: "${cell.innerHTML}" → ${val}`);

    }

  }

  

  const grandTotal = computeTotal + licenceTotal;

  

  console.log(`✅ FINALS: Compute: ${computeTotal} | Licence: ${licenceTotal} | Grand: ${grandTotal}`);

  

  // Set globals for capex table

  window.computeTotalForCapex = computeTotal;

  window.licenceTotalForCapex = licenceTotal;

  window.grandTotalForCapex = grandTotal;

  

  return {compute: computeTotal, licence: licenceTotal, grand: grandTotal};

}



function readComputeLicenceTotals1() {

  const table = document.getElementById('yourMainTable') || document.querySelector('table');

  if (!table) {

    console.error('No table found!');

    return {compute: 0, licence: 0, grand: 0};

  }

  

  // Find column indices by HEADER names (case-insensitive)

  const headers = Array.from(table.rows[0]?.cells || []);

  const computeCol = headers.findIndex(h => 

    h.textContent.toLowerCase().includes('compute')

  );

  const licenceCol = headers.findIndex(h => 

    h.textContent.toLowerCase().includes('licence') || 

    h.textContent.toLowerCase().includes('license')

  );

  

  console.log('Compute column:', computeCol, headers[computeCol]?.textContent);

  console.log('Licence column:', licenceCol, headers[licenceCol]?.textContent);

  

  if (computeCol === -1 || licenceCol === -1) {

    console.error('Columns not found! Headers:', headers.map(h => h.textContent));

    return {compute: 0, licence: 0, grand: 0};

  }

  

  // Sum values from data rows

  let computeTotal = 0, licenceTotal = 0;

  const dataRows = table.rows.length > 1 ? table.rows.length - 1 : 0; // Skip header

  

  for (let i = 1; i < table.rows.length; i++) {

    const row = table.rows[i];

    if (row.cells[computeCol]) {

      const val = parseFloat(row.cells[computeCol].textContent.replace(/[^d.,]/g, '').replace(/,/g, '')) || 0;

      computeTotal += val;

    }

    if (row.cells[licenceCol]) {

      const val = parseFloat(row.cells[licenceCol].textContent.replace(/[^d.,]/g, '').replace(/,/g, '')) || 0;

      licenceTotal += val;

    }

  }

  

  const grandTotal = computeTotal + licenceTotal;

  

  console.log('Totals:', {compute: computeTotal, licence: licenceTotal, grand: grandTotal});

  

  // Set globals

  window.computeTotalForCapex = computeTotal;

  window.licenceTotalForCapex = licenceTotal;

  window.grandTotalForCapex = grandTotal;

  

  return {compute: computeTotal, licence: licenceTotal, grand: grandTotal};

}


 function addRowAndUpdateTotal() {

  // ... your existing add row code ...

  

  // READ Licence & Compute COLUMNS (adjust column indices/selectors)

  const table = document.getElementById('yourMainTable'); // Your table ID

  const rows = table.querySelectorAll('tbody tr'); // Data rows only

  

  let computeTotal = 0;

  let licenceTotal = 0;

  

  rows.forEach(row => {

    // Assuming: Col 1=Item, Col 2=Compute, Col 3=Licence (adjust indices)

    const computeVal = parseFloat(row.cells[1]?.textContent.replace(/[^d.,]/g, '').replace(/,/g, '')) || 0;

    const licenceVal = parseFloat(row.cells[2]?.textContent.replace(/[^d.,]/g, '').replace(/,/g, '')) || 0;

    

    computeTotal += computeVal;

    licenceTotal += licenceVal;

  });

  

  const grandTotal = computeTotal + licenceTotal;

  

  // Update your table's grand total display (if needed)

  const grandCell = document.getElementById('grandTotalCell');

  if (grandCell) grandCell.textContent = grandTotal.toLocaleString('en-IN');

  

  // Share for capex table

  window.computeTotalForCapex = computeTotal;

  window.licenceTotalForCapex = licenceTotal;

  window.grandTotalForCapex = grandTotal;

  

  window.dispatchEvent(new CustomEvent('totalsUpdated'));

}


function addRowAndUpdateTotal() {

  // ... your existing add row code ...

  

  // SAFE grand total (adjust ID to match your table)

  const grandTotalCell = document.getElementById('grandTotalCell') || 

                        document.querySelector('#yourTable tfoot td:last-child');

  

  if (grandTotalCell) {

    const grandTotal = parseFloat(grandTotalCell.textContent.replace(


function updateCapexTable() {

  // SAFE CHECK - No more null errors

  const container = document.getElementById('capexContainer');

  if (!container) {

    console.error('Missing #capexContainer div!');

    return;

  }

  

  const year1Capex = window.grandTotalForCapex || 0;

  const growthRate = 0.08;

  

  let html = `

    <table id="capexTable" border="1" style="border-collapse: collapse; width: 100%; font-family: monospace;">

      <thead style="background: #e3f2fd;">

        <tr>

          <th>Particulars</th>

          <th>Grand Total<br>(Year 1)</th>

          <th>Year 2 (+8%)</th>

          <th>Year 3</th>

          <th>Year 4</th>

          <th>Year 5</th>

          <th style="background: #c8e6c9;">Cumulative</th>

        </tr>

      </thead>

      <tbody>

        <tr style="font-weight: bold;">

          <td>Capex (₹ Cr)</td>

          <td style="text-align: right;">${year1Capex.toLocaleString('en-IN')}</td>`;


  let cumulative = year1Capex;

  for (let year = 2; year <= 5; year++) {

    const capex = year1Capex * Math.pow(1 + growthRate, year - 1);

    cumulative += capex;

    html += `<td style="text-align: right;">${capex.toLocaleString('en-IN')}</td>`;

  }

  

  html += `<td style="text-align: right; font-size: 1.1em;">${cumulative.toLocaleString('en-IN')}</td></tr></tbody></table>`;

  

  container.innerHTML = html; // SAFE - container exists

}


// SAFE listeners

if (document.readyState === 'loading') {

  document.addEventListener('DOMContentLoaded', updateCapexTable);

} else {

  updateCapexTable();

}


window.addEventListener('grandTotalUpdated', updateCapexTable);


<!-- ADD THIS DIV - Required for capex table -->

<div id="capexContainer" style="margin: 20px 0;"></div>



function updateCapexTable1() {

  const year1Capex = window.grandTotalForCapex || 0;

  const growthRate = 0.08;  // Edit as needed

  

  let html = `

    <table id="capexTable" border="1" style="border-collapse: collapse; margin-top: 20px; font-family: monospace;">

      <thead style="background: #e3f2fd; font-weight: bold;">

        <tr>

          <th style="padding: 12px 8px;">Particulars</th>

          <th style="padding: 12px 8px;">Grand Total<br>(Year 1 Base)</th>

          <th style="padding: 12px 8px;">Year 2<br>(+8%)</th>

          <th style="padding: 12px 8px;">Year 3</th>

          <th style="padding: 12px 8px;">Year 4</th>

          <th style="padding: 12px 8px;">Year 5</th>

          <th style="padding: 12px 8px;">Cumulative<br>Total</th>

        </tr>

      </thead>

      <tbody>

        <tr style="background: #f8f9ff; font-weight: bold;">

          <td style="padding: 10px 8px;">Capex (₹ Cr)</td>

          <td style="text-align: right; padding: 10px 8px;">${year1Capex.toLocaleString('en-IN')}</td>`;


  let cumulative = year1Capex;

  for (let year = 2; year <= 5; year++) {

    const capex = year1Capex * Math.pow(1 + growthRate, year - 1);

    cumulative += capex;

    html += `<td style="text-align: right; padding: 10px 8px;">${capex.toLocaleString('en-IN')}</td>`;

  }

  

  html += `

          <td style="text-align: right; padding: 10px 8px; background: #c8e

  

  document.getElementById('capexContainer').innerHTML = html;

}


function updateCapexTable() {

  const container = document.getElementById('capexContainer');

  if (!container) return;

  

  const computeY1 = window.computeTotalForCapex || 0;

  const licenceY1 = window.licenceTotalForCapex || 0;

  const grandY1 = window.grandTotalForCapex || 0;

  const growthRate = 0.08;

  

  let html = `

    <table id="capexTable" border="1" style="border-collapse: collapse; width: 100%; margin-top: 20px;">

      <thead style="background: #e3f2fd; font-weight: bold;">

        <tr>

          <th style="padding: 12px 8px;">Particulars</th>

          <th>Year 1<br>(Base)</th><th>Year 2<br>(+8%)</th><th>Year 3</th><th>Year 4</th><th>Year 5</th><th style="background: #c8e6c9;">Cumulative</th>

        </tr>

      </thead>

      <tbody>

        <tr>

          <td style="padding: 10px; background: #fff3e0;">Compute</td>

          <td style="text-align: right; padding: 10px;">${computeY1.toLocaleString('en-IN')}</td>`;


  let computeCum = computeY1;

  for (let y = 2; y <= 5; y++) {

    const val = computeY1 * Math.pow(1 + growthRate, y - 1);

    computeCum += val;

    html += `<td style="text-align: right; padding: 10px;">${val.toLocaleString('en-IN')}</td>`;

  }

  html += `<td style="text-align: right; padding: 10px; background: #fff3e0;">${computeCum.toLocaleString('en-IN')}</td></tr>`;


  // Licence row

  html += `

        <tr>

          <td style="padding: 10px; background: #e8f5e8;">Licence</td>

          <td style="text-align: right; padding: 10px;">${licenceY1.toLocaleString('en-IN')}</td>`;


  let licenceCum = licenceY1;

  for (let y = 2; y <= 5; y++) {

    const val = licenceY1 * Math.pow(1 + growthRate, y - 1);

    licenceCum += val;

    html += `<td style="text-align: right; padding: 10px;">${val.toLocaleString('en-IN')}</td>`;

  }

  html += `<td style="text-align: right; padding: 10px; background: #e8f5e8;">${licenceCum.toLocaleString('en-IN')}</td></tr>`;


  // Grand Total row

  html += `

        <tr style="font-weight: bold; background: #f5f5f5;">

          <td style="padding: 10px;">Grand Total</td>

          <td style="text-align: right; padding: 10px;">${grandY1.toLocaleString('en-IN')}</td>`;


  let grandCum = grandY1;

  for (let y = 2; y <= 5; y++) {

    const val = grandY1 * Math.pow(1 + growthRate, y - 1);

    grandCum += val;

    html += `<td style="text-align: right; padding: 10px;">${val.toLocaleString('en-IN')}</td>`;

  }

  html += `<td style="text-align: right; padding: 10px; font-size: 1.2em; background: #c8e6c9;">${grandCum.toLocaleString('en-IN')}</td></tr>

      </tbody>

    </table>

  `;

  

  container.innerHTML = html;

}


// Update listener

window.addEventListener('totalsUpdated', updateCapexTable);


No comments:

Post a Comment