Calcula imposto devido e restituição.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 26 Apr 2015 22:15:52 +0000 (22:15 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Mon, 11 May 2015 01:02:04 +0000 (01:02 +0000)
Makefile.am
calcula.c [new file with mode: 0644]
calcula.h [new file with mode: 0644]
declara.c

index 5fb2136..93997f5 100644 (file)
@@ -2,6 +2,7 @@ bin_PROGRAMS = declara
 declara_SOURCES = declara.c declaracao.c declaracao.h \
        token.c token.h \
        cmd.c cmd.h pmhash.c pmhash.h \
+       calcula.c calcula.h \
        base.c base.h \
        list.c list.h \
        util.c util.h \
diff --git a/calcula.c b/calcula.c
new file mode 100644 (file)
index 0000000..3e3e841
--- /dev/null
+++ b/calcula.c
@@ -0,0 +1,146 @@
+/*
+ *  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;
+}
diff --git a/calcula.h b/calcula.h
new file mode 100644 (file)
index 0000000..cc40110
--- /dev/null
+++ b/calcula.h
@@ -0,0 +1,24 @@
+/*
+ *  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
index db113ad..04a1c70 100644 (file)
--- a/declara.c
+++ b/declara.c
@@ -27,6 +27,7 @@
 #include "cmd.h"
 #include "base.h"
 #include "rendimento.h"
+#include "calcula.h"
 
 static int realprocess(struct declaracao *dec, int fd)
 {
@@ -80,6 +81,7 @@ int main(int argc, char **argv)
        cmd_init();
        base_cmd_init();
        rendimento_cmd_init();
+       calcula_cmd_init();
 
        filename = argv[1];
        r = process(filename);