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 r = totais_add(dec, "DOACOES", isento->valor);
36 r += totais_add(dec, "ISENTOS", isento->valor);
37 r += totais_add(dec, "ISENTOSTIT", isento->valor);
40 r = totais_add(dec, "PLR", isento->valor);
41 r += totais_add(dec, "EXCLUSIVOS", isento->valor);
42 r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
45 r = totais_add(dec, "POUPANCA", isento->valor);
46 r += totais_add(dec, "ISENTOS", isento->valor);
47 r += totais_add(dec, "ISENTOSTIT", isento->valor);
50 r = totais_add(dec, "APLICACOES", isento->valor);
51 r += totais_add(dec, "EXCLUSIVOS", isento->valor);
52 r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
58 void isento_free(void *pointer)
60 struct isento *isento = pointer;
68 static int isento_cmp(void *p1, void *p2)
70 struct isento *r1 = p1;
71 struct isento *r2 = p2;
72 /* O rendimento maior vem primeiro. */
73 if (r1->valor > r2->valor)
75 else if (r1->valor < r2->valor)
80 static struct isento * isento_new(char **args)
82 struct isento *isento;
84 isento = malloc(sizeof(*isento));
85 isento->cnpj = strdup(args[2]);
86 isento->nome = strdup(args[3]);
87 /* TODO: consertar set_int para funcionar como set_llong */
88 r += set_int(args, 2, &isento->codigo);
89 r += set_llong(args[4], &isento->valor);
90 if (!isento->cnpj || !isento->nome) {
94 if (r < 0 || isento->codigo < 0 ||
102 static int run_isento(struct declaracao *dec, char **args, int argc)
104 struct isento *isento;
108 isento = isento_new(args);
111 r = list_insert_ordered(&dec->isentos, isento, isento_cmp);
116 r = isento_totais_update(dec, isento);
124 void isento_salva(struct declaracao *dec, FILE *f)
128 for (i = 0; j = list_get(dec->isentos, i); i++)
129 fprintf(f, "isento %d \"%s\" \"%s\" %lld\n",
130 j->codigo, j->cnpj, j->nome, j->valor);
133 static struct cmd cmd_isento = {
138 int isento_cmd_init(void)
140 cmd_add(&cmd_isento);
144 struct isento * isento_get(struct declaracao *dec, int codigo, int n)
146 struct isento *isento;
149 for (i = 0; (isento = list_get(dec->isentos, i)); i++) {
150 if (isento->codigo == codigo && j++ == n)