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