2 * Copyright (C) 2015-2017 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"
28 #include "dependente.h"
29 #include "pagamento.h"
31 static const long long dependente[ANO(MAX_ANOS)] = {
37 static const long long instrucao[ANO(MAX_ANOS)] = {
43 long long deducao_dependente(struct declaracao *dec)
45 if (ANO_VALIDO(dec->ano))
46 return dependente[ANO(dec->ano)];
50 static void calculo_instrucao(struct declaracao *dec)
55 long long instrucao_titular = 0;
56 /* Instrução do titular */
57 for (i = 0; (p = list_get(dec->pagamentos, i)); i++) {
58 if (p->dependente == 0 && pagamento_instrucao(p)) {
59 instrucao_titular += (p->pagamento - p->reembolso);
62 if (instrucao_titular > instrucao[ANO(dec->ano)]) {
63 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
65 totais_add(dec, "INSTRUCAO", instrucao_titular);
67 /* Dependentes com instrução */
68 for (i = 0; (d = list_get(dec->dependentes, i)); i++) {
69 long long instrucao_dependente = 0;
70 for (j = 0; (p = list_get(dec->pagamentos, j)); j++) {
71 if (p->dependente == (i + 1) && pagamento_instrucao(p)) {
72 instrucao_dependente += (p->pagamento - p->reembolso);
75 if (instrucao_dependente) {
76 totais_add(dec, "DEPSINSTRUCAO", 1);
78 printf("Dependente %s (%d) tem instrução\n", d->nome, i);
81 if (instrucao_dependente > instrucao[ANO(dec->ano)]) {
82 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
84 totais_add(dec, "INSTRUCAO", instrucao_dependente);
89 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
90 * precisa ser ajustado para tais limites. Esta função considerará tais
91 * limites no futuro. */
92 static long long total_deducao(struct declaracao *dec)
96 calculo_instrucao(dec);
100 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
101 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
102 printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO")));
103 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
104 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
106 return totais_get(dec, "DEPENDENTES") +
107 totais_get(dec, "INSS") +
108 totais_get(dec, "INSTRUCAO") +
109 totais_get(dec, "MEDICAS") +
110 totais_get(dec, "PREVIDENCIA");
113 static void total_pago(struct declaracao *dec)
115 struct rendimento *rendimento;
117 dec->pago = dec->retido = 0;
118 for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
119 dec->pago += rendimento->imposto;
120 dec->retido += rendimento->imposto;
123 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
124 R(dec->pago), R(dec->retido));
134 static struct taxtable table2015[] = {
136 { 2145324, 750, 160899, },
137 { 3215148, 1500, 402035, },
138 { 4286917, 2250, 723554, },
139 { 5356572, 2750, 991383, },
140 { 9999999999999LL, 0, 0, },
143 static struct taxtable table2016[] = {
145 { 2249914, 750, 168743, },
146 { 3347773, 1500, 419826, },
147 { 4447675, 2250, 753402, },
148 { 5537355, 2750, 1030270, },
149 { 9999999999999LL, 0, 0, },
152 static struct taxtable table2017[] = {
154 { 2284777, 750, 171358, },
155 { 3391981, 1500, 425757, },
156 { 4501261, 2250, 763351, },
157 { 5597616, 2750, 1043232, },
158 { 9999999999999LL, 0, 0, },
161 static struct taxtable *table[ANO(MAX_ANOS)] = {
162 [ANO(2015)] = table2015,
163 [ANO(2016)] = table2016,
164 [ANO(2017)] = table2017,
167 static const long long simples[ANO(MAX_ANOS)] = {
168 [ANO(2015)] = 1588089,
169 [ANO(2016)] = 1675434,
170 [ANO(2017)] = 1675434,
173 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
174 [ANO(2015)] = 2681655,
175 [ANO(2016)] = 2812391,
176 [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */
179 static long long imposto(struct taxtable *tt, long long tr, int verbose)
182 for (i = 0; tr >= tt[i].base; i++);
185 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
186 tt[i].aliquota / 10, R(tt[i].deducao));
188 return tr * tt[i].aliquota / 10000 - tt[i].deducao;
191 static long long imposto_simples(struct declaracao *dec)
195 tt = table[ANO(dec->ano)];
196 tr = totais_get(dec, "RENDTRIB");
197 if (tr / 5 < simples[ANO(dec->ano)])
200 td = simples[ANO(dec->ano)];
201 totais_add(dec, "DESCONTO", td);
205 totais_add(dec, "BASESIMPLES", tr);
207 printf("Desconto simplificado é "FMT_R"\n", R(td));
209 return imposto(tt, tr, dec->verbose);
212 static long long imposto_completa(struct declaracao *dec)
216 tt = table[ANO(dec->ano)];
217 tr = totais_get(dec, "RENDTRIB");
218 td = total_deducao(dec);
219 totais_add(dec, "DEDUCOES", td);
223 totais_add(dec, "BASECOMPLETA", tr);
225 printf("Desconto completa é "FMT_R"\n", R(td));
227 return imposto(tt, tr, dec->verbose);
230 int calcula(struct declaracao *dec)
232 long long i_simples, i_completa;
233 if (!ANO_VALIDO(dec->ano)) {
234 dec_set_error(dec, "Ano %d não suportado.", dec->ano);
237 if (totais_get(dec, "RENDTRIB") > obrigatoriedade[ANO(dec->ano)]) {
239 printf("Declaracao obrigatoria pois rendimento e"
240 "maior que mínimo para declaracao: "
242 R(totais_get(dec, "RENDTRIB")),
243 R(obrigatoriedade[ANO(dec->ano)]));
245 dec->obrigatoria = 1;
247 i_simples = imposto_simples(dec);
248 i_completa = imposto_completa(dec);
251 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
252 R(i_simples), R(i_completa));
254 if (dec->tipo != FORCA_SIMPLES &&
255 (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
256 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
257 dec->tipo = COMPLETA;
258 dec->devido = i_completa;
260 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
262 dec->devido = i_simples;
264 if (dec->pago > dec->devido)
265 dec->restituicao = dec->pago - dec->devido;
267 dec->pagar = dec->devido - dec->pago;
271 static int run_calcula(struct declaracao *dec, char **args, int argc)
273 totais_add(dec, "EXCLUSIVOS_SEM_13o",
274 totais_get(dec, "EXCLUSIVOS") -
275 totais_get(dec, "DECIMOTERCEIRO"));
279 static struct cmd cmd_calcula = {
284 int calcula_cmd_init(void)
286 cmd_add(&cmd_calcula);