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