7eef34c57506c26b1a1ec652a6ae9becf61607ca
[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 "conjuge.h"
24 #include "sistema.h"
25 #include "rendimento.h"
26 #include "isento.h"
27 #include "pagamento.h"
28 #include "bem.h"
29 #include "dependente.h"
30 #include "pmhash.h"
31
32 struct declaracao * declaracao_new(int ano)
33 {
34         struct declaracao *dec;
35         dec = malloc(sizeof(*dec));
36         if (!dec)
37                 return NULL;
38         memset(dec, 0, sizeof(*dec));
39         dec->ano = ano;
40         dec->rendimento = list_new();
41         if (!dec->rendimento)
42                 goto out_rendimento;
43         dec->isentos = list_new();
44         if (!dec->isentos)
45                 goto out_isentos;
46         dec->pagamentos = list_new();
47         if (!dec->pagamentos)
48                 goto out_pagamentos;
49         dec->bens = list_new();
50         if (!dec->bens)
51                 goto out_bens;
52         dec->dependentes = list_new();
53         if (!dec->dependentes)
54                 goto out_dependentes;
55         dec->totais = pmhash_new();
56         if (!dec->totais)
57                 goto out_totais;
58         dec->pago = 0;
59         dec->devido = 0;
60         dec->restituicao = 0;
61         dec->tipo = SIMPLES;
62         return dec;
63 out_totais:
64         list_free(dec->dependentes, dependente_free);
65 out_dependentes:
66         list_free(dec->bens, bem_free);
67 out_bens:
68         list_free(dec->pagamentos, pagamento_free);
69 out_pagamentos:
70         list_free(dec->isentos, isento_free);
71 out_isentos:
72         list_free(dec->rendimento, rendimento_free);
73 out_rendimento:
74         free(dec);
75         return NULL;
76 }
77
78 void declaracao_free(struct declaracao *dec)
79 {
80         if (dec->cpf)
81                 free(dec->cpf);
82         if (dec->nome)
83                 free(dec->nome);
84         if (dec->recibo)
85                 free(dec->recibo);
86         if (dec->retifica)
87                 free(dec->retifica);
88         if (dec->banco)
89                 free(dec->banco);
90         if (dec->agencia)
91                 free(dec->agencia);
92         if (dec->contacorrente)
93                 free(dec->contacorrente);
94         if (dec->dvconta)
95                 free(dec->dvconta);
96         list_free(dec->rendimento, rendimento_free);
97         list_free(dec->isentos, isento_free);
98         list_free(dec->pagamentos, pagamento_free);
99         list_free(dec->bens, bem_free);
100         list_free(dec->dependentes, dependente_free);
101         pmhash_del(dec->totais);
102         conjuge_free(dec);
103         sistema_free(dec);
104         free(dec);
105 }