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