Salva demais partes da declaração
[cascardo/declara.git] / lib / base.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
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 "cmd.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include "rendimento.h"
25 #include "pagamento.h"
26 #include "bem.h"
27 #include "isento.h"
28 #include "dependente.h"
29 #include "conjuge.h"
30 #include "sistema.h"
31 #include "totais.h"
32 #include "util.h"
33
34 SET_INT(ano);
35 SET_STRING(cpf);
36 SET_STRING(nome);
37 SET_STRING(recibo);
38 SET_STRING(retifica);
39
40 SET_STRING(banco);
41 SET_STRING(agencia);
42 SET_STRING(contacorrente);
43 SET_STRING(dvconta);
44
45 static int run_resumo(struct declaracao *dec, char **args, int argc)
46 {
47         if (dec->retifica)
48                 printf("retificadora\n");
49         if (dec->obrigatoria)
50                 printf("obrigatoria\n");
51         switch (dec->tipo) {
52         case SIMPLES:
53                 printf("simples\n");
54                 break;
55         case COMPLETA:
56                 printf("completa\n");
57                 break;
58         }
59         printf("pago: %lld.%02d\n", reais(dec->pago), centavos(dec->pago));
60         printf("retido: %lld.%02d\n", reais(dec->retido),
61                                       centavos(dec->retido));
62         printf("devido: %lld.%02d\n", reais(dec->devido),
63                                       centavos(dec->devido));
64         if (dec->restituicao > 0)
65                 printf("restituicao: %lld.%02d\n", reais(dec->restituicao),
66                                                    centavos(dec->restituicao));
67         if (dec->pagar > 0)
68                 printf("a pagar: %lld.%02d\n", reais(dec->pagar),
69                                                centavos(dec->pagar));
70         printf("base de cálculo: %lld.%02d\n",
71                 reais(totais_get(dec, "BASE")),
72                 centavos(totais_get(dec, "BASE")));
73         printf("isentos: %lld.%02d\n",
74                 reais(totais_get(dec, "ISENTOS")),
75                 centavos(totais_get(dec, "ISENTOS")));
76         printf("exclusivos: %lld.%02d\n",
77                 reais(totais_get(dec, "EXCLUSIVOS")),
78                 centavos(totais_get(dec, "EXCLUSIVOS")));
79         printf("hash: %010ld\n", dec->hash);
80         return 0;
81 }
82
83 static void salva(struct declaracao *dec, FILE *f)
84 {
85         fprintf(f, "ano %d\n", dec->ano);
86         if (dec->cpf)
87                 fprintf(f, "cpf \"%s\"\n", dec->cpf);
88         if (dec->nome)
89                 fprintf(f, "nome \"%s\"\n", dec->nome);
90         if (dec->recibo)
91                 fprintf(f, "recibo \"%s\"\n", dec->recibo);
92         if (dec->retifica)
93                 fprintf(f, "retifica \"%s\"\n", dec->retifica);
94         if (dec->banco)
95                 fprintf(f, "banco \"%s\"\n", dec->banco);
96         if (dec->agencia)
97                 fprintf(f, "agencia \"%s\"\n", dec->agencia);
98         if (dec->contacorrente)
99                 fprintf(f, "contacorrente \"%s\"\n", dec->contacorrente);
100         if (dec->dvconta)
101                 fprintf(f, "dvconta \"%s\"\n", dec->dvconta);
102         contribuinte_salva(dec, f);
103         rendimento_salva(dec, f);
104         pagamento_salva(dec, f);
105         bem_salva(dec, f);
106         isento_salva(dec, f);
107         dependente_salva(dec, f);
108         conjuge_salva(dec, f);
109         sistema_salva(dec, f);
110 }
111
112 static int run_salva(struct declaracao *dec, char **args, int argc)
113 {
114         FILE *f;
115         char *filename;
116         if (argc != 2)
117                 return -EINVAL;
118         filename = args[1];
119         f = fopen(filename, "w");
120         if (!f)
121                 return -errno;
122         salva(dec, f);
123         fclose(f);
124         return 0;
125 }
126
127 static int run_simples(struct declaracao *dec, char **args, int argc)
128 {
129         if (argc != 1)
130                 return -EINVAL;
131         dec->tipo = FORCA_SIMPLES;
132         return 0;
133 }
134
135 static int run_completa(struct declaracao *dec, char **args, int argc)
136 {
137         if (argc != 1)
138                 return -EINVAL;
139         dec->tipo = FORCA_COMPLETA;
140         return 0;
141 }
142
143 static struct cmd cmd_salva = {
144         .name = "salva",
145         .run = run_salva,
146 };
147
148 static struct cmd cmd_resumo = {
149         .name = "resumo",
150         .run = run_resumo,
151 };
152
153 static struct cmd cmd_simples = {
154         .name = "simples",
155         .run = run_simples,
156 };
157
158 static struct cmd cmd_completa = {
159         .name = "completa",
160         .run = run_completa,
161 };
162
163 int base_cmd_init(void)
164 {
165         cmd_add(&cmd_salva);
166         cmd_add(&cmd_resumo);
167
168         cmd_add(&cmd_ano);
169         cmd_add(&cmd_cpf);
170         cmd_add(&cmd_recibo);
171         cmd_add(&cmd_retifica);
172         cmd_add(&cmd_nome);
173
174         cmd_add(&cmd_banco);
175         cmd_add(&cmd_agencia);
176         cmd_add(&cmd_contacorrente);
177         cmd_add(&cmd_dvconta);
178
179         cmd_add(&cmd_simples);
180         cmd_add(&cmd_completa);
181
182         return 0;
183 }