+ fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimentos PJ titular */
+ fprintf(f, "%013lld", totais_get(dec, "RENDPFTIT")); /* TODO: rendimentos PF titular */
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
+ fprintf(f, "%013lld", 0); /* TODO: resultado atividade rural */
+ fprintf(f, "%013lld", totais_get(dec, "RENDTRIB")); /* total rendimentos tributáveis */
+ /* TODO (2016): somar FUNPRESP até o limite */
+ fprintf(f, "%013lld", totais_get(dec, "INSS")); /* previdencia oficial */
+ /* 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", 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 */
+ fprintf(f, "%013lld", 0); /* TODO: livro caixa */
+ fprintf(f, "%013lld", totais_get(dec, "DEDUCOES")); /* total deduções */
+ fprintf(f, "%013lld", totais_get(dec, "BASE")); /* base de cálculo */
+ fprintf(f, "%013lld", dec->devido); /* TODO: valor do imposto */
+ fprintf(f, "%013lld", 0); /* TODO: deducao do imposto (limitado a 6%) */
+ fprintf(f, "%013lld", dec->devido); /* imposto devido */
+ fprintf(f, "%013lld", 0); /* TODO: contribuicao patronal */
+ fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido menos patronal */
+ fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido mais RRA */
+ fprintf(f, "%013lld", dec->retido); /* imposto na fonte */
+ fprintf(f, "%013lld", totais_get(dec, "RENDPF")); /* TODO: carne leao */
+
+ fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
+ fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
+ fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
+ fprintf(f, "%013lld", dec->retido); /* TODO: total imposto pago */
+ fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
+ fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
+ fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
+ fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
+
+ fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */
+ fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */
+ fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
+ fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
+ if (dec->ano == 2015) {
+ fprintf(f, "%013lld", dec->conjuge.total); /* conjuge */
+ } else if (dec->ano >= 2016) {
+ fprintf(f, "%013lld", 0); /* FILLER */
+ }
+ fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */
+ fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */
+ fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
+ fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
+ fprintf(f, "%013lld", 0); /* TODO: imposto renda variável */
+
+ fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
+ fprintf(f, "%013lld", 0); /* TODO: imposto fonte dependente */
+ fprintf(f, "%013lld", 0); /* TODO: imposto moeda estrangeira */
+ fprintf(f, "%013lld", 0); /* TODO: imposto ganhos capital moeda */
+ fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */
+ fprintf(f, "%013lld", totais_get(dec, "ISENTOSDEP")); /* rendimentos isentos dependentes */
+ fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */
+ fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSDEP")); /* tributação exclusiva dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: doações campanha */
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
+ fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
+
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
+ fprintf(f, "%013lld", 0); /* TODO: previdencia RRA titular */
+ fprintf(f, "%013lld", 0); /* TODO: pensao RRA titular */
+ fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
+ fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: previdencia RRA dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: pensao RRA dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
+
+ fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
+ fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
+ fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
+ fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
+ if (dec->ano >= 2018) {
+ fprintf(f, "%05lld", dec->aliquota_efetiva);
+ }
+}
+
+static void gera_isentos(struct declaracao *dec, FILE *f)
+{
+ fprintf(f, "23");
+ fprintf(f, "%-11.11s", dec->cpf);
+ fprintf(f, "%013lld", 0); /* TODO: bolsa de estudo */
+ fprintf(f, "%013lld", 0); /* TODO: pecúlio de previdência privada */
+ fprintf(f, "%013lld", totais_get(dec, "INDENIZACOES")); /* FGTS, aviso prévio indenizado */
+ fprintf(f, "%013lld", 0); /* TODO: ganho de capital isento */
+ fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
+ fprintf(f, "%013lld", 0); /* TODO: atividade rural */
+ fprintf(f, "%013lld", 0); /* TODO: contribuintes com 65 anos ou mais */
+ fprintf(f, "%013lld", 0); /* TODO: pensão por invalidez */
+ fprintf(f, "%013lld", totais_get(dec, "POUPANCA"));
+ fprintf(f, "%013lld", 0); /* TODO: sócio ou titular de empresa */
+ fprintf(f, "%013lld", totais_get(dec, "DOACOES")); /* herança, transferências patrimoniais */
+ fprintf(f, "%013lld", 0); /* TODO: outros rendimentos isentos */
+ fprintf(f, "%-60.60s", ""); /* FILLER1 */
+ fprintf(f, "%-13.13s", ""); /* FILLER2 */
+ fprintf(f, "%013lld", 0); /* TODO: alienação bens pequeno valor */
+ fprintf(f, "%013lld", 0); /* TODO: alienação único imóvel */
+ fprintf(f, "%013lld", 0); /* TODO: redução ganho capital */
+ fprintf(f, "%013lld", 0); /* TODO: total bens pequeno valor, transportado GC */
+ fprintf(f, "%013lld", 0); /* TODO: alienção único imóvel, transportado GC */
+ fprintf(f, "%013lld", 0); /* TODO: redução ganho capital, transportado GC */
+ fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira */
+ fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira, transportado */
+ fprintf(f, "%013lld", 0); /* TODO: GC total informado */
+ fprintf(f, "%013lld", 0); /* TODO: GC total transportado */
+ fprintf(f, "%013lld", 0); /* TODO: imposto renda anos anteriores */
+ fprintf(f, "%013lld", 0); /* TODO: rendimentos assalariados moeda estrangeira */
+ fprintf(f, "%013lld", 0); /* TODO: bonificações ações */
+ fprintf(f, "%013lld", 0); /* TODO: médicos residentes */
+ fprintf(f, "%013lld", 0); /* TODO: volutários da Copa */
+ fprintf(f, "%013lld", 0); /* TODO: meação e dissolução */
+ fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos com ações */
+ fprintf(f, "%013lld", 0); /* TODO: ganhos de capital com ouro */
+ fprintf(f, "%013lld", 0); /* TODO: recuperação prejuízos na bolsa */
+ fprintf(f, "%013lld", 0); /* TODO: transportador de cargas */
+ fprintf(f, "%013lld", 0); /* TODO: transportador de passageiros */
+ fprintf(f, "%013lld", 0); /* TODO: retistuição imposto anos anteriores */
+}
+
+static void gera_isentos2(struct declaracao *dec, FILE *f, int codigo, char *nome)
+{
+ long long total = totais_get(dec, nome);
+ if (total == 0)
+ return;
+ fprintf(f, "23");
+ fprintf(f, "%-11.11s", dec->cpf);
+ fprintf(f, "%04d", codigo);
+ fprintf(f, "%013lld", total);
+}
+
+static void gera_23_poupanca(struct declaracao *dec, FILE *f)
+{
+ gera_isentos2(dec, f, 12, "POUPANCA");
+}
+
+static void gera_23_lucrome(struct declaracao *dec, FILE *f)
+{
+ gera_isentos2(dec, f, 13, "LUCROME");
+}
+
+static void gera_23_outrosisentos(struct declaracao *dec, FILE *f)
+{
+ gera_isentos2(dec, f, 26, "OUTROSISENTOS");
+}
+
+static void gera_exclusivos(struct declaracao *dec, FILE *f)
+{
+ fprintf(f, "24");
+ fprintf(f, "%-11.11s", dec->cpf);
+ fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO"));
+ fprintf(f, "%013lld", 0); /* TODO: ganho de capital */
+ fprintf(f, "%013lld", 0); /* TODO: ganho líquido renda variável */
+ fprintf(f, "%013lld", totais_get(dec, "APLICACOES")); /* TODO: aplicações financeiras */
+ fprintf(f, "%013lld", 0); /* TODO: outros */
+ fprintf(f, "%-60.60s", ""); /* FILLER1 */
+ fprintf(f, "%013lld", 0); /* TODO: ganhos de capital alienação de bens */
+ fprintf(f, "%013lld", 0); /* TODO: ganhos de capital moeda estrangeira */
+ fprintf(f, "%013lld", 0); /* TODO: décimo terceiro dependentes */
+ fprintf(f, "%-13.13s", ""); /* FILLER2 */
+ fprintf(f, "%-60.60s", ""); /* FILLER3 */
+ fprintf(f, "%013lld", 0); /* TODO: RRA */
+ fprintf(f, "%013lld", 0); /* TODO: RRA dependentes */
+ fprintf(f, "%013lld", 0); /* TODO: Juros capital próprio */
+ fprintf(f, "%013lld", totais_get(dec, "PLR"));
+}
+
+static void gera_exclusivos2(struct declaracao *dec, FILE *f, int codigo, char *nome)
+{
+ long long total = totais_get(dec, nome);
+ if (total == 0)
+ return;
+ fprintf(f, "24");
+ fprintf(f, "%-11.11s", dec->cpf);
+ fprintf(f, "%04d", codigo);
+ fprintf(f, "%013lld", total); /* TODO: retistuição imposto anos anteriores */
+}
+
+static void gera_24_decimoterceiro(struct declaracao *dec, FILE *f)
+{
+ gera_exclusivos2(dec, f, 1, "DECIMOTERCEIRO");
+}
+
+static void gera_24_aplicacoes(struct declaracao *dec, FILE *f)
+{
+ gera_exclusivos2(dec, f, 6, "APLICACOES");
+}
+
+static void gera_24_plr(struct declaracao *dec, FILE *f)
+{
+ gera_exclusivos2(dec, f, 11, "PLR");
+}
+
+static void gera_conjuge(struct declaracao *dec, FILE *f)
+{
+ fprintf(f, "29");
+ fprintf(f, "%-11.11s", dec->cpf);
+ fprintf(f, "%-11.11s", dec->conjuge.cpf);
+ fprintf(f, "%013lld", dec->conjuge.base);
+ fprintf(f, "%013lld", dec->conjuge.imposto);
+ fprintf(f, "%013lld", dec->conjuge.isento);
+ fprintf(f, "%013lld", dec->conjuge.exclusivo);
+ fprintf(f, "%013lld", dec->conjuge.rendpj_exigibilidade_suspensa);
+ fprintf(f, "%013lld", dec->conjuge.total);
+ fprintf(f, "%c", dec->conjuge.entregou ? 'S' : 'N');
+}
+
+static int is_trailler_filler(int ano, int i)
+{
+ if (ano == 2018) {
+ return i == 29 || i == 31 || i == 33 || i == 36 || i == 37 || i == 44 || i == 82;
+ }
+#if 0
+ if (ano == 2019) {
+ return i == 57 || (i >= 76 && i <= 79);
+ }
+#endif
+ return (ano <= 2018 && i == 44);
+ /* Ano 2016, linha 29 é um FILLER, mas preenchida com múltiplos
+ * 0s. Como esta linha não é gerada na declaração de 2016, não
+ * deve constar aqui, e será contabilizada como 0. */
+ /* O mesmo vale para as linhas de 2019 comentadas acima. */
+ /* || (ano == 2016 && i == 29); */
+}
+
+static int is_trailler_spaces(int ano, int i)
+{
+ return (ano == 2018 && i > 92);
+}
+
+static int is_trailler_empty(int ano, int i)
+{
+ return
+ (ano == 2015 && i == 49) ||
+ (ano <= 2018 &&
+ (i == 57 || i == 64 || i == 79));