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)] = {
33 long long deducao_dependente(struct declaracao *dec)
35 if (ANO_VALIDO(dec->ano))
36 return dependente[ANO(dec->ano)];
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)
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")));
53 return totais_get(dec, "DEPENDENTES") +
54 totais_get(dec, "INSS") +
55 totais_get(dec, "PAGAMENTOS") -
56 totais_get(dec, "REEMBOLSOS");
59 static void total_pago(struct declaracao *dec)
61 struct rendimento *rendimento;
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;
69 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
70 R(dec->pago), R(dec->retido));
80 static struct taxtable table2015[] = {
82 { 2145324, 750, 160899, },
83 { 3215148, 1500, 402035, },
84 { 4286917, 2250, 723554, },
85 { 5356572, 2750, 991383, },
86 { 9999999999999LL, 0, 0, },
89 static struct taxtable *table[ANO(MAX_ANOS)] = {
90 [ANO(2015)] = table2015,
93 static const long long simples[ANO(MAX_ANOS)] = {
94 [ANO(2015)] = 1588089,
97 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
98 [ANO(2015)] = 2681655,
101 static long long imposto(struct taxtable *tt, long long tr, int verbose)
104 for (i = 0; tr >= tt[i].base; i++);
107 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
108 tt[i].aliquota / 10, R(tt[i].deducao));
110 return tr * tt[i].aliquota / 10000 - tt[i].deducao;
113 static long long imposto_simples(struct declaracao *dec)
117 tt = table[ANO(dec->ano)];
118 tr = totais_get(dec, "RENDPJ");
119 if (tr / 5 < simples[ANO(dec->ano)])
122 td = simples[ANO(dec->ano)];
123 totais_add(dec, "DESCONTO", td);
125 totais_add(dec, "BASESIMPLES", tr);
127 printf("Desconto simplificado é "FMT_R"\n", R(td));
129 return imposto(tt, tr, dec->verbose);
132 static long long imposto_completa(struct declaracao *dec)
136 tt = table[ANO(dec->ano)];
137 tr = totais_get(dec, "RENDPJ");
138 td = total_deducao(dec);
139 totais_add(dec, "DEDUCOES", td);
141 totais_add(dec, "BASECOMPLETA", tr);
143 printf("Desconto completa é "FMT_R"\n", R(td));
145 return imposto(tt, tr, dec->verbose);
148 int calcula(struct declaracao *dec)
150 long long i_simples, i_completa;
151 if (!ANO_VALIDO(dec->ano)) {
154 if (totais_get(dec, "RENDPJ") > obrigatoriedade[ANO(dec->ano)]) {
156 printf("Declaracao obrigatoria pois rendimento e"
157 "maior que mínimo para declaracao: "
159 R(totais_get(dec, "RENDPJ")),
160 R(obrigatoriedade[ANO(dec->ano)]));
162 dec->obrigatoria = 1;
164 i_simples = imposto_simples(dec);
165 i_completa = imposto_completa(dec);
168 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
169 R(i_simples), R(i_completa));
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;
177 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
179 dec->devido = i_simples;
181 if (dec->pago > dec->devido)
182 dec->restituicao = dec->pago - dec->devido;
184 dec->pagar = dec->devido - dec->pago;
188 static int run_calcula(struct declaracao *dec, char **args, int argc)
190 totais_add(dec, "EXCLUSIVOS_SEM_13o",
191 totais_get(dec, "EXCLUSIVOS") -
192 totais_get(dec, "DECIMOTERCEIRO"));
196 static struct cmd cmd_calcula = {
201 int calcula_cmd_init(void)
203 cmd_add(&cmd_calcula);