2 * Copyright (C) 2015-2017 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "declaracao.h"
27 #include "rendimento.h"
30 #include "pagamento.h"
32 #include "dependente.h"
38 static void gera_header(struct declaracao *dec, FILE *f)
43 fprintf(f, "%04d", dec->ano); /* Exercício */
44 fprintf(f, "%04d", dec->ano - 1); /* Ano base */
46 fprintf(f, "2500"); /* Código */
47 else if (dec->ano == 2016)
48 fprintf(f, "2600"); /* Código */
49 else if (dec->ano == 2017)
50 fprintf(f, "2700"); /* Código */
51 else if (dec->ano == 2018)
52 fprintf(f, "2800"); /* Código */
53 else if (dec->ano == 2019)
54 fprintf(f, "2900"); /* Código */
55 else if (dec->ano == 2020)
57 fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */
58 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
59 fprintf(f, " "); /* Filler */
60 fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */
62 fprintf(f, "130"); /* Versão IRPF */
63 else if (dec->ano == 2016)
64 fprintf(f, "110"); /* Versão IRPF */
65 else if (dec->ano == 2017)
66 fprintf(f, "130"); /* Versão IRPF */
67 else if (dec->ano == 2018)
68 fprintf(f, "160"); /* Versão IRPF */
69 else if (dec->ano == 2019)
70 fprintf(f, "150"); /* Versão IRPF */
71 else if (dec->ano == 2020)
72 fprintf(f, "180"); /* Versão IRPF */
73 fprintf(f, "%-60.60s", dec->nome);
74 fprintf(f, "%-2.2s", dec->contribuinte.uf ?: "");
75 fprintf(f, "%s", "0000000000"); /* FIXME: hash */
76 fprintf(f, "1"); /* TODO: certificavel */
77 fprintf(f, "%-8.8s", dec->contribuinte.dn ?: "");
78 fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
79 /* TODO: resultado imposto, 1 - pagar, 2 - restituicao */
80 fprintf(f, "%s", dec->restituicao ? "2" : (dec->pagar ? "1" : "0"));
81 fprintf(f, "S"); /* TODO: gerada? */
82 fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */
83 fprintf(f, "2"); /* TODO: PGD */
84 fprintf(f, "%-14.14s", sistema_get_so(dec));
85 fprintf(f, "%-7.7s", sistema_get_so_versao(dec));
86 fprintf(f, "%-9.9s", sistema_get_jvm_versao(dec));
87 fprintf(f, "%-10.10s", ""); /* TODO: última declaração transmitida (pode ser vazio?) */
88 fprintf(f, "%04d", dec->contribuinte.cd_municipio);
89 fprintf(f, "%-11.11s", dec->conjuge.cpf ? : ""); /* TODO: CPF conjuge */
90 fprintf(f, "%d", dec->obrigatoria ? 1 : 0); /* obrigatoria entrega */
91 fprintf(f, "%013lld", dec->devido); /* imposto devido, carne-leao, complementar dos dependentes */
92 fprintf(f, "%-10.10s", dec->recibo ?: ""); /* recibo ex. anterior */
93 /* Indicador de segurança: 0 se retificadora, 2 se recibo
94 * anterior, se não 1. */
95 fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1));
96 /* Indicador imposto pago, TODO: carnê leão e Lei 11.033. */
97 fprintf(f, "%02d", totais_get(dec, "CARNE") > 0 ? 7 :
98 (dec->retido ? 1 : 0));
99 /* Indicador imposto antecipado, TODO: carnê leão, Lei 11.033,
101 fprintf(f, "%d", dec->retido ? 1 : 0);
102 fprintf(f, "0"); /* TODO: mudanca endereco */
103 fprintf(f, "%-8.8s", dec->contribuinte.cep ?: "");
104 fprintf(f, "0"); /* TODO: debito 1a quota */
105 fprintf(f, "%-3.3s", dec->banco ? : "000"); /* no banco */
106 fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* agencia */
107 fprintf(f, " "); /* TODO: filler */
108 fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */
109 fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
110 fprintf(f, " "); /* RRA1 */
111 fprintf(f, "%-11.11s", ""); /* CPF RRA1 */
112 fprintf(f, " "); /* RRA2 */
113 fprintf(f, "%-11.11s", ""); /* CPF RRA2 */
114 fprintf(f, " "); /* RRA3 */
115 fprintf(f, "%-11.11s", ""); /* CPF RRA3 */
116 fprintf(f, " "); /* RRA4 */
117 fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
118 fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
119 fprintf(f, "0000000000000"); /* TODO: Doacao Idoso */
120 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* CNPJ maior */
121 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* CNPJ maior 2 */
122 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* CNPJ maior 3 */
123 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* CNPJ maior 4 */
124 if (dec->ano < 2017 || dec->ano >= 2019) {
125 /* CPF e DN de dependentes 1 a 6 */
126 for (i = 1; i <= 6; i++) {
127 struct dependente *dependente = list_get(dec->dependentes, i - 1);
128 if (dependente && dependente->cpf && dependente->cpf[0]) {
129 fprintf(f, "%-11.11s", dependente->cpf);
131 fprintf(f, "%-11.11s", "");
133 if (dependente && dependente->dn && dependente->dn[0]) {
134 fprintf(f, "%-8.8s", dependente->dn);
136 fprintf(f, "%-8.8s", "");
140 fprintf(f, "%-114.114s", "");
142 /* medico maior, TODO: outros códigos */
143 fprintf(f, "%-14.14s", medico_cnpj_ordenado(dec, 0));
144 /* medico maior 2, TODO: outros códigos */
145 fprintf(f, "%-14.14s", medico_cnpj_ordenado(dec, 1));
146 fprintf(f, "%-11.11s", ""); /* CPF pensao */
147 fprintf(f, "%-11.11s", ""); /* CPF inventariante */
148 fprintf(f, "%-40.40s", dec->contribuinte.municipio);
149 fprintf(f, "%-60.60s", dec->nome);
150 if (dec->ano >= 2017)
151 fprintf(f, "%010u ", crc32(0, dec->cpf, 11)); /* hash CPF */
153 fprintf(f, "%-11.11s", ""); /* CPF empregada, FILLER */
154 fprintf(f, "%-12.12s", sistema_get_mac(dec));
155 fprintf(f, "%08d", 0); /* Data saída */
156 fprintf(f, "%-11.11s", ""); /* CPF procurador */
157 fprintf(f, "%03d", dec->obrigatoria); /* criterio obrigatoriedade */
158 fprintf(f, "%013lld", totais_get(dec, "RENDTRIB")); /* Total tributavel */
159 /* CNPJ previdencia */
160 fprintf(f, "%-14.14s", inss_cnpj_ordenado(dec, 0));
161 /* CNPJ previdencia 2 */
162 fprintf(f, "%-14.14s", inss_cnpj_ordenado(dec, 1));
164 fprintf(f, "%013lld", totais_get(dec, "ISENTOS"));
165 /* Total exclusivo */
166 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS"));
167 /* Total pagamentos */
168 fprintf(f, "%013lld", totais_get(dec, "PAGAMENTOS"));
169 fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* numero conta */
170 fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
171 fprintf(f, "0"); /* TODO: revalidar DV conta */
173 /* Mudanças de 2015 */
175 fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* natureza ocupacao */
176 fprintf(f, "%-11.11s", ""); /* CPF domestica */
177 fprintf(f, "%-11.11s", ""); /* NI domestica */
178 fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */
179 fprintf(f, "%-11.11s", ""); /* NI domestica 2 */
180 fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */
181 fprintf(f, "%-11.11s", ""); /* NI domestica 3 */
182 fprintf(f, "1"); /* Declaracao iniciada */
183 fprintf(f, "1"); /* Utilizou PGD */
184 fprintf(f, "0"); /* Utilizou APP */
185 fprintf(f, "0"); /* Utilizou online */
186 fprintf(f, "0"); /* Utilizou rascunho */
187 fprintf(f, "0"); /* Utilizou pre preenchida */
188 fprintf(f, "0"); /* Utilizou fontes */
189 fprintf(f, "0"); /* Utilizou plano saude */
190 fprintf(f, "0"); /* Utilizou recuperar */
191 fprintf(f, "1"); /* Declaracao iniciada */
193 /* Fim das mudanças de 2015 */
195 /* Mudanças de 2016 */
197 if (dec->ano >= 2016) {
198 /* CNPJ maior pagamento dedutível */
199 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 0));
200 /* CNPJ maior pagamento dedutível */
201 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 1));
202 /* CNPJ maior pagamento dedutível */
203 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 2));
204 /* CNPJ maior pagamento dedutível */
205 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 3));
206 /* CNPJ maior pagamento dedutível */
207 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 4));
208 /* CNPJ maior pagamento dedutível */
209 fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 5));
210 /* TODO: CNPJ maior pagamento FUNPRESP */
211 fprintf(f, "%-14.14s", "");
214 /* Fim das mudanças de 2016 */
216 if (dec->ano >= 2020)
217 fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor ?: "");
219 fprintf(f, " "); /* Teste PGD, FILLER2 */
222 static void gera_contribuinte(struct declaracao *dec, FILE *f)
225 fprintf(f, "%s", dec->cpf);
226 fprintf(f, "%-60.60s", dec->nome);
227 fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
228 fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
229 fprintf(f, "%-6.6s", dec->contribuinte.numero);
230 fprintf(f, "%-21.21s", dec->contribuinte.complemento ?: "");
231 fprintf(f, "%-19.19s", dec->contribuinte.bairro);
232 fprintf(f, "%-9.9s", dec->contribuinte.cep);
233 fprintf(f, "%04d", dec->contribuinte.cd_municipio);
234 fprintf(f, "%-40.40s", dec->contribuinte.municipio);
235 fprintf(f, "%-2.2s", dec->contribuinte.uf);
236 fprintf(f, " "); /* TODO: código exterior */
237 fprintf(f, "105"); /* TODO: código país */
238 if (dec->ano == 2015) {
239 fprintf(f, "%-100.100s", ""); /* FILLER3 */
240 fprintf(f, "%-4.4s", ""); /* FILLER1 */
241 fprintf(f, "%-8.8s", ""); /* FILLER2 */
242 } else if (dec->ano >= 2016) {
243 fprintf(f, "%-90.90s", ""); /* FILLER3 */
244 /* TODO: NIT/PIS/PASEP de Pessoa Física no exterior */
245 fprintf(f, "%-11.11s", dec->contribuinte.nit ?: "");
247 fprintf(f, "%-11.11s", dec->conjuge.cpf ?: "");
249 if (dec->ano <= 2017) {
250 fprintf(f, "%-4.4s", dec->contribuinte.ddd ?: "");
251 fprintf(f, "%-9.9s", dec->contribuinte.telefone ?: "");
252 } else if (dec->ano >= 2020) {
253 fprintf(f, "%-4.4s", dec->contribuinte.ddd ?: "");
254 fprintf(f, "%-9.9s", "");
256 fprintf(f, "%-13.13s", ""); /* FILLER */
258 fprintf(f, "%-8.8s", dec->contribuinte.dn);
259 fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor ?: "");
260 fprintf(f, "%03d", dec->contribuinte.ocupacao_principal);
261 fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao);
262 fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: qnt quotas */
263 fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
264 fprintf(f, "%s", dec->retifica ? "S" : "N");
265 fprintf(f, "S"); /* GERADO */
266 fprintf(f, "N"); /* TODO: mudança de endereço */
267 fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */
268 fprintf(f, "%-3.3s", dec->banco ? : "000"); /* banco */
269 fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* agência */
270 fprintf(f, "N"); /* TODO: deficiência */
271 fprintf(f, "0"); /* FILLER: pré-preenchida */
272 fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* conta */
273 fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
274 fprintf(f, "N"); /* TODO: débito automático */
275 fprintf(f, "0"); /* TODO: débito primeira quota */
276 /* CNPJ principal fonte */
277 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0));
278 fprintf(f, "%-10.10s", dec->recibo ?: ""); /* Recibo última declaração ano anterior */
279 fprintf(f, "A"); /* Declaração de Ajuste */
280 fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */
281 if (dec->ano >= 2016)
282 fprintf(f, "%-20.20s", ""); /* TODO: registro profissional */
283 if (dec->ano >= 2017) {
284 fprintf(f, "%-11.11s", dec->contribuinte.celular ?: ""); /* celular */
285 fprintf(f, "%s", dec->conjuge.cpf ? "S" : "N");
287 if (dec->ano >= 2020) {
288 fprintf(f, "%-11.11s", dec->contribuinte.telefone ?: "");
289 } else if (dec->ano >= 2018) {
290 fprintf(f, "%-2.2s%-9.9s", dec->contribuinte.ddd ?: "", dec->contribuinte.telefone ?: "");
294 static void gera_simples(struct declaracao *dec, FILE *f)
297 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
298 fprintf(f, "%013lld", 0); /* TODO: carnê leão */
299 fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
300 /* rendimentos isentos titular */
301 fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT"));
302 /* rendimentos tributação exclusiva, exceto décimo terceiro */
303 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS_SEM_13o"));
305 fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO"));
306 fprintf(f, "%013lld", 0); /* TODO: retido na fonte (Lei 11.033) */
307 fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */
308 fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */
309 /* BUG IRPF: O IRPF imprime 0 aqui mesmo com doações e poupança. */
310 /* fprintf(f, "%013lld", totais_get(dec, "ISENTOSDEP")); */
311 fprintf(f, "%013lld", 0); /* isentos dependentes */
312 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSDEP")); /* exclusivos dependentes */
313 fprintf(f, "%-13.13s", ""); /* FILLER */
314 fprintf(f, "%-13.13s", ""); /* FILLER */
315 fprintf(f, "%013lld", totais_get(dec, "RENDPFTIT")); /* rendimentos PF titular */
316 fprintf(f, "%013lld", totais_get(dec, "RENDPFDEP")); /* rendimentos PF dependentes */
317 fprintf(f, "%013lld", totais_get(dec, "RENDEXTTIT")); /* FIXME: rendimentos PF exterior titular */
318 fprintf(f, "%013lld", totais_get(dec, "RENDEXTDEP")); /* FIXME: rendimentos PF ext. depend. */
320 /* FIXME: um teste demonstra que não há valor quando existem rendimentos de PF para o titular */
321 /* totais_get(dec, "RENDPF")); */
322 /* TODO: carnê-leão PF? */
323 fprintf(f, "%013lld", totais_get(dec, "CARNETIT"));
325 fprintf(f, "%013lld", totais_get(dec, "CARNEDEP")); /* carnê-leão dependentes */
326 fprintf(f, "%013lld", totais_get(dec, "DEPENDENTES")); /* dedução dependentes */
327 fprintf(f, "%013lld", 0); /* TODO: previdência RRA */
328 fprintf(f, "%013lld", 0); /* TODO: previdência RRA dependentes */
329 fprintf(f, "%013lld", 0); /* TODO: pensão RRA */
330 fprintf(f, "%013lld", 0); /* TODO: pensão RRA dependentes */
331 fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
332 fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
333 fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
336 static void gera_totais_simples(struct declaracao *dec, FILE *f)
339 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
340 fprintf(f, "%013lld", totais_get(dec, "RENDTRIB")); /* rendimentos tributáveis */
341 /* desconto simplificado */
342 fprintf(f, "%013lld", totais_get(dec, "DESCONTO"));
343 /* base de cálculo */
344 fprintf(f, "%013lld", totais_get(dec, "BASE"));
345 fprintf(f, "%013lld", dec->devido); /* imposto devido */
346 fprintf(f, "%013lld", dec->retido); /* imposto retido */
347 fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
348 fprintf(f, "%013lld", totais_get(dec, "CARNE")); /* TODO: imposto pago carnê-leão */
349 fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
350 fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
351 fprintf(f, "%013lld", dec->pagar); /* imposto pagar */
352 fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
353 fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
354 fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */
355 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */
356 if (dec->ano == 2015) {
357 fprintf(f, "%013lld", dec->conjuge.total); /* conjuge */
358 } else if (dec->ano >= 2016) {
359 fprintf(f, "%013lld", 0); /* FILLER */
361 fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
362 fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */
363 fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
364 fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimento tributável PJ titular */
365 fprintf(f, "%013lld", 0); /* TODO: total rural */
366 fprintf(f, "%013lld", dec->retido); /* FIXME: imposto retido titular */
367 fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */
368 fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */
369 fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */
370 fprintf(f, "%013lld", totais_get(dec, "ISENTOSDEP")); /* rendimentos isentos dependentes */
371 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* tributação exclusiva titular */
372 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSDEP")); /* tributação exclusiva dependentes */
373 fprintf(f, "%013lld", 0); /* TODO: total não tributável AR */
374 fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
375 fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
376 fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
377 fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis, transportado */
378 fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva, transportado */
379 fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */
380 fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */
381 fprintf(f, "%013lld", totais_get(dec, "RENDPFTIT") + totais_get(dec, "RENDEXTTIT")); /* TODO: rendimentos PF + exterior titular */
382 fprintf(f, "%013lld", totais_get(dec, "RENDPFDEP") + totais_get(dec, "RENDEXTDEP")); /* TODO: rendimentos PF exterior dependentes */
383 fprintf(f, "%013lld", 0); /* TODO: doações campanha */
384 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
385 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
386 fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
387 fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
388 fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
389 fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
390 fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
391 fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
392 fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
393 fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
394 fprintf(f, "%013lld", dec->devido); /* imposto devido */
395 fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
396 fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
397 fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
398 fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
399 fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
400 if (dec->ano >= 2018) {
401 fprintf(f, "%05lld", dec->aliquota_efetiva);
405 static void gera_completa(struct declaracao *dec, FILE *f)
408 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
409 fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* CNPJ maior */
410 fprintf(f, "%013lld", 0); /* TODO: imposto pago no exterior */
411 fprintf(f, "%013lld", 0); /* TODO: imposto complementar pago */
412 fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
413 fprintf(f, "%013lld", 0); /* TODO: rendimentos recebidos no exterior - titular */
414 fprintf(f, "%013lld", 0); /* TODO: livro caixa - titular */
415 fprintf(f, "%013lld", 0); /* TODO: carnê-leão - titular */
416 fprintf(f, "%013lld", 0); /* TODO: rendimentos recebidos no exterior - dependente */
417 fprintf(f, "%013lld", 0); /* TODO: livro caixa - dependente */
418 fprintf(f, "%013lld", 0); /* TODO: carnê-leão - dependente */
419 fprintf(f, "%013lld", totais_get(dec, "PREVIDENCIA")); /* TODO: contribuição previdenciária privada */
420 fprintf(f, "%013lld", 0); /* TODO: contribuição FAPI */
421 /* INSS titular, TODO (2016): sem RRA */
422 fprintf(f, "%013lld", totais_get(dec, "INSS"));
423 /* TODO: INSS dependente, TODO (2016): sem RRA */
424 fprintf(f, "%013lld", 0);
425 fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO")); /* TODO: 13o titular */
426 fprintf(f, "%013lld", 0); /* TODO: 13o dependente */
427 fprintf(f, "%05d", totais_get(dec, "DEPSINSTRUCAO")); /* dependentes com instrucao */
428 fprintf(f, "%05d", 0); /* TODO: alimentandos com instrucao */
429 fprintf(f, "%013lld", totais_get(dec, "RENDPFTIT")); /* TODO: rendimentos PF titular */
430 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
431 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */
432 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependente */
433 /* IRPF-2015-1.3 gera este valor como 0, antes era dec->devido */
434 fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
435 fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
436 if (dec->ano >= 2016) {
437 /* TODO: valor até o limite da FUNPRESP */
438 fprintf(f, "%013lld", 0);
439 /* TODO: valor acima do limite da FUNPRESP */
440 fprintf(f, "%013lld", 0);
444 static void gera_totais_completa(struct declaracao *dec, FILE *f)
447 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
448 /* FIXME: completar campos */
449 fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimentos PJ titular */
450 fprintf(f, "%013lld", totais_get(dec, "RENDPFTIT")); /* TODO: rendimentos PF titular */
451 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
452 fprintf(f, "%013lld", 0); /* TODO: resultado atividade rural */
453 fprintf(f, "%013lld", totais_get(dec, "RENDTRIB")); /* total rendimentos tributáveis */
454 /* TODO (2016): somar FUNPRESP até o limite */
455 fprintf(f, "%013lld", totais_get(dec, "INSS")); /* previdencia oficial */
456 /* TODO (2016): somar FUNPRESP acima do limite */
457 fprintf(f, "%013lld", totais_get(dec, "PREVIDENCIA")); /* previdencia privada */
458 fprintf(f, "%013lld", totais_get(dec, "DEPENDENTES")); /* deducao dependentes */
459 fprintf(f, "%013lld", totais_get(dec, "INSTRUCAO")); /* deducao instrucao */
460 fprintf(f, "%013lld", totais_get(dec, "MEDICAS")); /* despesas medicas */
461 fprintf(f, "%013lld", 0); /* TODO: pensao */
462 fprintf(f, "%013lld", 0); /* TODO: pensao escritura publica */
463 fprintf(f, "%013lld", 0); /* TODO: livro caixa */
464 fprintf(f, "%013lld", totais_get(dec, "DEDUCOES")); /* total deduções */
465 fprintf(f, "%013lld", totais_get(dec, "BASE")); /* base de cálculo */
466 fprintf(f, "%013lld", dec->devido); /* TODO: valor do imposto */
467 fprintf(f, "%013lld", 0); /* TODO: deducao do imposto (limitado a 6%) */
468 fprintf(f, "%013lld", dec->devido); /* imposto devido */
469 fprintf(f, "%013lld", 0); /* TODO: contribuicao patronal */
470 fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido menos patronal */
471 fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido mais RRA */
472 fprintf(f, "%013lld", dec->retido); /* imposto na fonte */
473 fprintf(f, "%013lld", totais_get(dec, "RENDPF")); /* TODO: carne leao */
475 fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
476 fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
477 fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
478 fprintf(f, "%013lld", dec->retido); /* TODO: total imposto pago */
479 fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
480 fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
481 fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
482 fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
484 fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */
485 fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */
486 fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
487 fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
488 if (dec->ano == 2015) {
489 fprintf(f, "%013lld", dec->conjuge.total); /* conjuge */
490 } else if (dec->ano >= 2016) {
491 fprintf(f, "%013lld", 0); /* FILLER */
493 fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */
494 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */
495 fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
496 fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
497 fprintf(f, "%013lld", 0); /* TODO: imposto renda variável */
499 fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
500 fprintf(f, "%013lld", 0); /* TODO: imposto fonte dependente */
501 fprintf(f, "%013lld", 0); /* TODO: imposto moeda estrangeira */
502 fprintf(f, "%013lld", 0); /* TODO: imposto ganhos capital moeda */
503 fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */
504 fprintf(f, "%013lld", totais_get(dec, "ISENTOSDEP")); /* rendimentos isentos dependentes */
505 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */
506 fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSDEP")); /* tributação exclusiva dependentes */
507 fprintf(f, "%013lld", 0); /* TODO: doações campanha */
508 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
509 fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
510 fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
511 fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
513 fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
514 fprintf(f, "%013lld", 0); /* TODO: previdencia RRA titular */
515 fprintf(f, "%013lld", 0); /* TODO: pensao RRA titular */
516 fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
517 fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
518 fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
519 fprintf(f, "%013lld", 0); /* TODO: previdencia RRA dependentes */
520 fprintf(f, "%013lld", 0); /* TODO: pensao RRA dependentes */
521 fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
522 fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
524 fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
525 fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
526 fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
527 fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
528 if (dec->ano >= 2018) {
529 fprintf(f, "%05lld", dec->aliquota_efetiva);
533 static void gera_isentos(struct declaracao *dec, FILE *f)
536 fprintf(f, "%-11.11s", dec->cpf);
537 fprintf(f, "%013lld", 0); /* TODO: bolsa de estudo */
538 fprintf(f, "%013lld", 0); /* TODO: pecúlio de previdência privada */
539 fprintf(f, "%013lld", totais_get(dec, "INDENIZACOES")); /* FGTS, aviso prévio indenizado */
540 fprintf(f, "%013lld", 0); /* TODO: ganho de capital isento */
541 fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
542 fprintf(f, "%013lld", 0); /* TODO: atividade rural */
543 fprintf(f, "%013lld", 0); /* TODO: contribuintes com 65 anos ou mais */
544 fprintf(f, "%013lld", 0); /* TODO: pensão por invalidez */
545 fprintf(f, "%013lld", totais_get(dec, "POUPANCA"));
546 fprintf(f, "%013lld", 0); /* TODO: sócio ou titular de empresa */
547 fprintf(f, "%013lld", totais_get(dec, "DOACOES")); /* herança, transferências patrimoniais */
548 fprintf(f, "%013lld", 0); /* TODO: outros rendimentos isentos */
549 fprintf(f, "%-60.60s", ""); /* FILLER1 */
550 fprintf(f, "%-13.13s", ""); /* FILLER2 */
551 fprintf(f, "%013lld", 0); /* TODO: alienação bens pequeno valor */
552 fprintf(f, "%013lld", 0); /* TODO: alienação único imóvel */
553 fprintf(f, "%013lld", 0); /* TODO: redução ganho capital */
554 fprintf(f, "%013lld", 0); /* TODO: total bens pequeno valor, transportado GC */
555 fprintf(f, "%013lld", 0); /* TODO: alienção único imóvel, transportado GC */
556 fprintf(f, "%013lld", 0); /* TODO: redução ganho capital, transportado GC */
557 fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira */
558 fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira, transportado */
559 fprintf(f, "%013lld", 0); /* TODO: GC total informado */
560 fprintf(f, "%013lld", 0); /* TODO: GC total transportado */
561 fprintf(f, "%013lld", 0); /* TODO: imposto renda anos anteriores */
562 fprintf(f, "%013lld", 0); /* TODO: rendimentos assalariados moeda estrangeira */
563 fprintf(f, "%013lld", 0); /* TODO: bonificações ações */
564 fprintf(f, "%013lld", 0); /* TODO: médicos residentes */
565 fprintf(f, "%013lld", 0); /* TODO: volutários da Copa */
566 fprintf(f, "%013lld", 0); /* TODO: meação e dissolução */
567 fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos com ações */
568 fprintf(f, "%013lld", 0); /* TODO: ganhos de capital com ouro */
569 fprintf(f, "%013lld", 0); /* TODO: recuperação prejuízos na bolsa */
570 fprintf(f, "%013lld", 0); /* TODO: transportador de cargas */
571 fprintf(f, "%013lld", 0); /* TODO: transportador de passageiros */
572 fprintf(f, "%013lld", 0); /* TODO: retistuição imposto anos anteriores */
575 static void gera_isentos2(struct declaracao *dec, FILE *f, int codigo, char *nome)
577 long long total = totais_get(dec, nome);
581 fprintf(f, "%-11.11s", dec->cpf);
582 fprintf(f, "%04d", codigo);
583 fprintf(f, "%013lld", total);
586 static void gera_23_poupanca(struct declaracao *dec, FILE *f)
588 gera_isentos2(dec, f, 12, "POUPANCA");
591 static void gera_23_lucrome(struct declaracao *dec, FILE *f)
593 gera_isentos2(dec, f, 13, "LUCROME");
596 static void gera_23_outrosisentos(struct declaracao *dec, FILE *f)
598 gera_isentos2(dec, f, 26, "OUTROSISENTOS");
601 static void gera_exclusivos(struct declaracao *dec, FILE *f)
604 fprintf(f, "%-11.11s", dec->cpf);
605 fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO"));
606 fprintf(f, "%013lld", 0); /* TODO: ganho de capital */
607 fprintf(f, "%013lld", 0); /* TODO: ganho líquido renda variável */
608 fprintf(f, "%013lld", totais_get(dec, "APLICACOES")); /* TODO: aplicações financeiras */
609 fprintf(f, "%013lld", 0); /* TODO: outros */
610 fprintf(f, "%-60.60s", ""); /* FILLER1 */
611 fprintf(f, "%013lld", 0); /* TODO: ganhos de capital alienação de bens */
612 fprintf(f, "%013lld", 0); /* TODO: ganhos de capital moeda estrangeira */
613 fprintf(f, "%013lld", 0); /* TODO: décimo terceiro dependentes */
614 fprintf(f, "%-13.13s", ""); /* FILLER2 */
615 fprintf(f, "%-60.60s", ""); /* FILLER3 */
616 fprintf(f, "%013lld", 0); /* TODO: RRA */
617 fprintf(f, "%013lld", 0); /* TODO: RRA dependentes */
618 fprintf(f, "%013lld", 0); /* TODO: Juros capital próprio */
619 fprintf(f, "%013lld", totais_get(dec, "PLR"));
622 static void gera_exclusivos2(struct declaracao *dec, FILE *f, int codigo, char *nome)
624 long long total = totais_get(dec, nome);
628 fprintf(f, "%-11.11s", dec->cpf);
629 fprintf(f, "%04d", codigo);
630 fprintf(f, "%013lld", total); /* TODO: retistuição imposto anos anteriores */
633 static void gera_24_decimoterceiro(struct declaracao *dec, FILE *f)
635 gera_exclusivos2(dec, f, 1, "DECIMOTERCEIRO");
638 static void gera_24_aplicacoes(struct declaracao *dec, FILE *f)
640 gera_exclusivos2(dec, f, 6, "APLICACOES");
643 static void gera_24_plr(struct declaracao *dec, FILE *f)
645 gera_exclusivos2(dec, f, 11, "PLR");
648 static void gera_conjuge(struct declaracao *dec, FILE *f)
651 fprintf(f, "%-11.11s", dec->cpf);
652 fprintf(f, "%-11.11s", dec->conjuge.cpf);
653 fprintf(f, "%013lld", dec->conjuge.base);
654 fprintf(f, "%013lld", dec->conjuge.imposto);
655 fprintf(f, "%013lld", dec->conjuge.isento);
656 fprintf(f, "%013lld", dec->conjuge.exclusivo);
657 fprintf(f, "%013lld", dec->conjuge.rendpj_exigibilidade_suspensa);
658 fprintf(f, "%013lld", dec->conjuge.total);
659 fprintf(f, "%c", dec->conjuge.entregou ? 'S' : 'N');
662 static int is_trailler_filler(int ano, int i)
665 return i == 29 || i == 31 || i == 33 || i == 36 || i == 37 || i == 44 || i == 82;
669 return i == 57 || (i >= 76 && i <= 79);
672 return (ano <= 2018 && i == 44);
673 /* Ano 2016, linha 29 é um FILLER, mas preenchida com múltiplos
674 * 0s. Como esta linha não é gerada na declaração de 2016, não
675 * deve constar aqui, e será contabilizada como 0. */
676 /* O mesmo vale para as linhas de 2019 comentadas acima. */
677 /* || (ano == 2016 && i == 29); */
680 static int is_trailler_spaces(int ano, int i)
682 return (ano == 2018 && i > 92);
685 static int is_trailler_empty(int ano, int i)
688 (ano == 2015 && i == 49) ||
690 (i == 57 || i == 64 || i == 79));
693 static void gera_trailler(struct declaracao *dec, FILE *f)
697 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
698 fprintf(f, "%06d", dec->linhas[0]);
699 for (i = 16; i < 100; i++) {
700 if (is_trailler_filler(dec->ano, i))
701 fprintf(f, "%-5.5s", "0"); /* FILLER */
702 else if (is_trailler_spaces(dec->ano, i))
703 fprintf(f, "%-5.5s", "");
704 else if (is_trailler_empty(dec->ano, i))
706 else if (dec->ano == 2018 && i == 92)
707 fprintf(f, "%-5s", "0");
709 fprintf(f, "%05d", dec->linhas[i]);
713 static void gera_reciboheader(struct declaracao *dec, FILE *f)
716 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
717 fprintf(f, "%-3.3s", ""); /* FILLER */
720 static void gera_recibodetalhe(struct declaracao *dec, FILE *f)
723 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
724 fprintf(f, "%-3.3s", ""); /* FILLER */
725 fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
726 fprintf(f, "%-60.60s", dec->nome);
727 fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
728 fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
729 fprintf(f, "%-6.6s", dec->contribuinte.numero);
730 fprintf(f, "%-21.21s", dec->contribuinte.complemento ?: "");
731 fprintf(f, "%-19.19s", dec->contribuinte.bairro);
732 fprintf(f, "%-9.9s", dec->contribuinte.cep);
733 fprintf(f, "%04d", dec->contribuinte.cd_municipio);
734 fprintf(f, "%-40.40s", dec->contribuinte.municipio);
735 fprintf(f, "%-2.2s", dec->contribuinte.uf);
736 fprintf(f, "%-100.100s", ""); /* FILLER */
737 fprintf(f, "%-4.4s", ""); /* FILLER */
738 fprintf(f, "%-8.8s", ""); /* FILLER */
739 if (dec->ano >= 2020) {
740 fprintf(f, "%-4.4s", dec->contribuinte.ddd ?: "");
741 fprintf(f, "%-9.9s", "");
742 } else if (dec->ano >= 2018) {
743 fprintf(f, "%-13.13s", "");
745 fprintf(f, "%-4.4s", dec->contribuinte.ddd ?: "");
746 fprintf(f, "%-9.9s", dec->contribuinte.telefone ?: "");
748 fprintf(f, "%s", dec->retifica ? "S" : "N"); /* Retificadora */
749 fprintf(f, "%013lld", totais_get(dec, "RENDTRIB")); /* Total tributavel */
750 fprintf(f, "%013lld", dec->devido); /* Imposto devido */
751 fprintf(f, "%013lld", dec->restituicao); /* Imposto a restituir */
752 fprintf(f, "%013lld", dec->pagar); /* Imposto a pagar */
753 fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
754 fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
755 fprintf(f, "%-3.3s", dec->banco ? : "000"); /* numero banco */
756 fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* numero agencia */
757 fprintf(f, " "); /* FILLER */
758 fprintf(f, "%d", 0); /* TODO: debito primeira quota */
759 fprintf(f, "%013lld", 0); /* TODO: imposto pago ganho de capital */
760 fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* numero conta */
761 fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
762 fprintf(f, "%013lld", 0); /* TODO: imposto alienacao moeda estrangeira em especie */
763 if (dec->ano >= 2020) {
764 fprintf(f, "%-11.11s", dec->contribuinte.telefone ?: "");
765 } else if (dec->ano >= 2018) {
766 fprintf(f, "%-2.2s%-9.9s", dec->contribuinte.ddd ?: "", dec->contribuinte.telefone ?: "");
770 static void gera_recibotrailler(struct declaracao *dec, FILE *f)
773 fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
774 fprintf(f, "%-3.3s", ""); /* FILLER */
775 fprintf(f, "%010u", dec->rhash);
778 static void gera_rendimento(struct declaracao *dec, FILE *f)
780 struct rendimento *r;
781 r = list_get(dec->rendimento, dec->linhas[21]);
783 fprintf(f, "%s", dec->cpf);
784 fprintf(f, "%-14.14s", r->cnpj);
785 fprintf(f, "%-60.60s", r->nome);
786 fprintf(f, "%013lld", r->rendimento);
787 fprintf(f, "%013lld", r->previdencia);
788 fprintf(f, "%013lld", r->decimoterceiro);
789 fprintf(f, "%013lld", r->imposto);
790 fprintf(f, "%-8.8s", r->saida);
791 fprintf(f, "%013lld", r->imposto_13o);
794 static void gera_pagamento(struct declaracao *dec, FILE *f)
797 p = list_get(dec->pagamentos, dec->linhas[26]);
799 fprintf(f, "%s", dec->cpf);
800 fprintf(f, "%02d", p->codigo);
801 fprintf(f, "%05d", p->dependente);
802 fprintf(f, "%-14.14s", p->cnpj);
803 fprintf(f, "%-60.60s", p->nome);
804 fprintf(f, "%-11.11s", ""); /* TODO: NIT */
805 fprintf(f, "%013lld", p->pagamento);
806 fprintf(f, "%013lld", p->reembolso);
807 if (dec->ano >= 2016) {
808 /* TODO: contribuição do ente público patrocinador (FUNPRESP?) */
809 fprintf(f, "%013lld", 0);
811 fprintf(f, "%d", strlen(p->cnpj) == 11 ? 1 : 2);
812 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
813 fprintf(f, "%c", p->dependente ? 'D' : 'T');
816 static void gera_isento(struct declaracao *dec, FILE *f, int codigo)
819 i = isento_get(dec, codigo, dec->linhas[codigo]);
820 fprintf(f, "%02d", codigo);
821 fprintf(f, "%-11.11s", dec->cpf); /* Titular */
822 fprintf(f, "%05d", dec->linhas[codigo] + 1); /* Chave */
823 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
824 fprintf(f, "%c", i->dependente ? 'D' : 'T');
825 fprintf(f, "%-14.14s", i->cnpj);
826 fprintf(f, "%-60.60s", i->nome);
827 fprintf(f, "%013lld", i->valor);
829 struct dependente *d;
830 d = list_get(dec->dependentes, i->dependente - 1);
831 fprintf(f, "%-11.11s", d ? d->cpf : "");
833 fprintf(f, "%-11.11s", dec->cpf);
837 static void gera_isento2(struct declaracao *dec, FILE *f, int codigo,
838 int subcodigo, int codigoreal)
842 fprintf(f, "%02d", codigoreal);
843 fprintf(f, "%-11.11s", dec->cpf); /* Titular */
844 /* Titular (T), Dependente (D), Alimentando (A), FIXME Alimentando */
845 fprintf(f, "%c", i->dependente ? 'D' : 'T');
847 struct dependente *d;
848 d = list_get(dec->dependentes, i->dependente - 1);
849 fprintf(f, "%-11.11s", d ? d->cpf : "");
851 fprintf(f, "%-11.11s", dec->cpf);
853 fprintf(f, "%04d", subcodigo);
854 fprintf(f, "%-14.14s", i->cnpj);
855 fprintf(f, "%-60.60s", i->nome);
856 fprintf(f, "%013lld", i->valor);
859 static void gera_doacao(struct declaracao *dec, FILE *f)
861 gera_isento(dec, f, 82);
864 static void gera_indenizacoes(struct declaracao *dec, FILE *f)
866 gera_isento(dec, f, 93);
869 static void gera_plr(struct declaracao *dec, FILE *f)
871 gera_isento(dec, f, 96);
874 static void gera_plr2(struct declaracao *dec, FILE *f)
876 gera_isento2(dec, f, 11, 11, 88);
879 static void gera_lucrome2(struct declaracao *dec, FILE *f)
881 gera_isento2(dec, f, 13, 13, 84);
884 static void gera_outrosisentos(struct declaracao *dec, FILE *f)
888 i = isento_get(dec, codigo, dec->linhas[codigo]);
889 gera_isento(dec, f, codigo);
890 fprintf(f, "%d", i->exclusivo == 0 ? 1 : 2);
891 fprintf(f, "%-60.60s", i->descricao);
894 static void gera_outrosisentos2(struct declaracao *dec, FILE *f)
898 gera_isento2(dec, f, 26, 26, 86);
899 fprintf(f, "%-60.60s", i->descricao);
902 static void gera_poupanca(struct declaracao *dec, FILE *f)
904 gera_isento(dec, f, 98);
907 static void gera_poupanca2(struct declaracao *dec, FILE *f)
909 gera_isento2(dec, f, 12, 12, 84);
912 static void gera_aplicacoes(struct declaracao *dec, FILE *f)
914 gera_isento(dec, f, 99);
917 static void gera_aplicacoes2(struct declaracao *dec, FILE *f)
919 gera_isento2(dec, f, 6, 6, 88);
922 static void gera_dependente(struct declaracao *dec, FILE *f)
924 struct dependente *d;
925 d = list_get(dec->dependentes, dec->linhas[25]);
928 fprintf(f, "%s", dec->cpf);
929 fprintf(f, "%05d", dec->linhas[25] + 1);
930 fprintf(f, "%02d", d->codigo);
931 fprintf(f, "%-60.60s", d->nome);
932 fprintf(f, "%-8.8s", d->dn);
933 fprintf(f, "%-11.11s", d->cpf);
934 /* TODO: Indicador de saída */
936 if (dec->ano >= 2016) {
937 /* TODO: NIT/PIS/PASEP de Pessoa Física no exterior */
938 fprintf(f, "%-11.11s", "");
942 static void gera_bem(struct declaracao *dec, FILE *f)
945 b = list_get(dec->bens, dec->linhas[27]);
948 fprintf(f, "%s", dec->cpf);
949 fprintf(f, "%02d", b->codigo);
951 /* FIXME: exterior */
953 fprintf(f, "105"); /* código Brasil */
955 fprintf(f, "%-512.512s", b->descricao);
956 fprintf(f, "%013lld", b->valor_anterior);
957 fprintf(f, "%013lld", b->valor);
960 attr_out(f, b->attr, "logradouro", 40);
961 attr_out(f, b->attr, "numero", 6);
962 attr_out(f, b->attr, "complemento", 40);
963 attr_out(f, b->attr, "bairro", 40);
964 attr_out(f, b->attr, "cep", 9);
965 attr_out(f, b->attr, "uf", 2);
966 attr_out(f, b->attr, "cd_municipio", 4);
967 attr_out(f, b->attr, "municipio", 40);
968 /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
969 fprintf(f, "%d", attr_get(b->attr, "registro") ? 1 : 2);
970 attr_out(f, b->attr, "matricula", 40);
971 attr_out(f, b->attr, "registro", 40);
972 attr_out(f, b->attr, "area", 11);
973 /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
974 fprintf(f, "%d", attr_get(b->attr, "area") ? 0 : 2);
975 attr_out(f, b->attr, "cartorio", 60);
977 /* Número de chave */
978 fprintf(f, "%05d", dec->linhas[27] + 1);
981 if (dec->ano >= 2018) {
982 fprintf(f, "%-8.8s", "00000000"); /* TODO: Data de aquisição */
983 fprintf(f, "%-20.20s", ""); /* TODO: IPTU, 2019, filler */
984 fprintf(f, "%-8.8s", "00000000"); /* TODO: NIRF */
985 fprintf(f, "%-30.30s", ""); /* TODO: RENAVAN */
986 fprintf(f, "%-30.30s", ""); /* TODO: Registro aviação civil */
987 fprintf(f, "%-30.30s", ""); /* TODO: Registro Capitania dos Portos */
988 fprintf(f, "%04d", 0); /* TODO: Agência bancária */
989 fprintf(f, "%-15.15s", ""); /* TODO: Conta bancária e dígito verificador */
990 attr_out(f, b->attr, "cnpj", 14);
993 if (dec->ano >= 2019) {
994 attr_out(f, b->attr, "iptu", 30);
996 if (dec->ano >= 2020) {
997 fprintf(f, "%-3.3s", ""); /* TODO: Banco */
998 fprintf(f, "%c", b->dependente ? 'D' : 'T');
1000 struct dependente *d;
1001 d = list_get(dec->dependentes, b->dependente - 1);
1002 fprintf(f, "%-11.11s", d ? d->cpf : "");
1004 fprintf(f, "%-11.11s", dec->cpf);
1009 static void gera_carne(struct declaracao *dec, FILE *f)
1011 struct carne *carne;
1012 carne = list_get(dec->carne, dec->linhas[22]);
1014 fprintf(f, "%-11.11s", dec->cpf); /* Titular */
1015 if (carne->dependente) {
1016 struct dependente *d;
1017 d = list_get(dec->dependentes, carne->dependente - 1);
1018 fprintf(f, "S%-11.11s", d ? d->cpf : ""); /* CPF dependente. */
1020 fprintf(f, "N%-11.11s", ""); /* Não é dependente. */
1022 fprintf(f, "%02d", carne->mes);
1023 fprintf(f, "%013lld", carne->rendimento);
1024 fprintf(f, "%013lld", carne->alugueis); /* Aluguéis. */
1025 fprintf(f, "%013lld", carne->outros); /* Outros. */
1026 fprintf(f, "%013lld", carne->exterior); /* Exterior. */
1027 fprintf(f, "%013lld", carne->caixa); /* Livro-caixa. */
1028 fprintf(f, "%013lld", carne->alimentos); /* Pensão alimentícia. */
1029 fprintf(f, "%013lld", carne->dependentes); /* Dedução com dependentes. */
1030 fprintf(f, "%013lld", carne->previdencia); /* Previdência. */
1031 fprintf(f, "%013lld", carne->base); /* Base cálculo. */
1032 fprintf(f, "%013lld", carne->imposto); /* Imposto pago. */
1035 typedef void (gera_linha)(struct declaracao *dec, FILE *f);
1037 static void update_hash(struct declaracao *dec, char *buf, size_t len)
1041 r = sscanf(buf, "%2d", &linha);
1042 if (r == 1 || !strncmp(buf, "T9", 2)) {
1043 dec->hash = crc32(dec->hash, buf, len - 2);
1044 dec->rhash = crc32(dec->rhash, buf, len - 2);
1046 } else if (strncmp(buf, "R9", 2) && strncmp(buf, "IRPF", 4)) {
1047 dec->rhash = crc32(dec->rhash, buf, len - 2);
1051 static int wrap(gera_linha fn, struct declaracao *dec, struct list *l)
1058 uLong c = crc32(0L, Z_NULL, 0);
1059 m = open_memstream(&buf, &bsize);
1071 /* Calcula CRC32 para campo de controle dos registros. */
1072 c = crc32(c, buf, bsize);
1073 fprintf(m, "%010ld\r\n", c);
1076 r = sscanf(buf, "%2d", &linha);
1077 if (r == 1 && linha >= 0 && linha < 100) {
1078 dec->linhas[linha]++;
1080 update_hash(dec, buf, bsize);
1081 return list_add(&l, buf);
1084 static void insert_hash(struct declaracao *dec, char *irpf)
1086 uLong c = crc32(0L, NULL, 0);
1089 snprintf(cpf, 13, "%-8.8s.DEC", dec->cpf);
1090 c = crc32(c, cpf, 12);
1091 snprintf(crc, 11, "%010ld", dec->hash);
1092 memcpy(irpf + 101, crc, 10);
1093 c = crc32(c, irpf, strlen(irpf) - 12);
1094 snprintf(crc, 11, "%010ld", c);
1095 memcpy(irpf + strlen(irpf) - 12, crc, 10);
1098 static int gera(struct declaracao *dec, char *filename)
1103 struct rendimento *rendimento;
1104 struct carne *carne;
1105 struct isento *isento;
1106 struct pagamento *pagamento;
1108 struct dependente *dependente;
1109 struct list *linhas;
1112 linhas = list_new();
1116 dec->hash = crc32(0L, NULL, 0);
1117 dec->rhash = crc32(0L, NULL, 0);
1121 r = wrap(fn, dec, linhas); \
1126 f = fopen(filename, "w");
1129 dec_set_error(dec, "Não foi possível abrir arquivo %s: %s.",
1130 filename, strerror(errno));
1133 memset(dec->linhas, 0, sizeof(dec->linhas));
1135 W(gera_contribuinte);
1136 if (dec->tipo == COMPLETA) {
1138 W(gera_totais_completa);
1141 W(gera_totais_simples);
1143 for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
1147 for (i = 0; (carne = list_get(dec->carne, i)); i++) {
1151 if (dec->ano <= 2017) {
1152 if (totais_get(dec, "ISENTOS"))
1154 if (totais_get(dec, "EXCLUSIVOS"))
1157 W(gera_23_poupanca);
1159 W(gera_23_outrosisentos);
1160 W(gera_24_decimoterceiro);
1161 W(gera_24_aplicacoes);
1165 for (i = 0; (dependente = list_get(dec->dependentes, i)); i++) {
1168 for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
1171 for (i = 0; (bem = list_get(dec->bens, i)); i++) {
1175 if (dec->ano == 2015 && dec->conjuge.cpf)
1178 /* Rendimentos isentos e com tributação exclusiva */
1179 /* Registros 82 a 89, e 92 a 99 */
1180 #define IW(fn, codigo) \
1181 for (i = 0; ; i++) { \
1182 isento = isento_get(dec, codigo, i); \
1185 dec->ctx = isento; \
1188 if (dec->ano <= 2017) {
1189 IW(gera_doacao, 82);
1190 IW(gera_indenizacoes, 93);
1192 IW(gera_outrosisentos, 97);
1193 IW(gera_poupanca, 98);
1194 IW(gera_aplicacoes, 99);
1196 IW(gera_poupanca2, 12);
1197 //dec->linhas[12] = 0;
1198 IW(gera_lucrome2, 13);
1199 IW(gera_outrosisentos2, 26);
1200 IW(gera_aplicacoes2, 6);
1201 //dec->linhas[99] = 0;
1203 //dec->linhas[96] = 0;
1207 W(gera_reciboheader);
1208 W(gera_recibodetalhe);
1209 W(gera_recibotrailler);
1211 insert_hash(dec, list_get(linhas, 0));
1212 for (i = 0; (buf = list_get(linhas, i)); i++) {
1213 fwrite(buf, strlen(buf), 1, f);
1217 list_free(linhas, free);
1222 list_free(linhas, free);
1226 static int run_gera(struct declaracao *dec, char **args, int argc)
1229 dec_set_error(dec, "Comando %s recebe um nome de arquivo como parâmetro.",
1233 if (!ANO_VALIDO(dec->ano)) {
1234 dec_set_error(dec, "Comando %s não suporta ano %d.",
1238 return gera(dec, args[1]);
1241 static struct cmd cmd_gera = {
1246 int gera_cmd_init(void)