Move macros de comandos para definir inteiros e strings.
[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 "declaracao.h"
21 #include "cmd.h"
22 #include "rendimento.h"
23 #include <errno.h>
24 #include <stdio.h>
25
26 static long long total_rendimento(struct declaracao *dec)
27 {
28         long long tr = 0;
29         struct rendimento *rendimento;
30         int i;
31         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
32                 tr += rendimento->rendimento;
33         }
34         return tr;
35 }
36
37 static long long total_deducao(struct declaracao *dec)
38 {
39         long long td = 0;
40         struct rendimento *rendimento;
41         int i;
42         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
43                 td += rendimento->previdencia;
44         }
45         return td;
46 }
47
48 static long long total_pago(struct declaracao *dec)
49 {
50         long long tt = 0;
51         struct rendimento *rendimento;
52         int i;
53         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
54                 tt += rendimento->imposto;
55         }
56         return tt;
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 long long imposto(struct taxtable *tt, long long tr)
77 {
78         int i;
79         for (i = 0; tr >= tt[i].base; i++);
80         i--;
81         return tr * tt[i].aliquota / 10000 - tt[i].deducao;
82 }
83
84 static long long imposto_simples(struct declaracao *dec)
85 {
86         struct taxtable *tt;
87         long long tr, td;
88         if (dec->ano != 2015) {
89                 return -EINVAL;
90         }
91         tt = table2015;
92         tr = total_rendimento(dec);
93         dec->totalrendimento = tr;
94         if (tr / 5 < simples2015)
95                 td = tr / 5;
96         else
97                 td = simples2015;
98         tr -= td;
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 = total_rendimento(dec);
111         td = total_deducao(dec);
112         dec->totalrendimento = tr;
113         tr -= td;
114         return imposto(tt, tr);
115 }
116
117 void calcula(struct declaracao *dec)
118 {
119         long long i_simples, i_completa;
120         i_simples = imposto_simples(dec);
121         i_completa = imposto_completa(dec);
122         dec->pago = total_pago(dec);
123         if (i_simples > i_completa) {
124                 dec->tipo = COMPLETA;
125                 dec->devido = i_completa;
126         } else {
127                 dec->tipo = SIMPLES;
128                 dec->devido = i_simples;
129         }
130         dec->restituicao = dec->pago - dec->devido;
131 }
132
133 static int run_calcula(struct declaracao *dec, char **args, int argc)
134 {
135         calcula(dec);
136         return 0;
137 }
138
139 static struct cmd cmd_calcula = {
140         .name = "calcula",
141         .run = run_calcula,
142 };
143
144 int calcula_cmd_init(void)
145 {
146         cmd_add(&cmd_calcula);
147         return 0;
148 }