Created
December 12, 2021 14:52
-
-
Save alichry/ae872573c3d0a2e80f820c9cc28e3d27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$federalRates = [ | |
[49020, 15], | |
[49020, 20.5], | |
[53939, 26], | |
[64533, 29], | |
[216511, 33] | |
]; | |
$bcRates2021 = [ | |
[42184, 5.06], | |
[84369, 7.7], | |
[96866, 10.5], | |
[117623, 12.29], | |
[159483, 14.7], | |
[222420, 16.8], | |
[PHP_INT_MAX, 20.5] | |
]; | |
function f($x) { | |
return number_format($x, 2, '.', ','); | |
} | |
function federal($salary) { | |
global $federalRates; | |
$slices = $federalRates; | |
$remaining = $salary; | |
$tax = 0; | |
for ($i = 0; $i < count($slices) && $remaining > 0; $i++) { | |
list($slice, $rate) = $slices[$i]; | |
$amount = min($slice, $remaining); | |
$tax += $amount * $rate / 100; | |
$remaining -= $amount; | |
} | |
return [$tax, $salary - $tax]; | |
} | |
function bc($salary) { | |
global $bcRates2021; | |
$rates = $bcRates2021; | |
$taxed = 0; | |
$tax = 0; | |
for ($i = 0; $i < count($rates) && $taxed <= $salary; $i++) { | |
list($end, $rate) = $rates[$i]; | |
if ($i === 0) { | |
$start = 0; | |
} else { | |
list($start) = $rates[$i - 1]; | |
} | |
$amount = min($end - $start, $salary - $taxed); | |
$taxed += $amount; | |
$tax += $amount * $rate / 100; | |
} | |
return [$tax, $salary - $tax]; | |
} | |
function owed($salary) { | |
list($fdTax) = federal($salary); | |
list($bcTax) = bc($salary); | |
$total = $fdTax + $bcTax; | |
return [$total, $salary - $total]; | |
} | |
$salary = $argv[1] ?? die("Usage: ${argv[0]} <salary>\n"); | |
list($tax, $after) = owed($salary); | |
printf("Tax: %s\nSalary after tax: %s\n", f($tax), f($after)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment