Mudanças no cálculo das deduções.
[cascardo/declara.git] / lib / calcula.c
index 7b22639..d222f96 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *  Copyright (C) 2015-2016  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 "cmd.h"
 #include "rendimento.h"
 #include "totais.h"
+#include "util.h"
+#include "ano.h"
+
+static const long long dependente[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = 215652,
+       [ANO(2016)] = 227508,
+};
+
+long long deducao_dependente(struct declaracao *dec)
+{
+       if (ANO_VALIDO(dec->ano))
+               return dependente[ANO(dec->ano)];
+       return 0;
+}
 
 /* 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)
 {
-       return totais_get(dec, "INSS") + totais_get(dec, "PAGAMENTOS");
+       int i;
+       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("\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, "MEDICAS") +
+              totais_get(dec, "PREVIDENCIA");
 }
 
 static void total_pago(struct declaracao *dec)
@@ -41,6 +66,10 @@ static void total_pago(struct declaracao *dec)
                dec->pago += rendimento->imposto;
                dec->retido += rendimento->imposto;
        }
+       if (dec->verbose) {
+               printf("Total pago e retido: "FMT_R" "FMT_R"\n",
+                       R(dec->pago), R(dec->retido));
+       }
 }
 
 struct taxtable {
@@ -58,15 +87,39 @@ 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 const long long obrigatoriedade2015 = 2681655;
+static struct taxtable *table[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = table2015,
+       [ANO(2016)] = table2016,
+};
 
-static long long imposto(struct taxtable *tt, long long tr)
+static const long long simples[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = 1588089,
+       [ANO(2016)] = 1675434,
+};
+
+static const long long obrigatoriedade[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = 2681655,
+       [ANO(2016)] = 2812391,
+};
+
+static long long imposto(struct taxtable *tt, long long tr, int verbose)
 {
        int i;
        for (i = 0; tr >= tt[i].base; i++);
        i--;
+       if (verbose) {
+               printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
+                       tt[i].aliquota / 10, R(tt[i].deducao));
+       }
        return tr * tt[i].aliquota / 10000 - tt[i].deducao;
 }
 
@@ -74,47 +127,71 @@ static long long imposto_simples(struct declaracao *dec)
 {
        struct taxtable *tt;
        long long tr, td;
-       tt = table2015;
+       tt = table[ANO(dec->ano)];
        tr = totais_get(dec, "RENDPJ");
-       if (tr / 5 < simples2015)
+       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;
-       totais_add(dec, "BASE", tr);
-       return imposto(tt, tr);
+       if (tr < 0)
+               tr = 0;
+       totais_add(dec, "BASESIMPLES", tr);
+       if (dec->verbose) {
+               printf("Desconto simplificado é "FMT_R"\n", R(td));
+       }
+       return imposto(tt, tr, dec->verbose);
 }
 
 static long long imposto_completa(struct declaracao *dec)
 {
        struct taxtable *tt;
        long long tr, td;
-       if (dec->ano != 2015) {
-               return -EINVAL;
-       }
-       tt = table2015;
+       tt = table[ANO(dec->ano)];
        tr = totais_get(dec, "RENDPJ");
        td = total_deducao(dec);
+       totais_add(dec, "DEDUCOES", td);
        tr -= td;
-       return imposto(tt, tr);
+       if (tr < 0)
+               tr = 0;
+       totais_add(dec, "BASECOMPLETA", tr);
+       if (dec->verbose) {
+               printf("Desconto completa é "FMT_R"\n", R(td));
+       }
+       return imposto(tt, tr, dec->verbose);
 }
 
 int calcula(struct declaracao *dec)
 {
        long long i_simples, i_completa;
-       if (dec->ano != 2015) {
+       if (!ANO_VALIDO(dec->ano)) {
                return -EINVAL;
        }
-       if (totais_get(dec, "RENDPJ") > obrigatoriedade2015)
+       if (totais_get(dec, "RENDPJ") > 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(obrigatoriedade[ANO(dec->ano)]));
+               }
                dec->obrigatoria = 1;
+       }
        i_simples = imposto_simples(dec);
        i_completa = imposto_completa(dec);
        total_pago(dec);
-       if (i_simples > i_completa) {
+       if (dec->verbose) {
+               printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
+                       R(i_simples), R(i_completa));
+       }
+       if (dec->tipo != FORCA_SIMPLES &&
+           (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
+               totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
                dec->tipo = COMPLETA;
                dec->devido = i_completa;
        } else {
+               totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
                dec->tipo = SIMPLES;
                dec->devido = i_simples;
        }