From 6612f883a37af7273563d908d9dece30e27631f8 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 5 Sep 2015 22:37:46 -0300 Subject: [PATCH] Adiciona suporte a linha de dependentes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit A linha 25 é utilizada para listar os dependentes. O suporte completo a dependentes ainda exige tanto o cálculo de dedução, quanto a lista de demais campos para dependentes, como rendimentos e pagamentos, seja o dependente o autor ou o beneficiário. Vários totais também dependem destes campos. --- lib/Makefile.am | 3 +- lib/declaracao.c | 7 ++++ lib/declaracao.h | 1 + lib/dependente.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/dependente.h | 37 ++++++++++++++++++ lib/gera.c | 21 +++++++++++ src/declara.c | 2 + 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 lib/dependente.c create mode 100644 lib/dependente.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 9efbb78..f0fc33a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -13,4 +13,5 @@ libreceita_la_SOURCES = declaracao.c declaracao.h \ rendimento.c rendimento.h \ isento.c isento.h \ pagamento.c pagamento.h \ - bem.c bem.h + bem.c bem.h \ + dependente.c dependente.h diff --git a/lib/declaracao.c b/lib/declaracao.c index e4afe91..b1cfb55 100644 --- a/lib/declaracao.c +++ b/lib/declaracao.c @@ -25,6 +25,7 @@ #include "isento.h" #include "pagamento.h" #include "bem.h" +#include "dependente.h" #include "pmhash.h" struct declaracao * declaracao_new(int ano) @@ -47,6 +48,9 @@ struct declaracao * declaracao_new(int ano) dec->bens = list_new(); if (!dec->bens) goto out_bens; + dec->dependentes = list_new(); + if (!dec->dependentes) + goto out_dependentes; dec->totais = pmhash_new(); if (!dec->totais) goto out_totais; @@ -56,6 +60,8 @@ struct declaracao * declaracao_new(int ano) dec->tipo = SIMPLES; return dec; out_totais: + list_free(dec->dependentes, dependente_free); +out_dependentes: list_free(dec->bens, bem_free); out_bens: list_free(dec->pagamentos, pagamento_free); @@ -90,6 +96,7 @@ void declaracao_free(struct declaracao *dec) list_free(dec->isentos, isento_free); list_free(dec->pagamentos, pagamento_free); list_free(dec->bens, bem_free); + list_free(dec->dependentes, dependente_free); pmhash_del(dec->totais); sistema_free(dec); free(dec); diff --git a/lib/declaracao.h b/lib/declaracao.h index e94fd41..be91d96 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -36,6 +36,7 @@ struct declaracao { struct list *isentos; struct list *pagamentos; struct list *bens; + struct list *dependentes; struct contribuinte contribuinte; struct sistema sistema; long long pago; diff --git a/lib/dependente.c b/lib/dependente.c new file mode 100644 index 0000000..20dfa98 --- /dev/null +++ b/lib/dependente.c @@ -0,0 +1,98 @@ +/* + * 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 "dependente.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" + +void dependente_free(void *pointer) +{ + struct dependente *dependente = pointer; + if (dependente->nome) + free(dependente->nome); + if (dependente->dn) + free(dependente->dn); + if (dependente->cpf) + free(dependente->cpf); + free(dependente); +} + +static struct dependente * dependente_new(char **args) +{ + struct dependente *dependente; + int r = 0; + dependente = malloc(sizeof(*dependente)); + /* TODO: consertar set_int para funcionar como set_llong */ + r += set_int(args, 2, &dependente->codigo); + dependente->nome = strdup(args[2]); + dependente->dn = strdup(args[3]); + dependente->cpf = strdup(args[4]); + if (!dependente->nome || !dependente->dn || !dependente->cpf) { + dependente_free(dependente); + return NULL; + } + if (r < 0 || dependente->codigo < 0) { + dependente_free(dependente); + return NULL; + } + return dependente; +} + +static int run_dependente(struct declaracao *dec, char **args, int argc) +{ + struct dependente *dependente; + int r; + if (argc != 5) + return -EINVAL; + dependente = dependente_new(args); + if (!dependente) + return -ENOMEM; + r = list_add(&dec->dependentes, dependente); + if (r < 0) { + dependente_free(dependente); + return r; + } + return 0; +} + +void dependente_salva(struct declaracao *dec, FILE *f) +{ + int i; + struct dependente *j; + for (i = 0; j = list_get(dec->dependentes, i); i++) { + fprintf(f, "dependente %d \"%s\" \"%s\" \"%s\"\n", + j->codigo, j->nome, j->dn, j->cpf); + } +} + +static struct cmd cmd_dependente = { + .name = "dependente", + .run = run_dependente, +}; + +int dependente_cmd_init(void) +{ + cmd_add(&cmd_dependente); + return 0; +} diff --git a/lib/dependente.h b/lib/dependente.h new file mode 100644 index 0000000..796081a --- /dev/null +++ b/lib/dependente.h @@ -0,0 +1,37 @@ +/* + * 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 _DEPENDENTE_H +#define _DEPENDENTE_H + +#include +#include "declaracao.h" + +struct dependente { + int codigo; + char *nome; + char *dn; + char *cpf; +}; + +void dependente_salva(struct declaracao *dec, FILE *f); +void dependente_free(void *pointer); + +int dependente_cmd_init(void); + +#endif diff --git a/lib/gera.c b/lib/gera.c index 121805e..e16f49c 100644 --- a/lib/gera.c +++ b/lib/gera.c @@ -28,6 +28,7 @@ #include "isento.h" #include "pagamento.h" #include "bem.h" +#include "dependente.h" #include "totais.h" #include "sistema.h" @@ -503,6 +504,22 @@ static void gera_poupanca(struct declaracao *dec, FILE *f) gera_isento(dec, f, 98); } +static void gera_dependente(struct declaracao *dec, FILE *f) +{ + struct dependente *d; + d = list_get(dec->dependentes, dec->linhas[25]); + + fprintf(f, "25"); + fprintf(f, "%s", dec->cpf); + fprintf(f, "%05d", dec->linhas[25] + 1); + fprintf(f, "%02d", d->codigo); + fprintf(f, "%-60.60s", d->nome); + fprintf(f, "%-8.8s", d->dn); + fprintf(f, "%-11.11s", d->cpf); + /* TODO: Indicador de saída */ + fprintf(f, " "); +} + static void gera_bem(struct declaracao *dec, FILE *f) { struct bem *b; @@ -610,6 +627,7 @@ static int gera(struct declaracao *dec, char *filename) struct isento *isento; struct pagamento *pagamento; struct bem *bem; + struct dependente *dependente; struct list *linhas; char *buf; @@ -647,6 +665,9 @@ static int gera(struct declaracao *dec, char *filename) W(gera_isentos); W(gera_exclusivos); + for (i = 0; (dependente = list_get(dec->dependentes, i)); i++) { + W(gera_dependente); + } for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { W(gera_pagamento); } diff --git a/src/declara.c b/src/declara.c index c2a22bb..7677e22 100644 --- a/src/declara.c +++ b/src/declara.c @@ -31,6 +31,7 @@ #include "isento.h" #include "pagamento.h" #include "bem.h" +#include "dependente.h" #include "calcula.h" #include "gera.h" @@ -96,6 +97,7 @@ int main(int argc, char **argv) isento_cmd_init(); pagamento_cmd_init(); bem_cmd_init(); + dependente_cmd_init(); calcula_cmd_init(); gera_cmd_init(); sistema_cmd_init(); -- 2.20.1