c928a02244af5c4db0435b73b365900f8a175d45
[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
29 static const long long dependente[ANO(MAX_ANOS)] = {
30         [ANO(2015)] = 215652,
31         [ANO(2016)] = 227508,
32 };
33
34 long long deducao_dependente(struct declaracao *dec)
35 {
36         if (ANO_VALIDO(dec->ano))
37                 return dependente[ANO(dec->ano)];
38         return 0;
39 }
40
41 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
42  * precisa ser ajustado para tais limites. Esta função considerará tais
43  * limites no futuro. */
44 static long long total_deducao(struct declaracao *dec)
45 {
46         int i;
47         if (dec->verbose) {
48                 printf("Dedução:\n");
49                 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
50                 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
51                 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
52                 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
53         }
54         return totais_get(dec, "DEPENDENTES") +
55                totais_get(dec, "INSS") +
56                totais_get(dec, "PAGAMENTOS") -
57                totais_get(dec, "REEMBOLSOS");
58 }
59
60 static void total_pago(struct declaracao *dec)
61 {
62         struct rendimento *rendimento;
63         int i;
64         dec->pago = dec->retido = 0;
65         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
66                 dec->pago += rendimento->imposto;
67                 dec->retido += rendimento->imposto;
68         }
69         if (dec->verbose) {
70                 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
71                         R(dec->pago), R(dec->retido));
72         }
73 }
74
75 struct taxtable {
76         long long base;
77         long long aliquota;
78         long long deducao;
79 };
80
81 static struct taxtable table2015[] = {
82         {       0,    0,      0, },
83         { 2145324,  750, 160899, },
84         { 3215148, 1500, 402035, },
85         { 4286917, 2250, 723554, },
86         { 5356572, 2750, 991383, },
87         { 9999999999999LL, 0, 0, },
88 };
89
90 static struct taxtable table2016[] = {
91         {       0,    0,       0, },
92         { 2249914,  750,  168743, },
93         { 3347773, 1500,  419826, },
94         { 4447675, 2250,  753402, },
95         { 5537355, 2750, 1030270, },
96         { 9999999999999LL, 0, 0, },
97 };
98
99 static struct taxtable *table[ANO(MAX_ANOS)] = {
100         [ANO(2015)] = table2015,
101         [ANO(2016)] = table2016,
102 };
103
104 static const long long simples[ANO(MAX_ANOS)] = {
105         [ANO(2015)] = 1588089,
106         [ANO(2016)] = 1675434,
107 };
108
109 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
110         [ANO(2015)] = 2681655,
111         [ANO(2016)] = 2812391,
112 };
113
114 static long long imposto(struct taxtable *tt, long long tr, int verbose)
115 {
116         int i;
117         for (i = 0; tr >= tt[i].base; i++);
118         i--;
119         if (verbose) {
120                 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
121                         tt[i].aliquota / 10, R(tt[i].deducao));
122         }
123         return tr * tt[i].aliquota / 10000 - tt[i].deducao;
124 }
125
126 static long long imposto_simples(struct declaracao *dec)
127 {
128         struct taxtable *tt;
129         long long tr, td;
130         tt = table[ANO(dec->ano)];
131         tr = totais_get(dec, "RENDPJ");
132         if (tr / 5 < simples[ANO(dec->ano)])
133                 td = tr / 5;
134         else
135                 td = simples[ANO(dec->ano)];
136         totais_add(dec, "DESCONTO", td);
137         tr -= td;
138         if (tr < 0)
139                 tr = 0;
140         totais_add(dec, "BASESIMPLES", tr);
141         if (dec->verbose) {
142                 printf("Desconto simplificado é "FMT_R"\n", R(td));
143         }
144         return imposto(tt, tr, dec->verbose);
145 }
146
147 static long long imposto_completa(struct declaracao *dec)
148 {
149         struct taxtable *tt;
150         long long tr, td;
151         tt = table[ANO(dec->ano)];
152         tr = totais_get(dec, "RENDPJ");
153         td = total_deducao(dec);
154         totais_add(dec, "DEDUCOES", td);
155         tr -= td;
156         if (tr < 0)
157                 tr = 0;
158         totais_add(dec, "BASECOMPLETA", tr);
159         if (dec->verbose) {
160                 printf("Desconto completa é "FMT_R"\n", R(td));
161         }
162         return imposto(tt, tr, dec->verbose);
163 }
164
165 int calcula(struct declaracao *dec)
166 {
167         long long i_simples, i_completa;
168         if (!ANO_VALIDO(dec->ano)) {
169                 return -EINVAL;
170         }
171         if (totais_get(dec, "RENDPJ") > obrigatoriedade[ANO(dec->ano)]) {
172                 if (dec->verbose) {
173                         printf("Declaracao obrigatoria pois rendimento e"
174                                 "maior que mínimo para declaracao: "
175                                 FMT_R" > "FMT_R"\n",
176                                 R(totais_get(dec, "RENDPJ")),
177                                 R(obrigatoriedade[ANO(dec->ano)]));
178                 }
179                 dec->obrigatoria = 1;
180         }
181         i_simples = imposto_simples(dec);
182         i_completa = imposto_completa(dec);
183         total_pago(dec);
184         if (dec->verbose) {
185                 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
186                         R(i_simples), R(i_completa));
187         }
188         if (dec->tipo != FORCA_SIMPLES &&
189             (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
190                 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
191                 dec->tipo = COMPLETA;
192                 dec->devido = i_completa;
193         } else {
194                 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
195                 dec->tipo = SIMPLES;
196                 dec->devido = i_simples;
197         }
198         if (dec->pago > dec->devido)
199                 dec->restituicao = dec->pago - dec->devido;
200         else
201                 dec->pagar = dec->devido - dec->pago;
202         return 0;
203 }
204
205 static int run_calcula(struct declaracao *dec, char **args, int argc)
206 {
207         totais_add(dec, "EXCLUSIVOS_SEM_13o",
208                 totais_get(dec, "EXCLUSIVOS") -
209                 totais_get(dec, "DECIMOTERCEIRO"));
210         return calcula(dec);
211 }
212
213 static struct cmd cmd_calcula = {
214         .name = "calcula",
215         .run = run_calcula,
216 };
217
218 int calcula_cmd_init(void)
219 {
220         cmd_add(&cmd_calcula);
221         return 0;
222 }