Implementa opção verbose.
[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 #include "util.h"
27
28 static const long long dependente2015 = 215652;
29
30 long long deducao_dependente(struct declaracao *dec)
31 {
32         if (dec->ano == 2015)
33                 return dependente2015;
34         return 0;
35 }
36
37 /* Alguns totais precisam ser limitados. Portanto, um total de decuções
38  * precisa ser ajustado para tais limites. Esta função considerará tais
39  * limites no futuro. */
40 static long long total_deducao(struct declaracao *dec)
41 {
42         int i;
43         if (dec->verbose) {
44                 printf("Dedução:\n");
45                 printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES")));
46                 printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS")));
47                 printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS")));
48                 printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS")));
49         }
50         return totais_get(dec, "DEPENDENTES") +
51                totais_get(dec, "INSS") +
52                totais_get(dec, "PAGAMENTOS") -
53                totais_get(dec, "REEMBOLSOS");
54 }
55
56 static void total_pago(struct declaracao *dec)
57 {
58         struct rendimento *rendimento;
59         int i;
60         dec->pago = dec->retido = 0;
61         for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
62                 dec->pago += rendimento->imposto;
63                 dec->retido += rendimento->imposto;
64         }
65         if (dec->verbose) {
66                 printf("Total pago e retido: "FMT_R" "FMT_R"\n",
67                         R(dec->pago), R(dec->retido));
68         }
69 }
70
71 struct taxtable {
72         long long base;
73         long long aliquota;
74         long long deducao;
75 };
76
77 static struct taxtable table2015[] = {
78         {       0,    0,      0, },
79         { 2145324,  750, 160899, },
80         { 3215148, 1500, 402035, },
81         { 4286917, 2250, 723554, },
82         { 5356572, 2750, 991383, },
83         { 9999999999999LL, 0, 0, },
84 };
85
86 static const long long simples2015 = 1588089;
87
88 static const long long obrigatoriedade2015 = 2681655;
89
90 static long long imposto(struct taxtable *tt, long long tr, int verbose)
91 {
92         int i;
93         for (i = 0; tr >= tt[i].base; i++);
94         i--;
95         if (verbose) {
96                 printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n",
97                         tt[i].aliquota / 10, R(tt[i].deducao));
98         }
99         return tr * tt[i].aliquota / 10000 - tt[i].deducao;
100 }
101
102 static long long imposto_simples(struct declaracao *dec)
103 {
104         struct taxtable *tt;
105         long long tr, td;
106         tt = table2015;
107         tr = totais_get(dec, "RENDPJ");
108         if (tr / 5 < simples2015)
109                 td = tr / 5;
110         else
111                 td = simples2015;
112         totais_add(dec, "DESCONTO", td);
113         tr -= td;
114         totais_add(dec, "BASESIMPLES", tr);
115         if (dec->verbose) {
116                 printf("Desconto simplificado é "FMT_R"\n", R(td));
117         }
118         return imposto(tt, tr, dec->verbose);
119 }
120
121 static long long imposto_completa(struct declaracao *dec)
122 {
123         struct taxtable *tt;
124         long long tr, td;
125         if (dec->ano != 2015) {
126                 return -EINVAL;
127         }
128         tt = table2015;
129         tr = totais_get(dec, "RENDPJ");
130         td = total_deducao(dec);
131         totais_add(dec, "DEDUCOES", td);
132         tr -= td;
133         totais_add(dec, "BASECOMPLETA", tr);
134         if (dec->verbose) {
135                 printf("Desconto completa é "FMT_R"\n", R(td));
136         }
137         return imposto(tt, tr, dec->verbose);
138 }
139
140 int calcula(struct declaracao *dec)
141 {
142         long long i_simples, i_completa;
143         if (dec->ano != 2015) {
144                 return -EINVAL;
145         }
146         if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) {
147                 if (dec->verbose) {
148                         printf("Declaracao obrigatoria pois rendimento e"
149                                 "maior que mínimo para declaracao: "
150                                 FMT_R" > "FMT_R"\n",
151                                 R(totais_get(dec, "RENDPJ")),
152                                 R(obrigatoriedade2015));
153                 }
154                 dec->obrigatoria = 1;
155         }
156         i_simples = imposto_simples(dec);
157         i_completa = imposto_completa(dec);
158         total_pago(dec);
159         if (dec->verbose) {
160                 printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n",
161                         R(i_simples), R(i_completa));
162         }
163         if (dec->tipo != FORCA_SIMPLES &&
164             (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) {
165                 totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA"));
166                 dec->tipo = COMPLETA;
167                 dec->devido = i_completa;
168         } else {
169                 totais_add(dec, "BASE", totais_get(dec, "BASESIMPLES"));
170                 dec->tipo = SIMPLES;
171                 dec->devido = i_simples;
172         }
173         if (dec->pago > dec->devido)
174                 dec->restituicao = dec->pago - dec->devido;
175         else
176                 dec->pagar = dec->devido - dec->pago;
177         return 0;
178 }
179
180 static int run_calcula(struct declaracao *dec, char **args, int argc)
181 {
182         totais_add(dec, "EXCLUSIVOS_SEM_13o",
183                 totais_get(dec, "EXCLUSIVOS") -
184                 totais_get(dec, "DECIMOTERCEIRO"));
185         return calcula(dec);
186 }
187
188 static struct cmd cmd_calcula = {
189         .name = "calcula",
190         .run = run_calcula,
191 };
192
193 int calcula_cmd_init(void)
194 {
195         cmd_add(&cmd_calcula);
196         return 0;
197 }