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