Gera outros totais de declaração simplificada.
[cascardo/declara.git] / calcula.c
index 3e3e841..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 long long total_pago(struct declaracao *dec)
+static void total_pago(struct declaracao *dec)
 {
-       long long tt = 0;
        struct rendimento *rendimento;
        int i;
+       dec->pago = dec->retido = 0;
        for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
-               tt += rendimento->imposto;
+               dec->pago += rendimento->imposto;
+               dec->retido += rendimento->imposto;
        }
-       return tt;
 }
 
 struct taxtable {
@@ -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;
@@ -85,16 +74,15 @@ static long long imposto_simples(struct declaracao *dec)
 {
        struct taxtable *tt;
        long long tr, td;
-       if (dec->ano != 2015) {
-               return -EINVAL;
-       }
        tt = table2015;
-       tr = total_rendimento(dec);
+       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);
 }
 
@@ -106,18 +94,23 @@ 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);
        tr -= td;
        return imposto(tt, tr);
 }
 
-void calcula(struct declaracao *dec)
+int calcula(struct declaracao *dec)
 {
        long long i_simples, i_completa;
+       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);
-       dec->pago = total_pago(dec);
+       total_pago(dec);
        if (i_simples > i_completa) {
                dec->tipo = COMPLETA;
                dec->devido = i_completa;
@@ -125,13 +118,19 @@ void calcula(struct declaracao *dec)
                dec->tipo = SIMPLES;
                dec->devido = i_simples;
        }
-       dec->restituicao = dec->pago - dec->devido;
+       if (dec->pago > dec->devido)
+               dec->restituicao = dec->pago - dec->devido;
+       else
+               dec->pagar = dec->devido - dec->pago;
+       return 0;
 }
 
 static int run_calcula(struct declaracao *dec, char **args, int argc)
 {
-       calcula(dec);
-       return 0;
+       totais_add(dec, "EXCLUSIVOS_SEM_13o",
+               totais_get(dec, "EXCLUSIVOS") -
+               totais_get(dec, "DECIMOTERCEIRO"));
+       return calcula(dec);
 }
 
 static struct cmd cmd_calcula = {