382ec06bb39b7cf877429168cef12ce0bf67c1e2
[cascardo/declara.git] / lib / gera.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "gera.h"
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <zlib.h>
25 #include "declaracao.h"
26 #include "cmd.h"
27 #include "rendimento.h"
28 #include "isento.h"
29 #include "pagamento.h"
30 #include "bem.h"
31 #include "dependente.h"
32 #include "totais.h"
33 #include "sistema.h"
34
35 static void gera_header(struct declaracao *dec, FILE *f)
36 {
37         fprintf(f, "IRPF    ");
38         fprintf(f, "%04d", dec->ano); /* Exercício */
39         fprintf(f, "%04d", dec->ano - 1); /* Ano base */
40         fprintf(f, "2500"); /* Código */
41         fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */
42         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
43         fprintf(f, "   "); /* Filler */
44         fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */
45         fprintf(f, "130"); /* Versão IRPF */
46         fprintf(f, "%-60.60s", dec->nome);
47         fprintf(f, "%s", dec->contribuinte.uf);
48         fprintf(f, "%s", "0000000000"); /* FIXME: hash */
49         fprintf(f, "1"); /* TODO: certificavel */
50         fprintf(f, "%-8.8s", dec->contribuinte.dn);
51         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
52         /* TODO: resultado imposto, 1 - pagar, 2 - restituicao */
53         fprintf(f, "%s", dec->restituicao ? "2" : (dec->pagar ? "1" : "0"));
54         fprintf(f, "S"); /* TODO: gerada? */
55         fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */
56         fprintf(f, "2"); /* TODO: PGD */
57         fprintf(f, "%-14.14s", sistema_get_so(dec));
58         fprintf(f, "%-7.7s", sistema_get_so_versao(dec));
59         fprintf(f, "%-9.9s", sistema_get_jvm_versao(dec));
60         fprintf(f, "%-10.10s", ""); /* TODO: última declaração transmitida (pode ser vazio?) */
61         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
62         fprintf(f, "%-11.11s", dec->conjuge.cpf ? : ""); /* TODO: CPF conjuge */
63         fprintf(f, "%d", dec->obrigatoria ? 1 : 0); /* obrigatoria entrega */
64         fprintf(f, "%013lld", dec->devido); /* imposto devido, carne-leao, complementar dos dependentes */
65         fprintf(f, "%-10.10s", dec->recibo ?: ""); /* recibo ex. anterior */
66         /* Indicador de segurança: 0 se retificadora, 2 se recibo
67          * anterior, se não 1. */
68         fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1));
69         /* Indicador imposto pago, TODO: carnê leão e Lei 11.033. */
70         fprintf(f, "%02d", dec->retido ? 1 : 0);
71         /* Indicador imposto antecipado, TODO: carnê leão, Lei 11.033,
72          * outros. */
73         fprintf(f, "%d", dec->retido ? 1 : 0);
74         fprintf(f, "0"); /* TODO: mudanca endereco */
75         fprintf(f, "%s", dec->contribuinte.cep);
76         fprintf(f, "0"); /* TODO: debito 1a quota */
77         fprintf(f, "%-3.3s", dec->banco ? : "000"); /* no banco */
78         fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* agencia */
79         fprintf(f, " "); /* TODO: filler */
80         fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */
81         fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
82         fprintf(f, " "); /* RRA1 */
83         fprintf(f, "%-11.11s", ""); /* CPF RRA1 */
84         fprintf(f, " "); /* RRA2 */
85         fprintf(f, "%-11.11s", ""); /* CPF RRA2 */
86         fprintf(f, " "); /* RRA3 */
87         fprintf(f, "%-11.11s", ""); /* CPF RRA3 */
88         fprintf(f, " "); /* RRA4 */
89         fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
90         fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
91         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* CNPJ maior */
92         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* CNPJ maior 2 */
93         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* CNPJ maior 3 */
94         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* CNPJ maior 4 */
95         fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */
96         fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */
97         fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */
98         fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */
99         fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */
100         fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */
101         fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */
102         fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */
103         fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */
104         fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */
105         fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */
106         fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */
107         /* medico maior, TODO: outros códigos */
108         fprintf(f, "%-14.14s", medico_cnpj_ordenado(dec, 0));
109         /* medico maior 2, TODO: outros códigos */
110         fprintf(f, "%-14.14s", medico_cnpj_ordenado(dec, 1));
111         fprintf(f, "%-11.11s", ""); /* CPF pensao */
112         fprintf(f, "%-11.11s", ""); /* CPF inventariante */
113         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
114         fprintf(f, "%-60.60s", dec->nome);
115         fprintf(f, "%-11.11s", ""); /* CPF empregada */
116         fprintf(f, "%-12.12s", sistema_get_mac(dec));
117         fprintf(f, "%08d", 0); /* Data saída */
118         fprintf(f, "%-11.11s", ""); /* CPF procurador */
119         fprintf(f, "%03d", dec->obrigatoria); /* criterio obrigatoriedade */
120         fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */
121         /* CNPJ previdencia */
122         fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 0));
123         /* CNPJ previdencia 2 */
124         fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 1));
125         /* Total isentos */
126         fprintf(f, "%013lld", totais_get(dec, "ISENTOS"));
127         /* Total exclusivo */
128         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS"));
129         /* Total pagamentos */
130         fprintf(f, "%013lld", totais_get(dec, "PAGAMENTOS"));
131         fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* numero conta */
132         fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
133         fprintf(f, "0"); /* TODO: revalidar DV conta */
134
135         /* Mudanças de 2015 */
136
137         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* natureza ocupacao */
138         fprintf(f, "%-11.11s", ""); /* CPF domestica */
139         fprintf(f, "%-11.11s", ""); /* NI domestica */
140         fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */
141         fprintf(f, "%-11.11s", ""); /* NI domestica 2 */
142         fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */
143         fprintf(f, "%-11.11s", ""); /* NI domestica 3 */
144         fprintf(f, "1"); /* Declaracao iniciada */
145         fprintf(f, "1"); /* Utilizou PGD */
146         fprintf(f, "0"); /* Utilizou APP */
147         fprintf(f, "0"); /* Utilizou online */
148         fprintf(f, "0"); /* Utilizou rascunho */
149         fprintf(f, "0"); /* Utilizou pre preenchida */
150         fprintf(f, "0"); /* Utilizou fontes */
151         fprintf(f, "0"); /* Utilizou plano saude */
152         fprintf(f, "0"); /* Utilizou recuperar */
153         fprintf(f, "1"); /* Declaracao iniciada */
154
155         /* Fim das mudanças de 2015 */
156
157         fprintf(f, "   "); /* Teste PGD, FILLER2 */
158 }
159
160 static void gera_contribuinte(struct declaracao *dec, FILE *f)
161 {
162         fprintf(f, "16");
163         fprintf(f, "%s", dec->cpf);
164         fprintf(f, "%-60.60s", dec->nome);
165         fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
166         fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
167         fprintf(f, "%-6.6s", dec->contribuinte.numero);
168         fprintf(f, "%-21.21s", dec->contribuinte.complemento ?: "");
169         fprintf(f, "%-19.19s", dec->contribuinte.bairro);
170         fprintf(f, "%-9.9s", dec->contribuinte.cep);
171         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
172         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
173         fprintf(f, "%-2.2s", dec->contribuinte.uf);
174         fprintf(f, "   "); /* TODO: código exterior */
175         fprintf(f, "105"); /* TODO: código país */
176         fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */
177         fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */
178         fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */
179         fprintf(f, "%-4.4s", dec->contribuinte.ddd);
180         fprintf(f, "%-9.9s", dec->contribuinte.telefone);
181         fprintf(f, "%-8.8s", dec->contribuinte.dn);
182         fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor);
183         fprintf(f, "%03d", dec->contribuinte.ocupacao_principal);
184         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao);
185         fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: qnt quotas */
186         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
187         fprintf(f, "%s", dec->retifica ? "S" : "N");
188         fprintf(f, "S"); /* GERADO */
189         fprintf(f, "N"); /* TODO: mudança de endereço */
190         fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */
191         fprintf(f, "%-3.3s", dec->banco ? : "000"); /* banco */
192         fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* agência */
193         fprintf(f, "N"); /* TODO: deficiência */
194         fprintf(f, "0"); /* FILLER: pré-preenchida */
195         fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* conta */
196         fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
197         fprintf(f, "N"); /* TODO: débito automático */
198         fprintf(f, "0"); /* TODO: débito primeira quota */
199         /* CNPJ principal fonte */
200         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0));
201         fprintf(f, "%-10.10s", dec->recibo ?: ""); /* Recibo última declaração ano anterior */
202         fprintf(f, "A"); /* Declaração de Ajuste */
203         fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */
204 }
205
206 static void gera_simples(struct declaracao *dec, FILE *f)
207 {
208         fprintf(f, "17");
209         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
210         fprintf(f, "%013lld", 0); /* TODO: carnê leão */
211         fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
212         /* rendimentos isentos */
213         fprintf(f, "%013lld", totais_get(dec, "ISENTOS"));
214         /* rendimentos tributação exclusiva, exceto décimo terceiro */
215         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS_SEM_13o"));
216         /* 13o. */
217         fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO"));
218         fprintf(f, "%013lld", 0); /* TODO: retido na fonte (Lei 11.033) */
219         fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */
220         fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */
221         fprintf(f, "%013lld", 0); /* TODO: isentos dependentes */
222         fprintf(f, "%013lld", 0); /* TODO: exclusivos dependentes */
223         fprintf(f, "%-13.13s", ""); /* FILLER */
224         fprintf(f, "%-13.13s", ""); /* FILLER */
225         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF */
226         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependentes */
227         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior */
228         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF ext. depend. */
229         fprintf(f, "%013lld", 0); /* TODO: carnê-leão PF? */
230         fprintf(f, "%013lld", 0); /* TODO: carnê-leão dependentes */
231         fprintf(f, "%013lld", totais_get(dec, "DEPENDENTES")); /* TODO: dedução dependentes */
232         fprintf(f, "%013lld", 0); /* TODO: previdência RRA */
233         fprintf(f, "%013lld", 0); /* TODO: previdência RRA dependentes */
234         fprintf(f, "%013lld", 0); /* TODO: pensão RRA */
235         fprintf(f, "%013lld", 0); /* TODO: pensão RRA dependentes */
236         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
237         fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
238         fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
239 }
240
241 static void gera_totais_simples(struct declaracao *dec, FILE *f)
242 {
243         fprintf(f, "18");
244         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
245         fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* TODO: rendimentos tributáveis */
246         /* desconto simplificado */
247         fprintf(f, "%013lld", totais_get(dec, "DESCONTO"));
248          /* base de cálculo */
249         fprintf(f, "%013lld", totais_get(dec, "BASE"));
250         fprintf(f, "%013lld", dec->devido); /* imposto devido */
251         fprintf(f, "%013lld", dec->retido); /* imposto retido */
252         fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
253         fprintf(f, "%013lld", 0); /* TODO: carnê-leão */
254         fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
255         fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
256         fprintf(f, "%013lld", dec->pagar); /* imposto pagar */
257         fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
258         fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
259         fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */
260         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */
261         fprintf(f, "%013lld", dec->conjuge.total); /* conjuge */
262         fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
263         fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */
264         fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
265         fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimento tributável PJ titular */
266         fprintf(f, "%013lld", 0); /* TODO: total rural */
267         fprintf(f, "%013lld", dec->retido); /* imposto retido titular */
268         fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */
269         fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */
270         fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */
271         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */
272         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */
273         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */
274         fprintf(f, "%013lld", 0); /* TODO: total não tributável */
275         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
276         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
277         fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
278         fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis, transportado */
279         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva, transportado */
280         fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */
281         fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */
282         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */
283         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependentes */
284         fprintf(f, "%013lld", 0); /* TODO: doações campanha */
285         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
286         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
287         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
288         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
289         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
290         fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
291         fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
292         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
293         fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
294         fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
295         fprintf(f, "%013lld", dec->devido); /* imposto devido */
296         fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
297         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
298         fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
299         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
300         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
301 }
302
303 static void gera_completa(struct declaracao *dec, FILE *f)
304 {
305         fprintf(f, "19");
306         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
307         fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* CNPJ maior */
308         fprintf(f, "%013lld", 0); /* TODO: imposto pago no exterior */
309         fprintf(f, "%013lld", 0); /* TODO: imposto complementar pago */
310         fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
311         fprintf(f, "%013lld", 0); /* TODO: rendimentos recebidos no exterior - titular */
312         fprintf(f, "%013lld", 0); /* TODO: livro caixa - titular */
313         fprintf(f, "%013lld", 0); /* TODO: carnê-leão - titular */
314         fprintf(f, "%013lld", 0); /* TODO: rendimentos recebidos no exterior - dependente */
315         fprintf(f, "%013lld", 0); /* TODO: livro caixa - dependente */
316         fprintf(f, "%013lld", 0); /* TODO: carnê-leão - dependente */
317         fprintf(f, "%013lld", totais_get(dec, "PREVIDENCIA")); /* TODO: contribuição previdenciária privada */
318         fprintf(f, "%013lld", 0); /* TODO: contribuição FAPI */
319         fprintf(f, "%013lld", totais_get(dec, "INSS")); /* TODO: INSS titular */
320         fprintf(f, "%013lld", 0); /* TODO: INSS dependente */
321         fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO")); /* TODO: 13o titular */
322         fprintf(f, "%013lld", 0); /* TODO: 13o dependente */
323         fprintf(f, "%05d", 0); /* TODO: dependentes com instrucao */
324         fprintf(f, "%05d", 0); /* TODO: alimentandos com instrucao */
325         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF titular */
326         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
327         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */
328         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependente */
329         /* IRPF-2015-1.3 gera este valor como 0, antes era dec->devido */
330         fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
331         fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
332 }
333
334 static void gera_totais_completa(struct declaracao *dec, FILE *f)
335 {
336         fprintf(f, "20");
337         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
338         /* FIXME: completar campos */
339         fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimentos PJ titular */
340         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF titular */
341         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependente */
342         fprintf(f, "%013lld", 0); /* TODO: resultado atividade rural */
343         fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* TODO: total rendimentos tributáveis */
344         fprintf(f, "%013lld", totais_get(dec, "INSS")); /* previdencia oficial */
345         fprintf(f, "%013lld", totais_get(dec, "PREVIDENCIA")); /* previdencia privada */
346         fprintf(f, "%013lld", totais_get(dec, "DEPENDENTES")); /* deducao dependentes */
347         fprintf(f, "%013lld", 0); /* TODO: deducao instrucao */
348         fprintf(f, "%013lld", totais_get(dec, "MEDICAS")); /* despesas medicas */
349         fprintf(f, "%013lld", 0); /* TODO: pensao */
350         fprintf(f, "%013lld", 0); /* TODO: pensao escritura publica */
351         fprintf(f, "%013lld", 0); /* TODO: livro caixa */
352         fprintf(f, "%013lld", totais_get(dec, "DEDUCOES")); /* total deduções */
353         fprintf(f, "%013lld", totais_get(dec, "BASE")); /* base de cálculo */
354         fprintf(f, "%013lld", dec->devido); /* TODO: valor do imposto */
355         fprintf(f, "%013lld", 0); /* TODO: deducao do imposto (limitado a 6%) */
356         fprintf(f, "%013lld", dec->devido); /* imposto devido */
357         fprintf(f, "%013lld", 0); /* TODO: contribuicao patronal */
358         fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido menos patronal */
359         fprintf(f, "%013lld", dec->devido); /* TODO: imposto devido mais RRA */
360         fprintf(f, "%013lld", dec->retido); /* imposto na fonte */
361         fprintf(f, "%013lld", 0); /* TODO: carne leao */
362
363         fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
364         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
365         fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
366         fprintf(f, "%013lld", dec->retido); /* TODO: total imposto pago */
367         fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
368         fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
369         fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
370         fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
371
372         fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */
373         fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */
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", dec->conjuge.total); /* conjuge */
377         fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */
378         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */
379         fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
380         fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
381         fprintf(f, "%013lld", 0); /* TODO: imposto renda variável */
382
383         fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
384         fprintf(f, "%013lld", 0); /* TODO: imposto fonte dependente */
385         fprintf(f, "%013lld", 0); /* TODO: imposto moeda estrangeira */
386         fprintf(f, "%013lld", 0); /* TODO: imposto ganhos capital moeda */
387         fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */
388         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */
389         fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */
390         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */
391         fprintf(f, "%013lld", 0); /* TODO: doações campanha */
392         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
393         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
394         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
395         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
396
397         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
398         fprintf(f, "%013lld", 0); /* TODO: previdencia RRA titular */
399         fprintf(f, "%013lld", 0); /* TODO: pensao RRA titular */
400         fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
401         fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
402         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
403         fprintf(f, "%013lld", 0); /* TODO: previdencia RRA dependentes */
404         fprintf(f, "%013lld", 0); /* TODO: pensao RRA dependentes */
405         fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
406         fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
407
408         fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
409         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
410         fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
411         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
412 }
413
414 static void gera_isentos(struct declaracao *dec, FILE *f)
415 {
416         fprintf(f, "23");
417         fprintf(f, "%-11.11s", dec->cpf);
418         fprintf(f, "%013lld", 0); /* TODO: bolsa de estudo */
419         fprintf(f, "%013lld", 0); /* TODO: pecúlio de previdência privada */
420         fprintf(f, "%013lld", 0); /* TODO: FGTS, aviso prévio indenizado */
421         fprintf(f, "%013lld", 0); /* TODO: ganho de capital isento */
422         fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
423         fprintf(f, "%013lld", 0); /* TODO: atividade rural */
424         fprintf(f, "%013lld", 0); /* TODO: contribuintes com 65 anos ou mais */
425         fprintf(f, "%013lld", 0); /* TODO: pensão por invalidez */
426         fprintf(f, "%013lld", totais_get(dec, "POUPANCA"));
427         fprintf(f, "%013lld", 0); /* TODO: sócio ou titular de empresa */
428         fprintf(f, "%013lld", 0); /* TODO: herança, transferências patrimoniais */
429         fprintf(f, "%013lld", 0); /* TODO: outros rendimentos isentos */
430         fprintf(f, "%-60.60s", ""); /* FILLER1 */
431         fprintf(f, "%-13.13s", ""); /* FILLER2 */
432         fprintf(f, "%013lld", 0); /* TODO: alienação bens pequeno valor */
433         fprintf(f, "%013lld", 0); /* TODO: alienação único imóvel */
434         fprintf(f, "%013lld", 0); /* TODO: redução ganho capital */
435         fprintf(f, "%013lld", 0); /* TODO: total bens pequeno valor, transportado GC */
436         fprintf(f, "%013lld", 0); /* TODO: alienção único imóvel, transportado GC */
437         fprintf(f, "%013lld", 0); /* TODO: redução ganho capital, transportado GC */
438         fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira */
439         fprintf(f, "%013lld", 0); /* TODO: ganho capital - moeda estrangeira, transportado */
440         fprintf(f, "%013lld", 0); /* TODO: GC total informado */
441         fprintf(f, "%013lld", 0); /* TODO: GC total transportado */
442         fprintf(f, "%013lld", 0); /* TODO: imposto renda anos anteriores */
443         fprintf(f, "%013lld", 0); /* TODO: rencimentos assalariados moeda estrangeira */
444         fprintf(f, "%013lld", 0); /* TODO: bonificações ações */
445         fprintf(f, "%013lld", 0); /* TODO: médicos residentes */
446         fprintf(f, "%013lld", 0); /* TODO: volutários da Copa */
447         fprintf(f, "%013lld", 0); /* TODO: meação e dissolução */
448         fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos com ações */
449         fprintf(f, "%013lld", 0); /* TODO: ganhos de capital com ouro */
450         fprintf(f, "%013lld", 0); /* TODO: recuperação prejuízos na bolsa */
451         fprintf(f, "%013lld", 0); /* TODO: transportador de cargas */
452         fprintf(f, "%013lld", 0); /* TODO: transportador de passageiros */
453         fprintf(f, "%013lld", 0); /* TODO: retistuição imposto anos anteriores */
454 }
455
456 static void gera_exclusivos(struct declaracao *dec, FILE *f)
457 {
458         fprintf(f, "24");
459         fprintf(f, "%-11.11s", dec->cpf);
460         fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO"));
461         fprintf(f, "%013lld", 0); /* TODO: ganho de capital */
462         fprintf(f, "%013lld", 0); /* TODO: ganho líquido renda variável */
463         fprintf(f, "%013lld", totais_get(dec, "APLICACOES")); /* TODO: aplicações financeiras */
464         fprintf(f, "%013lld", 0); /* TODO: outros */
465         fprintf(f, "%-60.60s", ""); /* FILLER1 */
466         fprintf(f, "%013lld", 0); /* TODO: ganhos de capital alienação de bens */
467         fprintf(f, "%013lld", 0); /* TODO: ganhos de capital moeda estrangeira */
468         fprintf(f, "%013lld", 0); /* TODO: décimo terceiro dependentes */
469         fprintf(f, "%-13.13s", ""); /* FILLER2 */
470         fprintf(f, "%-60.60s", ""); /* FILLER3 */
471         fprintf(f, "%013lld", 0); /* TODO: RRA */
472         fprintf(f, "%013lld", 0); /* TODO: RRA dependentes */
473         fprintf(f, "%013lld", 0); /* TODO: Juros capital próprio */
474         fprintf(f, "%013lld", totais_get(dec, "PLR"));
475 }
476
477 static void gera_conjuge(struct declaracao *dec, FILE *f)
478 {
479         fprintf(f, "29");
480         fprintf(f, "%-11.11s", dec->cpf);
481         fprintf(f, "%-11.11s", dec->conjuge.cpf);
482         fprintf(f, "%013lld", dec->conjuge.base);
483         fprintf(f, "%013lld", dec->conjuge.imposto);
484         fprintf(f, "%013lld", dec->conjuge.isento);
485         fprintf(f, "%013lld", dec->conjuge.exclusivo);
486         fprintf(f, "%013lld", dec->conjuge.rendpj_exigibilidade_suspensa);
487         fprintf(f, "%013lld", dec->conjuge.total);
488         fprintf(f, "%c", dec->conjuge.entregou ? 'S' : 'N');
489 }
490
491 static void gera_trailler(struct declaracao *dec, FILE *f)
492 {
493         int i;
494         fprintf(f, "T9");
495         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
496         fprintf(f, "%06d", dec->linhas[0]);
497         for (i = 16; i < 100; i++)
498                 if (i == 44)
499                         fprintf(f, "%-5.5s", "0"); /* FILLER */
500                 else if (i == 49 || i == 57 || i == 64 || i == 79)
501                         ;
502                 else
503                         fprintf(f, "%05d", dec->linhas[i]);
504 }
505
506 static void gera_reciboheader(struct declaracao *dec, FILE *f)
507 {
508         fprintf(f, "HR");
509         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
510         fprintf(f, "%-3.3s", ""); /* FILLER */
511 }
512
513 static void gera_recibodetalhe(struct declaracao *dec, FILE *f)
514 {
515         fprintf(f, "DR");
516         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
517         fprintf(f, "%-3.3s", ""); /* FILLER */
518         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
519         fprintf(f, "%-60.60s", dec->nome);
520         fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
521         fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
522         fprintf(f, "%-6.6s", dec->contribuinte.numero);
523         fprintf(f, "%-21.21s", dec->contribuinte.complemento ?: "");
524         fprintf(f, "%-19.19s", dec->contribuinte.bairro);
525         fprintf(f, "%-9.9s", dec->contribuinte.cep);
526         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
527         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
528         fprintf(f, "%-2.2s", dec->contribuinte.uf);
529         fprintf(f, "%-100.100s", ""); /* FILLER */
530         fprintf(f, "%-4.4s", ""); /* FILLER */
531         fprintf(f, "%-8.8s", ""); /* FILLER */
532         fprintf(f, "%-4.4s", dec->contribuinte.ddd);
533         fprintf(f, "%-9.9s", dec->contribuinte.telefone);
534         fprintf(f, "%s", dec->retifica ? "S" : "N"); /* Retificadora */
535         fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */
536         fprintf(f, "%013lld", dec->devido); /* Imposto devido */
537         fprintf(f, "%013lld", dec->restituicao); /* Imposto a restituir */
538         fprintf(f, "%013lld", dec->pagar); /* Imposto a pagar */
539         fprintf(f, "%d", dec->pagar ? 1 : 0); /* TODO/FIXME: quotas */
540         fprintf(f, "%013lld", dec->pagar); /* TODO: valor quota */
541         fprintf(f, "%-3.3s", dec->banco ? : "000"); /* numero banco */
542         fprintf(f, "%-4.4s", dec->agencia ? : "0000"); /* numero agencia */
543         fprintf(f, " "); /* FILLER */
544         fprintf(f, "%d", 0); /* TODO: debito primeira quota */
545         fprintf(f, "%013lld", 0); /* TODO: imposto pago ganho de capital */
546         fprintf(f, "%-13.13s", dec->contacorrente ? : ""); /* numero conta */
547         fprintf(f, "%-2.2s", dec->dvconta ? : ""); /* DV conta */
548         fprintf(f, "%013lld", 0); /* TODO: imposto alienacao moeda estrangeira em especie */
549 }
550
551 static void gera_recibotrailler(struct declaracao *dec, FILE *f)
552 {
553         fprintf(f, "R9");
554         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
555         fprintf(f, "%-3.3s", ""); /* FILLER */
556         fprintf(f, "%010u", dec->rhash);
557 }
558
559 static void gera_rendimento(struct declaracao *dec, FILE *f)
560 {
561         struct rendimento *r;
562         r = list_get(dec->rendimento, dec->linhas[21]);
563         fprintf(f, "21");
564         fprintf(f, "%s", dec->cpf);
565         fprintf(f, "%-14.14s", r->cnpj);
566         fprintf(f, "%-60.60s", r->nome);
567         fprintf(f, "%013lld", r->rendimento);
568         fprintf(f, "%013lld", r->previdencia);
569         fprintf(f, "%013lld", r->decimoterceiro);
570         fprintf(f, "%013lld", r->imposto);
571         fprintf(f, "%-8.8s", r->saida);
572         fprintf(f, "%013lld", r->imposto_13o);
573 }
574
575 static void gera_pagamento(struct declaracao *dec, FILE *f)
576 {
577         struct pagamento *p;
578         p = list_get(dec->pagamentos, dec->linhas[26]);
579         fprintf(f, "26");
580         fprintf(f, "%s", dec->cpf);
581         fprintf(f, "%02d", p->codigo);
582         fprintf(f, "%05d", 0); /* TODO: chave dependente */
583         fprintf(f, "%-14.14s", p->cnpj);
584         fprintf(f, "%-60.60s", p->nome);
585         fprintf(f, "%-11.11s", ""); /* TODO: NIT */
586         fprintf(f, "%013lld", p->pagamento);
587         fprintf(f, "%013lld", p->reembolso);
588         fprintf(f, "%d", strlen(p->cnpj) == 11 ? 1 : 2);
589         /* Titular (T), Dependente (D), Alimentando (A), FIXME */
590         fprintf(f, "T");
591 }
592
593 static void gera_isento(struct declaracao *dec, FILE *f, int codigo)
594 {
595         struct isento *i;
596         i = isento_get(dec, codigo, dec->linhas[codigo]);
597         fprintf(f, "%02d", codigo);
598         fprintf(f, "%-11.11s", dec->cpf); /* Titular, TODO: dependente */
599         fprintf(f, "%05d", dec->linhas[codigo] + 1); /* Chave */
600         fprintf(f, "%c", 'T'); /* FIXME: dependente */
601         fprintf(f, "%-14.14s", i->cnpj);
602         fprintf(f, "%-60.60s", i->nome);
603         fprintf(f, "%013lld", i->valor);
604         fprintf(f, "%-11.11s", dec->cpf);
605 }
606
607 static void gera_plr(struct declaracao *dec, FILE *f)
608 {
609         gera_isento(dec, f, 96);
610 }
611
612 static void gera_poupanca(struct declaracao *dec, FILE *f)
613 {
614         gera_isento(dec, f, 98);
615 }
616
617 static void gera_aplicacoes(struct declaracao *dec, FILE *f)
618 {
619         gera_isento(dec, f, 99);
620 }
621
622 static void gera_dependente(struct declaracao *dec, FILE *f)
623 {
624         struct dependente *d;
625         d = list_get(dec->dependentes, dec->linhas[25]);
626
627         fprintf(f, "25");
628         fprintf(f, "%s", dec->cpf);
629         fprintf(f, "%05d", dec->linhas[25] + 1);
630         fprintf(f, "%02d", d->codigo);
631         fprintf(f, "%-60.60s", d->nome);
632         fprintf(f, "%-8.8s", d->dn);
633         fprintf(f, "%-11.11s", d->cpf);
634         /* TODO: Indicador de saída */
635         fprintf(f, " ");
636 }
637
638 static void gera_bem(struct declaracao *dec, FILE *f)
639 {
640         struct bem *b;
641         b = list_get(dec->bens, dec->linhas[27]);
642
643         fprintf(f, "27");
644         fprintf(f, "%s", dec->cpf);
645         fprintf(f, "%02d", b->codigo);
646
647         /* FIXME: exterior */
648         fprintf(f, "0");
649         fprintf(f, "105"); /* código Brasil */
650
651         fprintf(f, "%-512.512s", b->descricao);
652         fprintf(f, "%013lld", b->valor_anterior);
653         fprintf(f, "%013lld", b->valor);
654
655         /* Imóvel */
656         fprintf(f, "%-40.40s", b->logradouro ?: "");
657         fprintf(f, "%-6.6s", b->numero ?: "");
658         fprintf(f, "%-40.40s", b->complemento ?: "");
659         fprintf(f, "%-40.40s", b->bairro ?: "");
660         fprintf(f, "%-9.9s", b->cep ?: "");
661         fprintf(f, "%-2.2s", b->uf ?: "");
662         fprintf(f, "%04d", b->cd_municipio);
663         fprintf(f, "%-40.40s", b->municipio ?: "");
664         /* FIXME: Registro de imóveis, Nao (0), Sim (1), Vazio (2) */
665         fprintf(f, "%d", b->registro ? 1 : 0);
666         fprintf(f, "%-40.40s", b->matricula ?: "");
667         fprintf(f, "%-40.40s", b->registro ?: "");
668         fprintf(f, "%011lld", b->area);
669         /* FIXME: Area, M2 (0), Ha (1), Vazio (2) */
670         fprintf(f, "%d", 0);
671         fprintf(f, "%-60.60s", b->cartorio ?: "");
672
673         /* Número de chave */
674         fprintf(f, "%05d", dec->linhas[27] + 1);
675 }
676
677 typedef void (gera_linha)(struct declaracao *dec, FILE *f);
678
679 static void update_hash(struct declaracao *dec, char *buf, size_t len)
680 {
681         int r;
682         int linha;
683         r = sscanf(buf, "%2d", &linha);
684         if (r == 1 || !strncmp(buf, "T9", 2)) {
685                 dec->hash = crc32(dec->hash, buf, len - 2);
686                 dec->rhash = crc32(dec->rhash, buf, len - 2);
687                 return;
688         } else if (strncmp(buf, "R9", 2) && strncmp(buf, "IRPF", 4)) {
689                 dec->rhash = crc32(dec->rhash, buf, len - 2);
690         }
691 }
692
693 static int wrap(gera_linha fn, struct declaracao *dec, struct list *l)
694 {
695         FILE *m;
696         char *buf = NULL;
697         size_t bsize;
698         int linha;
699         int r;
700         uLong c = crc32(0L, Z_NULL, 0);
701         m = open_memstream(&buf, &bsize);
702         if (!m) {
703                 return -errno;
704         }
705         fn(dec, m);
706         dec->linhas[0]++;
707         fflush(m);
708
709         /* Calcula CRC32 para campo de controle dos registros. */
710         c = crc32(c, buf, bsize);
711         fprintf(m, "%010ld\r\n", c);
712
713         fclose(m);
714         r = sscanf(buf, "%2d", &linha);
715         if (r == 1 && linha >= 0 && linha < 100) {
716                 dec->linhas[linha]++;
717         }
718         update_hash(dec, buf, bsize);
719         return list_add(&l, buf);
720 }
721
722 static void insert_hash(struct declaracao *dec, char *irpf)
723 {
724         uLong c = crc32(0L, NULL, 0);
725         char cpf[13];
726         char crc[11];
727         snprintf(cpf, 13, "%-8.8s.DEC", dec->cpf);
728         c = crc32(c, cpf, 12);
729         snprintf(crc, 11, "%010ld", dec->hash);
730         memcpy(irpf + 101, crc, 10);
731         c = crc32(c, irpf, strlen(irpf) - 12);
732         snprintf(crc, 11, "%010ld", c);
733         memcpy(irpf + strlen(irpf) - 12, crc, 10);
734 }
735
736 static int gera(struct declaracao *dec, char *filename)
737 {
738         FILE *f;
739         int r;
740         int i;
741         struct rendimento *rendimento;
742         struct isento *isento;
743         struct pagamento *pagamento;
744         struct bem *bem;
745         struct dependente *dependente;
746         struct list *linhas;
747         char *buf;
748
749         linhas = list_new();
750         if (!linhas)
751                 return -ENOMEM;
752
753         dec->hash = crc32(0L, NULL, 0);
754         dec->rhash = crc32(0L, NULL, 0);
755
756 #define W(fn) \
757         do { \
758                 r = wrap(fn, dec, linhas); \
759                 if (r < 0) \
760                         goto out; \
761         } while (0);
762
763         f = fopen(filename, "w");
764         if (!f)
765                 goto out_file;
766         memset(dec->linhas, 0, sizeof(dec->linhas));
767         W(gera_header);
768         W(gera_contribuinte);
769         if (dec->tipo == COMPLETA) {
770                 W(gera_completa);
771                 W(gera_totais_completa);
772         } else {
773                 W(gera_simples);
774                 W(gera_totais_simples);
775         }
776         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
777                 W(gera_rendimento);
778         }
779
780         W(gera_isentos);
781         W(gera_exclusivos);
782
783         for (i = 0; (dependente = list_get(dec->dependentes, i)); i++) {
784                 W(gera_dependente);
785         }
786         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
787                 W(gera_pagamento);
788         }
789         for (i = 0; (bem = list_get(dec->bens, i)); i++) {
790                 W(gera_bem);
791         }
792
793         if (dec->conjuge.cpf)
794                 W(gera_conjuge);
795
796         /* Rendimentos isentos e com tributação exclusiva */
797         /* Registros 82 a 89, e 92 a 99 */
798 #define IW(fn, codigo) \
799         for (i = 0; ; i++) { \
800                 isento = isento_get(dec, codigo, i); \
801                 if (!isento) \
802                         break; \
803                 W(fn); \
804         }
805         IW(gera_plr, 96);
806         IW(gera_poupanca, 98);
807         IW(gera_aplicacoes, 99);
808
809         W(gera_trailler);
810         W(gera_reciboheader);
811         W(gera_recibodetalhe);
812         W(gera_recibotrailler);
813
814         insert_hash(dec, list_get(linhas, 0));
815         for (i = 0; (buf = list_get(linhas, i)); i++) {
816                 fwrite(buf, strlen(buf), 1, f);
817         }
818
819         fclose(f);
820         list_free(linhas, free);
821         return 0;
822 out:
823         fclose(f);
824 out_file:
825         list_free(linhas, free);
826         return r;
827 }
828
829 static int run_gera(struct declaracao *dec, char **args, int argc)
830 {
831         if (argc != 2)
832                 return -EINVAL;
833         if (dec->ano != 2015)
834                 return -EINVAL;
835         return gera(dec, args[1]);
836 }
837
838 static struct cmd cmd_gera = {
839         .name = "gera",
840         .run = run_gera,
841 };
842
843 int gera_cmd_init(void)
844 {
845         cmd_add(&cmd_gera);
846         return 0;
847 }