4f5c5fdb25263b8d942669387bc153dd537b8ea6
[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 struct cmd cmd_salva = {
120         .name = "salva",
121         .run = run_salva,
122 };
123
124 static struct cmd cmd_resumo = {
125         .name = "resumo",
126         .run = run_resumo,
127 };
128
129 int base_cmd_init(void)
130 {
131         cmd_add(&cmd_salva);
132         cmd_add(&cmd_resumo);
133
134         cmd_add(&cmd_ano);
135         cmd_add(&cmd_cpf);
136         cmd_add(&cmd_recibo);
137         cmd_add(&cmd_retifica);
138         cmd_add(&cmd_nome);
139
140         cmd_add(&cmd_banco);
141         cmd_add(&cmd_agencia);
142         cmd_add(&cmd_contacorrente);
143         cmd_add(&cmd_dvconta);
144
145         return 0;
146 }