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