Nova versão do IRPF 2021.
[cascardo/declara.git] / lib / rendimento.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 "rendimento.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "cmd.h"
26 #include "list.h"
27 #include "util.h"
28 #include "totais.h"
29
30 void rendimento_free(void *pointer)
31 {
32         struct rendimento *rendimento = pointer;
33         if (rendimento->cnpj)
34                 free(rendimento->cnpj);
35         if (rendimento->nome)
36                 free(rendimento->nome);
37         if (rendimento->saida)
38                 free(rendimento->saida);
39         free(rendimento);
40 }
41
42 static int rendimento_cmp(void *p1, void *p2)
43 {
44         struct rendimento *r1 = p1;
45         struct rendimento *r2 = p2;
46         /* O rendimento maior vem primeiro. */
47         if (r1->rendimento > r2->rendimento)
48                 return -1;
49         else if (r1->rendimento < r2->rendimento)
50                 return 1;
51         return 0;
52 }
53
54 static struct rendimento * rendimento_new(char **args)
55 {
56         struct rendimento *rendimento;
57         int r = 0;
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);
69                 return NULL;
70         }
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);
75                 return NULL;
76         }
77         return rendimento;
78 }
79
80 static int run_rendimento(struct declaracao *dec, char **args, int argc)
81 {
82         struct rendimento *rendimento;
83         int r;
84         if (argc != 9)
85                 return -EINVAL;
86         rendimento = rendimento_new(args);
87         if (!rendimento)
88                 return -ENOMEM;
89         r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp);
90         if (r < 0) {
91                 rendimento_free(rendimento);
92                 return r;
93         }
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);
101         if (r) {
102                 rendimento_free(rendimento);
103                 return r;
104         }
105         return 0;
106 }
107
108 void rendimento_salva(struct declaracao *dec, FILE *f)
109 {
110         int i;
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);
116 }
117
118 static struct cmd cmd_rendimento = {
119         .name = "rendimento",
120         .run = run_rendimento,
121 };
122
123 int rendimento_cmd_init(void)
124 {
125         cmd_add(&cmd_rendimento);
126         return 0;
127 }
128
129 char * rendimento_cnpj_ordenado(struct declaracao *dec, int i)
130 {
131         struct rendimento *rendimento;
132         rendimento = list_get(dec->rendimento, i);
133         if (!rendimento)
134                 return "";
135         return rendimento->cnpj;
136 }