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