Utiliza macros para formatar Real no resumo.
[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: "FMT_R"\n", R(dec->pago));
60         printf("retido: "FMT_R"\n", R(dec->retido));
61         printf("devido: "FMT_R"\n", R(dec->devido));
62         if (dec->restituicao > 0)
63                 printf("restituicao: "FMT_R"\n", R(dec->restituicao));
64         if (dec->pagar > 0)
65                 printf("a pagar: "FMT_R"\n", R(dec->pagar));
66         printf("base de cálculo: "FMT_R"\n",
67                 R(totais_get(dec, "BASE")));
68         printf("isentos: "FMT_R"\n",
69                 R(totais_get(dec, "ISENTOS")));
70         printf("exclusivos: "FMT_R"\n",
71                 R(totais_get(dec, "EXCLUSIVOS")));
72         printf("bens: "FMT_R"\n",
73                 R(totais_get(dec, "BENS")));
74         printf("bens: "FMT_R"\n",
75                 R(totais_get(dec, "BENSANTERIOR")));
76         printf("hash: %010ld\n", dec->hash);
77         return 0;
78 }
79
80 static int run_verbose(struct declaracao *dec, char **args, int argc)
81 {
82         if (argc != 1)
83                 return -EINVAL;
84         dec->verbose = 1;
85         return 0;
86 }
87
88 static void salva(struct declaracao *dec, FILE *f)
89 {
90         fprintf(f, "ano %d\n", dec->ano);
91         if (dec->cpf)
92                 fprintf(f, "cpf \"%s\"\n", dec->cpf);
93         if (dec->nome)
94                 fprintf(f, "nome \"%s\"\n", dec->nome);
95         if (dec->recibo)
96                 fprintf(f, "recibo \"%s\"\n", dec->recibo);
97         if (dec->retifica)
98                 fprintf(f, "retifica \"%s\"\n", dec->retifica);
99         if (dec->banco)
100                 fprintf(f, "banco \"%s\"\n", dec->banco);
101         if (dec->agencia)
102                 fprintf(f, "agencia \"%s\"\n", dec->agencia);
103         if (dec->contacorrente)
104                 fprintf(f, "contacorrente \"%s\"\n", dec->contacorrente);
105         if (dec->dvconta)
106                 fprintf(f, "dvconta \"%s\"\n", dec->dvconta);
107         contribuinte_salva(dec, f);
108         rendimento_salva(dec, f);
109         pagamento_salva(dec, f);
110         bem_salva(dec, f);
111         isento_salva(dec, f);
112         dependente_salva(dec, f);
113         conjuge_salva(dec, f);
114         sistema_salva(dec, f);
115 }
116
117 static int run_salva(struct declaracao *dec, char **args, int argc)
118 {
119         FILE *f;
120         char *filename;
121         if (argc != 2)
122                 return -EINVAL;
123         filename = args[1];
124         f = fopen(filename, "w");
125         if (!f)
126                 return -errno;
127         salva(dec, f);
128         fclose(f);
129         return 0;
130 }
131
132 static int run_simples(struct declaracao *dec, char **args, int argc)
133 {
134         if (argc != 1)
135                 return -EINVAL;
136         dec->tipo = FORCA_SIMPLES;
137         return 0;
138 }
139
140 static int run_completa(struct declaracao *dec, char **args, int argc)
141 {
142         if (argc != 1)
143                 return -EINVAL;
144         dec->tipo = FORCA_COMPLETA;
145         return 0;
146 }
147
148 static struct cmd cmd_salva = {
149         .name = "salva",
150         .run = run_salva,
151 };
152
153 static struct cmd cmd_resumo = {
154         .name = "resumo",
155         .run = run_resumo,
156 };
157
158 static struct cmd cmd_verbose = {
159         .name = "verbose",
160         .run = run_verbose,
161 };
162
163 static struct cmd cmd_simples = {
164         .name = "simples",
165         .run = run_simples,
166 };
167
168 static struct cmd cmd_completa = {
169         .name = "completa",
170         .run = run_completa,
171 };
172
173 int base_cmd_init(void)
174 {
175         cmd_add(&cmd_salva);
176         cmd_add(&cmd_resumo);
177
178         cmd_add(&cmd_ano);
179         cmd_add(&cmd_cpf);
180         cmd_add(&cmd_recibo);
181         cmd_add(&cmd_retifica);
182         cmd_add(&cmd_nome);
183
184         cmd_add(&cmd_banco);
185         cmd_add(&cmd_agencia);
186         cmd_add(&cmd_contacorrente);
187         cmd_add(&cmd_dvconta);
188
189         cmd_add(&cmd_verbose);
190         cmd_add(&cmd_simples);
191         cmd_add(&cmd_completa);
192
193         return 0;
194 }