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