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