Escapa o carectere %.
[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 "carne.h"
26 #include "pagamento.h"
27 #include "bem.h"
28 #include "isento.h"
29 #include "dependente.h"
30 #include "conjuge.h"
31 #include "sistema.h"
32 #include "totais.h"
33 #include "util.h"
34
35 SET_INT(ano);
36 SET_STRING(cpf);
37 SET_STRING(nome);
38 SET_STRING(recibo);
39 SET_STRING(retifica);
40
41 SET_STRING(banco);
42 SET_STRING(agencia);
43 SET_STRING(contacorrente);
44 SET_STRING(dvconta);
45
46 static int run_resumo(struct declaracao *dec, char **args, int argc)
47 {
48         if (dec->retifica)
49                 printf("retificadora\n");
50         if (dec->obrigatoria)
51                 printf("obrigatoria\n");
52         switch (dec->tipo) {
53         case SIMPLES:
54                 printf("simples\n");
55                 break;
56         case COMPLETA:
57                 printf("completa\n");
58                 break;
59         }
60         printf("pago: "FMT_R"\n", R(dec->pago));
61         printf("retido: "FMT_R"\n", R(dec->retido));
62         printf("devido: "FMT_R"\n", R(dec->devido));
63         if (dec->restituicao > 0)
64                 printf("restituicao: "FMT_R"\n", R(dec->restituicao));
65         if (dec->pagar > 0)
66                 printf("a pagar: "FMT_R"\n", R(dec->pagar));
67         printf("base de cálculo: "FMT_R"\n",
68                 R(totais_get(dec, "BASE")));
69         printf("isentos: "FMT_R"\n",
70                 R(totais_get(dec, "ISENTOS")));
71         printf("exclusivos: "FMT_R"\n",
72                 R(totais_get(dec, "EXCLUSIVOS")));
73         printf("bens: "FMT_R"\n",
74                 R(totais_get(dec, "BENS")));
75         printf("bens: "FMT_R"\n",
76                 R(totais_get(dec, "BENSANTERIOR")));
77         printf("aliquota efetiva: "FMT_R"%%\n",
78                 R(dec->aliquota_efetiva));
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         carne_salva(dec, f);
113         pagamento_salva(dec, f);
114         bem_salva(dec, f);
115         isento_salva(dec, f);
116         dependente_salva(dec, f);
117         conjuge_salva(dec, f);
118         sistema_salva(dec, f);
119 }
120
121 static int run_salva(struct declaracao *dec, char **args, int argc)
122 {
123         FILE *f;
124         char *filename;
125         if (argc != 2)
126                 return -EINVAL;
127         filename = args[1];
128         f = fopen(filename, "w");
129         if (!f)
130                 return -errno;
131         salva(dec, f);
132         fclose(f);
133         return 0;
134 }
135
136 static int run_simples(struct declaracao *dec, char **args, int argc)
137 {
138         if (argc != 1)
139                 return -EINVAL;
140         dec->tipo = FORCA_SIMPLES;
141         return 0;
142 }
143
144 static int run_completa(struct declaracao *dec, char **args, int argc)
145 {
146         if (argc != 1)
147                 return -EINVAL;
148         dec->tipo = FORCA_COMPLETA;
149         return 0;
150 }
151
152 static struct cmd cmd_salva = {
153         .name = "salva",
154         .run = run_salva,
155 };
156
157 static struct cmd cmd_resumo = {
158         .name = "resumo",
159         .run = run_resumo,
160 };
161
162 static struct cmd cmd_verbose = {
163         .name = "verbose",
164         .run = run_verbose,
165 };
166
167 static struct cmd cmd_simples = {
168         .name = "simples",
169         .run = run_simples,
170 };
171
172 static struct cmd cmd_completa = {
173         .name = "completa",
174         .run = run_completa,
175 };
176
177 int base_cmd_init(void)
178 {
179         cmd_add(&cmd_salva);
180         cmd_add(&cmd_resumo);
181
182         cmd_add(&cmd_ano);
183         cmd_add(&cmd_cpf);
184         cmd_add(&cmd_recibo);
185         cmd_add(&cmd_retifica);
186         cmd_add(&cmd_nome);
187
188         cmd_add(&cmd_banco);
189         cmd_add(&cmd_agencia);
190         cmd_add(&cmd_contacorrente);
191         cmd_add(&cmd_dvconta);
192
193         cmd_add(&cmd_verbose);
194         cmd_add(&cmd_simples);
195         cmd_add(&cmd_completa);
196
197         return 0;
198 }