Inicia implementação do comando gera.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 2 May 2015 13:38:01 +0000 (13:38 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 13 Jun 2015 13:21:33 +0000 (13:21 +0000)
Este comando gera o arquivo que deve ser enviado à Receita Federal. O
arquivo é bastante complexo, com muitos campos, alguns obtidos
diretamente dos valores preenchidos pelo contribuinte, outros que exigem
cálculos, somas, ou ordenação de listas.

No momento, apenas as duas primeiras linhas estão implementadas, e ainda
falta calcular o CRC e o hash do arquivo. Vários campos ainda precisam
ser preenchidos corretamente.

Com exceção do endereço MAC, dos campos de controle (CRC32) e do hash, o
comando gera as duas primeiras linhas idênticas àquelas geradas pelo
IRPF privativo para uma declaração sem nenhum rendimento, apenas a ficha
de dados básicos do contribuinte preenchida.

O plano é implementar o cálculo do hash, as demais linhas necessárias à
uma declaração com apenas os dados básicos do contribuinte, e ir
acrescentando novas linhas e preenchendo outros campos à medida em que o
programa for sendo desenvolvido, considerando como novo suporte ou nova
funcionalidade.

Makefile.am
declara.c
gera.c [new file with mode: 0644]
gera.h [new file with mode: 0644]

index 1d671b8..a0d646a 100644 (file)
@@ -3,6 +3,7 @@ declara_SOURCES = declara.c declaracao.c declaracao.h \
        token.c token.h \
        cmd.c cmd.h pmhash.c pmhash.h \
        calcula.c calcula.h \
+       gera.c gera.h \
        base.c base.h \
        list.c list.h \
        util.c util.h \
index bbb6ad8..fd1d534 100644 (file)
--- a/declara.c
+++ b/declara.c
@@ -29,6 +29,7 @@
 #include "contribuinte.h"
 #include "rendimento.h"
 #include "calcula.h"
+#include "gera.h"
 
 static int realprocess(struct declaracao *dec, int fd)
 {
@@ -90,6 +91,7 @@ int main(int argc, char **argv)
        contribuinte_cmd_init();
        rendimento_cmd_init();
        calcula_cmd_init();
+       gera_cmd_init();
 
        filename = argv[1];
        r = process(filename);
diff --git a/gera.c b/gera.c
new file mode 100644 (file)
index 0000000..443e04d
--- /dev/null
+++ b/gera.c
@@ -0,0 +1,215 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "gera.h"
+#include "declaracao.h"
+#include "cmd.h"
+#include "rendimento.h"
+#include <errno.h>
+#include <stdio.h>
+
+static int gera_header(struct declaracao *dec, FILE *f)
+{
+       fprintf(f, "IRPF    ");
+       fprintf(f, "%04d", dec->ano); /* Exercício */
+       fprintf(f, "%04d", dec->ano - 1); /* Ano base */
+       fprintf(f, "2500"); /* Código */
+       fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */
+       fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
+       fprintf(f, "   "); /* Filler */
+       fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */
+       fprintf(f, "100"); /* Versão IRPF */
+       fprintf(f, "%-60.60s", dec->nome);
+       fprintf(f, "%s", dec->contribuinte.uf);
+       fprintf(f, "%s", "0000000000"); /* FIXME: hash */
+       fprintf(f, "1"); /* TODO: certificavel */
+       fprintf(f, "%s", dec->contribuinte.dn);
+       fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
+       fprintf(f, "0"); /* TODO: resultado imposto */
+       fprintf(f, "S"); /* TODO: gerada? */
+       fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */
+       fprintf(f, "2"); /* TODO: PGD */
+       fprintf(f, "LINUX         "); /* TODO: SO */
+       fprintf(f, "3.16.0-"); /* TODO: versao SO */
+       fprintf(f, "1.7.0_75 "); /* TODO: versao JVM */
+       fprintf(f, "%-10.10s", dec->retifica ? : ""); /* TODO: recibo da última declaração transmitida (recibo ou retifica?) */
+       fprintf(f, "%04d", dec->contribuinte.cd_municipio);
+       fprintf(f, "           "); /* TODO: CPF conjuge */
+       fprintf(f, "0"); /* TODO: obrigatoria entrega */
+       fprintf(f, "0000000000000"); /* TODO: imposto devido, carne-leao, complementar dos dependentes */
+       fprintf(f, "%-10.10s", dec->recibo); /* recibo ex. anterior */
+       fprintf(f, "2"); /* TODO: indicador seguranca */
+       fprintf(f, "00"); /* TODO: indicador imposto pago */
+       fprintf(f, "0"); /* TODO: indicador imposto antecipado */
+       fprintf(f, "0"); /* TODO: mudanca endereco */
+       fprintf(f, "%s", dec->contribuinte.cep);
+       fprintf(f, "0"); /* TODO: debito 1a quota */
+       fprintf(f, "000"); /* TODO: no banco */
+       fprintf(f, "0000"); /* TODO: agencia */
+       fprintf(f, " "); /* TODO: filler */
+       fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */
+       fprintf(f, "0000000000000"); /* TODO: imposto a pagar */
+       fprintf(f, " "); /* RRA1 */
+       fprintf(f, "%-11.11s", ""); /* CPF RRA1 */
+       fprintf(f, " "); /* RRA2 */
+       fprintf(f, "%-11.11s", ""); /* CPF RRA2 */
+       fprintf(f, " "); /* RRA3 */
+       fprintf(f, "%-11.11s", ""); /* CPF RRA3 */
+       fprintf(f, " "); /* RRA4 */
+       fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
+       fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 2 */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 3 */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 4 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */
+       fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */
+       fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */
+       fprintf(f, "%-14.14s", ""); /* TODO: medico maior */
+       fprintf(f, "%-14.14s", ""); /* TODO: medico maior 2 */
+       fprintf(f, "%-11.11s", ""); /* CPF pensao */
+       fprintf(f, "%-11.11s", ""); /* CPF inventariante */
+       fprintf(f, "%-40.40s", dec->contribuinte.municipio);
+       fprintf(f, "%-60.60s", dec->nome);
+       fprintf(f, "%-11.11s", ""); /* CPF empregada */
+       fprintf(f, "000000000000"); /* FIXME: MAC */
+       fprintf(f, "%08d", 0); /* Data saída */
+       fprintf(f, "%-11.11s", ""); /* CPF procurador */
+       fprintf(f, "000"); /* TODO: criterio obrigatoriedade */
+       fprintf(f, "%013lld", dec->totalrendimento); /* Total tributavel */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia 2 */
+       fprintf(f, "%013lld", 0); /* TODO: Total isentos */
+       fprintf(f, "%013lld", 0); /* TODO: Total exclusivo */
+       fprintf(f, "%013lld", 0); /* TODO: Total pagamentos */
+       fprintf(f, "%-13.13s", ""); /* TODO: numero conta */
+       fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
+       fprintf(f, "0"); /* TODO: revalidar DV conta */
+
+       /* Mudanças de 2015 */
+
+       fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* TODO: natureza ocupacao */
+       fprintf(f, "%-11.11s", ""); /* CPF domestica */
+       fprintf(f, "%-11.11s", ""); /* NI domestica */
+       fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */
+       fprintf(f, "%-11.11s", ""); /* NI domestica 2 */
+       fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */
+       fprintf(f, "%-11.11s", ""); /* NI domestica 3 */
+       fprintf(f, "1"); /* Declaracao iniciada */
+       fprintf(f, "1"); /* Utilizou PGD */
+       fprintf(f, "0"); /* Utilizou APP */
+       fprintf(f, "0"); /* Utilizou online */
+       fprintf(f, "0"); /* Utilizou rascunho */
+       fprintf(f, "0"); /* Utilizou pre preenchida */
+       fprintf(f, "0"); /* Utilizou fontes */
+       fprintf(f, "0"); /* Utilizou plano saude */
+       fprintf(f, "0"); /* Utilizou recuperar */
+       fprintf(f, "1"); /* Declaracao iniciada */
+
+       /* Fim das mudanças de 2015 */
+
+       fprintf(f, "   "); /* Teste PGD, FILLER2 */
+       fprintf(f, "0000000000"); /* FIXME: controle */
+       fprintf(f, "\r\n");
+}
+
+static int gera_contribuinte(struct declaracao *dec, FILE *f)
+{
+       fprintf(f, "16");
+       fprintf(f, "%s", dec->cpf);
+       fprintf(f, "%-60.60s", dec->nome);
+       fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
+       fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
+       fprintf(f, "%-6.6s", dec->contribuinte.numero);
+       fprintf(f, "%-21.21s", dec->contribuinte.complemento);
+       fprintf(f, "%-19.19s", dec->contribuinte.bairro);
+       fprintf(f, "%-9.9s", dec->contribuinte.cep);
+       fprintf(f, "%04d", dec->contribuinte.cd_municipio);
+       fprintf(f, "%-40.40s", dec->contribuinte.municipio);
+       fprintf(f, "%-2.2s", dec->contribuinte.uf);
+       fprintf(f, "   "); /* TODO: código exterior */
+       fprintf(f, "105"); /* TODO: código país */
+       fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */
+       fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */
+       fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */
+       fprintf(f, "%-4.4s", dec->contribuinte.ddd);
+       fprintf(f, "%-9.9s", dec->contribuinte.telefone);
+       fprintf(f, "%-8.8s", dec->contribuinte.dn);
+       fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor);
+       fprintf(f, "%03d", dec->contribuinte.ocupacao_principal);
+       fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao);
+       fprintf(f, "0"); /* TODO: qnt quotas */
+       fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
+       fprintf(f, "%s", dec->retifica ? "S" : "N");
+       fprintf(f, "S"); /* GERADO */
+       fprintf(f, "N"); /* TODO: mudança de endereço */
+       fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */
+       fprintf(f, "%-3.3s", "000"); /* TODO: banco */
+       fprintf(f, "%-4.4s", "0000"); /* TODO: agência */
+       fprintf(f, "N"); /* TODO: deficiência */
+       fprintf(f, "0"); /* FILLER: pré-preenchida */
+       fprintf(f, "%-13.13s", ""); /* TODO: conta */
+       fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
+       fprintf(f, "N"); /* TODO: débito automático */
+       fprintf(f, "0"); /* TODO: débito primeira quota */
+       fprintf(f, "%-14.14s", ""); /* TODO: CNPJ principal fonte */
+       fprintf(f, "%-10.10s", dec->recibo); /* Recibo última declaração ano anterior */
+       fprintf(f, "A"); /* Declaração de Ajuste */
+       fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */
+       fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
+       fprintf(f, "\r\n");
+}
+
+static int gera(struct declaracao *dec, char *filename)
+{
+       FILE *f;
+       f = fopen(filename, "w");
+       if (!f)
+               return -errno;
+       gera_header(dec, f);
+       gera_contribuinte(dec, f);
+       fclose(f);
+       return 0;
+}
+
+static int run_gera(struct declaracao *dec, char **args, int argc)
+{
+       if (argc != 2)
+               return -EINVAL;
+       return gera(dec, args[1]);
+}
+
+static struct cmd cmd_gera = {
+       .name = "gera",
+       .run = run_gera,
+};
+
+int gera_cmd_init(void)
+{
+       cmd_add(&cmd_gera);
+       return 0;
+}
diff --git a/gera.h b/gera.h
new file mode 100644 (file)
index 0000000..f97ab5a
--- /dev/null
+++ b/gera.h
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _GERA_H
+#define _GERA_H
+
+int gera_cmd_init(void);
+
+#endif