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)] = {
39 static const long long instrucao[ANO(MAX_ANOS)] = {
47 long long deducao_dependente(struct declaracao *dec)
49 if (ANO_VALIDO(dec->ano))
50 return dependente[ANO(dec->ano)];
54 static void calculo_instrucao(struct declaracao *dec)
59 long long instrucao_titular = 0;
60 /* Instrução do titular */
61 for (i = 0; (p = list_get(dec->pagamentos, i)); i++) {
62 if (p->dependente == 0 && pagamento_instrucao(p)) {
63 instrucao_titular += (p->pagamento - p->reembolso);
66 if (instrucao_titular > instrucao[ANO(dec->ano)]) {
67 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
69 totais_add(dec, "INSTRUCAO", instrucao_titular);
71 /* Dependentes com instrução */
72 for (i = 0; (d = list_get(dec->dependentes, i)); i++) {
73 long long instrucao_dependente = 0;
74 for (j = 0; (p = list_get(dec->pagamentos, j)); j++) {
75 if (p->dependente == (i + 1) && pagamento_instrucao(p)) {
76 instrucao_dependente += (p->pagamento - p->reembolso);
79 if (instrucao_dependente) {
80 /* Conta número de dependentes com instrução. */
81 totais_add(dec, "DEPSINSTRUCAO", 1);
83 printf("Dependente %s (%d) tem instrução\n", d->nome, i);
86 if (instrucao_dependente > instrucao[ANO(dec->ano)]) {
87 totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
89 totais_add(dec, "INSTRUCAO", instrucao_dependente);
94 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
95 * precisa ser ajustado para tais limites. Esta função considerará tais
96 * limites no futuro. */
97 static long long total_deducao(struct declaracao *dec)
101 calculo_instrucao(dec);
104 printf("Dedução:\n");
105 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
106 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
107 printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO")));
108 printf("\tMédicas: "FMT_R"\n", R(totais_get(dec, "MEDICAS")));
109 printf("\tPrevidência: "FMT_R"\n", R(totais_get(dec, "PREVIDENCIA")));
110 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
111 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
113 return totais_get(dec, "DEPENDENTES") +
114 totais_get(dec, "INSS") +
115 totais_get(dec, "INSTRUCAO") +
116 totais_get(dec, "MEDICAS") +
117 totais_get(dec, "PREVIDENCIA");
120 static void total_pago(struct declaracao *dec)
122 struct rendimento *rendimento;
124 dec->pago = dec->retido = totais_get(dec, "PAGO");
125 dec->retido -= totais_get(dec, "CARNE");
126 for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
127 dec->pago += rendimento->imposto;
128 dec->retido += rendimento->imposto;
131 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
132 R(dec->pago), R(dec->retido));
142 static struct taxtable table2015[] = {
144 { 2145324, 750, 160899, },
145 { 3215148, 1500, 402035, },
146 { 4286917, 2250, 723554, },
147 { 5356572, 2750, 991383, },
148 { 9999999999999LL, 0, 0, },
151 static struct taxtable table2016[] = {
153 { 2249914, 750, 168743, },
154 { 3347773, 1500, 419826, },
155 { 4447675, 2250, 753402, },
156 { 5537355, 2750, 1030270, },
157 { 9999999999999LL, 0, 0, },
160 static struct taxtable table2017[] = {
162 { 2284777, 750, 171358, },
163 { 3391981, 1500, 425757, },
164 { 4501261, 2250, 763351, },
165 { 5597616, 2750, 1043232, },
166 { 9999999999999LL, 0, 0, },
169 static struct taxtable *table[ANO(MAX_ANOS)] = {
170 [ANO(2015)] = table2015,
171 [ANO(2016)] = table2016,
172 [ANO(2017)] = table2017,
173 [ANO(2018)] = table2017,
174 [ANO(2019)] = table2017,
177 static const long long simples[ANO(MAX_ANOS)] = {
178 [ANO(2015)] = 1588089,
179 [ANO(2016)] = 1675434,
180 [ANO(2017)] = 1675434,
181 [ANO(2018)] = 1675434,
182 [ANO(2019)] = 1675434,
185 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
186 [ANO(2015)] = 2681655,
187 [ANO(2016)] = 2812391,
188 [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */
189 [ANO(2018)] = 2855970,
190 [ANO(2019)] = 2855970,
193 static long long imposto(struct taxtable *tt, long long tr, int verbose)
196 for (i = 0; tr >= tt[i].base; i++);
199 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
200 tt[i].aliquota / 10, R(tt[i].deducao));
202 return tr * tt[i].aliquota / 10000 - tt[i].deducao;
205 static long long imposto_simples(struct declaracao *dec)
209 tt = table[ANO(dec->ano)];
210 tr = totais_get(dec, "RENDTRIB");
211 if (tr / 5 < simples[ANO(dec->ano)])
214 td = simples[ANO(dec->ano)];
215 totais_add(dec, "DESCONTO", td);
219 totais_add(dec, "BASESIMPLES", tr);
221 printf("Desconto simplificado é "FMT_R"\n", R(td));
223 return imposto(tt, tr, dec->verbose);
226 static long long imposto_completa(struct declaracao *dec)
230 tt = table[ANO(dec->ano)];
231 tr = totais_get(dec, "RENDTRIB");
232 td = total_deducao(dec);
233 totais_add(dec, "DEDUCOES", td);
237 totais_add(dec, "BASECOMPLETA", tr);
239 printf("Desconto completa é "FMT_R"\n", R(td));
241 return imposto(tt, tr, dec->verbose);
244 int calcula(struct declaracao *dec)
246 long long i_simples, i_completa;
247 if (!ANO_VALIDO(dec->ano)) {
248 dec_set_error(dec, "Ano %d não suportado.", dec->ano);
251 if (totais_get(dec, "RENDTRIB") > obrigatoriedade[ANO(dec->ano)]) {
253 printf("Declaracao obrigatoria pois rendimento e"
254 "maior que mínimo para declaracao: "
256 R(totais_get(dec, "RENDTRIB")),
257 R(obrigatoriedade[ANO(dec->ano)]));
259 dec->obrigatoria += 1;
261 if (totais_get(dec, "BENS") > 30000000) {
263 printf("Declaracao obrigatoria pois bens e direitos e"
264 " maior que minimo para declaracao: "
266 R(totais_get(dec, "BENS")),
269 dec->obrigatoria += 32;
271 i_simples = imposto_simples(dec);
272 i_completa = imposto_completa(dec);
275 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
276 R(i_simples), R(i_completa));
278 if (dec->tipo != FORCA_SIMPLES &&
279 (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
280 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
281 dec->tipo = COMPLETA;
282 dec->devido = i_completa;
284 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
286 dec->devido = i_simples;
288 if (dec->pago > dec->devido)
289 dec->restituicao = dec->pago - dec->devido;
291 dec->pagar = dec->devido - dec->pago;
292 if (totais_get(dec, "RENDTRIB") == 0)
293 dec->aliquota_efetiva = 0;
295 dec->aliquota_efetiva = dec->devido * 10000 / totais_get(dec, "RENDTRIB");
299 static int run_calcula(struct declaracao *dec, char **args, int argc)
301 totais_add(dec, "EXCLUSIVOS_SEM_13o",
302 totais_get(dec, "EXCLUSIVOS") -
303 totais_get(dec, "DECIMOTERCEIRO"));
307 static struct cmd cmd_calcula = {
312 int calcula_cmd_init(void)
314 cmd_add(&cmd_calcula);