Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.

Commit eb2ff54

Browse files
Merge pull request #4 from kmee/fci
Ficha de conteudo de importação
2 parents f6728aa + 798286f commit eb2ff54

11 files changed

Lines changed: 429 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ dist/
4242
build/
4343
.eggs/
4444
*.egg-info
45+
46+

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ install:
88
- pip install coveralls
99
script:
1010
- coverage run --source=sped setup.py test
11+
- python test/fci_test.py
1112
deploy:
1213
provider: pypi
1314
user: ginx

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ O projeto está em fase inicial de desenvolvimento e **não deve** ser usado em
4747
| ECF | Funcional |
4848
| EFD-PIS/COFINS | Funcional |
4949
| EFD-ICMS/IPI | Funcional |
50+
| FCI | Funcional |
5051

5152
### ECD
5253

@@ -75,3 +76,10 @@ Este módulo está funcional, com todos seus registros codificados, porém muito
7576
adequada, consultado tabelas externas por exemplo, ou validando corretamente todos os tamanhos de campos.
7677

7778
Ele pode ser usado para gerar um arquivo digital, com validações de abertura e fechamento de bloco automaticamente.
79+
80+
### FCI
81+
82+
Este módulo está funcional, com todos seus registros codificados, porém muitos campos ainda não possuem uma validação
83+
adequada, validando corretamente todos os tamanhos de campos.
84+
85+
Ele pode ser usado para gerar um arquivo digital, com validações de abertura e fechamento de bloco automaticamente.

sped/arquivos.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
class ArquivoDigital(object):
9+
910
registro_abertura = None
1011
registro_fechamento = None
1112
registros = None
@@ -19,15 +20,15 @@ def __init__(self):
1920
def readfile(self, filename):
2021
with open(filename) as file:
2122
for line in [line.rstrip('\r\n') for line in file]:
22-
self.read_registro(line)
23+
self.read_registro(line.decode('utf8'))
2324

2425
def read_registro(self, line):
2526
reg_id = line.split('|')[1]
2627

2728
try:
2829
registro_class = getattr(self.__class__.registros, 'Registro' + reg_id)
2930
except AttributeError:
30-
raise RuntimeError("Arquivo inválido para EFD - PIS/COFINS")
31+
raise RuntimeError(u"Arquivo inválido para EFD - PIS/COFINS")
3132

3233
registro = registro_class(line)
3334

@@ -47,6 +48,7 @@ def write_to(self, buffer):
4748
bloco = self._blocos[key]
4849
reg_count += len(bloco.registros)
4950
for r in bloco.registros:
51+
a = r.as_line()
5052
buffer.write(r.as_line() + u'\r\n')
5153

5254
self._registro_fechamento[2] = reg_count
@@ -57,3 +59,10 @@ def getstring(self):
5759
buffer = StringIO()
5860
self.write_to(buffer)
5961
return buffer.getvalue()
62+
63+
# def getFile(self):
64+
# from tempfile import TemporaryFile
65+
# tmp = TemporaryFile()
66+
# self.write_to(tmp)
67+
# tmp.seek(0)
68+
# return tmp

sped/fci/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

sped/fci/arquivos.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from ..arquivos import ArquivoDigital
4+
from . import blocos
5+
from . import registros
6+
from .blocos import Bloco0
7+
from .blocos import Bloco5
8+
from .blocos import Bloco9
9+
from .registros import Registro0000
10+
from .registros import Registro9999
11+
12+
13+
class ArquivoDigital(ArquivoDigital):
14+
registro_abertura = Registro0000
15+
registro_fechamento = Registro9999
16+
registros = registros
17+
blocos = blocos
18+
19+
def __init__(self):
20+
super(ArquivoDigital, self).__init__()
21+
self._blocos['0'] = Bloco0()
22+
self._blocos['5'] = Bloco5()
23+
self._blocos['9'] = Bloco9()
24+
25+
utf = (u"0001|Texto em caracteres UTF-8: (dígrafo BR)'ção',(dígrafo "
26+
u"espanhol-enhe)'ñ',(trema)'Ü',(ordinais)'ªº',(ligamento s+z a"
27+
u"lemão)'ß'.")
28+
self.read_registro(utf)
29+
self.read_registro('|9900|0000|1')
30+
self.read_registro('|9900|0010|1')
31+
self.read_registro('|9900|5020|')
32+
33+
def read_registro(self, line):
34+
35+
# caso o usuario insira linha sem pip
36+
pipe = '|' if line[0] != '|' else ''
37+
line = pipe + line
38+
reg_id = line.split('|')[1]
39+
40+
try:
41+
registro_class = \
42+
getattr(self.__class__.registros, 'Registro' + reg_id)
43+
except AttributeError:
44+
raise RuntimeError(u"Arquivo inválido para FCI")
45+
46+
registro = registro_class(line)
47+
if registro.__class__ == self.__class__.registro_abertura:
48+
self._registro_abertura = registro
49+
elif registro.__class__ == self.__class__.registro_fechamento:
50+
self._registro_fechamento = registro
51+
elif registro.__class__ == \
52+
self.__class__.blocos.Bloco0.registro_abertura:
53+
self.blocos.Bloco0.abertura = registro
54+
else:
55+
bloco_id = reg_id[0]
56+
bloco = self._blocos[bloco_id]
57+
bloco.add(registro)
58+
59+
# Contabiliza os registros 5020
60+
if reg_id == '5020':
61+
registros_9 = self._blocos['9'].registros[3]
62+
registros_9.valores[3] = \
63+
str((len(self._blocos['5'].registros)) - 2)
64+
65+
def write_to(self, buffer):
66+
67+
linha_abertura = self._registro_abertura.as_line()[1:]
68+
buffer.write(linha_abertura + u'\r\n')
69+
reg_count = 2
70+
for key in self._blocos.keys():
71+
bloco = self._blocos[key]
72+
reg_count += len(bloco.registros)
73+
for r in bloco.registros:
74+
a = r.as_line()
75+
a = a[1:]
76+
buffer.write(a + u'\r\n')
77+
78+
self._registro_fechamento[2] = reg_count
79+
linha_fechamento = self._registro_fechamento.as_line()[1:]
80+
buffer.write(linha_fechamento + u'\r\n')
81+
82+
def readfile(self, filename):
83+
84+
with open(filename) as file:
85+
for line in [line.rstrip('\r\n') for line in file]:
86+
if (line[:4] != '9900' and line[:4] != '0001' and line[:4] != '0990'):
87+
self.read_registro(line.decode('utf-8-sig'))

sped/fci/blocos.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from ..blocos import Bloco
4+
5+
from .registros import Registro0001
6+
from .registros import Registro0990
7+
from .registros import Registro5001
8+
from .registros import Registro5990
9+
from .registros import Registro9001
10+
from .registros import Registro9990
11+
12+
13+
class Bloco0(Bloco):
14+
"""
15+
Cabeçalho da FCI: Identificação do contribuinte
16+
"""
17+
registro_abertura = Registro0001
18+
registro_fechamento = Registro0990
19+
20+
@property
21+
def abertura(self):
22+
registro = self.__class__.registro_abertura()
23+
return registro
24+
25+
@property
26+
def fechamento(self):
27+
registro = self.__class__.registro_fechamento()
28+
# Define a quantidade de registros
29+
registro[2] = len(self._registros) + 3
30+
return registro
31+
32+
def add(self, registro):
33+
self._registros.append(registro)
34+
35+
36+
class Bloco5(Bloco):
37+
registro_abertura = Registro5001
38+
registro_fechamento = Registro5990
39+
40+
@property
41+
def abertura(self):
42+
registro = self.__class__.registro_abertura()
43+
return registro
44+
45+
46+
class Bloco9(Bloco):
47+
"""
48+
Controle e Encerramento do Arquivo Digital
49+
"""
50+
registro_abertura = Registro9001
51+
registro_fechamento = Registro9990
52+
53+
@property
54+
def abertura(self):
55+
registro = self.__class__.registro_abertura()
56+
return registro

0 commit comments

Comments
 (0)