X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=lib%2Fcalcula.c;h=8aa0cc19aae000336a9e394b1b7f8521c4dab7fb;hb=1d63cf7d852bef19b1a4724d22162d176417481d;hp=71b94b45c32e2db9275e10b3e8d179d411df16f9;hpb=2d177b9242186e1dd6832460ff1b2cae282af2d1;p=cascardo%2Fdeclara.git diff --git a/lib/calcula.c b/lib/calcula.c index 71b94b4..8aa0cc1 100644 --- a/lib/calcula.c +++ b/lib/calcula.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * Copyright (C) 2015-2017 Thadeu Lima de Souza Cascardo * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,33 +24,93 @@ #include "rendimento.h" #include "totais.h" #include "util.h" +#include "ano.h" +#include "dependente.h" +#include "pagamento.h" -static const long long dependente2015 = 215652; +static const long long dependente[ANO(MAX_ANOS)] = { + [ANO(2015)] = 215652, + [ANO(2016)] = 227508, + [ANO(2017)] = 227508, +}; + +static const long long instrucao[ANO(MAX_ANOS)] = { + [ANO(2015)] = 337583, + [ANO(2016)] = 356150, + [ANO(2017)] = 356150, +}; long long deducao_dependente(struct declaracao *dec) { - if (dec->ano == 2015) - return dependente2015; + if (ANO_VALIDO(dec->ano)) + return dependente[ANO(dec->ano)]; return 0; } +static void calculo_instrucao(struct declaracao *dec) +{ + int i, j; + struct dependente *d; + struct pagamento *p; + long long instrucao_titular = 0; + /* Instrução do titular */ + for (i = 0; (p = list_get(dec->pagamentos, i)); i++) { + if (p->dependente == 0 && pagamento_instrucao(p)) { + instrucao_titular += (p->pagamento - p->reembolso); + } + } + if (instrucao_titular > instrucao[ANO(dec->ano)]) { + totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]); + } else { + totais_add(dec, "INSTRUCAO", instrucao_titular); + } + /* Dependentes com instrução */ + for (i = 0; (d = list_get(dec->dependentes, i)); i++) { + long long instrucao_dependente = 0; + for (j = 0; (p = list_get(dec->pagamentos, j)); j++) { + if (p->dependente == (i + 1) && pagamento_instrucao(p)) { + instrucao_dependente += (p->pagamento - p->reembolso); + } + } + if (instrucao_dependente) { + /* Conta número de dependentes com instrução. */ + totais_add(dec, "DEPSINSTRUCAO", 1); + if (dec->verbose) { + printf("Dependente %s (%d) tem instrução\n", d->nome, i); + } + } + if (instrucao_dependente > instrucao[ANO(dec->ano)]) { + totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]); + } else { + totais_add(dec, "INSTRUCAO", instrucao_dependente); + } + } +} + /* Alguns totais precisam ser limitados. Portanto, um total de decuções * precisa ser ajustado para tais limites. Esta função considerará tais * limites no futuro. */ static long long total_deducao(struct declaracao *dec) { int i; + + calculo_instrucao(dec); + if (dec->verbose) { printf("Dedução:\n"); printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES"))); printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS"))); + printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO"))); + printf("\tMédicas: "FMT_R"\n", R(totais_get(dec, "MEDICAS"))); + printf("\tPrevidência: "FMT_R"\n", R(totais_get(dec, "PREVIDENCIA"))); printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS"))); printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS"))); } return totais_get(dec, "DEPENDENTES") + totais_get(dec, "INSS") + - totais_get(dec, "PAGAMENTOS") - - totais_get(dec, "REEMBOLSOS"); + totais_get(dec, "INSTRUCAO") + + totais_get(dec, "MEDICAS") + + totais_get(dec, "PREVIDENCIA"); } static void total_pago(struct declaracao *dec) @@ -83,9 +143,41 @@ static struct taxtable table2015[] = { { 9999999999999LL, 0, 0, }, }; -static const long long simples2015 = 1588089; +static struct taxtable table2016[] = { + { 0, 0, 0, }, + { 2249914, 750, 168743, }, + { 3347773, 1500, 419826, }, + { 4447675, 2250, 753402, }, + { 5537355, 2750, 1030270, }, + { 9999999999999LL, 0, 0, }, +}; + +static struct taxtable table2017[] = { + { 0, 0, 0, }, + { 2284777, 750, 171358, }, + { 3391981, 1500, 425757, }, + { 4501261, 2250, 763351, }, + { 5597616, 2750, 1043232, }, + { 9999999999999LL, 0, 0, }, +}; + +static struct taxtable *table[ANO(MAX_ANOS)] = { + [ANO(2015)] = table2015, + [ANO(2016)] = table2016, + [ANO(2017)] = table2017, +}; -static const long long obrigatoriedade2015 = 2681655; +static const long long simples[ANO(MAX_ANOS)] = { + [ANO(2015)] = 1588089, + [ANO(2016)] = 1675434, + [ANO(2017)] = 1675434, +}; + +static const long long obrigatoriedade[ANO(MAX_ANOS)] = { + [ANO(2015)] = 2681655, + [ANO(2016)] = 2812391, + [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */ +}; static long long imposto(struct taxtable *tt, long long tr, int verbose) { @@ -103,14 +195,16 @@ static long long imposto_simples(struct declaracao *dec) { struct taxtable *tt; long long tr, td; - tt = table2015; - tr = totais_get(dec, "RENDPJ"); - if (tr / 5 < simples2015) + tt = table[ANO(dec->ano)]; + tr = totais_get(dec, "RENDTRIB"); + if (tr / 5 < simples[ANO(dec->ano)]) td = tr / 5; else - td = simples2015; + td = simples[ANO(dec->ano)]; totais_add(dec, "DESCONTO", td); tr -= td; + if (tr < 0) + tr = 0; totais_add(dec, "BASESIMPLES", tr); if (dec->verbose) { printf("Desconto simplificado é "FMT_R"\n", R(td)); @@ -122,14 +216,13 @@ static long long imposto_completa(struct declaracao *dec) { struct taxtable *tt; long long tr, td; - if (dec->ano != 2015) { - return -EINVAL; - } - tt = table2015; - tr = totais_get(dec, "RENDPJ"); + tt = table[ANO(dec->ano)]; + tr = totais_get(dec, "RENDTRIB"); td = total_deducao(dec); totais_add(dec, "DEDUCOES", td); tr -= td; + if (tr < 0) + tr = 0; totais_add(dec, "BASECOMPLETA", tr); if (dec->verbose) { printf("Desconto completa é "FMT_R"\n", R(td)); @@ -140,16 +233,17 @@ static long long imposto_completa(struct declaracao *dec) int calcula(struct declaracao *dec) { long long i_simples, i_completa; - if (dec->ano != 2015) { + if (!ANO_VALIDO(dec->ano)) { + dec_set_error(dec, "Ano %d não suportado.", dec->ano); return -EINVAL; } - if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) { + if (totais_get(dec, "RENDTRIB") > obrigatoriedade[ANO(dec->ano)]) { if (dec->verbose) { printf("Declaracao obrigatoria pois rendimento e" "maior que mínimo para declaracao: " FMT_R" > "FMT_R"\n", - R(totais_get(dec, "RENDPJ")), - R(obrigatoriedade2015)); + R(totais_get(dec, "RENDTRIB")), + R(obrigatoriedade[ANO(dec->ano)])); } dec->obrigatoria = 1; }