Implementa opção verbose.
[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 int run_verbose(struct declaracao *dec, char **args, int argc)
84 {
85         if (argc != 1)
86                 return -EINVAL;
87         dec->verbose = 1;
88         return 0;
89 }
90
91 static void salva(struct declaracao *dec, FILE *f)
92 {
93         fprintf(f, "ano %d\n", dec->ano);
94         if (dec->cpf)
95                 fprintf(f, "cpf \"%s\"\n", dec->cpf);
96         if (dec->nome)
97                 fprintf(f, "nome \"%s\"\n", dec->nome);
98         if (dec->recibo)
99                 fprintf(f, "recibo \"%s\"\n", dec->recibo);
100         if (dec->retifica)
101                 fprintf(f, "retifica \"%s\"\n", dec->retifica);
102         if (dec->banco)
103                 fprintf(f, "banco \"%s\"\n", dec->banco);
104         if (dec->agencia)
105                 fprintf(f, "agencia \"%s\"\n", dec->agencia);
106         if (dec->contacorrente)
107                 fprintf(f, "contacorrente \"%s\"\n", dec->contacorrente);
108         if (dec->dvconta)
109                 fprintf(f, "dvconta \"%s\"\n", dec->dvconta);
110         contribuinte_salva(dec, f);
111         rendimento_salva(dec, f);
112         pagamento_salva(dec, f);
113         bem_salva(dec, f);
114         isento_salva(dec, f);
115         dependente_salva(dec, f);
116         conjuge_salva(dec, f);
117         sistema_salva(dec, f);
118 }
119
120 static int run_salva(struct declaracao *dec, char **args, int argc)
121 {
122         FILE *f;
123         char *filename;
124         if (argc != 2)
125                 return -EINVAL;
126         filename = args[1];
127         f = fopen(filename, "w");
128         if (!f)
129                 return -errno;
130         salva(dec, f);
131         fclose(f);
132         return 0;
133 }
134
135 static int run_simples(struct declaracao *dec, char **args, int argc)
136 {
137         if (argc != 1)
138                 return -EINVAL;
139         dec->tipo = FORCA_SIMPLES;
140         return 0;
141 }
142
143 static int run_completa(struct declaracao *dec, char **args, int argc)
144 {
145         if (argc != 1)
146                 return -EINVAL;
147         dec->tipo = FORCA_COMPLETA;
148         return 0;
149 }
150
151 static struct cmd cmd_salva = {
152         .name = "salva",
153         .run = run_salva,
154 };
155
156 static struct cmd cmd_resumo = {
157         .name = "resumo",
158         .run = run_resumo,
159 };
160
161 static struct cmd cmd_verbose = {
162         .name = "verbose",
163         .run = run_verbose,
164 };
165
166 static struct cmd cmd_simples = {
167         .name = "simples",
168         .run = run_simples,
169 };
170
171 static struct cmd cmd_completa = {
172         .name = "completa",
173         .run = run_completa,
174 };
175
176 int base_cmd_init(void)
177 {
178         cmd_add(&cmd_salva);
179         cmd_add(&cmd_resumo);
180
181         cmd_add(&cmd_ano);
182         cmd_add(&cmd_cpf);
183         cmd_add(&cmd_recibo);
184         cmd_add(&cmd_retifica);
185         cmd_add(&cmd_nome);
186
187         cmd_add(&cmd_banco);
188         cmd_add(&cmd_agencia);
189         cmd_add(&cmd_contacorrente);
190         cmd_add(&cmd_dvconta);
191
192         cmd_add(&cmd_verbose);
193         cmd_add(&cmd_simples);
194         cmd_add(&cmd_completa);
195
196         return 0;
197 }