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