Imprime no resumo se declaração é simples ou completa
[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 "util.h"
28
29 SET_INT(ano);
30 SET_STRING(cpf);
31 SET_STRING(nome);
32 SET_STRING(recibo);
33 SET_STRING(retifica);
34
35 SET_STRING(banco);
36 SET_STRING(agencia);
37 SET_STRING(contacorrente);
38 SET_STRING(dvconta);
39
40 static int run_resumo(struct declaracao *dec, char **args, int argc)
41 {
42         if (dec->retifica)
43                 printf("retificadora\n");
44         if (dec->obrigatoria)
45                 printf("obrigatoria\n");
46         switch (dec->tipo) {
47         case SIMPLES:
48                 printf("simples\n");
49                 break;
50         case COMPLETA:
51                 printf("completa\n");
52                 break;
53         }
54         printf("pago: %lld.%02d\n", reais(dec->pago), centavos(dec->pago));
55         printf("retido: %lld.%02d\n", reais(dec->retido),
56                                       centavos(dec->retido));
57         printf("devido: %lld.%02d\n", reais(dec->devido),
58                                       centavos(dec->devido));
59         if (dec->restituicao > 0)
60                 printf("restituicao: %lld.%02d\n", reais(dec->restituicao),
61                                                    centavos(dec->restituicao));
62         if (dec->pagar > 0)
63                 printf("a pagar: %lld.%02d\n", reais(dec->pagar),
64                                                centavos(dec->pagar));
65         return 0;
66 }
67
68 static void salva(struct declaracao *dec, FILE *f)
69 {
70         fprintf(f, "ano %d\n", dec->ano);
71         if (dec->cpf)
72                 fprintf(f, "cpf \"%s\"\n", dec->cpf);
73         if (dec->nome)
74                 fprintf(f, "nome \"%s\"\n", dec->nome);
75         if (dec->recibo)
76                 fprintf(f, "recibo \"%s\"\n", dec->recibo);
77         if (dec->retifica)
78                 fprintf(f, "retifica \"%s\"\n", dec->retifica);
79         if (dec->banco)
80                 fprintf(f, "banco \"%s\"\n", dec->banco);
81         if (dec->agencia)
82                 fprintf(f, "agencia \"%s\"\n", dec->agencia);
83         if (dec->contacorrente)
84                 fprintf(f, "contacorrente \"%s\"\n", dec->contacorrente);
85         if (dec->dvconta)
86                 fprintf(f, "dvconta \"%s\"\n", dec->dvconta);
87         contribuinte_salva(dec, f);
88         rendimento_salva(dec, f);
89         pagamento_salva(dec, f);
90         bem_salva(dec, f);
91 }
92
93 static int run_salva(struct declaracao *dec, char **args, int argc)
94 {
95         FILE *f;
96         char *filename;
97         if (argc != 2)
98                 return -EINVAL;
99         filename = args[1];
100         f = fopen(filename, "w");
101         if (!f)
102                 return -errno;
103         salva(dec, f);
104         fclose(f);
105         return 0;
106 }
107
108 static struct cmd cmd_salva = {
109         .name = "salva",
110         .run = run_salva,
111 };
112
113 static struct cmd cmd_resumo = {
114         .name = "resumo",
115         .run = run_resumo,
116 };
117
118 int base_cmd_init(void)
119 {
120         cmd_add(&cmd_salva);
121         cmd_add(&cmd_resumo);
122
123         cmd_add(&cmd_ano);
124         cmd_add(&cmd_cpf);
125         cmd_add(&cmd_recibo);
126         cmd_add(&cmd_retifica);
127         cmd_add(&cmd_nome);
128
129         cmd_add(&cmd_banco);
130         cmd_add(&cmd_agencia);
131         cmd_add(&cmd_contacorrente);
132         cmd_add(&cmd_dvconta);
133
134         return 0;
135 }