util.c util.h \
contribuinte.c contribuinte.h \
rendimento.c rendimento.h \
- pagamento.c pagamento.h
+ pagamento.c pagamento.h \
+ bem.c bem.h
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * 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 <stdio.h>
+#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
#include "list.h"
#include "rendimento.h"
#include "pagamento.h"
+#include "bem.h"
#include "pmhash.h"
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;
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);
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);
}
char *nome;
struct list *rendimento;
struct list *pagamentos;
+ struct list *bens;
struct contribuinte contribuinte;
long long pago;
long long retido;
#include "cmd.h"
#include "rendimento.h"
#include "pagamento.h"
+#include "bem.h"
#include "totais.h"
static void gera_header(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);
int i;
struct rendimento *rendimento;
struct pagamento *pagamento;
+ struct bem *bem;
#define W(fn, dec, f) \
do { \
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);
contribuinte_cmd_init();
rendimento_cmd_init();
pagamento_cmd_init();
+ bem_cmd_init();
calcula_cmd_init();
gera_cmd_init();