Imprime linha de resumo e totais da declaração simplificada.
[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
26 static void gera_header(struct declaracao *dec, FILE *f)
27 {
28         fprintf(f, "IRPF    ");
29         fprintf(f, "%04d", dec->ano); /* Exercício */
30         fprintf(f, "%04d", dec->ano - 1); /* Ano base */
31         fprintf(f, "2500"); /* Código */
32         fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */
33         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
34         fprintf(f, "   "); /* Filler */
35         fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */
36         fprintf(f, "100"); /* Versão IRPF */
37         fprintf(f, "%-60.60s", dec->nome);
38         fprintf(f, "%s", dec->contribuinte.uf);
39         fprintf(f, "%s", "0000000000"); /* FIXME: hash */
40         fprintf(f, "1"); /* TODO: certificavel */
41         fprintf(f, "%s", dec->contribuinte.dn);
42         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
43         fprintf(f, "0"); /* TODO: resultado imposto */
44         fprintf(f, "S"); /* TODO: gerada? */
45         fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */
46         fprintf(f, "2"); /* TODO: PGD */
47         fprintf(f, "LINUX         "); /* TODO: SO */
48         fprintf(f, "3.16.0-"); /* TODO: versao SO */
49         fprintf(f, "1.7.0_75 "); /* TODO: versao JVM */
50         fprintf(f, "%-10.10s", dec->retifica ? : ""); /* TODO: recibo da última declaração transmitida (recibo ou retifica?) */
51         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
52         fprintf(f, "           "); /* TODO: CPF conjuge */
53         fprintf(f, "0"); /* TODO: obrigatoria entrega */
54         fprintf(f, "0000000000000"); /* TODO: imposto devido, carne-leao, complementar dos dependentes */
55         fprintf(f, "%-10.10s", dec->recibo); /* recibo ex. anterior */
56         fprintf(f, "2"); /* TODO: indicador seguranca */
57         fprintf(f, "00"); /* TODO: indicador imposto pago */
58         fprintf(f, "0"); /* TODO: indicador imposto antecipado */
59         fprintf(f, "0"); /* TODO: mudanca endereco */
60         fprintf(f, "%s", dec->contribuinte.cep);
61         fprintf(f, "0"); /* TODO: debito 1a quota */
62         fprintf(f, "000"); /* TODO: no banco */
63         fprintf(f, "0000"); /* TODO: agencia */
64         fprintf(f, " "); /* TODO: filler */
65         fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */
66         fprintf(f, "0000000000000"); /* TODO: imposto a pagar */
67         fprintf(f, " "); /* RRA1 */
68         fprintf(f, "%-11.11s", ""); /* CPF RRA1 */
69         fprintf(f, " "); /* RRA2 */
70         fprintf(f, "%-11.11s", ""); /* CPF RRA2 */
71         fprintf(f, " "); /* RRA3 */
72         fprintf(f, "%-11.11s", ""); /* CPF RRA3 */
73         fprintf(f, " "); /* RRA4 */
74         fprintf(f, "%-11.11s", ""); /* CPF RRA4 */
75         fprintf(f, "0000000000000"); /* TODO: Doacao ECA */
76         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
77         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 2 */
78         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 3 */
79         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 4 */
80         fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */
81         fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */
82         fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */
83         fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */
84         fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */
85         fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */
86         fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */
87         fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */
88         fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */
89         fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */
90         fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */
91         fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */
92         fprintf(f, "%-14.14s", ""); /* TODO: medico maior */
93         fprintf(f, "%-14.14s", ""); /* TODO: medico maior 2 */
94         fprintf(f, "%-11.11s", ""); /* CPF pensao */
95         fprintf(f, "%-11.11s", ""); /* CPF inventariante */
96         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
97         fprintf(f, "%-60.60s", dec->nome);
98         fprintf(f, "%-11.11s", ""); /* CPF empregada */
99         fprintf(f, "000000000000"); /* FIXME: MAC */
100         fprintf(f, "%08d", 0); /* Data saída */
101         fprintf(f, "%-11.11s", ""); /* CPF procurador */
102         fprintf(f, "000"); /* TODO: criterio obrigatoriedade */
103         fprintf(f, "%013lld", dec->totalrendimento); /* Total tributavel */
104         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia */
105         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ previdencia 2 */
106         fprintf(f, "%013lld", 0); /* TODO: Total isentos */
107         fprintf(f, "%013lld", 0); /* TODO: Total exclusivo */
108         fprintf(f, "%013lld", 0); /* TODO: Total pagamentos */
109         fprintf(f, "%-13.13s", ""); /* TODO: numero conta */
110         fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
111         fprintf(f, "0"); /* TODO: revalidar DV conta */
112
113         /* Mudanças de 2015 */
114
115         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* TODO: natureza ocupacao */
116         fprintf(f, "%-11.11s", ""); /* CPF domestica */
117         fprintf(f, "%-11.11s", ""); /* NI domestica */
118         fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */
119         fprintf(f, "%-11.11s", ""); /* NI domestica 2 */
120         fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */
121         fprintf(f, "%-11.11s", ""); /* NI domestica 3 */
122         fprintf(f, "1"); /* Declaracao iniciada */
123         fprintf(f, "1"); /* Utilizou PGD */
124         fprintf(f, "0"); /* Utilizou APP */
125         fprintf(f, "0"); /* Utilizou online */
126         fprintf(f, "0"); /* Utilizou rascunho */
127         fprintf(f, "0"); /* Utilizou pre preenchida */
128         fprintf(f, "0"); /* Utilizou fontes */
129         fprintf(f, "0"); /* Utilizou plano saude */
130         fprintf(f, "0"); /* Utilizou recuperar */
131         fprintf(f, "1"); /* Declaracao iniciada */
132
133         /* Fim das mudanças de 2015 */
134
135         fprintf(f, "   "); /* Teste PGD, FILLER2 */
136         fprintf(f, "0000000000"); /* FIXME: controle */
137         fprintf(f, "\r\n");
138 }
139
140 static void gera_contribuinte(struct declaracao *dec, FILE *f)
141 {
142         fprintf(f, "16");
143         fprintf(f, "%s", dec->cpf);
144         fprintf(f, "%-60.60s", dec->nome);
145         fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro);
146         fprintf(f, "%-40.40s", dec->contribuinte.logradouro);
147         fprintf(f, "%-6.6s", dec->contribuinte.numero);
148         fprintf(f, "%-21.21s", dec->contribuinte.complemento);
149         fprintf(f, "%-19.19s", dec->contribuinte.bairro);
150         fprintf(f, "%-9.9s", dec->contribuinte.cep);
151         fprintf(f, "%04d", dec->contribuinte.cd_municipio);
152         fprintf(f, "%-40.40s", dec->contribuinte.municipio);
153         fprintf(f, "%-2.2s", dec->contribuinte.uf);
154         fprintf(f, "   "); /* TODO: código exterior */
155         fprintf(f, "105"); /* TODO: código país */
156         fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */
157         fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */
158         fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */
159         fprintf(f, "%-4.4s", dec->contribuinte.ddd);
160         fprintf(f, "%-9.9s", dec->contribuinte.telefone);
161         fprintf(f, "%-8.8s", dec->contribuinte.dn);
162         fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor);
163         fprintf(f, "%03d", dec->contribuinte.ocupacao_principal);
164         fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao);
165         fprintf(f, "0"); /* TODO: qnt quotas */
166         fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N");
167         fprintf(f, "%s", dec->retifica ? "S" : "N");
168         fprintf(f, "S"); /* GERADO */
169         fprintf(f, "N"); /* TODO: mudança de endereço */
170         fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */
171         fprintf(f, "%-3.3s", "000"); /* TODO: banco */
172         fprintf(f, "%-4.4s", "0000"); /* TODO: agência */
173         fprintf(f, "N"); /* TODO: deficiência */
174         fprintf(f, "0"); /* FILLER: pré-preenchida */
175         fprintf(f, "%-13.13s", ""); /* TODO: conta */
176         fprintf(f, "%-2.2s", ""); /* TODO: DV conta */
177         fprintf(f, "N"); /* TODO: débito automático */
178         fprintf(f, "0"); /* TODO: débito primeira quota */
179         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ principal fonte */
180         fprintf(f, "%-10.10s", dec->recibo); /* Recibo última declaração ano anterior */
181         fprintf(f, "A"); /* Declaração de Ajuste */
182         fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */
183         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
184         fprintf(f, "\r\n");
185 }
186
187 static void gera_simples(struct declaracao *dec, FILE *f)
188 {
189         fprintf(f, "17");
190         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
191         fprintf(f, "%013lld", 0); /* TODO: carnê leão */
192         fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */
193         fprintf(f, "%013lld", 0); /* TODO: isentos */
194         fprintf(f, "%013lld", 0); /* TODO: rendimentos tributação exclusiva */
195         fprintf(f, "%013lld", 0); /* TODO: 13o. */
196         fprintf(f, "%013lld", 0); /* TODO: retido na fonte */
197         fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */
198         fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */
199         fprintf(f, "%013lld", 0); /* TODO: isentos dependentes */
200         fprintf(f, "%013lld", 0); /* TODO: exclusivos dependentes */
201         fprintf(f, "%-13.13s", ""); /* FILLER */
202         fprintf(f, "%-13.13s", ""); /* FILLER */
203         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF */
204         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependentes */
205         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior */
206         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF ext. depend. */
207         fprintf(f, "%013lld", 0); /* TODO: carnê-leão PF? */
208         fprintf(f, "%013lld", 0); /* TODO: carnê-leão dependentes */
209         fprintf(f, "%013lld", 0); /* TODO: dedução dependentes */
210         fprintf(f, "%013lld", 0); /* TODO: previdência */
211         fprintf(f, "%013lld", 0); /* TODO: previdência dependentes */
212         fprintf(f, "%013lld", 0); /* TODO: pensão */
213         fprintf(f, "%013lld", 0); /* TODO: pensão dependentes*/
214         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */
215         fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */
216         fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */
217         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
218         fprintf(f, "\r\n");
219 }
220
221 static void gera_totais_simples(struct declaracao *dec, FILE *f)
222 {
223         fprintf(f, "18");
224         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
225         fprintf(f, "%013lld", 0); /* TODO: rendimentos tributáveis */
226         fprintf(f, "%013lld", 0); /* TODO: desconto simplificado */
227         fprintf(f, "%013lld", 0); /* TODO: base cálculo */
228         fprintf(f, "%013lld", 0); /* TODO: imposto devido */
229         fprintf(f, "%013lld", 0); /* TODO: imposto retido */
230         fprintf(f, "%013lld", 0); /* TODO: imposto complementar */
231         fprintf(f, "%013lld", 0); /* TODO: carnê-leão */
232         fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */
233         fprintf(f, "%013lld", 0); /* TODO: imposto restituir */
234         fprintf(f, "%013lld", 0); /* TODO: imposto pagar */
235         fprintf(f, "%d", 0); /* TODO: quotas */
236         fprintf(f, "%013lld", 0); /* TODO: valor quota */
237         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos */
238         fprintf(f, "%013lld", 0); /* TODO: rendimentos tributação exclusiva */
239         fprintf(f, "%013lld", 0); /* TODO: conjuge */
240         fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */
241         fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */
242         fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */
243         fprintf(f, "%013lld", 0); /* TODO: rendimento tributável PJ titular */
244         fprintf(f, "%013lld", 0); /* TODO: total rural */
245         fprintf(f, "%013lld", 0); /* TODO: imposto retido titular */
246         fprintf(f, "%013lld", 0); /* TODO: total bens ano anterior */
247         fprintf(f, "%013lld", 0); /* TODO: total bens ano base */
248         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos titular */
249         fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */
250         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva titular */
251         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */
252         fprintf(f, "%013lld", 0); /* TODO: total não tributável */
253         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */
254         fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */
255         fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033*/
256         fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis */
257         fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva */
258         fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */
259         fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */
260         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */
261         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependentes */
262         fprintf(f, "%013lld", 0); /* TODO: doações campanha */
263         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */
264         fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */
265         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */
266         fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */
267         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */
268         fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */
269         fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */
270         fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */
271         fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */
272         fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */
273         fprintf(f, "%013lld", 0); /* TODO: imposto devido */
274         fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */
275         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */
276         fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */
277         fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */
278         fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior*/
279         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
280         fprintf(f, "\r\n");
281 }
282
283 static void gera_completa(struct declaracao *dec, FILE *f)
284 {
285         fprintf(f, "19");
286         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
287         fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */
288         /* FIXME: completar campos */
289         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
290         fprintf(f, "\r\n");
291 }
292
293 static void gera_totais_completa(struct declaracao *dec, FILE *f)
294 {
295         fprintf(f, "20");
296         fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */
297         /* FIXME: completar campos */
298         fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */
299         fprintf(f, "\r\n");
300 }
301
302 static int gera(struct declaracao *dec, char *filename)
303 {
304         FILE *f;
305         f = fopen(filename, "w");
306         if (!f)
307                 return -errno;
308         gera_header(dec, f);
309         gera_contribuinte(dec, f);
310         if (dec->tipo == COMPLETA) {
311                 gera_completa(dec, f);
312                 gera_totais_completa(dec, f);
313         } else {
314                 gera_simples(dec, f);
315                 gera_totais_simples(dec, f);
316         }
317         fclose(f);
318         return 0;
319 }
320
321 static int run_gera(struct declaracao *dec, char **args, int argc)
322 {
323         if (argc != 2)
324                 return -EINVAL;
325         return gera(dec, args[1]);
326 }
327
328 static struct cmd cmd_gera = {
329         .name = "gera",
330         .run = run_gera,
331 };
332
333 int gera_cmd_init(void)
334 {
335         cmd_add(&cmd_gera);
336         return 0;
337 }