From 5f22fbc13d58b333a22fed62c1614be2b84e69de Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sun, 2 Aug 2015 10:36:46 -0300 Subject: [PATCH] =?utf8?q?Suporta=20bens=20m=C3=B3veis.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit O comando bem permite adicionar bens móveis. A linha de bens, de número 27, é gerada. O suporte a bens imóveis está iniciado, mas ainda é necessária a implementação do comando que recebe todos os parâmetros exigidos. --- lib/Makefile.am | 3 +- lib/bem.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++ lib/bem.h | 53 +++++++++++++++++++ lib/declaracao.c | 7 +++ lib/declaracao.h | 1 + lib/gera.c | 48 +++++++++++++++++ src/declara.c | 1 + 7 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 lib/bem.c create mode 100644 lib/bem.h diff --git a/lib/Makefile.am b/lib/Makefile.am index a8f050d..459b497 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -10,4 +10,5 @@ libreceita_la_SOURCES = declaracao.c declaracao.h \ util.c util.h \ contribuinte.c contribuinte.h \ rendimento.c rendimento.h \ - pagamento.c pagamento.h + pagamento.c pagamento.h \ + bem.c bem.h diff --git a/lib/bem.c b/lib/bem.c new file mode 100644 index 0000000..70828ba --- /dev/null +++ b/lib/bem.c @@ -0,0 +1,135 @@ +/* + * 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 "bem.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" +#include "totais.h" + +void bem_free(void *pointer) +{ + struct bem *bem = pointer; + if (bem->descricao) + free(bem->descricao); + /* Imóvel */ + if (bem->logradouro) + free(bem->logradouro); + if (bem->numero) + free(bem->numero); + if (bem->complemento) + free(bem->complemento); + if (bem->bairro) + free(bem->bairro); + if (bem->cep) + free(bem->cep); + if (bem->uf) + free(bem->uf); + if (bem->municipio) + free(bem->municipio); + if (bem->matricula) + free(bem->matricula); + if (bem->registro) + free(bem->registro); + if (bem->cartorio) + free(bem->cartorio); + + free(bem); +} + +static int bem_cmp(void *p1, void *p2) +{ + struct bem *b1 = p1; + struct bem *b2 = p2; + /* O bem de valor maior vem primeiro. */ + if (b1->valor > b2->valor) + return -1; + else if (b1->valor < b2->valor) + return 1; + return 0; +} + +static struct bem * bem_new(char **args) +{ + struct bem *bem; + int r = 0; + bem = malloc(sizeof(*bem)); + bem->descricao = strdup(args[2]); + /* TODO: consertar set_int para funcionar como set_llong */ + r += set_int(args, 2, &bem->codigo); + r += set_llong(args[3], &bem->valor_anterior); + r += set_llong(args[4], &bem->valor); + if (!bem->descricao) { + bem_free(bem); + return NULL; + } + if (r < 0 || bem->codigo < 0 || + bem->valor_anterior < 0 || bem->valor < 0) { + bem_free(bem); + return NULL; + } + return bem; +} + +static int run_bem(struct declaracao *dec, char **args, int argc) +{ + struct bem *bem; + int r; + if (argc != 5) + return -EINVAL; + bem = bem_new(args); + if (!bem) + return -ENOMEM; + r = list_insert_ordered(&dec->bens, bem, bem_cmp); + if (r < 0) { + bem_free(bem); + return r; + } + r = totais_add(dec, "BENSANTERIOR", bem->valor_anterior); + r += totais_add(dec, "BENS", bem->valor); + if (r) { + bem_free(bem); + return r; + } + return 0; +} + +void bem_salva(struct declaracao *dec, FILE *f) +{ + int i; + struct bem *j; + for (i = 0; j = list_get(dec->bens, i); i++) + fprintf(f, "bem %d \"%s\" %lld %lld\n", + j->codigo, j->descricao, j->valor_anterior, j->valor); +} + +static struct cmd cmd_bem = { + .name = "bem", + .run = run_bem, +}; + +int bem_cmd_init(void) +{ + cmd_add(&cmd_bem); + return 0; +} diff --git a/lib/bem.h b/lib/bem.h new file mode 100644 index 0000000..6656ffa --- /dev/null +++ b/lib/bem.h @@ -0,0 +1,53 @@ +/* + * 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 _BEM_H +#define _BEM_H + +#include +#include "declaracao.h" + +struct bem { + int codigo; + char *descricao; + long long valor_anterior; + long long valor; + + /* Endereço Imóvel */ + char *logradouro; + char *numero; + char *complemento; + char *bairro; + char *cep; + char *uf; + int cd_municipio; + char *municipio; + + /* Imóvel */ + char *matricula; + char *registro; + long long area; + char *cartorio; +}; + +void bem_salva(struct declaracao *dec, FILE *f); +void bem_free(void *pointer); + +int bem_cmd_init(void); + +#endif diff --git a/lib/declaracao.c b/lib/declaracao.c index 53ff1e4..4c5f188 100644 --- a/lib/declaracao.c +++ b/lib/declaracao.c @@ -22,6 +22,7 @@ #include "list.h" #include "rendimento.h" #include "pagamento.h" +#include "bem.h" #include "pmhash.h" struct declaracao * declaracao_new(int ano) @@ -38,6 +39,9 @@ struct declaracao * declaracao_new(int ano) dec->pagamentos = list_new(); if (!dec->pagamentos) goto out_pagamentos; + dec->bens = list_new(); + if (!dec->bens) + goto out_bens; dec->totais = pmhash_new(); if (!dec->totais) goto out_totais; @@ -47,6 +51,8 @@ struct declaracao * declaracao_new(int ano) dec->tipo = SIMPLES; return dec; out_totais: + list_free(dec->bens, bem_free); +out_bens: list_free(dec->pagamentos, pagamento_free); out_pagamentos: list_free(dec->rendimento, rendimento_free); @@ -75,6 +81,7 @@ void declaracao_free(struct declaracao *dec) free(dec->dvconta); list_free(dec->rendimento, rendimento_free); list_free(dec->pagamentos, pagamento_free); + list_free(dec->bens, bem_free); pmhash_del(dec->totais); free(dec); } diff --git a/lib/declaracao.h b/lib/declaracao.h index 1694661..2dfe454 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -33,6 +33,7 @@ struct declaracao { char *nome; struct list *rendimento; struct list *pagamentos; + struct list *bens; struct contribuinte contribuinte; long long pago; long long retido; diff --git a/lib/gera.c b/lib/gera.c index 09c1bdd..7965b23 100644 --- a/lib/gera.c +++ b/lib/gera.c @@ -25,6 +25,7 @@ #include "cmd.h" #include "rendimento.h" #include "pagamento.h" +#include "bem.h" #include "totais.h" static void gera_header(struct declaracao *dec, FILE *f) @@ -436,6 +437,49 @@ static void gera_pagamento(struct declaracao *dec, FILE *f) fprintf(f, "\r\n"); } +static void gera_bem(struct declaracao *dec, FILE *f) +{ + struct bem *b; + b = list_get(dec->bens, dec->linhas[27]); + + fprintf(f, "27"); + fprintf(f, "%s", dec->cpf); + fprintf(f, "%02d", b->codigo); + + /* FIXME: exterior */ + fprintf(f, "0"); + fprintf(f, "105"); /* código Brasil */ + + fprintf(f, "%-512.512s", b->descricao); + fprintf(f, "%013lld", b->valor_anterior); + fprintf(f, "%013lld", b->valor); + + /* Imóvel */ + fprintf(f, "%-40.40s", b->logradouro ?: ""); + fprintf(f, "%-6.6s", b->numero ?: ""); + fprintf(f, "%-40.40s", b->complemento ?: ""); + fprintf(f, "%-40.40s", b->bairro ?: ""); + fprintf(f, "%-9.9s", b->cep ?: ""); + fprintf(f, "%-2.2s", b->uf ?: ""); + fprintf(f, "%04d", b->cd_municipio); + fprintf(f, "%-40.40s", b->municipio ?: ""); + /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */ + fprintf(f, "%d", b->registro ? 1 : 0); + fprintf(f, "%-40.40s", b->matricula ?: ""); + fprintf(f, "%-40.40s", b->registro ?: ""); + fprintf(f, "%011lld", b->area); + /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */ + fprintf(f, "%d", 0); + fprintf(f, "%-60.60s", b->cartorio ?: ""); + + /* Número de chave */ + fprintf(f, "%05d", dec->linhas[27] + 1); + + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + + typedef void (gera_linha)(struct declaracao *dec, FILE *f); @@ -469,6 +513,7 @@ static int gera(struct declaracao *dec, char *filename) int i; struct rendimento *rendimento; struct pagamento *pagamento; + struct bem *bem; #define W(fn, dec, f) \ do { \ @@ -496,6 +541,9 @@ static int gera(struct declaracao *dec, char *filename) for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { W(gera_pagamento, dec, f); } + for (i = 0; (bem = list_get(dec->bens, i)); i++) { + W(gera_bem, dec, f); + } W(gera_trailler, dec, f); W(gera_reciboheader, dec, f); W(gera_recibodetalhe, dec, f); diff --git a/src/declara.c b/src/declara.c index 8db644a..b16090c 100644 --- a/src/declara.c +++ b/src/declara.c @@ -92,6 +92,7 @@ int main(int argc, char **argv) contribuinte_cmd_init(); rendimento_cmd_init(); pagamento_cmd_init(); + bem_cmd_init(); calcula_cmd_init(); gera_cmd_init(); -- 2.20.1