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"
38 if (size > sizeof(buffer) - 2) \
40 if (pos + size >= len) \
42 memcpy(buffer, line + pos, size); \
46 #define COPYI(field, size) \
50 #define COPYL(field, size) \
52 field = strtoull(buffer, NULL, 10);
54 #define COPYS(field, size) \
56 field = strdup(buffer);
58 #define COPYDI(field, size) \
59 COPYI(dec->field, size)
61 #define COPYDL(field, size) \
62 COPYL(dec->field, size)
64 #define COPYDS(field, size) \
65 COPYS(dec->field, size)
67 #define COPYC(field, size) \
68 COPYS(dec->contribuinte.field, size)
70 static int importa_header(struct declaracao *dec, char *line, size_t len)
98 COPYDS(sistema.so, 14);
99 COPYDS(sistema.so_versao, 7);
100 COPYDS(sistema.jvm_versao, 9);
102 COPYDI(contribuinte.cd_municipio, 4);
108 //fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1));
136 COPYC(municipio, 40);
139 COPYDS(sistema.mac, 12);
152 COPYDI(contribuinte.natureza_ocupacao, 2);
156 /* Mudanças de 2016 */
158 if (dec->ano >= 2016) {
162 /* Fim das mudanças de 2016 */
169 static int importa_contribuinte(struct declaracao *dec, char *line, size_t len)
178 COPYC(tipo_logradouro, 15);
179 COPYC(logradouro, 40);
181 COPYC(complemento, 21);
185 COPYDI(contribuinte.cd_municipio, 4);
186 COPYC(municipio, 40);
191 if (dec->ano == 2015) {
193 } else if (dec->ano >= 2016) {
196 COPYDS(conjuge.cpf, 11);
198 if (dec->ano <= 2017) {
205 COPYC(titulo_eleitor, 13);
206 COPYDI(contribuinte.ocupacao_principal, 3);
207 COPYDI(contribuinte.natureza_ocupacao, 2);
213 COPYDS(retifica, 12);
218 COPYDS(contacorrente, 13);
226 if (dec->ano >= 2016)
228 if (dec->ano >= 2017) {
232 if (dec->ano >= 2018) {
240 static int importa_conjuge(struct declaracao *dec, char *line, size_t len)
247 COPYDS(conjuge.cpf, 11);
248 COPYDL(conjuge.base, 13);
249 COPYDL(conjuge.imposto, 13);
250 COPYDL(conjuge.isento, 13);
251 COPYDL(conjuge.exclusivo, 13);
252 COPYDL(conjuge.rendpj_exigibilidade_suspensa, 13);
253 COPYDL(conjuge.total, 13);
255 if (buffer[0] == 'S')
256 dec->conjuge.entregou = 1;
258 dec->conjuge.entregou = 0;
263 static int ignore_line(struct declaracao *dec, char *line, size_t len)
268 static int importa_rendimento(struct declaracao *dec, char *line, size_t len)
270 struct rendimento *rend;
275 rend = malloc(sizeof(*rend));
280 COPYS(rend->cnpj, 14);
281 COPYS(rend->nome, 60);
282 COPYL(rend->rendimento, 13);
283 COPYL(rend->previdencia, 13);
284 COPYL(rend->decimoterceiro, 13);
285 COPYL(rend->imposto, 13);
286 COPYS(rend->saida, 8);
287 COPYL(rend->imposto_13o, 13);
288 if ((r = list_add(&dec->rendimento, rend)) < 0) {
289 rendimento_free(rend);
295 static int importa_pagamento(struct declaracao *dec, char *line, size_t len)
302 p = malloc(sizeof(*p));
309 COPYI(p->dependente, 5);
314 COPYL(p->pagamento, 13);
315 COPYL(p->reembolso, 13);
316 if (dec->ano >= 2016) {
317 /* TODO: contribuição do ente público patrocinador (FUNPRESP?) */
322 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
324 if ((r = list_add(&dec->pagamentos, p)) < 0) {
331 static int importa_isento(struct declaracao *dec, char *line, size_t len)
338 i = malloc(sizeof(*i));
345 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
350 /* TODO: Suportar dependente */
352 if ((r = list_add(&dec->isentos, i)) < 0) {
359 static int importa_outrosisentos(struct declaracao *dec, char *line, size_t len)
366 r = importa_isento(dec, line, len);
371 i = list_get(dec->isentos, list_size(dec->isentos));
374 COPYI(i->exclusivo, 1);
376 COPYS(i->descricao, 60);
380 static int importa_isento2(struct declaracao *dec, char *line, size_t len)
387 i = malloc(sizeof(*i));
393 /* TODO: Suporte a dependente */
401 if ((r = list_add(&dec->isentos, i)) < 0) {
409 static int importa_dependente(struct declaracao *dec, char *line, size_t len)
411 struct dependente *d;
416 d = malloc(sizeof(*d));
428 if (dec->ano >= 2016) {
429 /* TODO: NIT/PIS/PASEP de Pessoa Física no exterior */
432 if ((r = list_add(&dec->dependentes, d)) < 0) {
439 static int importa_bem(struct declaracao *dec, char *line, size_t len)
446 b = malloc(sizeof(*b));
454 /* FIXME: exterior */
458 COPYS(b->descricao, 512);
459 COPYL(b->valor_anterior, 13);
463 COPYS(b->logradouro, 40);
465 COPYS(b->complemento, 40);
466 COPYS(b->bairro, 40);
469 COPYI(b->cd_municipio, 4);
470 COPYS(b->municipio, 40);
471 /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
473 COPYS(b->matricula, 40);
474 COPYS(b->registro, 40);
476 /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
478 COPYS(b->cartorio, 60);
480 /* Número de chave */
482 if ((r = list_add(&dec->bens, b)) < 0) {
489 static int importa_carne(struct declaracao *dec, char *line, size_t len)
496 carne = malloc(sizeof(*carne));
502 if (buffer[0] == 'S') {
506 COPYI(carne->mes, 2);
507 COPYL(carne->rendimento, 13);
508 COPYL(carne->alugueis, 13);
509 COPYL(carne->outros, 13);
510 COPYL(carne->exterior, 13);
511 COPYL(carne->caixa, 13);
512 COPYL(carne->alimentos, 13);
513 COPYL(carne->dependentes, 13);
514 COPYL(carne->previdencia, 13);
515 COPYL(carne->base, 13);
516 COPYL(carne->imposto, 13);
517 if ((r = list_add(&dec->carne, carne)) < 0) {
526 int (*func)(struct declaracao *, char *, size_t);
528 { "IRPF", importa_header },
529 { "16", importa_contribuinte },
530 { "17", ignore_line },
531 { "18", ignore_line },
532 { "19", ignore_line },
533 { "20", ignore_line },
534 { "21", importa_rendimento },
535 { "22", importa_carne },
536 { "23", ignore_line },
537 { "24", ignore_line },
538 { "25", importa_dependente },
539 { "26", importa_pagamento },
540 { "27", importa_bem },
541 { "29", importa_conjuge },
542 { "82", importa_isento },
543 { "84", importa_isento2 },
544 { "88", importa_isento2 },
545 { "93", importa_isento },
546 { "96", importa_isento },
547 { "97", importa_outrosisentos },
548 { "98", importa_isento },
549 { "99", importa_isento },
550 { "T9", ignore_line },
551 { "HR", ignore_line },
552 { "DR", ignore_line },
553 { "R9", ignore_line },
556 static int importa_linha(struct declaracao *dec, char *line, size_t len)
560 for (i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); i++)
561 if (!strncmp(prefixes[i].prefix, line, strlen(prefixes[i].prefix)))
562 return prefixes[i].func(dec, line, len);
567 static int importa(struct declaracao *dec, char *filename)
575 f = fopen(filename, "r");
578 dec_set_error(dec, "Não foi possível abrir arquivo %s: %s.",
579 filename, strerror(errno));
582 while ((r = getline(&line, &lsz, f)) > 0) {
583 r = importa_linha(dec, line, r);
585 fprintf(stderr, "Não foi possível importar linha %d: %s\n",
586 n, dec->error ?: strerror(-r));
597 static int run_importa(struct declaracao *dec, char **args, int argc)
600 dec_set_error(dec, "Comando %s recebe um nome de arquivo como parâmetro.",
604 return importa(dec, args[1]);
607 static struct cmd cmd_importa = {
612 int importa_cmd_init(void)
614 cmd_add(&cmd_importa);