2 * Copyright (C) 2015-2016 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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.
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.
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.
22 #include "declaracao.h"
24 #include "rendimento.h"
29 static const long long dependente[ANO(MAX_ANOS)] = {
34 long long deducao_dependente(struct declaracao *dec)
36 if (ANO_VALIDO(dec->ano))
37 return dependente[ANO(dec->ano)];
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)
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")));
54 return totais_get(dec, "DEPENDENTES") +
55 totais_get(dec, "INSS") +
56 totais_get(dec, "PAGAMENTOS") -
57 totais_get(dec, "REEMBOLSOS");
60 static void total_pago(struct declaracao *dec)
62 struct rendimento *rendimento;
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;
70 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
71 R(dec->pago), R(dec->retido));
81 static struct taxtable table2015[] = {
83 { 2145324, 750, 160899, },
84 { 3215148, 1500, 402035, },
85 { 4286917, 2250, 723554, },
86 { 5356572, 2750, 991383, },
87 { 9999999999999LL, 0, 0, },
90 static struct taxtable table2016[] = {
92 { 2249914, 750, 168743, },
93 { 3347773, 1500, 419826, },
94 { 4447675, 2250, 753402, },
95 { 5537355, 2750, 1030270, },
96 { 9999999999999LL, 0, 0, },
99 static struct taxtable *table[ANO(MAX_ANOS)] = {
100 [ANO(2015)] = table2015,
101 [ANO(2016)] = table2016,
104 static const long long simples[ANO(MAX_ANOS)] = {
105 [ANO(2015)] = 1588089,
106 [ANO(2016)] = 1675434,
109 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
110 [ANO(2015)] = 2681655,
111 [ANO(2016)] = 2812391,
114 static long long imposto(struct taxtable *tt, long long tr, int verbose)
117 for (i = 0; tr >= tt[i].base; i++);
120 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
121 tt[i].aliquota / 10, R(tt[i].deducao));
123 return tr * tt[i].aliquota / 10000 - tt[i].deducao;
126 static long long imposto_simples(struct declaracao *dec)
130 tt = table[ANO(dec->ano)];
131 tr = totais_get(dec, "RENDPJ");
132 if (tr / 5 < simples[ANO(dec->ano)])
135 td = simples[ANO(dec->ano)];
136 totais_add(dec, "DESCONTO", td);
138 totais_add(dec, "BASESIMPLES", tr);
140 printf("Desconto simplificado é "FMT_R"\n", R(td));
142 return imposto(tt, tr, dec->verbose);
145 static long long imposto_completa(struct declaracao *dec)
149 tt = table[ANO(dec->ano)];
150 tr = totais_get(dec, "RENDPJ");
151 td = total_deducao(dec);
152 totais_add(dec, "DEDUCOES", td);
154 totais_add(dec, "BASECOMPLETA", tr);
156 printf("Desconto completa é "FMT_R"\n", R(td));
158 return imposto(tt, tr, dec->verbose);
161 int calcula(struct declaracao *dec)
163 long long i_simples, i_completa;
164 if (!ANO_VALIDO(dec->ano)) {
167 if (totais_get(dec, "RENDPJ") > obrigatoriedade[ANO(dec->ano)]) {
169 printf("Declaracao obrigatoria pois rendimento e"
170 "maior que mínimo para declaracao: "
172 R(totais_get(dec, "RENDPJ")),
173 R(obrigatoriedade[ANO(dec->ano)]));
175 dec->obrigatoria = 1;
177 i_simples = imposto_simples(dec);
178 i_completa = imposto_completa(dec);
181 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
182 R(i_simples), R(i_completa));
184 if (dec->tipo != FORCA_SIMPLES &&
185 (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
186 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
187 dec->tipo = COMPLETA;
188 dec->devido = i_completa;
190 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
192 dec->devido = i_simples;
194 if (dec->pago > dec->devido)
195 dec->restituicao = dec->pago - dec->devido;
197 dec->pagar = dec->devido - dec->pago;
201 static int run_calcula(struct declaracao *dec, char **args, int argc)
203 totais_add(dec, "EXCLUSIVOS_SEM_13o",
204 totais_get(dec, "EXCLUSIVOS") -
205 totais_get(dec, "DECIMOTERCEIRO"));
209 static struct cmd cmd_calcula = {
214 int calcula_cmd_init(void)
216 cmd_add(&cmd_calcula);