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