Campo resultado de imposto no cabeƧalho
[cascardo/declara.git] / calcula.c
index 3e3e841..a8accc3 100644 (file)
--- a/calcula.c
+++ b/calcula.c
@@ -23,7 +23,7 @@
 #include <errno.h>
 #include <stdio.h>
 
-static long long total_rendimento(struct declaracao *dec)
+static void total_rendimento(struct declaracao *dec)
 {
        long long tr = 0;
        struct rendimento *rendimento;
@@ -31,7 +31,7 @@ static long long total_rendimento(struct declaracao *dec)
        for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
                tr += rendimento->rendimento;
        }
-       return tr;
+       dec->totalrendimento = tr;
 }
 
 static long long total_deducao(struct declaracao *dec)
@@ -45,15 +45,15 @@ static long long total_deducao(struct declaracao *dec)
        return td;
 }
 
-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 {
@@ -85,11 +85,9 @@ 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 = dec->totalrendimento;
+       dec->totalrendimento = tr;
        if (tr / 5 < simples2015)
                td = tr / 5;
        else
@@ -106,18 +104,22 @@ static long long imposto_completa(struct declaracao *dec)
                return -EINVAL;
        }
        tt = table2015;
-       tr = total_rendimento(dec);
+       tr = dec->totalrendimento;
        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;
+       }
+       total_rendimento(dec);
        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 +127,16 @@ 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;
+       return calcula(dec);
 }
 
 static struct cmd cmd_calcula = {