From fae07989c1dc84e67ba6517b1fff984b0fe911b2 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 8 Aug 2015 16:55:06 -0300 Subject: [PATCH] =?utf8?q?Rendimentos=20isentos=20e=20de=20tributa=C3=A7?= =?utf8?q?=C3=A3o=20exclusiva.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Alguns registros dos dois tipos exigem os mesmos campos. No momento, apenas poupança e participação nos lucros e resultados são suportados. Suporte a novos registros pode ou não ser trivial, dependendo dos campos exigidos pelo registro. --- lib/Makefile.am | 1 + lib/declaracao.c | 7 +++ lib/declaracao.h | 1 + lib/gera.c | 40 +++++++++++++ lib/isento.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++ lib/isento.h | 39 +++++++++++++ src/declara.c | 2 + 7 files changed, 234 insertions(+) create mode 100644 lib/isento.c create mode 100644 lib/isento.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 459b497..68b9f89 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -10,5 +10,6 @@ libreceita_la_SOURCES = declaracao.c declaracao.h \ util.c util.h \ contribuinte.c contribuinte.h \ rendimento.c rendimento.h \ + isento.c isento.h \ pagamento.c pagamento.h \ bem.c bem.h diff --git a/lib/declaracao.c b/lib/declaracao.c index 4c5f188..9d36937 100644 --- a/lib/declaracao.c +++ b/lib/declaracao.c @@ -21,6 +21,7 @@ #include #include "list.h" #include "rendimento.h" +#include "isento.h" #include "pagamento.h" #include "bem.h" #include "pmhash.h" @@ -36,6 +37,9 @@ struct declaracao * declaracao_new(int ano) dec->rendimento = list_new(); if (!dec->rendimento) goto out_rendimento; + dec->isentos = list_new(); + if (!dec->isentos) + goto out_isentos; dec->pagamentos = list_new(); if (!dec->pagamentos) goto out_pagamentos; @@ -55,6 +59,8 @@ out_totais: out_bens: list_free(dec->pagamentos, pagamento_free); out_pagamentos: + list_free(dec->isentos, isento_free); +out_isentos: list_free(dec->rendimento, rendimento_free); out_rendimento: free(dec); @@ -80,6 +86,7 @@ void declaracao_free(struct declaracao *dec) if (dec->dvconta) free(dec->dvconta); list_free(dec->rendimento, rendimento_free); + list_free(dec->isentos, isento_free); list_free(dec->pagamentos, pagamento_free); list_free(dec->bens, bem_free); pmhash_del(dec->totais); diff --git a/lib/declaracao.h b/lib/declaracao.h index 2dfe454..bdd2a3a 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -32,6 +32,7 @@ struct declaracao { char *cpf; char *nome; struct list *rendimento; + struct list *isentos; struct list *pagamentos; struct list *bens; struct contribuinte contribuinte; diff --git a/lib/gera.c b/lib/gera.c index dc4a92e..f5515d7 100644 --- a/lib/gera.c +++ b/lib/gera.c @@ -25,6 +25,7 @@ #include "declaracao.h" #include "cmd.h" #include "rendimento.h" +#include "isento.h" #include "pagamento.h" #include "bem.h" #include "totais.h" @@ -414,6 +415,30 @@ static void gera_pagamento(struct declaracao *dec, FILE *f) fprintf(f, "T"); } +static void gera_isento(struct declaracao *dec, FILE *f, int codigo) +{ + struct isento *i; + i = isento_get(dec, codigo, dec->linhas[codigo]); + fprintf(f, "%02d", codigo); + fprintf(f, "%-11.11s", dec->cpf); /* Titular, TODO: dependente */ + fprintf(f, "%05d", dec->linhas[codigo] + 1); /* Chave */ + fprintf(f, "%c", 'T'); /* FIXME: dependente */ + fprintf(f, "%-14.14s", i->cnpj); + fprintf(f, "%-60.60s", i->nome); + fprintf(f, "%013lld", i->valor); + fprintf(f, "%-11.11s", dec->cpf); +} + +static void gera_plr(struct declaracao *dec, FILE *f) +{ + gera_isento(dec, f, 96); +} + +static void gera_poupanca(struct declaracao *dec, FILE *f) +{ + gera_isento(dec, f, 98); +} + static void gera_bem(struct declaracao *dec, FILE *f) { struct bem *b; @@ -491,6 +516,7 @@ static int gera(struct declaracao *dec, char *filename) int r; int i; struct rendimento *rendimento; + struct isento *isento; struct pagamento *pagamento; struct bem *bem; @@ -523,10 +549,24 @@ static int gera(struct declaracao *dec, char *filename) for (i = 0; (bem = list_get(dec->bens, i)); i++) { W(gera_bem, dec, f); } + + /* Rendimentos isentos e com tributação exclusiva */ + /* Registros 82 a 89, e 92 a 99 */ +#define IW(fn, codigo) \ + for (i = 0; ; i++) { \ + isento = isento_get(dec, codigo, i); \ + if (!isento) \ + break; \ + W(fn, dec, f); \ + } + IW(gera_plr, 96); + IW(gera_poupanca, 98); + W(gera_trailler, dec, f); W(gera_reciboheader, dec, f); W(gera_recibodetalhe, dec, f); W(gera_recibotrailler, dec, f); + fclose(f); return 0; out: diff --git a/lib/isento.c b/lib/isento.c new file mode 100644 index 0000000..c655f48 --- /dev/null +++ b/lib/isento.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * 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 "isento.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" +#include "totais.h" + +static int isento_totais_update(struct declaracao *dec, struct isento *isento) +{ + int r = 0; + switch (isento->codigo) { + case 96: + r = totais_add(dec, "PLR", isento->valor); + r += totais_add(dec, "EXCLUSIVOS", isento->valor); + r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor); + break; + case 98: + r = totais_add(dec, "POUPANCA", isento->valor); + r += totais_add(dec, "ISENTOS", isento->valor); + r += totais_add(dec, "ISENTOSTIT", isento->valor); + break; + } + return r; +} + +void isento_free(void *pointer) +{ + struct isento *isento = pointer; + if (isento->cnpj) + free(isento->cnpj); + if (isento->nome) + free(isento->nome); + free(isento); +} + +static int isento_cmp(void *p1, void *p2) +{ + struct isento *r1 = p1; + struct isento *r2 = p2; + /* O rendimento maior vem primeiro. */ + if (r1->valor > r2->valor) + return -1; + else if (r1->valor < r2->valor) + return 1; + return 0; +} + +static struct isento * isento_new(char **args) +{ + struct isento *isento; + int r = 0; + isento = malloc(sizeof(*isento)); + isento->cnpj = strdup(args[2]); + isento->nome = strdup(args[3]); + /* TODO: consertar set_int para funcionar como set_llong */ + r += set_int(args, 2, &isento->codigo); + r += set_llong(args[4], &isento->valor); + if (!isento->cnpj || !isento->nome) { + isento_free(isento); + return NULL; + } + if (r < 0 || isento->codigo < 0 || + isento->valor < 0) { + isento_free(isento); + return NULL; + } + return isento; +} + +static int run_isento(struct declaracao *dec, char **args, int argc) +{ + struct isento *isento; + int r; + if (argc != 5) + return -EINVAL; + isento = isento_new(args); + if (!isento) + return -ENOMEM; + r = list_insert_ordered(&dec->isentos, isento, isento_cmp); + if (r < 0) { + isento_free(isento); + return r; + } + r = isento_totais_update(dec, isento); + if (r) { + isento_free(isento); + return r; + } + return 0; +} + +void isento_salva(struct declaracao *dec, FILE *f) +{ + int i; + struct isento *j; + for (i = 0; j = list_get(dec->isentos, i); i++) + fprintf(f, "isento %d \"%s\" \"%s\" %lld\n", + j->codigo, j->cnpj, j->nome, j->valor); +} + +static struct cmd cmd_isento = { + .name = "isento", + .run = run_isento, +}; + +int isento_cmd_init(void) +{ + cmd_add(&cmd_isento); + return 0; +} + +struct isento * isento_get(struct declaracao *dec, int codigo, int n) +{ + struct isento *isento; + int i; + int j = 0; + for (i = 0; (isento = list_get(dec->isentos, i)); i++) { + if (isento->codigo == codigo && j++ == n) + break; + } + return isento; +} diff --git a/lib/isento.h b/lib/isento.h new file mode 100644 index 0000000..32897ac --- /dev/null +++ b/lib/isento.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * 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 _ISENTO_H +#define _ISENTO_H + +#include +#include "declaracao.h" + +struct isento { + int codigo; + char *cnpj; + char *nome; + long long valor; +}; + +void isento_salva(struct declaracao *dec, FILE *f); +void isento_free(void *pointer); + +int isento_cmd_init(void); + +struct isento * isento_get(struct declaracao *dec, int codigo, int n); + +#endif diff --git a/src/declara.c b/src/declara.c index b16090c..130826a 100644 --- a/src/declara.c +++ b/src/declara.c @@ -28,6 +28,7 @@ #include "base.h" #include "contribuinte.h" #include "rendimento.h" +#include "isento.h" #include "pagamento.h" #include "calcula.h" #include "gera.h" @@ -91,6 +92,7 @@ int main(int argc, char **argv) base_cmd_init(); contribuinte_cmd_init(); rendimento_cmd_init(); + isento_cmd_init(); pagamento_cmd_init(); bem_cmd_init(); calcula_cmd_init(); -- 2.20.1