Assume mesmos limites em 2021 comparados com 2020.
[cascardo/declara.git] / lib / calcula.c
1 /*
2  *  Copyright (C) 2015-2017  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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 "calcula.h"
20 #include <errno.h>
21 #include <stdio.h>
22 #include "declaracao.h"
23 #include "cmd.h"
24 #include "rendimento.h"
25 #include "totais.h"
26 #include "util.h"
27 #include "ano.h"
28 #include "dependente.h"
29 #include "pagamento.h"
30
31 static const long long dependente[ANO(MAX_ANOS)] = {
32         [ANO(2015)] = 215652,
33         [ANO(2016)] = 227508,
34         [ANO(2017)] = 227508,
35         [ANO(2018)] = 227508,
36         [ANO(2019)] = 227508,
37         [ANO(2020)] = 227508,
38         [ANO(2021)] = 227508,
39 };
40
41 static const long long instrucao[ANO(MAX_ANOS)] = {
42         [ANO(2015)] = 337583,
43         [ANO(2016)] = 356150,
44         [ANO(2017)] = 356150,
45         [ANO(2018)] = 356150,
46         [ANO(2019)] = 356150,
47         [ANO(2020)] = 356150,
48         [ANO(2021)] = 356150,
49 };
50
51 long long deducao_dependente(struct declaracao *dec)
52 {
53         if (ANO_VALIDO(dec->ano))
54                 return dependente[ANO(dec->ano)];
55         return 0;
56 }
57
58 static void calculo_instrucao(struct declaracao *dec)
59 {
60         int i, j;
61         struct dependente *d;
62         struct pagamento *p;
63         long long instrucao_titular = 0;
64         /* Instrução do titular */
65         for (i = 0; (p = list_get(dec->pagamentos, i)); i++) {
66                 if (p->dependente == 0 && pagamento_instrucao(p)) {
67                         instrucao_titular += (p->pagamento - p->reembolso);
68                 }
69         }
70         if (instrucao_titular > instrucao[ANO(dec->ano)]) {
71                 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
72         } else {
73                 totais_add(dec, "INSTRUCAO", instrucao_titular);
74         }
75         /* Dependentes com instrução */
76         for (i = 0; (d = list_get(dec->dependentes, i)); i++) {
77                 long long instrucao_dependente = 0;
78                 for (j = 0; (p = list_get(dec->pagamentos, j)); j++) {
79                         if (p->dependente == (i + 1) && pagamento_instrucao(p)) {
80                                 instrucao_dependente += (p->pagamento - p->reembolso);
81                         }
82                 }
83                 if (instrucao_dependente) {
84                         /* Conta número de dependentes com instrução. */
85                         totais_add(dec, "DEPSINSTRUCAO", 1);
86                         if (dec->verbose) {
87                                 printf("Dependente %s (%d) tem instrução\n", d->nome, i);
88                         }
89                 }
90                 if (instrucao_dependente > instrucao[ANO(dec->ano)]) {
91                         totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
92                 } else {
93                         totais_add(dec, "INSTRUCAO", instrucao_dependente);
94                 }
95         }
96 }
97
98 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
99  * precisa ser ajustado para tais limites. Esta função considerará tais
100  * limites no futuro. */
101 static long long total_deducao(struct declaracao *dec)
102 {
103         int i;
104
105         calculo_instrucao(dec);
106
107         if (dec->verbose) {
108                 printf("Dedução:\n");
109                 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
110                 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
111                 printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO")));
112                 printf("\tMédicas: "FMT_R"\n", R(totais_get(dec, "MEDICAS")));
113                 printf("\tPrevidência: "FMT_R"\n", R(totais_get(dec, "PREVIDENCIA")));
114                 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
115                 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
116         }
117         return totais_get(dec, "DEPENDENTES") +
118                totais_get(dec, "INSS") +
119                totais_get(dec, "INSTRUCAO") +
120                totais_get(dec, "MEDICAS") +
121                totais_get(dec, "PREVIDENCIA");
122 }
123
124 static void total_pago(struct declaracao *dec)
125 {
126         struct rendimento *rendimento;
127         int i;
128         dec->pago = dec->retido = totais_get(dec, "PAGO");
129         dec->retido -= totais_get(dec, "CARNE");
130         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
131                 dec->pago += rendimento->imposto;
132                 dec->retido += rendimento->imposto;
133         }
134         if (dec->verbose) {
135                 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
136                         R(dec->pago), R(dec->retido));
137         }
138 }
139
140 struct taxtable {
141         long long base;
142         long long aliquota;
143         long long deducao;
144 };
145
146 static struct taxtable table2015[] = {
147         {       0,    0,      0, },
148         { 2145324,  750, 160899, },
149         { 3215148, 1500, 402035, },
150         { 4286917, 2250, 723554, },
151         { 5356572, 2750, 991383, },
152         { 9999999999999LL, 0, 0, },
153 };
154
155 static struct taxtable table2016[] = {
156         {       0,    0,       0, },
157         { 2249914,  750,  168743, },
158         { 3347773, 1500,  419826, },
159         { 4447675, 2250,  753402, },
160         { 5537355, 2750, 1030270, },
161         { 9999999999999LL, 0, 0, },
162 };
163
164 static struct taxtable table2017[] = {
165         {       0,    0,       0, },
166         { 2284777,  750,  171358, },
167         { 3391981, 1500,  425757, },
168         { 4501261, 2250,  763351, },
169         { 5597616, 2750, 1043232, },
170         { 9999999999999LL, 0, 0, },
171 };
172
173 static struct taxtable *table[ANO(MAX_ANOS)] = {
174         [ANO(2015)] = table2015,
175         [ANO(2016)] = table2016,
176         [ANO(2017)] = table2017,
177         [ANO(2018)] = table2017,
178         [ANO(2019)] = table2017,
179         [ANO(2020)] = table2017,
180         [ANO(2021)] = table2017,
181 };
182
183 static const long long simples[ANO(MAX_ANOS)] = {
184         [ANO(2015)] = 1588089,
185         [ANO(2016)] = 1675434,
186         [ANO(2017)] = 1675434,
187         [ANO(2018)] = 1675434,
188         [ANO(2019)] = 1675434,
189         [ANO(2020)] = 1675434,
190         [ANO(2021)] = 1675434,
191 };
192
193 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
194         [ANO(2015)] = 2681655,
195         [ANO(2016)] = 2812391,
196         [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */
197         [ANO(2018)] = 2855970,
198         [ANO(2019)] = 2855970,
199         [ANO(2020)] = 2855970,
200         [ANO(2021)] = 2855970,
201 };
202
203 static long long imposto(struct taxtable *tt, long long tr, int verbose)
204 {
205         int i;
206         for (i = 0; tr >= tt[i].base; i++);
207         i--;
208         if (verbose) {
209                 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
210                         tt[i].aliquota / 10, R(tt[i].deducao));
211         }
212         return tr * tt[i].aliquota / 10000 - tt[i].deducao;
213 }
214
215 static long long imposto_simples(struct declaracao *dec)
216 {
217         struct taxtable *tt;
218         long long tr, td;
219         tt = table[ANO(dec->ano)];
220         tr = totais_get(dec, "RENDTRIB");
221         if (tr / 5 < simples[ANO(dec->ano)])
222                 td = tr / 5;
223         else
224                 td = simples[ANO(dec->ano)];
225         totais_add(dec, "DESCONTO", td);
226         tr -= td;
227         if (tr < 0)
228                 tr = 0;
229         totais_add(dec, "BASESIMPLES", tr);
230         if (dec->verbose) {
231                 printf("Desconto simplificado é "FMT_R"\n", R(td));
232         }
233         return imposto(tt, tr, dec->verbose);
234 }
235
236 static long long imposto_completa(struct declaracao *dec)
237 {
238         struct taxtable *tt;
239         long long tr, td;
240         tt = table[ANO(dec->ano)];
241         tr = totais_get(dec, "RENDTRIB");
242         td = total_deducao(dec);
243         totais_add(dec, "DEDUCOES", td);
244         tr -= td;
245         if (tr < 0)
246                 tr = 0;
247         totais_add(dec, "BASECOMPLETA", tr);
248         if (dec->verbose) {
249                 printf("Desconto completa é "FMT_R"\n", R(td));
250         }
251         return imposto(tt, tr, dec->verbose);
252 }
253
254 int calcula(struct declaracao *dec)
255 {
256         long long i_simples, i_completa;
257         long long isentos;
258         if (!ANO_VALIDO(dec->ano)) {
259                 dec_set_error(dec, "Ano %d não suportado.", dec->ano);
260                 return -EINVAL;
261         }
262         if (totais_get(dec, "RENDTRIB") > obrigatoriedade[ANO(dec->ano)]) {
263                 if (dec->verbose) {
264                         printf("Declaracao obrigatoria pois rendimento e"
265                                 "maior que mínimo para declaracao: "
266                                 FMT_R" > "FMT_R"\n",
267                                 R(totais_get(dec, "RENDTRIB")),
268                                 R(obrigatoriedade[ANO(dec->ano)]));
269                 }
270                 dec->obrigatoria += 1;
271         }
272         isentos = totais_get(dec, "ISENTOS") + totais_get(dec, "EXCLUSIVOS");
273         if (isentos > 4000000) {
274                 if (dec->verbose) {
275                         printf("Declaracao obrigatoria pois rendimentos "
276                                 "isentos e exclusivos maior que minimo para "
277                                 "declaracao: " FMT_R" > "FMT_R"\n",
278                                 R(isentos), R(4000000));
279                 }
280                 dec->obrigatoria += 2;
281         }
282         if (totais_get(dec, "BENS") > 30000000) {
283                 if (dec->verbose) {
284                         printf("Declaracao obrigatoria pois bens e direitos e"
285                                 " maior que minimo para declaracao: "
286                                 FMT_R" > "FMT_R"\n",
287                                 R(totais_get(dec, "BENS")),
288                                 R(30000000));
289                 }
290                 dec->obrigatoria += 32;
291         }
292         i_simples = imposto_simples(dec);
293         i_completa = imposto_completa(dec);
294         total_pago(dec);
295         if (dec->verbose) {
296                 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
297                         R(i_simples), R(i_completa));
298         }
299         if (dec->tipo != FORCA_SIMPLES &&
300             (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
301                 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
302                 dec->tipo = COMPLETA;
303                 dec->devido = i_completa;
304         } else {
305                 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
306                 dec->tipo = SIMPLES;
307                 dec->devido = i_simples;
308         }
309         if (dec->pago > dec->devido)
310                 dec->restituicao = dec->pago - dec->devido;
311         else
312                 dec->pagar = dec->devido - dec->pago;
313         if (totais_get(dec, "RENDTRIB") == 0)
314                 dec->aliquota_efetiva = 0;
315         else
316                 dec->aliquota_efetiva = dec->devido * 10000 / totais_get(dec, "RENDTRIB");
317         return 0;
318 }
319
320 static int run_calcula(struct declaracao *dec, char **args, int argc)
321 {
322         totais_add(dec, "EXCLUSIVOS_SEM_13o",
323                 totais_get(dec, "EXCLUSIVOS") -
324                 totais_get(dec, "DECIMOTERCEIRO"));
325         return calcula(dec);
326 }
327
328 static struct cmd cmd_calcula = {
329         .name = "calcula",
330         .run = run_calcula,
331 };
332
333 int calcula_cmd_init(void)
334 {
335         cmd_add(&cmd_calcula);
336         return 0;
337 }