Utiliza CNPJ de planos de saúde no cabeçalho.
[cascardo/declara.git] / rendimento.c
index 80311db..1f6cb82 100644 (file)
@@ -25,6 +25,7 @@
 #include "cmd.h"
 #include "list.h"
 #include "util.h"
+#include "totais.h"
 
 void rendimento_free(void *pointer)
 {
@@ -38,6 +39,18 @@ void rendimento_free(void *pointer)
        free(rendimento);
 }
 
+static int rendimento_cmp(void *p1, void *p2)
+{
+       struct rendimento *r1 = p1;
+       struct rendimento *r2 = p2;
+       /* O rendimento maior vem primeiro. */
+       if (r1->rendimento > r2->rendimento)
+               return -1;
+       else if (r1->rendimento < r2->rendimento)
+               return 1;
+       return 0;
+}
+
 static struct rendimento * rendimento_new(char **args)
 {
        struct rendimento *rendimento;
@@ -73,11 +86,18 @@ static int run_rendimento(struct declaracao *dec, char **args, int argc)
        rendimento = rendimento_new(args);
        if (!rendimento)
                return -ENOMEM;
-       r = list_add(&dec->rendimento, rendimento);
+       r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp);
        if (r < 0) {
                rendimento_free(rendimento);
                return r;
        }
+       r = totais_add(dec, "RENDPJ", rendimento->rendimento);
+       r += totais_add(dec, "RENDPJTIT", rendimento->rendimento);
+       r += totais_add(dec, "INSS", rendimento->previdencia);
+       if (r) {
+               rendimento_free(rendimento);
+               return r;
+       }
        return 0;
 }
 
@@ -101,3 +121,12 @@ int rendimento_cmd_init(void)
        cmd_add(&cmd_rendimento);
        return 0;
 }
+
+char * rendimento_cnpj_ordenado(struct declaracao *dec, int i)
+{
+       struct rendimento *rendimento;
+       rendimento = list_get(dec->rendimento, i);
+       if (!rendimento)
+               return "";
+       return rendimento->cnpj;
+}