2 * Copyright (C) 2015-2018 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "declaracao.h"
27 #include "rendimento.h"
30 #include "pagamento.h"
32 #include "dependente.h"
39 if (size > sizeof(buffer) - 2) \
41 if (pos + size >= len) \
43 memcpy(buffer, line + pos, size); \
47 #define COPYI(field, size) \
51 #define COPYL(field, size) \
53 field = strtoull(buffer, NULL, 10);
55 #define COPYS(field, size) \
57 field = strdup(buffer);
59 #define COPYA(attr, key, size) \
61 attr_set(&attr, key, buffer);
63 #define COPYDI(field, size) \
64 COPYI(dec->field, size)
66 #define COPYDL(field, size) \
67 COPYL(dec->field, size)
69 #define COPYDS(field, size) \
70 COPYS(dec->field, size)
72 #define COPYC(field, size) \
73 COPYS(dec->contribuinte.field, size)
75 static int importa_header(struct declaracao *dec, char *line, size_t len)
101 COPYDS(retifica, 10);
103 COPYDS(sistema.so, 14);
104 COPYDS(sistema.so_versao, 7);
105 COPYDS(sistema.jvm_versao, 9);
107 COPYDI(contribuinte.cd_municipio, 4);
113 //fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1));
141 COPYC(municipio, 40);
144 COPYDS(sistema.mac, 12);
157 COPYDI(contribuinte.natureza_ocupacao, 2);
161 /* Mudanças de 2016 */
163 if (dec->ano >= 2016) {
167 /* Fim das mudanças de 2016 */
174 static int importa_contribuinte(struct declaracao *dec, char *line, size_t len)
183 COPYC(tipo_logradouro, 15);
184 COPYC(logradouro, 40);
186 COPYC(complemento, 21);
190 COPYDI(contribuinte.cd_municipio, 4);
191 COPYC(municipio, 40);
196 if (dec->ano == 2015) {
198 } else if (dec->ano >= 2016) {
201 COPYDS(conjuge.cpf, 11);
203 if (dec->ano <= 2017) {
210 COPYC(titulo_eleitor, 13);
211 COPYDI(contribuinte.ocupacao_principal, 3);
212 COPYDI(contribuinte.natureza_ocupacao, 2);
218 COPYDS(retifica, 12);
223 COPYDS(contacorrente, 13);
231 if (dec->ano >= 2016)
233 if (dec->ano >= 2017) {
237 if (dec->ano >= 2018) {
245 static int importa_conjuge(struct declaracao *dec, char *line, size_t len)
252 COPYDS(conjuge.cpf, 11);
253 COPYDL(conjuge.base, 13);
254 COPYDL(conjuge.imposto, 13);
255 COPYDL(conjuge.isento, 13);
256 COPYDL(conjuge.exclusivo, 13);
257 COPYDL(conjuge.rendpj_exigibilidade_suspensa, 13);
258 COPYDL(conjuge.total, 13);
260 if (buffer[0] == 'S')
261 dec->conjuge.entregou = 1;
263 dec->conjuge.entregou = 0;
268 static int ignore_line(struct declaracao *dec, char *line, size_t len)
273 static int importa_rendimento(struct declaracao *dec, char *line, size_t len)
275 struct rendimento *rend;
280 rend = malloc(sizeof(*rend));
285 COPYS(rend->cnpj, 14);
286 COPYS(rend->nome, 60);
287 COPYL(rend->rendimento, 13);
288 COPYL(rend->previdencia, 13);
289 COPYL(rend->decimoterceiro, 13);
290 COPYL(rend->imposto, 13);
291 COPYS(rend->saida, 8);
292 COPYL(rend->imposto_13o, 13);
293 if ((r = list_add(&dec->rendimento, rend)) < 0) {
294 rendimento_free(rend);
300 static int importa_pagamento(struct declaracao *dec, char *line, size_t len)
307 p = malloc(sizeof(*p));
314 COPYI(p->dependente, 5);
319 COPYL(p->pagamento, 13);
320 COPYL(p->reembolso, 13);
321 if (dec->ano >= 2016) {
322 /* TODO: contribuição do ente público patrocinador (FUNPRESP?) */
327 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
329 if ((r = list_add(&dec->pagamentos, p)) < 0) {
336 static int importa_isento(struct declaracao *dec, char *line, size_t len)
343 i = malloc(sizeof(*i));
350 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
355 /* TODO: Suportar dependente */
357 if ((r = list_add(&dec->isentos, i)) < 0) {
364 static int importa_outrosisentos(struct declaracao *dec, char *line, size_t len)
371 r = importa_isento(dec, line, len);
376 i = list_get(dec->isentos, list_size(dec->isentos));
379 COPYI(i->exclusivo, 1);
381 COPYS(i->descricao, 60);
385 static int importa_isento2(struct declaracao *dec, char *line, size_t len)
392 i = malloc(sizeof(*i));
398 /* TODO: Suporte a dependente */
406 if ((r = list_add(&dec->isentos, i)) < 0) {
414 static int importa_dependente(struct declaracao *dec, char *line, size_t len)
416 struct dependente *d;
421 d = malloc(sizeof(*d));
433 if (dec->ano >= 2016) {
434 /* TODO: NIT/PIS/PASEP de Pessoa Física no exterior */
437 if ((r = list_add(&dec->dependentes, d)) < 0) {
444 static int importa_bem(struct declaracao *dec, char *line, size_t len)
451 b = malloc(sizeof(*b));
459 /* FIXME: exterior */
463 COPYS(b->descricao, 512);
464 COPYL(b->valor_anterior, 13);
468 COPYA(b->attr, "logradouro", 40);
469 COPYA(b->attr, "numero", 6);
470 COPYA(b->attr, "complemento", 40);
471 COPYA(b->attr, "bairro", 40);
472 COPYA(b->attr, "cep", 9);
473 COPYA(b->attr, "uf", 2);
474 COPYA(b->attr, "cd_municipio", 4);
475 COPYA(b->attr, "municipio", 40);
476 /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
478 COPYA(b->attr, "matricula", 40);
479 COPYA(b->attr, "registro", 40);
480 COPYA(b->attr, "area", 11);
481 /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
483 COPYA(b->attr, "cartorio", 60);
485 /* Número de chave */
487 if ((r = list_add(&dec->bens, b)) < 0) {
494 static int importa_carne(struct declaracao *dec, char *line, size_t len)
501 carne = malloc(sizeof(*carne));
507 if (buffer[0] == 'S') {
511 COPYI(carne->mes, 2);
512 COPYL(carne->rendimento, 13);
513 COPYL(carne->alugueis, 13);
514 COPYL(carne->outros, 13);
515 COPYL(carne->exterior, 13);
516 COPYL(carne->caixa, 13);
517 COPYL(carne->alimentos, 13);
518 COPYL(carne->dependentes, 13);
519 COPYL(carne->previdencia, 13);
520 COPYL(carne->base, 13);
521 COPYL(carne->imposto, 13);
522 if ((r = list_add(&dec->carne, carne)) < 0) {
531 int (*func)(struct declaracao *, char *, size_t);
533 { "IRPF", importa_header },
534 { "16", importa_contribuinte },
535 { "17", ignore_line },
536 { "18", ignore_line },
537 { "19", ignore_line },
538 { "20", ignore_line },
539 { "21", importa_rendimento },
540 { "22", importa_carne },
541 { "23", ignore_line },
542 { "24", ignore_line },
543 { "25", importa_dependente },
544 { "26", importa_pagamento },
545 { "27", importa_bem },
546 { "29", importa_conjuge },
547 { "82", importa_isento },
548 { "84", importa_isento2 },
549 { "88", importa_isento2 },
550 { "93", importa_isento },
551 { "96", importa_isento },
552 { "97", importa_outrosisentos },
553 { "98", importa_isento },
554 { "99", importa_isento },
555 { "T9", ignore_line },
556 { "HR", ignore_line },
557 { "DR", ignore_line },
558 { "R9", ignore_line },
561 static int importa_linha(struct declaracao *dec, char *line, size_t len)
565 for (i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); i++)
566 if (!strncmp(prefixes[i].prefix, line, strlen(prefixes[i].prefix)))
567 return prefixes[i].func(dec, line, len);
572 static int importa(struct declaracao *dec, char *filename)
580 f = fopen(filename, "r");
583 dec_set_error(dec, "Não foi possível abrir arquivo %s: %s.",
584 filename, strerror(errno));
587 while ((r = getline(&line, &lsz, f)) > 0) {
588 r = importa_linha(dec, line, r);
590 fprintf(stderr, "Não foi possível importar linha %d: %s\n",
591 n, dec->error ?: strerror(-r));
602 static int run_importa(struct declaracao *dec, char **args, int argc)
605 dec_set_error(dec, "Comando %s recebe um nome de arquivo como parâmetro.",
609 return importa(dec, args[1]);
612 static struct cmd cmd_importa = {
617 int importa_cmd_init(void)
619 cmd_add(&cmd_importa);