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