Rendimentos isentos e de tributação exclusiva.
[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 96:
35                 r = totais_add(dec, "PLR", isento->valor);
36                 r += totais_add(dec, "EXCLUSIVOS", isento->valor);
37                 r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
38                 break;
39         case 98:
40                 r = totais_add(dec, "POUPANCA", isento->valor);
41                 r += totais_add(dec, "ISENTOS", isento->valor);
42                 r += totais_add(dec, "ISENTOSTIT", isento->valor);
43                 break;
44         }
45         return r;
46 }
47
48 void isento_free(void *pointer)
49 {
50         struct isento *isento = pointer;
51         if (isento->cnpj)
52                 free(isento->cnpj);
53         if (isento->nome)
54                 free(isento->nome);
55         free(isento);
56 }
57
58 static int isento_cmp(void *p1, void *p2)
59 {
60         struct isento *r1 = p1;
61         struct isento *r2 = p2;
62         /* O rendimento maior vem primeiro. */
63         if (r1->valor > r2->valor)
64                 return -1;
65         else if (r1->valor < r2->valor)
66                 return 1;
67         return 0;
68 }
69
70 static struct isento * isento_new(char **args)
71 {
72         struct isento *isento;
73         int r = 0;
74         isento = malloc(sizeof(*isento));
75         isento->cnpj = strdup(args[2]);
76         isento->nome = strdup(args[3]);
77         /* TODO: consertar set_int para funcionar como set_llong */
78         r += set_int(args, 2, &isento->codigo);
79         r += set_llong(args[4], &isento->valor);
80         if (!isento->cnpj || !isento->nome) {
81                 isento_free(isento);
82                 return NULL;
83         }
84         if (r < 0 || isento->codigo < 0 ||
85             isento->valor < 0) {
86                 isento_free(isento);
87                 return NULL;
88         }
89         return isento;
90 }
91
92 static int run_isento(struct declaracao *dec, char **args, int argc)
93 {
94         struct isento *isento;
95         int r;
96         if (argc != 5)
97                 return -EINVAL;
98         isento = isento_new(args);
99         if (!isento)
100                 return -ENOMEM;
101         r = list_insert_ordered(&dec->isentos, isento, isento_cmp);
102         if (r < 0) {
103                 isento_free(isento);
104                 return r;
105         }
106         r = isento_totais_update(dec, isento);
107         if (r) {
108                 isento_free(isento);
109                 return r;
110         }
111         return 0;
112 }
113
114 void isento_salva(struct declaracao *dec, FILE *f)
115 {
116         int i;
117         struct isento *j;
118         for (i = 0; j = list_get(dec->isentos, i); i++)
119                 fprintf(f, "isento %d \"%s\" \"%s\" %lld\n",
120                         j->codigo, j->cnpj, j->nome, j->valor);
121 }
122
123 static struct cmd cmd_isento = {
124         .name = "isento",
125         .run = run_isento,
126 };
127
128 int isento_cmd_init(void)
129 {
130         cmd_add(&cmd_isento);
131         return 0;
132 }
133
134 struct isento * isento_get(struct declaracao *dec, int codigo, int n)
135 {
136         struct isento *isento;
137         int i;
138         int j = 0;
139         for (i = 0; (isento = list_get(dec->isentos, i)); i++) {
140                 if (isento->codigo == codigo && j++ == n)
141                         break;
142         }
143         return isento;
144 }