2 * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
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.
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.
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.
30 static int isento_totais_update(struct declaracao *dec, struct isento *isento)
33 switch (isento->codigo) {
35 isento->exclusivo = 0;
36 r = totais_add(dec, "DOACOES", isento->valor);
39 isento->exclusivo = 0;
40 r = totais_add(dec, "INDENIZACOES", isento->valor);
44 isento->exclusivo = 1;
45 r = totais_add(dec, "PLR", isento->valor);
49 isento->exclusivo = 0;
50 r = totais_add(dec, "OUTROSISENTOS", isento->valor);
54 isento->exclusivo = 0;
55 r = totais_add(dec, "POUPANCA", isento->valor);
58 isento->exclusivo = 0;
59 r = totais_add(dec, "LUCROME", isento->valor);
63 isento->exclusivo = 1;
64 r = totais_add(dec, "APLICACOES", isento->valor);
67 if (isento->exclusivo) {
68 r += totais_add(dec, "EXCLUSIVOS", isento->valor);
69 if (isento->dependente) {
70 r += totais_add(dec, "EXCLUSIVOSDEP", isento->valor);
72 r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
75 r += totais_add(dec, "ISENTOS", isento->valor);
76 if (isento->dependente) {
77 r += totais_add(dec, "ISENTOSDEP", isento->valor);
79 r += totais_add(dec, "ISENTOSTIT", isento->valor);
85 void isento_free(void *pointer)
87 struct isento *isento = pointer;
92 if (isento->descricao)
93 free(isento->descricao);
97 static int isento_cmp(void *p1, void *p2)
99 struct isento *r1 = p1;
100 struct isento *r2 = p2;
101 if (r1->exclusivo != r2->exclusivo)
102 return r2->exclusivo - r1->exclusivo;
103 /* O rendimento maior vem primeiro. */
104 if (r1->valor > r2->valor)
106 else if (r1->valor < r2->valor)
111 static struct isento * isento_new(char **args, int argc)
113 struct isento *isento;
115 isento = malloc(sizeof(*isento));
117 isento->exclusivo = 0;
118 if (!strcmp(args[0], "exclusivo")) {
119 isento->exclusivo = 1;
122 r += set_int(args[1], &isento->codigo);
123 r += set_string(args[2], &isento->cnpj);
124 r += set_string(args[3], &isento->nome);
125 r += set_llong(args[4], &isento->valor);
126 if (r < 0 || isento->codigo < 0 ||
132 r = set_int(args[5], &isento->dependente);
134 isento->dependente = 0;
136 if (r < 0 || isento->dependente < 0) {
141 r = set_string(args[6], &isento->descricao);
147 isento->descricao = NULL;
152 static int run_isento(struct declaracao *dec, char **args, int argc)
154 struct isento *isento;
156 if (argc < 5 || argc > 7)
158 isento = isento_new(args, argc);
161 if (isento->dependente > list_size(dec->dependentes)) {
165 r = isento_totais_update(dec, isento);
170 r = list_add(&dec->isentos, isento);
178 void isento_salva(struct declaracao *dec, FILE *f)
182 for (i = 0; j = list_get(dec->isentos, i); i++) {
183 if (j->codigo == 97 && j->exclusivo)
184 fprintf(f, "exclusivo ");
186 fprintf(f, "isento ");
187 fprintf(f, "%d \"%s\" \"%s\" %lld %d",
188 j->codigo, j->cnpj, j->nome, j->valor, j->dependente);
190 fprintf(f, " \"%s\"", j->descricao);
195 static struct cmd cmd_isento = {
200 static struct cmd cmd_exclusivo = {
205 int isento_cmd_init(void)
207 cmd_add(&cmd_isento);
208 cmd_add(&cmd_exclusivo);
212 struct isento * isento_get(struct declaracao *dec, int codigo, int n)
214 struct isento *isento;
217 for (i = 0; (isento = list_get(dec->isentos, i)); i++) {
218 if (isento->codigo == codigo && j++ == n)