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