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