Insere bens e rendimentos na ordem em que aparecem.
[cascardo/declara.git] / lib / isento.c
index f078804..ab023d7 100644 (file)
@@ -32,26 +32,53 @@ static int isento_totais_update(struct declaracao *dec, struct isento *isento)
        int r = 0;
        switch (isento->codigo) {
        case 82:
+               isento->exclusivo = 0;
                r = totais_add(dec, "DOACOES", isento->valor);
-               r += totais_add(dec, "ISENTOS", isento->valor);
-               r += totais_add(dec, "ISENTOSTIT", isento->valor);
                break;
+       case 93:
+               isento->exclusivo = 0;
+               r = totais_add(dec, "INDENIZACOES", isento->valor);
+               break;
+       case 11:
        case 96:
+               isento->exclusivo = 1;
                r = totais_add(dec, "PLR", isento->valor);
-               r += totais_add(dec, "EXCLUSIVOS", isento->valor);
-               r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
                break;
+       case 97:
+       case 26:
+               isento->exclusivo = 0;
+               r = totais_add(dec, "OUTROSISENTOS", isento->valor);
+               break;
+       case 12:
        case 98:
+               isento->exclusivo = 0;
                r = totais_add(dec, "POUPANCA", isento->valor);
-               r += totais_add(dec, "ISENTOS", isento->valor);
-               r += totais_add(dec, "ISENTOSTIT", isento->valor);
                break;
+       case 13:
+               isento->exclusivo = 0;
+               r = totais_add(dec, "LUCROME", isento->valor);
+               break;
+       case 6:
        case 99:
+               isento->exclusivo = 1;
                r = totais_add(dec, "APLICACOES", isento->valor);
-               r += totais_add(dec, "EXCLUSIVOS", isento->valor);
-               r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
                break;
        }
+       if (isento->exclusivo) {
+               r += totais_add(dec, "EXCLUSIVOS", isento->valor);
+               if (isento->dependente) {
+                       r += totais_add(dec, "EXCLUSIVOSDEP", isento->valor);
+               } else {
+                       r += totais_add(dec, "EXCLUSIVOSTIT", isento->valor);
+               }
+       } else {
+               r += totais_add(dec, "ISENTOS", isento->valor);
+               if (isento->dependente) {
+                       r += totais_add(dec, "ISENTOSDEP", isento->valor);
+               } else {
+                       r += totais_add(dec, "ISENTOSTIT", isento->valor);
+               }
+       }
        return r;
 }
 
@@ -62,6 +89,8 @@ void isento_free(void *pointer)
                free(isento->cnpj);
        if (isento->nome)
                free(isento->nome);
+       if (isento->descricao)
+               free(isento->descricao);
        free(isento);
 }
 
@@ -69,6 +98,8 @@ static int isento_cmp(void *p1, void *p2)
 {
        struct isento *r1 = p1;
        struct isento *r2 = p2;
+       if (r1->exclusivo != r2->exclusivo)
+               return r2->exclusivo - r1->exclusivo;
        /* O rendimento maior vem primeiro. */
        if (r1->valor > r2->valor)
                return -1;
@@ -77,25 +108,44 @@ static int isento_cmp(void *p1, void *p2)
        return 0;
 }
 
-static struct isento * isento_new(char **args)
+static struct isento * isento_new(char **args, int argc)
 {
        struct isento *isento;
        int r = 0;
        isento = malloc(sizeof(*isento));
-       isento->cnpj = strdup(args[2]);
-       isento->nome = strdup(args[3]);
-       /* TODO: consertar set_int para funcionar como set_llong */
-       r += set_int(args, 2, &isento->codigo);
+
+       isento->exclusivo = 0;
+       if (!strcmp(args[0], "exclusivo")) {
+               isento->exclusivo = 1;
+       }
+
+       r += set_int(args[1], &isento->codigo);
+       r += set_string(args[2], &isento->cnpj);
+       r += set_string(args[3], &isento->nome);
        r += set_llong(args[4], &isento->valor);
-       if (!isento->cnpj || !isento->nome) {
+       if (r < 0 || isento->codigo < 0 ||
+           isento->valor < 0) {
                isento_free(isento);
                return NULL;
        }
-       if (r < 0 || isento->codigo < 0 ||
-           isento->valor < 0) {
+       if (argc == 6) {
+               r = set_int(args[5], &isento->dependente);
+       } else {
+               isento->dependente = 0;
+       }
+       if (r < 0 || isento->dependente < 0) {
                isento_free(isento);
                return NULL;
        }
+       if (argc == 7) {
+               r = set_string(args[6], &isento->descricao);
+               if (r < 0) {
+                       isento_free(isento);
+                       return NULL;
+               }
+       } else {
+               isento->descricao = NULL;
+       }
        return isento;
 }
 
@@ -103,21 +153,25 @@ static int run_isento(struct declaracao *dec, char **args, int argc)
 {
        struct isento *isento;
        int r;
-       if (argc != 5)
+       if (argc < 5 || argc > 7)
                return -EINVAL;
-       isento = isento_new(args);
+       isento = isento_new(args, argc);
        if (!isento)
                return -ENOMEM;
-       r = list_insert_ordered(&dec->isentos, isento, isento_cmp);
-       if (r < 0) {
+       if (isento->dependente > list_size(dec->dependentes)) {
                isento_free(isento);
-               return r;
+               return -EINVAL;
        }
        r = isento_totais_update(dec, isento);
        if (r) {
                isento_free(isento);
                return r;
        }
+       r = list_add(&dec->isentos, isento);
+       if (r < 0) {
+               isento_free(isento);
+               return r;
+       }
        return 0;
 }
 
@@ -125,9 +179,17 @@ void isento_salva(struct declaracao *dec, FILE *f)
 {
        int i;
        struct isento *j;
-       for (i = 0; j = list_get(dec->isentos, i); i++)
-               fprintf(f, "isento %d \"%s\" \"%s\" %lld\n",
-                       j->codigo, j->cnpj, j->nome, j->valor);
+       for (i = 0; j = list_get(dec->isentos, i); i++) {
+               if (j->codigo == 97 && j->exclusivo)
+                       fprintf(f, "exclusivo ");
+               else
+                       fprintf(f, "isento ");
+               fprintf(f, "%d \"%s\" \"%s\" %lld %d",
+                       j->codigo, j->cnpj, j->nome, j->valor, j->dependente);
+               if (j->descricao)
+                       fprintf(f, " \"%s\"", j->descricao);
+               fprintf(f, "\n");
+       }
 }
 
 static struct cmd cmd_isento = {
@@ -135,9 +197,15 @@ static struct cmd cmd_isento = {
        .run = run_isento,
 };
 
+static struct cmd cmd_exclusivo = {
+       .name = "exclusivo",
+       .run = run_isento,
+};
+
 int isento_cmd_init(void)
 {
        cmd_add(&cmd_isento);
+       cmd_add(&cmd_exclusivo);
        return 0;
 }