2 * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
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.
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.
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.
19 #include "rendimento.h"
29 void rendimento_free(void *pointer)
31 struct rendimento *rendimento = pointer;
33 free(rendimento->cnpj);
35 free(rendimento->nome);
36 if (rendimento->saida)
37 free(rendimento->saida);
41 static int rendimento_cmp(void *p1, void *p2)
43 struct rendimento *r1 = p1;
44 struct rendimento *r2 = p2;
45 /* O rendimento maior vem primeiro. */
46 if (r1->rendimento > r2->rendimento)
48 else if (r1->rendimento < r2->rendimento)
53 static struct rendimento * rendimento_new(char **args)
55 struct rendimento *rendimento;
57 rendimento = malloc(sizeof(*rendimento));
58 rendimento->cnpj = strdup(args[1]);
59 rendimento->nome = strdup(args[2]);
60 rendimento->saida = strdup(args[8]);
61 r += set_llong(args[3], &rendimento->rendimento);
62 r += set_llong(args[4], &rendimento->previdencia);
63 r += set_llong(args[5], &rendimento->decimoterceiro);
64 r += set_llong(args[6], &rendimento->imposto);
65 r += set_llong(args[7], &rendimento->imposto_13o);
66 if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) {
67 rendimento_free(rendimento);
70 if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 ||
71 rendimento->decimoterceiro < 0 || rendimento->imposto < 0 ||
72 rendimento->imposto_13o < 0) {
73 rendimento_free(rendimento);
79 static int run_rendimento(struct declaracao *dec, char **args, int argc)
81 struct rendimento *rendimento;
85 rendimento = rendimento_new(args);
88 r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp);
90 rendimento_free(rendimento);
96 void rendimento_salva(struct declaracao *dec, FILE *f)
100 for (i = 0; j = list_get(dec->rendimento, i); i++)
101 fprintf(f, "rendimento \"%s\" \"%s\" %lld %lld %lld %lld %lld \"%s\"\n",
102 j->cnpj, j->nome, j->rendimento, j->previdencia,
103 j->decimoterceiro, j->imposto, j->imposto_13o, j->saida);
106 static struct cmd cmd_rendimento = {
107 .name = "rendimento",
108 .run = run_rendimento,
111 int rendimento_cmd_init(void)
113 cmd_add(&cmd_rendimento);
117 char * rendimento_cnpj_ordenado(struct declaracao *dec, int i)
119 struct rendimento *rendimento;
120 rendimento = list_get(dec->rendimento, i);
123 return rendimento->cnpj;