Skip to content

Instantly share code, notes, and snippets.

@colobas
Last active October 13, 2017 08:57
Show Gist options
  • Save colobas/a427621ee0b8ddf2e6e3aec721195a21 to your computer and use it in GitHub Desktop.
Save colobas/a427621ee0b8ddf2e6e3aec721195a21 to your computer and use it in GitHub Desktop.
[
(7035, 0.145),
(20100-7035, 0.285),
(40200-20100, 0.37),
(80000-40200, 0.45),
("inf", 0.48)
]
615.00 0.0%
623.00 1.8%
645.00 5.0%
683.00 6.0%
736.00 7.5%
811.00 8.5%
919.00 11.0%
1001.00 12.5%
1061.00 13.5%
1139.00 14.5%
1221.00 15.5%
1317.00 16.5%
1419.00 17.5%
1557.00 18.5%
1705.00 20.0%
1864.00 21.5%
1971.00 22.5%
2083.00 23.5%
2211.00 24.5%
2359.00 25.5%
2527.00 26.5%
2758.00 27.5%
3094.00 28.5%
3523.00 29.5%
4105.00 30.7%
4636.00 32.5%
5178.00 33.5%
5862.00 34.5%
6706.00 36.5%
7915.00 37.5%
9531.00 39.5%
11248.00 40.5%
18797.00 41.5%
20160.00 42.5%
22680.00 43.3%
25200.00 44.3%
import pandas as pd
import sys
from scipy.misc import derivative
from ast import literal_eval
# baseado na info retirada de http://www.ricardomcarvalho.pt/blog/como-funciona-o-irs/
try:
with open(sys.argv[2], "r") as f:
escaloes = literal_eval(f.read())
df_irs = pd.read_csv(sys.argv[1], header=None)
df_irs[0] = df_irs[0].astype("int")
df_irs[1] = df_irs[1].str.replace("%","").astype("float")
X = df_irs[0]
df_irs = df_irs.set_index([0])
except:
print("utilização: \n\tpython irs.py PATH_CSV_RET_FONTE.csv PATH_TXT_ESCALOES_IRS.txt MONTANTE_LIQUIDO_DESEJADO")
sys.exit()
def i(x):
return df_irs.loc[min(X[X >= x])].values.item()/100
def f(x,y):
return x*(0.89 - i(x)) - y
def fp(x):
return 0.89 - (i(x) + x * derivative(i, x, dx=1e-6))
def x_np1(x_n, y):
return x_n - (f(x_n,y)/fp(x_n))
def liq2bru(liq, x0):
x_n = x0
for _ in range(500):
x_n = x_np1(x_n, liq)
return x_n
def IRS(Y):
Y -= 4104
irs = 0
for lim,tax in escaloes:
if lim != "inf":
if Y <= lim:
return irs + Y*tax
else:
irs += lim*tax
Y -= lim
else:
return irs + Y*tax
if __name__ == "__main__":
try:
liq = float(sys.argv[3])
print("Queres receber {:.2f}€ líquidos por mês.".format(liq))
bru = liq2bru(liq, liq)
print("\nIsso equivale a {:.2f}€ brutos por mês.".format(bru))
Y = bru*14
print("e a {:.2f}€ brutos por ano.".format(Y))
retf = i(bru)
print("\nNesse escalão, a retenção na fonte é {:.2f}%".format(retf*100))
retf_val = retf*bru
print("o que corresponde a {:.2f}€ em retenção na fonte por mês, mais {:.2f}€ em Seg. Social".format(retf_val, bru*0.11))
irs = IRS(Y)
print("\nA estimativa (sem abates) do teu IRS total anual é de {:.2f}€".format(irs))
irs_ta = irs/Y
print("O que equivale a uma taxa média de {:.2f}%".format(irs_ta*100))
acerto = abs(retf - irs_ta)
if retf >= irs_ta:
print("Por isso vais ter um acerto positivo ou neutro. Irás receber aprox: {:.2f}€".format(acerto*Y))
else:
print("Por isso vais ter um acerto negativo. Terás de pagar aprox: {:.2f}€".format(acerto*Y))
except Exception as e:
print(e)
print("utilização: \n\tpython irs.py PATH_CSV_RET_FONTE.csv PATH_TXT_ESCALOES_IRS.txt MONTANTE_LIQUIDO_DESEJADO")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment