Soma total de decuções para declaração completa.
[cascardo/declara.git] / lib / calcula.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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 "calcula.h"
20 #include <errno.h>
21 #include <stdio.h>
22 #include "declaracao.h"
23 #include "cmd.h"
24 #include "rendimento.h"
25 #include "totais.h"
26
27 static const long long dependente2015 = 215652;
28
29 long long deducao_dependente(struct declaracao *dec)
30 {
31         if (dec->ano == 2015)
32                 return dependente2015;
33         return 0;
34 }
35
36 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
37  * precisa ser ajustado para tais limites. Esta função considerará tais
38  * limites no futuro. */
39 static long long total_deducao(struct declaracao *dec)
40 {
41         int i;
42         return totais_get(dec, "DEPENDENTES") +
43                totais_get(dec, "INSS") +
44                totais_get(dec, "PAGAMENTOS") -
45                totais_get(dec, "REEMBOLSOS");
46 }
47
48 static void total_pago(struct declaracao *dec)
49 {
50         struct rendimento *rendimento;
51         int i;
52         dec->pago = dec->retido = 0;
53         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
54                 dec->pago += rendimento->imposto;
55                 dec->retido += rendimento->imposto;
56         }
57 }
58
59 struct taxtable {
60         long long base;
61         long long aliquota;
62         long long deducao;
63 };
64
65 static struct taxtable table2015[] = {
66         {       0,    0,      0, },
67         { 2145324,  750, 160899, },
68         { 3215148, 1500, 402035, },
69         { 4286917, 2250, 723554, },
70         { 5356572, 2750, 991383, },
71         { 9999999999999LL, 0, 0, },
72 };
73
74 static const long long simples2015 = 1588089;
75
76 static const long long obrigatoriedade2015 = 2681655;
77
78 static long long imposto(struct taxtable *tt, long long tr)
79 {
80         int i;
81         for (i = 0; tr >= tt[i].base; i++);
82         i--;
83         return tr * tt[i].aliquota / 10000 - tt[i].deducao;
84 }
85
86 static long long imposto_simples(struct declaracao *dec)
87 {
88         struct taxtable *tt;
89         long long tr, td;
90         tt = table2015;
91         tr = totais_get(dec, "RENDPJ");
92         if (tr / 5 < simples2015)
93                 td = tr / 5;
94         else
95                 td = simples2015;
96         totais_add(dec, "DESCONTO", td);
97         tr -= td;
98         totais_add(dec, "BASESIMPLES", tr);
99         return imposto(tt, tr);
100 }
101
102 static long long imposto_completa(struct declaracao *dec)
103 {
104         struct taxtable *tt;
105         long long tr, td;
106         if (dec->ano != 2015) {
107                 return -EINVAL;
108         }
109         tt = table2015;
110         tr = totais_get(dec, "RENDPJ");
111         td = total_deducao(dec);
112         totais_add(dec, "DEDUCOES", td);
113         tr -= td;
114         totais_add(dec, "BASECOMPLETA", tr);
115         return imposto(tt, tr);
116 }
117
118 int calcula(struct declaracao *dec)
119 {
120         long long i_simples, i_completa;
121         if (dec->ano != 2015) {
122                 return -EINVAL;
123         }
124         if (totais_get(dec, "RENDPJ") > obrigatoriedade2015)
125                 dec->obrigatoria = 1;
126         i_simples = imposto_simples(dec);
127         i_completa = imposto_completa(dec);
128         total_pago(dec);
129         if (dec->tipo != FORCA_SIMPLES &&
130             (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
131                 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
132                 dec->tipo = COMPLETA;
133                 dec->devido = i_completa;
134         } else {
135                 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
136                 dec->tipo = SIMPLES;
137                 dec->devido = i_simples;
138         }
139         if (dec->pago > dec->devido)
140                 dec->restituicao = dec->pago - dec->devido;
141         else
142                 dec->pagar = dec->devido - dec->pago;
143         return 0;
144 }
145
146 static int run_calcula(struct declaracao *dec, char **args, int argc)
147 {
148         totais_add(dec, "EXCLUSIVOS_SEM_13o",
149                 totais_get(dec, "EXCLUSIVOS") -
150                 totais_get(dec, "DECIMOTERCEIRO"));
151         return calcula(dec);
152 }
153
154 static struct cmd cmd_calcula = {
155         .name = "calcula",
156         .run = run_calcula,
157 };
158
159 int calcula_cmd_init(void)
160 {
161         cmd_add(&cmd_calcula);
162         return 0;
163 }