--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "calcula.h"
+#include "declaracao.h"
+#include "cmd.h"
+#include "rendimento.h"
+#include <errno.h>
+#include <stdio.h>
+
+static long long total_rendimento(struct declaracao *dec)
+{
+ long long tr = 0;
+ struct rendimento *rendimento;
+ int i;
+ for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
+ tr += rendimento->rendimento;
+ }
+ return tr;
+}
+
+static long long total_deducao(struct declaracao *dec)
+{
+ long long td = 0;
+ struct rendimento *rendimento;
+ int i;
+ for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
+ td += rendimento->previdencia;
+ }
+ return td;
+}
+
+static long long total_pago(struct declaracao *dec)
+{
+ long long tt = 0;
+ struct rendimento *rendimento;
+ int i;
+ for (i = 0; rendimento = list_get(dec->rendimento, i); i++) {
+ tt += rendimento->imposto;
+ }
+ return tt;
+}
+
+struct taxtable {
+ long long base;
+ long long aliquota;
+ long long deducao;
+};
+
+static struct taxtable table2015[] = {
+ { 0, 0, 0, },
+ { 2145324, 750, 160899, },
+ { 3215148, 1500, 402035, },
+ { 4286917, 2250, 723554, },
+ { 5356572, 2750, 991383, },
+ { 9999999999999LL, 0, 0, },
+};
+
+static const long long simples2015 = 1588089;
+
+static long long imposto(struct taxtable *tt, long long tr)
+{
+ int i;
+ for (i = 0; tr >= tt[i].base; i++);
+ i--;
+ return tr * tt[i].aliquota / 10000 - tt[i].deducao;
+}
+
+static long long imposto_simples(struct declaracao *dec)
+{
+ struct taxtable *tt;
+ long long tr, td;
+ if (dec->ano != 2015) {
+ return -EINVAL;
+ }
+ tt = table2015;
+ tr = total_rendimento(dec);
+ if (tr / 5 < simples2015)
+ td = tr / 5;
+ else
+ td = simples2015;
+ tr -= td;
+ return imposto(tt, tr);
+}
+
+static long long imposto_completa(struct declaracao *dec)
+{
+ struct taxtable *tt;
+ long long tr, td;
+ if (dec->ano != 2015) {
+ return -EINVAL;
+ }
+ tt = table2015;
+ tr = total_rendimento(dec);
+ td = total_deducao(dec);
+ tr -= td;
+ return imposto(tt, tr);
+}
+
+void calcula(struct declaracao *dec)
+{
+ long long i_simples, i_completa;
+ i_simples = imposto_simples(dec);
+ i_completa = imposto_completa(dec);
+ dec->pago = total_pago(dec);
+ if (i_simples > i_completa) {
+ dec->tipo = COMPLETA;
+ dec->devido = i_completa;
+ } else {
+ dec->tipo = SIMPLES;
+ dec->devido = i_simples;
+ }
+ dec->restituicao = dec->pago - dec->devido;
+}
+
+static int run_calcula(struct declaracao *dec, char **args, int argc)
+{
+ calcula(dec);
+ return 0;
+}
+
+static struct cmd cmd_calcula = {
+ .name = "calcula",
+ .run = run_calcula,
+};
+
+int calcula_cmd_init(void)
+{
+ cmd_add(&cmd_calcula);
+ return 0;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CALCULA_H
+#define _CALCULA_H
+
+int calcula_cmd_init(void);
+
+#endif