Update to accept either high or low amounts, and add some formatting.

This commit is contained in:
Timothy Allen 2020-11-05 12:44:33 +02:00
parent a0936fd8ad
commit 687739acdc

View File

@ -3,49 +3,53 @@
import locale import locale
import math import math
import pprint
import re import re
import sys import sys
'''
Rates from https://www.sars.gov.za/Tax-Rates/Income-Tax/Pages/Rates%20of%20Tax%20for%20Individuals.aspx
low is the lowest value for that bracket; rate is the tax percentage applied to that bracket
'''
tax_rates = [ tax_rates = [
dict( dict(
rate = 18, rate = 18,
low = 1, high = 205_900,
), dict( ), dict(
rate = 26, rate = 26,
low = 205_901, high = 321_600,
), dict( ), dict(
rate = 31, rate = 31,
low = 321_601, high = 445_100,
), dict( ), dict(
rate = 36, rate = 36,
low = 445_101, high = 584_200,
), dict( ), dict(
rate = 39, rate = 39,
low = 584_201, high = 744_800,
), dict( ), dict(
rate = 41, rate = 41,
low = 744_801, high = 1_577_300,
), dict( ), dict(
rate = 45, rate = 45,
low = 1_577_301,
), ),
] ]
for r in tax_rates: for r in tax_rates:
idx = tax_rates.index(r) idx = tax_rates.index(r)
if not r.get('high'):
if idx < len(tax_rates) - 1: if idx < len(tax_rates) - 1:
high = tax_rates[idx+1].get('low') high = tax_rates[idx+1].get('low')
r['high'] = high - 1 r['high'] = high - 1
if not r.get('low'):
if idx > 0:
low = tax_rates[idx-1].get('high')
r['low'] = low + 1
else: else:
r['high'] = False r['low'] = 1
#pprint.pprint(tax_rates)
def error(e):
if e:
print(e);
print("{0}: Please enter the annual or monthly amount before tax".format(sys.argv[0]))
sys.exit()
locale.setlocale( locale.LC_MONETARY, 'en_ZA.UTF-8' ) locale.setlocale( locale.LC_MONETARY, 'en_ZA.UTF-8' )
locale._override_localeconv = {'mon_thousands_sep': ' ', 'mon_decimal_point': ','} locale._override_localeconv = {'mon_thousands_sep': ' ', 'mon_decimal_point': '.'}
currency = locale.localeconv() currency = locale.localeconv()
if len(sys.argv) < 2: if len(sys.argv) < 2:
@ -70,7 +74,7 @@ if amount > 100_000:
is_annual = True is_annual = True
else: else:
is_annual = False is_annual = False
print("Notice: value {} determined to be per month, dividing tax rates by 12".format(amount)) print("Notice: value {} determined to be per month, dividing tax rates by 12".format(locale.currency(amount, grouping=True)))
total_tax = 0 total_tax = 0
for r in tax_rates: for r in tax_rates:
@ -91,3 +95,10 @@ for r in tax_rates:
break break
print("\nTotal tax on {} is {}".format(locale.currency(amount, grouping=True), locale.currency(total_tax, grouping=True))) print("\nTotal tax on {} is {}".format(locale.currency(amount, grouping=True), locale.currency(total_tax, grouping=True)))
def error(e):
if e:
print(e);
print("{0}: Please enter the annual or monthly amount before tax".format(sys.argv[0]))
sys.exit()