Cálculo de despesas com instrução.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 10 Apr 2016 19:38:17 +0000 (16:38 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 10 Apr 2016 19:42:56 +0000 (16:42 -0300)
Calcula despesas com instrução, limitados a seus valores por titular e
dependente. Soma tais valores às deduções. Também gera os valores
necessários para as linhas de declaração e totais, como número de
dependentes por instrução e total de despesas dedutíves com instrução.

lib/calcula.c
lib/gera.c
lib/pagamento.c
lib/pagamento.h

index d222f96..e9fe3d8 100644 (file)
 #include "totais.h"
 #include "util.h"
 #include "ano.h"
+#include "dependente.h"
+#include "pagamento.h"
 
 static const long long dependente[ANO(MAX_ANOS)] = {
        [ANO(2015)] = 215652,
        [ANO(2016)] = 227508,
 };
 
+static const long long instrucao[ANO(MAX_ANOS)] = {
+       [ANO(2015)] = 337583,
+       [ANO(2016)] = 356150,
+};
+
 long long deducao_dependente(struct declaracao *dec)
 {
        if (ANO_VALIDO(dec->ano))
@@ -38,21 +45,65 @@ long long deducao_dependente(struct declaracao *dec)
        return 0;
 }
 
+static void calculo_instrucao(struct declaracao *dec)
+{
+       int i, j;
+       struct dependente *d;
+       struct pagamento *p;
+       long long instrucao_titular = 0;
+       /* Instrução do titular */
+       for (i = 0; (p = list_get(dec->pagamentos, i)); i++) {
+               if (p->dependente == 0 && pagamento_instrucao(p)) {
+                       instrucao_titular += (p->pagamento - p->reembolso);
+               }
+       }
+       if (instrucao_titular > instrucao[ANO(dec->ano)]) {
+               totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
+       } else {
+               totais_add(dec, "INSTRUCAO", instrucao_titular);
+       }
+       /* Dependentes com instrução */
+       for (i = 0; (d = list_get(dec->dependentes, i)); i++) {
+               long long instrucao_dependente = 0;
+               for (j = 0; (p = list_get(dec->pagamentos, j)); j++) {
+                       if (p->dependente == (i + 1) && pagamento_instrucao(p)) {
+                               instrucao_dependente += (p->pagamento - p->reembolso);
+                       }
+               }
+               if (instrucao_dependente) {
+                       totais_add(dec, "DEPSINSTRUCAO", 1);
+                       if (dec->verbose) {
+                               printf("Dependente %s (%d) tem instrução\n", d->nome, i);
+                       }
+               }
+               if (instrucao_dependente > instrucao[ANO(dec->ano)]) {
+                       totais_add(dec, "INSTRUCAO", instrucao[ANO(dec->ano)]);
+               } else {
+                       totais_add(dec, "INSTRUCAO", instrucao_dependente);
+               }
+       }
+}
+
 /* 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)
 {
        int i;
+
+       calculo_instrucao(dec);
+
        if (dec->verbose) {
                printf("Dedução:\n");
                printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
                printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
+               printf("\tInstrução: "FMT_R"\n", R(totais_get(dec, "INSTRUCAO")));
                printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
                printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
        }
        return totais_get(dec, "DEPENDENTES") +
               totais_get(dec, "INSS") +
+              totais_get(dec, "INSTRUCAO") +
               totais_get(dec, "MEDICAS") +
               totais_get(dec, "PREVIDENCIA");
 }
index 2a2e8ee..ed83b1a 100644 (file)
@@ -369,7 +369,7 @@ static void gera_completa(struct declaracao *dec, FILE *f)
        fprintf(f, "%013lld", 0);
        fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO")); /* TODO: 13o titular */
        fprintf(f, "%013lld", 0); /* TODO: 13o dependente */
-       fprintf(f, "%05d", 0); /* TODO: dependentes com instrucao */
+       fprintf(f, "%05d", totais_get(dec, "DEPSINSTRUCAO")); /* dependentes com instrucao */
        fprintf(f, "%05d", 0); /* TODO: alimentandos com instrucao */
        fprintf(f, "%013lld", 0); /* TODO: rendimentos PF titular */
        fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
@@ -401,7 +401,7 @@ static void gera_totais_completa(struct declaracao *dec, FILE *f)
        /* TODO (2016): somar FUNPRESP acima do limite */
        fprintf(f, "%013lld", totais_get(dec, "PREVIDENCIA")); /* previdencia privada */
        fprintf(f, "%013lld", totais_get(dec, "DEPENDENTES")); /* deducao dependentes */
-       fprintf(f, "%013lld", 0); /* TODO: deducao instrucao */
+       fprintf(f, "%013lld", totais_get(dec, "INSTRUCAO")); /* deducao instrucao */
        fprintf(f, "%013lld", totais_get(dec, "MEDICAS")); /* despesas medicas */
        fprintf(f, "%013lld", 0); /* TODO: pensao */
        fprintf(f, "%013lld", 0); /* TODO: pensao escritura publica */
index 821a3f1..959a02c 100644 (file)
@@ -185,6 +185,17 @@ static int pagamento_medico(struct pagamento *pagamento)
        return 0;
 }
 
+int pagamento_instrucao(struct pagamento *pagamento)
+{
+       switch (pagamento->codigo) {
+       case 1:
+               return 1;
+       default:
+               return 0;
+       }
+       return 0;
+}
+
 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
 {
        return pagamento_cnpj_ordenado_cond(dec, pagamento_medico, n);
index adf105f..dcdfbf7 100644 (file)
@@ -42,4 +42,6 @@ char * pagamento_cnpj_ordenado(struct declaracao *dec, int n);
 char * medico_cnpj_ordenado(struct declaracao *dec, int n);
 char * inss_cnpj_ordenado(struct declaracao *dec, int n);
 
+int pagamento_instrucao(struct pagamento *pagamento);
+
 #endif