Untitled

 avatar
unknown
javascript
12 days ago
4.6 kB
3
Indexable
// Function to calculate corporate tax with Partial Tax Exemption
function calculateCorporateTax(chargeableIncome, corporateTaxRate) {
  let tax = 0;

  // Apply Partial Tax Exemption
  if (chargeableIncome <= 10000) {
    // 75% exemption on the first SGD 10,000
    tax = chargeableIncome * (corporateTaxRate / 100) * 0.25;
  } else if (chargeableIncome <= 200000) {
    // 75% exemption on the first SGD 10,000
    tax = 10000 * (corporateTaxRate / 100) * 0.25;
    // 50% exemption on the next SGD 190,000
    tax += (chargeableIncome - 10000) * (corporateTaxRate / 100) * 0.5;
  } else {
    // 75% exemption on the first SGD 10,000
    tax = 10000 * (corporateTaxRate / 100) * 0.25;
    // 50% exemption on the next SGD 190,000
    tax += 190000 * (corporateTaxRate / 100) * 0.5;
    // Remaining income taxed at the full rate
    tax += (chargeableIncome - 200000) * (corporateTaxRate / 100);
  }

  return tax;
}

// Function to calculate personal income tax
function calculatePersonalTax(income, personalTaxRates) {
  let tax = 0;
  for (const bracket of personalTaxRates) {
    if (income > bracket.min) {
      const taxableAmount = Math.min(income, bracket.max) - bracket.min;
      tax += taxableAmount * (bracket.rate / 100);
    }
  }
  return tax;
}

// Function to find the optimal split
function findOptimalSplit(totalAmount, corporateTaxRate, personalTaxRates) {
  let optimalDividends = 0;
  let optimalDirectorFees = 0;
  let minTotalTax = Infinity;

  // Iterate through all possible splits
  for (let dividends = 0; dividends <= totalAmount; dividends += 1000) {
    const directorFees = totalAmount - dividends;

    // Calculate corporate tax on dividends
    const corporateTax = calculateCorporateTax(dividends, corporateTaxRate);

    // Calculate personal tax on director fees
    const personalTax = calculatePersonalTax(directorFees, personalTaxRates);

    // Calculate total tax
    const totalTax = corporateTax + personalTax;

    // Check if this is the optimal split
    if (totalTax < minTotalTax) {
      minTotalTax = totalTax;
      optimalDividends = dividends;
      optimalDirectorFees = directorFees;
    }
  }

  return {
    dividends: optimalDividends,
    directorFees: optimalDirectorFees,
    totalTax: minTotalTax,
  };
}

// Function to add a new tax bracket
function addTaxBracket() {
  const container = document.getElementById('personalTaxRates');
  const bracket = document.createElement('div');
  bracket.className = 'tax-bracket';
  bracket.innerHTML = `
    <label>Income From:</label>
    <input type="number" class="min" placeholder="Min">
    <label>To:</label>
    <input type="number" class="max" placeholder="Max">
    <label>Rate (%):</label>
    <input type="number" class="rate" placeholder="Rate">
  `;
  container.appendChild(bracket);
}

// Main calculation function
function calculate() {
  const amount = parseFloat(document.getElementById('amount').value);
  const corporateTaxRate = parseFloat(document.getElementById('corporateTaxRate').value);

  if (isNaN(amount) || amount <= 0 || isNaN(corporateTaxRate) || corporateTaxRate < 0) {
    alert("Please enter valid amounts and tax rates.");
    return;
  }

  // Get personal tax rates from input fields
  const personalTaxRates = [];
  const brackets = document.querySelectorAll('.tax-bracket');
  brackets.forEach(bracket => {
    const min = parseFloat(bracket.querySelector('.min').value);
    const max = parseFloat(bracket.querySelector('.max').value);
    const rate = parseFloat(bracket.querySelector('.rate').value);

    if (!isNaN(min) && !isNaN(max) && !isNaN(rate)) {
      personalTaxRates.push({ min, max, rate });
    }
  });

  if (personalTaxRates.length === 0) {
    alert("Please enter at least one valid personal tax bracket.");
    return;
  }

  // Find the optimal split
  const optimalSplit = findOptimalSplit(amount, corporateTaxRate, personalTaxRates);

  // Display results
  document.getElementById('dividendsResult').innerText =
    `Dividends: SGD ${optimalSplit.dividends.toFixed(2)} (Tax: SGD ${calculateCorporateTax(optimalSplit.dividends, corporateTaxRate).toFixed(2)})`;

  document.getElementById('directorFeesResult').innerText =
    `Director Fees: SGD ${optimalSplit.directorFees.toFixed(2)} (Tax: SGD ${calculatePersonalTax(optimalSplit.directorFees, personalTaxRates).toFixed(2)})`;

  document.getElementById('optimalOption').innerText =
    `Optimal Split: Pay SGD ${optimalSplit.dividends.toFixed(2)} as Dividends and SGD ${optimalSplit.directorFees.toFixed(2)} as Director Fees. Total Tax: SGD ${optimalSplit.totalTax.toFixed(2)}`;
}
Leave a Comment