Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@
darf
====
====

São 4 programinhas para calcular o valor do DARF a ser pago ao se receber
ações GOOG através de GSU.

Dependências:

* [Python 3.2][py] ou mais recente
* [dateutil][dateutil]
* [lxml][lxml]

[py]: http://python.org/
[dateutil]: http://labix.org/python-dateutil
[lxml]: http://lxml.de/

exchangerate.py
---------------

Usa UOL e Google Finance para pegar o valor da ação GOOG em uma certa data e o
valor do dólar no último dia útil da primeira quinzena do mês anterior.

./exchangerate.py 20-Jan-2012
usdbrl@15-Dec-2011: 1.860800 goog@20-Jan-2012: 585.990000

A data pode ser digitada em qualquer formato suportado pelo
[`dateutil.parser.parse`][parse].

[parse]: http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c

tax.py
------

Realiza uma busca na tabela progressiva do imposto para fazer a mesma conta
que o [carnê-leão][carneleao] faz.

./tax.py 5000 2012-Jan
R$5000.000000 @Jan-2012 gives tax of R$618.470000

O valor de entrada (no exemplo acima, 5000) corresponde à seguinte fórmula:

num. de ações × valor da ação × cotação do dólar

[carneleao]: http://www.receita.fazenda.gov.br/PessoaFisica/CarneLeao/default.htm

sicalc.py
---------

Recebe o valor de imposto e usa o sicalc para gerar o DARF.

# cpf com 12 dígitos, mês, valor
./sicalc.py 12345678909 2012-Jan 618.47
Darf written at carne-leao-2012-Jan-darf-para-2012-Aug.html

gsu_irpf.py
-----------

É a cola de todos os programas anteriores. Recebe o CPF e o arquivo CSV
*transaction history* (exportado do [benefitaccess.com][mssb]) e gera todos os
DARFs, incluindo multa e juros se aplicável. Basta apenas escolher os que não
foram pagos e pagá-los.

./gsu_irpf.py 12345678909 sample_benefit_access_csv.csv
2012-08-01: vested 3 shares for total value R$3872.00 (1 GSU = 632.68 @2012-08-01, 1 USD = 2.04 @2012-07-13)
2012-03-26: vested 5 shares for total value R$5584.24 (1 GSU = 649.33 @2012-03-26, 1 USD = 1.72 @2012-02-15)
2012-03-25: vested 5 shares for total value R$5526.27 (1 GSU = 642.59 @2012-03-25, 1 USD = 1.72 @2012-02-15)
---
carne leao 2012-03: exterior 11110.51 imposto devido: 2298.86
carne leao 2012-08: exterior 3872.00 imposto devido: 308.27
Generated darf carne-leao-2012-Mar-darf-para-2012-Aug.html
Generated darf carne-leao-2012-Aug-darf-para-2012-Aug.html
Point your browser to file://.../index.html

[mssb]: http://benefitaccess.com/
2 changes: 1 addition & 1 deletion exchangerate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/homebrew/bin/python3
#!/usr/bin/env python3

import argparse
import logging
Expand Down
5 changes: 2 additions & 3 deletions gsu_irpf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/homebrew/bin/python3
#!/usr/bin/env python3

import bisect
import csv
Expand Down Expand Up @@ -45,7 +45,6 @@ def __init__(self, output_dir):
self.xchgdb = exchangerate.ExchangeRateDB('xchgrate')
self.calculator = tax.LionTax('xchgrate')
self.sicalc = sicalc.Sicalc()
self.darf_css='https://pagamento.serpro.gov.br/Darf/estiloDARF.css'

def _ParseTransactions(self, benefit_access_csv):
reader = csv.reader(benefit_access_csv)
Expand Down Expand Up @@ -89,7 +88,7 @@ def Generate(self, benefit_access_csv, cpf, startdate):

index_file = os.path.join(self.output_dir, 'index.html')
index = open(index_file, 'w')
index.write('<html><head/><body><a href="%s">Click me</a> to fix ssl permissions if having rendering issues.<br/><br/>' % self.darf_css)
index.write('<html><head/><body>')
index.write('Tax computation <a href="file://%s">log</a><br/><br/>' % os.path.abspath(self.logfile_name))

summary = {}
Expand Down
4 changes: 3 additions & 1 deletion sicalc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/homebrew/bin/python3
#!/usr/bin/env python3

from argparse import ArgumentParser, FileType
from lxml.html import parse, submit_form, fromstring, tostring
Expand Down Expand Up @@ -82,6 +82,8 @@ def GenerateDarf(self, cpf, monthstr, main_tax):
darf = darf.replace('./', 'https://pagamento.serpro.gov.br/Darf/')
# Remove 404
darf = darf.replace('<link rel="stylesheet" type="text/css" media="print" href="./estiloDARFprint.css" />', '')
# pagamento.serpro.gov.br has an expired SSL certificate.
darf = darf.replace('https://', 'http://')
return darf

if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions tax.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/homebrew/bin/python3
#!/usr/bin/env python3

# Crazy pickle wants this here
class TaxTableEntry:
Expand Down Expand Up @@ -41,19 +41,19 @@ def _crawlTaxTable(self, url):
date = self._findDate(tree, v)
celltype = i % 3
if celltype == 0:
m = re.search(' (\d\.\d\d\d),(\d\d)</span>', txt)
m = re.search(' (\d\.\d\d\d),(\d\d)\s*</span>', txt)
assert(m)
base = float(re.sub('\.', '', m.group(1))) + float(m.group(2))/100
if 'Acima' in txt:
base = float("inf")
elif celltype == 1:
m = re.search('>((\d?\d),(\d)|-)</p>', txt)
m = re.search('<p[^>]*>\s*((\d?\d),(\d)|-)\s*</p>', txt)
assert(m)
tax = 0
if m.group(1) != '-':
tax = float(m.group(2)) + float(m.group(3))/10
elif celltype == 2:
m = re.search('>((\d\d\d),(\d\d)|-)</p>', txt)
m = re.search('<p[^>]*>\s*((\d\d\d),(\d\d)|-)\s*</p>', txt)
assert(m)
deduction = 0
if m.group(1) != '-':
Expand Down