Inicializa membro da estrutura de rendimentos isentos.
[cascardo/declara.git] / lib / isento.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 "isento.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "cmd.h"
26 #include "list.h"
27 #include "util.h"
28 #include "totais.h"
29
30 static int isento_totais_update(struct declaracao *dec, struct isento *isento)
31 {
32         int r = 0;
33         switch (isento->codigo) {
34         case 82:
35                 isento->exclusivo = 0;
36                 r = totais_add(dec, "DOACOES", isento->valor);
37                 break;
38         case 93:
39                 isento->exclusivo = 0;
40                 r = totais_add(dec, "INDENIZACOES", isento->valor);
41                 break;
42         case 96:
43                 isento->exclusivo = 1;
44                 r = totais_add(dec, "PLR", isento->valor);
45                 break;
46         case 97:
47                 break;
48         case 98:
49                 isento->exclusivo = 0;
50                 r = totais_add(dec, "POUPANCA", isento->valor);
51                 break;
52         case 99:
53                 isento->exclusivo = 1;
54                 r = totais_add(dec, "APLICACOES", isento->valor);
55                 break;
56         }
57         if (isento->exclusivo) {
58                 r += totais_add(dec, "EXCLUSIVOS", isento->valor);
59                 if (isento->dependente) {
60                         r += totais_add(dec, "EXCLUSIVOSDEP", isento->valor);
61                 } else {
62                         r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
63                 }
64         } else {
65                 r += totais_add(dec, "ISENTOS", isento->valor);
66                 if (isento->dependente) {
67                         r += totais_add(dec, "ISENTOSDEP", isento->valor);
68                 } else {
69                         r += totais_add(dec, "ISENTOSTIT", isento->valor);
70                 }
71         }
72         return r;
73 }
74
75 void isento_free(void *pointer)
76 {
77         struct isento *isento = pointer;
78         if (isento->cnpj)
79                 free(isento->cnpj);
80         if (isento->nome)
81                 free(isento->nome);
82         if (isento->descricao)
83                 free(isento->descricao);
84         free(isento);
85 }
86
87 static int isento_cmp(void *p1, void *p2)
88 {
89         struct isento *r1 = p1;
90         struct isento *r2 = p2;
91         if (r1->exclusivo != r2->exclusivo)
92                 return r2->exclusivo - r1->exclusivo;
93         /* O rendimento maior vem primeiro. */
94         if (r1->valor > r2->valor)
95                 return -1;
96         else if (r1->valor < r2->valor)
97                 return 1;
98         return 0;
99 }
100
101 static struct isento * isento_new(char **args, int argc)
102 {
103         struct isento *isento;
104         int r = 0;
105         isento = malloc(sizeof(*isento));
106
107         isento->exclusivo = 0;
108         if (!strcmp(args[0], "exclusivo")) {
109                 isento->exclusivo = 1;
110         }
111
112         r += set_int(args[1], &isento->codigo);
113         r += set_string(args[2], &isento->cnpj);
114         r += set_string(args[3], &isento->nome);
115         r += set_llong(args[4], &isento->valor);
116         if (r < 0 || isento->codigo < 0 ||
117             isento->valor < 0) {
118                 isento_free(isento);
119                 return NULL;
120         }
121         if (argc == 6) {
122                 r = set_int(args[5], &isento->dependente);
123         } else {
124                 isento->dependente = 0;
125         }
126         if (r < 0 || isento->dependente < 0) {
127                 isento_free(isento);
128                 return NULL;
129         }
130         if (argc == 7) {
131                 r = set_string(args[6], &isento->descricao);
132                 if (r < 0) {
133                         isento_free(isento);
134                         return NULL;
135                 }
136         } else {
137                 isento->descricao = NULL;
138         }
139         return isento;
140 }
141
142 static int run_isento(struct declaracao *dec, char **args, int argc)
143 {
144         struct isento *isento;
145         int r;
146         if (argc < 5 || argc > 7)
147                 return -EINVAL;
148         isento = isento_new(args, argc);
149         if (!isento)
150                 return -ENOMEM;
151         if (isento->dependente > list_size(dec->dependentes)) {
152                 isento_free(isento);
153                 return -EINVAL;
154         }
155         r = isento_totais_update(dec, isento);
156         if (r) {
157                 isento_free(isento);
158                 return r;
159         }
160         r = list_insert_ordered(&dec->isentos, isento, isento_cmp);
161         if (r < 0) {
162                 isento_free(isento);
163                 return r;
164         }
165         return 0;
166 }
167
168 void isento_salva(struct declaracao *dec, FILE *f)
169 {
170         int i;
171         struct isento *j;
172         for (i = 0; j = list_get(dec->isentos, i); i++) {
173                 if (j->codigo == 97 && j->exclusivo)
174                         fprintf(f, "exclusivo ");
175                 else
176                         fprintf(f, "isento ");
177                 fprintf(f, "%d \"%s\" \"%s\" %lld %d",
178                         j->codigo, j->cnpj, j->nome, j->valor, j->dependente);
179                 if (j->descricao)
180                         fprintf(f, " \"%s\"", j->descricao);
181                 fprintf(f, "\n");
182         }
183 }
184
185 static struct cmd cmd_isento = {
186         .name = "isento",
187         .run = run_isento,
188 };
189
190 static struct cmd cmd_exclusivo = {
191         .name = "exclusivo",
192         .run = run_isento,
193 };
194
195 int isento_cmd_init(void)
196 {
197         cmd_add(&cmd_isento);
198         cmd_add(&cmd_exclusivo);
199         return 0;
200 }
201
202 struct isento * isento_get(struct declaracao *dec, int codigo, int n)
203 {
204         struct isento *isento;
205         int i;
206         int j = 0;
207         for (i = 0; (isento = list_get(dec->isentos, i)); i++) {
208                 if (isento->codigo == codigo && j++ == n)
209                         break;
210         }
211         return isento;
212 }