a46d94be9c9d02b514800f770792e51ec78dea09
[cascardo/declara.git] / 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 "declaracao.h"
21 #include "cmd.h"
22 #include "rendimento.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 static void gera_header(struct declaracao *dec, FILE *f)
29 {
30         fprintf(f, "IRPF    ");
31         fprintf(f, "%04d", dec->ano); /* Exercício */
32         fprintf(f, "%04d", dec->ano - 1); /* Ano base */
33         fprintf(f, "2500"); /* Código */
34         fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */
35         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
36         fprintf(f, "   "); /* Filler */
37         fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */
38         fprintf(f, "100"); /* Versão IRPF */
39         fprintf(f, "%-60.60s", dec->nome);
40         fprintf(f, "%s", dec->contribuinte.uf);
41         fprintf(f, "%s", "0000000000"); /* FIXME: hash */
42         fprintf(f, "1"); /* TODO: certificavel */
43         fprintf(f, "%s", dec->contribuinte.dn);
44         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
45         fprintf(f, "%s", dec->restituicao ? "2" : "0"); /* TODO: resultado imposto, 2 - restituicao */
46         fprintf(f, "S"); /* TODO: gerada? */
47         fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */
48         fprintf(f, "2"); /* TODO: PGD */
49         fprintf(f, "LINUX         "); /* TODO: SO */
50         fprintf(f, "3.16.0-"); /* TODO: versao SO */
51         fprintf(f, "1.7.0_75 "); /* TODO: versao JVM */
52         fprintf(f, "%-10.10s", dec->retifica ? : ""); /* TODO: recibo da última declaração transmitida (recibo ou retifica?) */
53         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
54         fprintf(f, "           "); /* TODO: CPF conjuge */
55         fprintf(f, "0"); /* TODO: obrigatoria entrega */
56         fprintf(f, "0000000000000"); /* TODO: imposto devido, carne-leao, complementar dos dependentes */
57         fprintf(f, "%-10.10s", dec->recibo); /* recibo ex. anterior */
58         fprintf(f, "2"); /* TODO: indicador seguranca */
59         fprintf(f, "00"); /* TODO: indicador imposto pago */
60         fprintf(f, "0"); /* TODO: indicador imposto antecipado */
61         fprintf(f, "0"); /* TODO: mudanca endereco */
62         fprintf(f, "%s", dec->contribuinte.cep);
63         fprintf(f, "0"); /* TODO: debito 1a quota */
64         fprintf(f, "000"); /* TODO: no banco */
65         fprintf(f, "0000"); /* TODO: agencia */
66         fprintf(f, " "); /* TODO: filler */
67         fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */
68         fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */
69         fprintf(f, " "); /* RRA1 */
70         fprintf(f, "%-11.11s", ""); /* CPF RRA1 */
71         fprintf(f, " "); /* RRA2 */
72         fprintf(f, "%-11.11s", ""); /* CPF RRA2 */
73         fprintf(f, " "); /* RRA3 */
74         fprintf(f, "%-11.11s", ""); /* CPF RRA3 */
75         fprintf(f, " "); /* RRA4 */
76         fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
77         fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
78         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
79         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 2 */
80         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 3 */
81         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 4 */
82         fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */
83         fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */
84         fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */
85         fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */
86         fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */
87         fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */
88         fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */
89         fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */
90         fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */
91         fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */
92         fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */
93         fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */
94         fprintf(f, "%-14.14s", ""); /* TODO: medico maior */
95         fprintf(f, "%-14.14s", ""); /* TODO: medico maior 2 */
96         fprintf(f, "%-11.11s", ""); /* CPF pensao */
97         fprintf(f, "%-11.11s", ""); /* CPF inventariante */
98         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
99         fprintf(f, "%-60.60s", dec->nome);
100         fprintf(f, "%-11.11s", ""); /* CPF empregada */
101         fprintf(f, "000000000000"); /* FIXME: MAC */
102         fprintf(f, "%08d", 0); /* Data saída */
103         fprintf(f, "%-11.11s", ""); /* CPF procurador */
104         fprintf(f, "000"); /* TODO: criterio obrigatoriedade */
105         fprintf(f, "%013lld", dec->totalrendimento); /* Total tributavel */
106         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia */
107         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia 2 */
108         fprintf(f, "%013lld", 0); /* TODO: Total isentos */
109         fprintf(f, "%013lld", 0); /* TODO: Total exclusivo */
110         fprintf(f, "%013lld", 0); /* TODO: Total pagamentos */
111         fprintf(f, "%-13.13s", ""); /* TODO: numero conta */
112         fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
113         fprintf(f, "0"); /* TODO: revalidar DV conta */
114
115         /* Mudanças de 2015 */
116
117         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* natureza ocupacao */
118         fprintf(f, "%-11.11s", ""); /* CPF domestica */
119         fprintf(f, "%-11.11s", ""); /* NI domestica */
120         fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */
121         fprintf(f, "%-11.11s", ""); /* NI domestica 2 */
122         fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */
123         fprintf(f, "%-11.11s", ""); /* NI domestica 3 */
124         fprintf(f, "1"); /* Declaracao iniciada */
125         fprintf(f, "1"); /* Utilizou PGD */
126         fprintf(f, "0"); /* Utilizou APP */
127         fprintf(f, "0"); /* Utilizou online */
128         fprintf(f, "0"); /* Utilizou rascunho */
129         fprintf(f, "0"); /* Utilizou pre preenchida */
130         fprintf(f, "0"); /* Utilizou fontes */
131         fprintf(f, "0"); /* Utilizou plano saude */
132         fprintf(f, "0"); /* Utilizou recuperar */
133         fprintf(f, "1"); /* Declaracao iniciada */
134
135         /* Fim das mudanças de 2015 */
136
137         fprintf(f, "   "); /* Teste PGD, FILLER2 */
138         fprintf(f, "0000000000"); /* FIXME: controle */
139         fprintf(f, "\r\n");
140 }
141
142 static void gera_contribuinte(struct declaracao *dec, FILE *f)
143 {
144         fprintf(f, "16");
145         fprintf(f, "%s", dec->cpf);
146         fprintf(f, "%-60.60s", dec->nome);
147         fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
148         fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
149         fprintf(f, "%-6.6s", dec->contribuinte.numero);
150         fprintf(f, "%-21.21s", dec->contribuinte.complemento);
151         fprintf(f, "%-19.19s", dec->contribuinte.bairro);
152         fprintf(f, "%-9.9s", dec->contribuinte.cep);
153         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
154         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
155         fprintf(f, "%-2.2s", dec->contribuinte.uf);
156         fprintf(f, "   "); /* TODO: código exterior */
157         fprintf(f, "105"); /* TODO: código país */
158         fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */
159         fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */
160         fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */
161         fprintf(f, "%-4.4s", dec->contribuinte.ddd);
162         fprintf(f, "%-9.9s", dec->contribuinte.telefone);
163         fprintf(f, "%-8.8s", dec->contribuinte.dn);
164         fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor);
165         fprintf(f, "%03d", dec->contribuinte.ocupacao_principal);
166         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao);
167         fprintf(f, "0"); /* TODO: qnt quotas */
168         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
169         fprintf(f, "%s", dec->retifica ? "S" : "N");
170         fprintf(f, "S"); /* GERADO */
171         fprintf(f, "N"); /* TODO: mudança de endereço */
172         fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */
173         fprintf(f, "%-3.3s", "000"); /* TODO: banco */
174         fprintf(f, "%-4.4s", "0000"); /* TODO: agência */
175         fprintf(f, "N"); /* TODO: deficiência */
176         fprintf(f, "0"); /* FILLER: pré-preenchida */
177         fprintf(f, "%-13.13s", ""); /* TODO: conta */
178         fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
179         fprintf(f, "N"); /* TODO: débito automático */
180         fprintf(f, "0"); /* TODO: débito primeira quota */
181         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ principal fonte */
182         fprintf(f, "%-10.10s", dec->recibo); /* Recibo última declaração ano anterior */
183         fprintf(f, "A"); /* Declaração de Ajuste */
184         fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */
185         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
186         fprintf(f, "\r\n");
187 }
188
189 static void gera_simples(struct declaracao *dec, FILE *f)
190 {
191         fprintf(f, "17");
192         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
193         fprintf(f, "%013lld", 0); /* TODO: carnê leão */
194         fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
195         fprintf(f, "%013lld", 0); /* TODO: isentos */
196         fprintf(f, "%013lld", 0); /* TODO: rendimentos tributação exclusiva */
197         fprintf(f, "%013lld", 0); /* TODO: 13o. */
198         fprintf(f, "%013lld", dec->retido); /* retido na fonte */
199         fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */
200         fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */
201         fprintf(f, "%013lld", 0); /* TODO: isentos dependentes */
202         fprintf(f, "%013lld", 0); /* TODO: exclusivos dependentes */
203         fprintf(f, "%-13.13s", ""); /* FILLER */
204         fprintf(f, "%-13.13s", ""); /* FILLER */
205         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF */
206         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependentes */
207         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior */
208         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF ext. depend. */
209         fprintf(f, "%013lld", 0); /* TODO: carnê-leão PF? */
210         fprintf(f, "%013lld", 0); /* TODO: carnê-leão dependentes */
211         fprintf(f, "%013lld", 0); /* TODO: dedução dependentes */
212         fprintf(f, "%013lld", 0); /* TODO: previdência */
213         fprintf(f, "%013lld", 0); /* TODO: previdência dependentes */
214         fprintf(f, "%013lld", 0); /* TODO: pensão */
215         fprintf(f, "%013lld", 0); /* TODO: pensão dependentes */
216         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
217         fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
218         fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
219         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
220         fprintf(f, "\r\n");
221 }
222
223 static void gera_totais_simples(struct declaracao *dec, FILE *f)
224 {
225         fprintf(f, "18");
226         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
227         fprintf(f, "%013lld", dec->totalrendimento); /* TODO: rendimentos tributáveis */
228         fprintf(f, "%013lld", 0); /* TODO: desconto simplificado */
229         fprintf(f, "%013lld", 0); /* TODO: base cálculo */
230         fprintf(f, "%013lld", dec->devido); /* imposto devido */
231         fprintf(f, "%013lld", dec->retido); /* imposto retido */
232         fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
233         fprintf(f, "%013lld", 0); /* TODO: carnê-leão */
234         fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
235         fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */
236         fprintf(f, "%013lld", dec->pagar); /* imposto pagar */
237         fprintf(f, "%d", 0); /* TODO: quotas */
238         fprintf(f, "%013lld", 0); /* TODO: valor quota */
239         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos */
240         fprintf(f, "%013lld", 0); /* TODO: rendimentos tributação exclusiva */
241         fprintf(f, "%013lld", 0); /* TODO: conjuge */
242         fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
243         fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */
244         fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
245         fprintf(f, "%013lld", 0); /* TODO: rendimento tributável PJ titular */
246         fprintf(f, "%013lld", 0); /* TODO: total rural */
247         fprintf(f, "%013lld", dec->retido); /* imposto retido titular */
248         fprintf(f, "%013lld", 0); /* TODO: total bens ano anterior */
249         fprintf(f, "%013lld", 0); /* TODO: total bens ano base */
250         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos titular */
251         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */
252         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva titular */
253         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */
254         fprintf(f, "%013lld", 0); /* TODO: total não tributável */
255         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
256         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
257         fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */
258         fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis */
259         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva */
260         fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */
261         fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */
262         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */
263         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependentes */
264         fprintf(f, "%013lld", 0); /* TODO: doações campanha */
265         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
266         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
267         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
268         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
269         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
270         fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
271         fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
272         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
273         fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
274         fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
275         fprintf(f, "%013lld", dec->devido); /* imposto devido */
276         fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
277         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
278         fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
279         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
280         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
281         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
282         fprintf(f, "\r\n");
283 }
284
285 static void gera_completa(struct declaracao *dec, FILE *f)
286 {
287         fprintf(f, "19");
288         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
289         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
290         /* FIXME: completar campos */
291         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
292         fprintf(f, "\r\n");
293 }
294
295 static void gera_totais_completa(struct declaracao *dec, FILE *f)
296 {
297         fprintf(f, "20");
298         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
299         /* FIXME: completar campos */
300         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
301         fprintf(f, "\r\n");
302 }
303
304 static void gera_trailler(struct declaracao *dec, FILE *f)
305 {
306         int i;
307         fprintf(f, "T9");
308         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
309         fprintf(f, "%06d", dec->linhas[0]);
310         for (i = 16; i < 100; i++)
311                 if (i == 44)
312                         fprintf(f, "%-5.5s", "0"); /* FILLER */
313                 else if (i == 49 || i == 57 || i == 64 || i == 79)
314                         ;
315                 else
316                         fprintf(f, "%05d", dec->linhas[i]);
317         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
318         fprintf(f, "\r\n");
319 }
320
321 static void gera_reciboheader(struct declaracao *dec, FILE *f)
322 {
323         fprintf(f, "HR");
324         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
325         fprintf(f, "%-3.3s", ""); /* FILLER */
326         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
327         fprintf(f, "\r\n");
328 }
329
330 static void gera_recibodetalhe(struct declaracao *dec, FILE *f)
331 {
332         fprintf(f, "DR");
333         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
334         fprintf(f, "%-3.3s", ""); /* FILLER */
335         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
336         fprintf(f, "%-60.60s", dec->nome);
337         fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
338         fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
339         fprintf(f, "%-6.6s", dec->contribuinte.numero);
340         fprintf(f, "%-21.21s", dec->contribuinte.complemento);
341         fprintf(f, "%-19.19s", dec->contribuinte.bairro);
342         fprintf(f, "%-9.9s", dec->contribuinte.cep);
343         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
344         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
345         fprintf(f, "%-2.2s", dec->contribuinte.uf);
346         fprintf(f, "%-100.100s", ""); /* FILLER */
347         fprintf(f, "%-4.4s", ""); /* FILLER */
348         fprintf(f, "%-8.8s", ""); /* FILLER */
349         fprintf(f, "%-4.4s", dec->contribuinte.ddd);
350         fprintf(f, "%-9.9s", dec->contribuinte.telefone);
351         fprintf(f, "%s", dec->retifica ? "S" : "N"); /* Retificadora */
352         fprintf(f, "%013lld", dec->totalrendimento); /* Total tributavel */
353         fprintf(f, "%013lld", dec->devido); /* Imposto devido */
354         fprintf(f, "%013lld", dec->restituicao); /* Imposto a restituir */
355         fprintf(f, "%013lld", dec->pagar); /* Imposto a pagar */
356         fprintf(f, "%d", 0); /* TODO: quotas */
357         fprintf(f, "%013lld", 0); /* TODO: valor quota */
358         fprintf(f, "%03d", 0); /* TODO: numero banco */
359         fprintf(f, "%04d", 0); /* TODO: numero agencia */
360         fprintf(f, " "); /* FILLER */
361         fprintf(f, "%d", 0); /* TODO: debito primeira quota */
362         fprintf(f, "%013lld", 0); /* TODO: imposto pago ganho de capital */
363         fprintf(f, "%-13.13s", ""); /* TODO: numero conta */
364         fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
365         fprintf(f, "%013lld", 0); /* TODO: imposto alienacao moeda estrangeira em especie */
366         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
367         fprintf(f, "\r\n");
368 }
369
370 static void gera_recibotrailler(struct declaracao *dec, FILE *f)
371 {
372         fprintf(f, "R9");
373         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
374         fprintf(f, "%-3.3s", ""); /* FILLER */
375         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: hash */
376         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
377         fprintf(f, "\r\n");
378 }
379
380 typedef void (gera_linha)(struct declaracao *dec, FILE *f);
381
382 static int wrap(gera_linha fn, struct declaracao *dec, FILE *f)
383 {
384         FILE *m;
385         char *buf = NULL;
386         size_t bsize;
387         int linha;
388         int r;
389         m = open_memstream(&buf, &bsize);
390         if (!m) {
391                 return -errno;
392         }
393         fn(dec, m);
394         dec->linhas[0]++;
395         fclose(m);
396         r = sscanf(buf, "%2d", &linha);
397         if (r == 1 && linha >= 0 && linha < 100) {
398                 dec->linhas[linha]++;
399         }
400         fwrite(buf, bsize, 1, f);
401         free(buf);
402         return 0;
403 }
404
405 static int gera(struct declaracao *dec, char *filename)
406 {
407         FILE *f;
408         int r;
409
410 #define W(fn, dec, f) \
411         do { \
412                 r = wrap(fn, dec, f); \
413                 if (r < 0) \
414                         goto out; \
415         } while (0);
416
417         f = fopen(filename, "w");
418         if (!f)
419                 return -errno;
420         memset(dec->linhas, 0, sizeof(dec->linhas));
421         W(gera_header, dec, f);
422         W(gera_contribuinte, dec, f);
423         if (dec->tipo == COMPLETA) {
424                 W(gera_completa, dec, f);
425                 W(gera_totais_completa, dec, f);
426         } else {
427                 W(gera_simples, dec, f);
428                 W(gera_totais_simples, dec, f);
429         }
430         W(gera_trailler, dec, f);
431         W(gera_reciboheader, dec, f);
432         W(gera_recibodetalhe, dec, f);
433         W(gera_recibotrailler, dec, f);
434         fclose(f);
435         return 0;
436 out:
437         fclose(f);
438         return r;
439 }
440
441 static int run_gera(struct declaracao *dec, char **args, int argc)
442 {
443         if (argc != 2)
444                 return -EINVAL;
445         return gera(dec, args[1]);
446 }
447
448 static struct cmd cmd_gera = {
449         .name = "gera",
450         .run = run_gera,
451 };
452
453 int gera_cmd_init(void)
454 {
455         cmd_add(&cmd_gera);
456         return 0;
457 }