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