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