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"
30 void rendimento_free(void *pointer)
32 struct rendimento *rendimento = pointer;
34 free(rendimento->cnpj);
36 free(rendimento->nome);
37 if (rendimento->saida)
38 free(rendimento->saida);
42 static int rendimento_cmp(void *p1, void *p2)
44 struct rendimento *r1 = p1;
45 struct rendimento *r2 = p2;
46 /* O rendimento maior vem primeiro. */
47 if (r1->rendimento > r2->rendimento)
49 else if (r1->rendimento < r2->rendimento)
54 static struct rendimento * rendimento_new(char **args)
56 struct rendimento *rendimento;
58 rendimento = malloc(sizeof(*rendimento));
59 rendimento->cnpj = strdup(args[1]);
60 rendimento->nome = strdup(args[2]);
61 rendimento->saida = strdup(args[8]);
62 r += set_llong(args[3], &rendimento->rendimento);
63 r += set_llong(args[4], &rendimento->previdencia);
64 r += set_llong(args[5], &rendimento->decimoterceiro);
65 r += set_llong(args[6], &rendimento->imposto);
66 r += set_llong(args[7], &rendimento->imposto_13o);
67 if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) {
68 rendimento_free(rendimento);
71 if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 ||
72 rendimento->decimoterceiro < 0 || rendimento->imposto < 0 ||
73 rendimento->imposto_13o < 0) {
74 rendimento_free(rendimento);
80 static int run_rendimento(struct declaracao *dec, char **args, int argc)
82 struct rendimento *rendimento;
86 rendimento = rendimento_new(args);
89 r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp);
91 rendimento_free(rendimento);
94 r = totais_add(dec, "RENDTRIB", rendimento->rendimento);
95 r += totais_add(dec, "RENDPJ", rendimento->rendimento);
96 r += totais_add(dec, "RENDPJTIT", rendimento->rendimento);
97 r += totais_add(dec, "INSS", rendimento->previdencia);
98 r += totais_add(dec, "DECIMOTERCEIRO", rendimento->decimoterceiro);
99 r += totais_add(dec, "EXCLUSIVOS", rendimento->decimoterceiro);
100 r += totais_add(dec, "EXCLUSIVOSTIT", rendimento->decimoterceiro);
102 rendimento_free(rendimento);
108 void rendimento_salva(struct declaracao *dec, FILE *f)
111 struct rendimento *j;
112 for (i = 0; j = list_get(dec->rendimento, i); i++)
113 fprintf(f, "rendimento \"%s\" \"%s\" %lld %lld %lld %lld %lld \"%s\"\n",
114 j->cnpj, j->nome, j->rendimento, j->previdencia,
115 j->decimoterceiro, j->imposto, j->imposto_13o, j->saida);
118 static struct cmd cmd_rendimento = {
119 .name = "rendimento",
120 .run = run_rendimento,
123 int rendimento_cmd_init(void)
125 cmd_add(&cmd_rendimento);
129 char * rendimento_cnpj_ordenado(struct declaracao *dec, int i)
131 struct rendimento *rendimento;
132 rendimento = list_get(dec->rendimento, i);
135 return rendimento->cnpj;