Gera critério de obrigatoriedade.
[cascardo/declara.git] / rendimento.c
index d50e259..1f6cb82 100644 (file)
 #include "cmd.h"
 #include "list.h"
 #include "util.h"
-
-struct rendimento {
-       char *cnpj;
-       char *nome;
-       long long rendimento;
-       long long previdencia;
-       long long decimoterceiro;
-       long long imposto;
-       char *saida;
-};
+#include "totais.h"
 
 void rendimento_free(void *pointer)
 {
@@ -48,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;
@@ -55,17 +58,19 @@ static struct rendimento * rendimento_new(char **args)
        rendimento = malloc(sizeof(*rendimento));
        rendimento->cnpj = strdup(args[1]);
        rendimento->nome = strdup(args[2]);
-       rendimento->saida = strdup(args[7]);
+       rendimento->saida = strdup(args[8]);
        r += set_llong(args[3], &rendimento->rendimento);
        r += set_llong(args[4], &rendimento->previdencia);
        r += set_llong(args[5], &rendimento->decimoterceiro);
        r += set_llong(args[6], &rendimento->imposto);
+       r += set_llong(args[7], &rendimento->imposto_13o);
        if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) {
                rendimento_free(rendimento);
                return NULL;
        }
        if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 ||
-           rendimento->decimoterceiro < 0 || rendimento->imposto < 0) {
+           rendimento->decimoterceiro < 0 || rendimento->imposto < 0 ||
+           rendimento->imposto_13o < 0) {
                rendimento_free(rendimento);
                return NULL;
        }
@@ -76,27 +81,34 @@ static int run_rendimento(struct declaracao *dec, char **args, int argc)
 {
        struct rendimento *rendimento;
        int r;
-       if (argc != 8)
+       if (argc != 9)
                return -EINVAL;
        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;
 }
 
-void rendimento_dump(struct declaracao *dec)
+void rendimento_salva(struct declaracao *dec, FILE *f)
 {
        int i;
        struct rendimento *j;
        for (i = 0; j = list_get(dec->rendimento, i); i++)
-               printf("rendimento: %s %s %013lld %013lld %013lld %013lld %s\n",
+               fprintf(f, "rendimento \"%s\" \"%s\" %lld %lld %lld %lld %lld \"%s\"\n",
                        j->cnpj, j->nome, j->rendimento, j->previdencia,
-                       j->decimoterceiro, j->imposto, j->saida);
+                       j->decimoterceiro, j->imposto, j->imposto_13o, j->saida);
 }
 
 static struct cmd cmd_rendimento = {
@@ -109,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;
+}