Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Last active August 17, 2017 11:35
Show Gist options
  • Save joshuabaker/ed6cc21c8c341e3875ae9624357551dd to your computer and use it in GitHub Desktop.
Save joshuabaker/ed6cc21c8c341e3875ae9624357551dd to your computer and use it in GitHub Desktop.
Stamp duty land tax calculator that allows variable bands.
<?php
$bands = [
[
'threshold' => 0,
'primaryRate' => 0,
'secondaryRate' => 0.03,
],
[
'threshold' => 125000,
'primaryRate' => 0.02,
'secondaryRate' => 0.05,
],
[
'threshold' => 250000,
'primaryRate' => 0.05,
'secondaryRate' => 0.08,
],
[
'threshold' => 925000,
'primaryRate' => 0.1,
'secondaryRate' => 0.13,
],
[
'threshold' => 1500000,
'primaryRate' => 0.12,
'secondaryRate' => 0.15,
],
];
$price = 960000;
$isSecondary = true;
$totalTax = 0;
foreach ($bands as $index => $band) {
$rate = $isSecondary ? $band['secondaryRate'] : $band['primaryRate'];
$nextThreshold = 0;
if (isset($bands[$index + 1])) {
$nextThreshold = $bands[$index + 1]['threshold'];
}
$taxableSum = max(0, min($price - $band['threshold'], $nextThreshold - $band['threshold']));
$totalTax += $taxableSum * $rate;
}
echo "{$totalTax}\n";
function calculateTaxForBands(amount, bands) {
var totalTax = 0;
for (var i = 0; i < bands.length; i++) {
var nextFrom = i === bands.length - 1 ? amount : bands[i + 1].from;
totalTax += Math.max(0, Math.min(amount - bands[i].from, nextFrom - bands[i].from)) * bands[i].rate;
}
return totalTax;
}
var bands = {
uk: {
mainHome: [
{ from: 0, rate: 0 },
{ from: 125000, rate: 0.02 },
{ from: 250000, rate: 0.05 },
{ from: 925000, rate: 0.1 },
{ from: 1500000, rate: 0.12 }
],
secondHome: [
{ from: 0, rate: 0.03 },
{ from: 125000, rate: 0.05 },
{ from: 250000, rate: 0.08 },
{ from: 925000, rate: 0.13 },
{ from: 1500000, rate: 0.15 }
]
}
};
calculateTaxForBands(80000, bands.uk.mainHome); // 0
calculateTaxForBands(80000, bands.uk.secondHome); // 2400
calculateTaxForBands(175000, bands.uk.mainHome); // 1000
calculateTaxForBands(325000, bands.uk.mainHome); // 6250
calculateTaxForBands(1200000, bands.uk.mainHome); // 63750
calculateTaxForBands(4100000, bands.uk.mainHome); // 405750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment