Gera outros totais de declaração simplificada.
[cascardo/declara.git] / calcula.c
index a6bac0e..7b22639 100644 (file)
--- a/calcula.c
+++ b/calcula.c
  */
 
 #include "calcula.h"
+#include <errno.h>
+#include <stdio.h>
 #include "declaracao.h"
 #include "cmd.h"
 #include "rendimento.h"
-#include <errno.h>
-#include <stdio.h>
-
-static long long total_rendimento(struct declaracao *dec)
-{
-       long long tr = 0;
-       struct rendimento *rendimento;
-       int i;
-       for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
-               tr += rendimento->rendimento;
-       }
-       return tr;
-}
+#include "totais.h"
 
+/* 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)
 {
-       long long td = 0;
-       struct rendimento *rendimento;
-       int i;
-       for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
-               td += rendimento->previdencia;
-       }
-       return td;
+       return totais_get(dec, "INSS") + totais_get(dec, "PAGAMENTOS");
 }
 
 static void total_pago(struct declaracao *dec)
@@ -73,6 +60,8 @@ static struct taxtable table2015[] = {
 
 static const long long simples2015 = 1588089;
 
+static const long long obrigatoriedade2015 = 2681655;
+
 static long long imposto(struct taxtable *tt, long long tr)
 {
        int i;
@@ -86,13 +75,14 @@ static long long imposto_simples(struct declaracao *dec)
        struct taxtable *tt;
        long long tr, td;
        tt = table2015;
-       tr = total_rendimento(dec);
-       dec->totalrendimento = tr;
+       tr = totais_get(dec, "RENDPJ");
        if (tr / 5 < simples2015)
                td = tr / 5;
        else
                td = simples2015;
+       totais_add(dec, "DESCONTO", td);
        tr -= td;
+       totais_add(dec, "BASE", tr);
        return imposto(tt, tr);
 }
 
@@ -104,9 +94,8 @@ static long long imposto_completa(struct declaracao *dec)
                return -EINVAL;
        }
        tt = table2015;
-       tr = total_rendimento(dec);
+       tr = totais_get(dec, "RENDPJ");
        td = total_deducao(dec);
-       dec->totalrendimento = tr;
        tr -= td;
        return imposto(tt, tr);
 }
@@ -117,6 +106,8 @@ int calcula(struct declaracao *dec)
        if (dec->ano != 2015) {
                return -EINVAL;
        }
+       if (totais_get(dec, "RENDPJ") > obrigatoriedade2015)
+               dec->obrigatoria = 1;
        i_simples = imposto_simples(dec);
        i_completa = imposto_completa(dec);
        total_pago(dec);
@@ -136,6 +127,9 @@ int calcula(struct declaracao *dec)
 
 static int run_calcula(struct declaracao *dec, char **args, int argc)
 {
+       totais_add(dec, "EXCLUSIVOS_SEM_13o",
+               totais_get(dec, "EXCLUSIVOS") -
+               totais_get(dec, "DECIMOTERCEIRO"));
        return calcula(dec);
 }