Assume mesmos limites em 2021 comparados com 2020.
[cascardo/declara.git] / lib / calcula.c
index c928a02..40c0536 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2015-2016  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *  Copyright (C) 2015-2017  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
  *
  *  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
 #include "totais.h"
 #include "util.h"
 #include "ano.h"
+#include "dependente.h"
+#include "pagamento.h"
 
 static const long long dependente[ANO(MAX_ANOS)] = {
        [ANO(2015)] = 215652,
        [ANO(2016)] = 227508,
+       [ANO(2017)] = 227508,
+       [ANO(2018)] = 227508,
+       [ANO(2019)] = 227508,
+       [ANO(2020)] = 227508,
+       [ANO(2021)] = 227508,
+};
+
+static const long long instrucao[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = 337583,
+       [ANO(2016)] = 356150,
+       [ANO(2017)] = 356150,
+       [ANO(2018)] = 356150,
+       [ANO(2019)] = 356150,
+       [ANO(2020)] = 356150,
+       [ANO(2021)] = 356150,
 };
 
 long long deducao_dependente(struct declaracao *dec)
@@ -38,30 +55,78 @@ long long deducao_dependente(struct declaracao *dec)
        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)
 {
        struct rendimento *rendimento;
        int i;
-       dec->pago = dec->retido = 0;
+       dec->pago = dec->retido = totais_get(dec, "PAGO");
+       dec->retido -= totais_get(dec, "CARNE");
        for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
                dec->pago += rendimento->imposto;
                dec->retido += rendimento->imposto;
@@ -96,19 +161,43 @@ static struct taxtable table2016[] = {
        { 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,
+       [ANO(2018)] = table2017,
+       [ANO(2019)] = table2017,
+       [ANO(2020)] = table2017,
+       [ANO(2021)] = table2017,
 };
 
 static const long long simples[ANO(MAX_ANOS)] = {
        [ANO(2015)] = 1588089,
        [ANO(2016)] = 1675434,
+       [ANO(2017)] = 1675434,
+       [ANO(2018)] = 1675434,
+       [ANO(2019)] = 1675434,
+       [ANO(2020)] = 1675434,
+       [ANO(2021)] = 1675434,
 };
 
 static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
        [ANO(2015)] = 2681655,
        [ANO(2016)] = 2812391,
+       [ANO(2017)] = 2855970, /* De acordo com IN 1671/2016 */
+       [ANO(2018)] = 2855970,
+       [ANO(2019)] = 2855970,
+       [ANO(2020)] = 2855970,
+       [ANO(2021)] = 2855970,
 };
 
 static long long imposto(struct taxtable *tt, long long tr, int verbose)
@@ -128,7 +217,7 @@ static long long imposto_simples(struct declaracao *dec)
        struct taxtable *tt;
        long long tr, td;
        tt = table[ANO(dec->ano)];
-       tr = totais_get(dec, "RENDPJ");
+       tr = totais_get(dec, "RENDTRIB");
        if (tr / 5 < simples[ANO(dec->ano)])
                td = tr / 5;
        else
@@ -149,7 +238,7 @@ static long long imposto_completa(struct declaracao *dec)
        struct taxtable *tt;
        long long tr, td;
        tt = table[ANO(dec->ano)];
-       tr = totais_get(dec, "RENDPJ");
+       tr = totais_get(dec, "RENDTRIB");
        td = total_deducao(dec);
        totais_add(dec, "DEDUCOES", td);
        tr -= td;
@@ -165,18 +254,40 @@ static long long imposto_completa(struct declaracao *dec)
 int calcula(struct declaracao *dec)
 {
        long long i_simples, i_completa;
+       long long isentos;
        if (!ANO_VALIDO(dec->ano)) {
+               dec_set_error(dec, "Ano %d não suportado.", dec->ano);
                return -EINVAL;
        }
-       if (totais_get(dec, "RENDPJ") > obrigatoriedade[ANO(dec->ano)]) {
+       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(totais_get(dec, "RENDTRIB")),
                                R(obrigatoriedade[ANO(dec->ano)]));
                }
-               dec->obrigatoria = 1;
+               dec->obrigatoria += 1;
+       }
+       isentos = totais_get(dec, "ISENTOS") + totais_get(dec, "EXCLUSIVOS");
+       if (isentos > 4000000) {
+               if (dec->verbose) {
+                       printf("Declaracao obrigatoria pois rendimentos "
+                               "isentos e exclusivos maior que minimo para "
+                               "declaracao: " FMT_R" > "FMT_R"\n",
+                               R(isentos), R(4000000));
+               }
+               dec->obrigatoria += 2;
+       }
+       if (totais_get(dec, "BENS") > 30000000) {
+               if (dec->verbose) {
+                       printf("Declaracao obrigatoria pois bens e direitos e"
+                               " maior que minimo para declaracao: "
+                               FMT_R" > "FMT_R"\n",
+                               R(totais_get(dec, "BENS")),
+                               R(30000000));
+               }
+               dec->obrigatoria += 32;
        }
        i_simples = imposto_simples(dec);
        i_completa = imposto_completa(dec);
@@ -199,6 +310,10 @@ int calcula(struct declaracao *dec)
                dec->restituicao = dec->pago - dec->devido;
        else
                dec->pagar = dec->devido - dec->pago;
+       if (totais_get(dec, "RENDTRIB") == 0)
+               dec->aliquota_efetiva = 0;
+       else
+               dec->aliquota_efetiva = dec->devido * 10000 / totais_get(dec, "RENDTRIB");
        return 0;
 }