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)] = {
40 static const long long instrucao[ANO(MAX_ANOS)] = {
49 long long deducao_dependente(struct declaracao *dec)
51 if (ANO_VALIDO(dec->ano))
52 return dependente[ANO(dec->ano)];
56 static void calculo_instrucao(struct declaracao *dec)
61 long long instrucao_titular = 0;
62 /* Instrução do titular */
63 for (i = 0; (p = list_get(dec->pagamentos, i)); i++) {
64 if (p->dependente == 0 && pagamento_instrucao(p)) {
65 instrucao_titular += (p->pagamento - p->reembolso);
68 if (instrucao_titular > instrucao[ANO(dec->ano)]) {
69 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
71 totais_add(dec, "INSTRUCAO", instrucao_titular);
73 /* Dependentes com instrução */
74 for (i = 0; (d = list_get(dec->dependentes, i)); i++) {
75 long long instrucao_dependente = 0;
76 for (j = 0; (p = list_get(dec->pagamentos, j)); j++) {
77 if (p->dependente == (i + 1) && pagamento_instrucao(p)) {
78 instrucao_dependente += (p->pagamento - p->reembolso);
81 if (instrucao_dependente) {
82 /* Conta número de dependentes com instrução. */
83 totais_add(dec, "DEPSINSTRUCAO", 1);
85 printf("Dependente %s (%d) tem instrução\n", d->nome, i);
88 if (instrucao_dependente > instrucao[ANO(dec->ano)]) {
89 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
91 totais_add(dec, "INSTRUCAO", instrucao_dependente);
96 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
97 * precisa ser ajustado para tais limites. Esta função considerará tais
98 * limites no futuro. */
99 static long long total_deducao(struct declaracao *dec)
103 calculo_instrucao(dec);
106 printf("Dedução:\n");
107 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
108 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
109 printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO")));
110 printf("\tMédicas: "FMT_R"\n", R(totais_get(dec, "MEDICAS")));
111 printf("\tPrevidência: "FMT_R"\n", R(totais_get(dec, "PREVIDENCIA")));
112 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
113 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
115 return totais_get(dec, "DEPENDENTES") +
116 totais_get(dec, "INSS") +
117 totais_get(dec, "INSTRUCAO") +
118 totais_get(dec, "MEDICAS") +
119 totais_get(dec, "PREVIDENCIA");
122 static void total_pago(struct declaracao *dec)
124 struct rendimento *rendimento;
126 dec->pago = dec->retido = totais_get(dec, "PAGO");
127 dec->retido -= totais_get(dec, "CARNE");
128 for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
129 dec->pago += rendimento->imposto;
130 dec->retido += rendimento->imposto;
133 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
134 R(dec->pago), R(dec->retido));
144 static struct taxtable table2015[] = {
146 { 2145324, 750, 160899, },
147 { 3215148, 1500, 402035, },
148 { 4286917, 2250, 723554, },
149 { 5356572, 2750, 991383, },
150 { 9999999999999LL, 0, 0, },
153 static struct taxtable table2016[] = {
155 { 2249914, 750, 168743, },
156 { 3347773, 1500, 419826, },
157 { 4447675, 2250, 753402, },
158 { 5537355, 2750, 1030270, },
159 { 9999999999999LL, 0, 0, },
162 static struct taxtable table2017[] = {
164 { 2284777, 750, 171358, },
165 { 3391981, 1500, 425757, },
166 { 4501261, 2250, 763351, },
167 { 5597616, 2750, 1043232, },
168 { 9999999999999LL, 0, 0, },
171 static struct taxtable *table[ANO(MAX_ANOS)] = {
172 [ANO(2015)] = table2015,
173 [ANO(2016)] = table2016,
174 [ANO(2017)] = table2017,
175 [ANO(2018)] = table2017,
176 [ANO(2019)] = table2017,
177 [ANO(2020)] = table2017,
180 static const long long simples[ANO(MAX_ANOS)] = {
181 [ANO(2015)] = 1588089,
182 [ANO(2016)] = 1675434,
183 [ANO(2017)] = 1675434,
184 [ANO(2018)] = 1675434,
185 [ANO(2019)] = 1675434,
186 [ANO(2020)] = 1675434,
189 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
190 [ANO(2015)] = 2681655,
191 [ANO(2016)] = 2812391,
192 [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */
193 [ANO(2018)] = 2855970,
194 [ANO(2019)] = 2855970,
195 [ANO(2020)] = 2855970,
198 static long long imposto(struct taxtable *tt, long long tr, int verbose)
201 for (i = 0; tr >= tt[i].base; i++);
204 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
205 tt[i].aliquota / 10, R(tt[i].deducao));
207 return tr * tt[i].aliquota / 10000 - tt[i].deducao;
210 static long long imposto_simples(struct declaracao *dec)
214 tt = table[ANO(dec->ano)];
215 tr = totais_get(dec, "RENDTRIB");
216 if (tr / 5 < simples[ANO(dec->ano)])
219 td = simples[ANO(dec->ano)];
220 totais_add(dec, "DESCONTO", td);
224 totais_add(dec, "BASESIMPLES", tr);
226 printf("Desconto simplificado é "FMT_R"\n", R(td));
228 return imposto(tt, tr, dec->verbose);
231 static long long imposto_completa(struct declaracao *dec)
235 tt = table[ANO(dec->ano)];
236 tr = totais_get(dec, "RENDTRIB");
237 td = total_deducao(dec);
238 totais_add(dec, "DEDUCOES", td);
242 totais_add(dec, "BASECOMPLETA", tr);
244 printf("Desconto completa é "FMT_R"\n", R(td));
246 return imposto(tt, tr, dec->verbose);
249 int calcula(struct declaracao *dec)
251 long long i_simples, i_completa;
253 if (!ANO_VALIDO(dec->ano)) {
254 dec_set_error(dec, "Ano %d não suportado.", dec->ano);
257 if (totais_get(dec, "RENDTRIB") > obrigatoriedade[ANO(dec->ano)]) {
259 printf("Declaracao obrigatoria pois rendimento e"
260 "maior que mínimo para declaracao: "
262 R(totais_get(dec, "RENDTRIB")),
263 R(obrigatoriedade[ANO(dec->ano)]));
265 dec->obrigatoria += 1;
267 isentos = totais_get(dec, "ISENTOS") + totais_get(dec, "EXCLUSIVOS");
268 if (isentos > 4000000) {
270 printf("Declaracao obrigatoria pois rendimentos "
271 "isentos e exclusivos maior que minimo para "
272 "declaracao: " FMT_R" > "FMT_R"\n",
273 R(isentos), R(4000000));
275 dec->obrigatoria += 2;
277 if (totais_get(dec, "BENS") > 30000000) {
279 printf("Declaracao obrigatoria pois bens e direitos e"
280 " maior que minimo para declaracao: "
282 R(totais_get(dec, "BENS")),
285 dec->obrigatoria += 32;
287 i_simples = imposto_simples(dec);
288 i_completa = imposto_completa(dec);
291 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
292 R(i_simples), R(i_completa));
294 if (dec->tipo != FORCA_SIMPLES &&
295 (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
296 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
297 dec->tipo = COMPLETA;
298 dec->devido = i_completa;
300 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
302 dec->devido = i_simples;
304 if (dec->pago > dec->devido)
305 dec->restituicao = dec->pago - dec->devido;
307 dec->pagar = dec->devido - dec->pago;
308 if (totais_get(dec, "RENDTRIB") == 0)
309 dec->aliquota_efetiva = 0;
311 dec->aliquota_efetiva = dec->devido * 10000 / totais_get(dec, "RENDTRIB");
315 static int run_calcula(struct declaracao *dec, char **args, int argc)
317 totais_add(dec, "EXCLUSIVOS_SEM_13o",
318 totais_get(dec, "EXCLUSIVOS") -
319 totais_get(dec, "DECIMOTERCEIRO"));
323 static struct cmd cmd_calcula = {
328 int calcula_cmd_init(void)
330 cmd_add(&cmd_calcula);