Adiciona suporte a linha de dependentes.
[cascardo/declara.git] / lib / declaracao.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "declaracao.h"
20 #include <stdlib.h>
21 #include <string.h>
22 #include "list.h"
23 #include "sistema.h"
24 #include "rendimento.h"
25 #include "isento.h"
26 #include "pagamento.h"
27 #include "bem.h"
28 #include "dependente.h"
29 #include "pmhash.h"
30
31 struct declaracao * declaracao_new(int ano)
32 {
33         struct declaracao *dec;
34         dec = malloc(sizeof(*dec));
35         if (!dec)
36                 return NULL;
37         memset(dec, 0, sizeof(*dec));
38         dec->ano = ano;
39         dec->rendimento = list_new();
40         if (!dec->rendimento)
41                 goto out_rendimento;
42         dec->isentos = list_new();
43         if (!dec->isentos)
44                 goto out_isentos;
45         dec->pagamentos = list_new();
46         if (!dec->pagamentos)
47                 goto out_pagamentos;
48         dec->bens = list_new();
49         if (!dec->bens)
50                 goto out_bens;
51         dec->dependentes = list_new();
52         if (!dec->dependentes)
53                 goto out_dependentes;
54         dec->totais = pmhash_new();
55         if (!dec->totais)
56                 goto out_totais;
57         dec->pago = 0;
58         dec->devido = 0;
59         dec->restituicao = 0;
60         dec->tipo = SIMPLES;
61         return dec;
62 out_totais:
63         list_free(dec->dependentes, dependente_free);
64 out_dependentes:
65         list_free(dec->bens, bem_free);
66 out_bens:
67         list_free(dec->pagamentos, pagamento_free);
68 out_pagamentos:
69         list_free(dec->isentos, isento_free);
70 out_isentos:
71         list_free(dec->rendimento, rendimento_free);
72 out_rendimento:
73         free(dec);
74         return NULL;
75 }
76
77 void declaracao_free(struct declaracao *dec)
78 {
79         if (dec->cpf)
80                 free(dec->cpf);
81         if (dec->nome)
82                 free(dec->nome);
83         if (dec->recibo)
84                 free(dec->recibo);
85         if (dec->retifica)
86                 free(dec->retifica);
87         if (dec->banco)
88                 free(dec->banco);
89         if (dec->agencia)
90                 free(dec->agencia);
91         if (dec->contacorrente)
92                 free(dec->contacorrente);
93         if (dec->dvconta)
94                 free(dec->dvconta);
95         list_free(dec->rendimento, rendimento_free);
96         list_free(dec->isentos, isento_free);
97         list_free(dec->pagamentos, pagamento_free);
98         list_free(dec->bens, bem_free);
99         list_free(dec->dependentes, dependente_free);
100         pmhash_del(dec->totais);
101         sistema_free(dec);
102         free(dec);
103 }