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