From 2d177b9242186e1dd6832460ff1b2cae282af2d1 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 12 Mar 2016 17:38:19 -0300 Subject: [PATCH] =?utf8?q?Implementa=20op=C3=A7=C3=A3o=20verbose.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Com esta opção, alguns resultados intermediários são exibidos, permitindo verificações e detecção de erros. --- lib/base.c | 14 ++++++++++++++ lib/calcula.c | 42 ++++++++++++++++++++++++++++++++++++++---- lib/declaracao.h | 1 + lib/totais.c | 5 +++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/base.c b/lib/base.c index c2f88b9..3db915e 100644 --- a/lib/base.c +++ b/lib/base.c @@ -80,6 +80,14 @@ static int run_resumo(struct declaracao *dec, char **args, int argc) return 0; } +static int run_verbose(struct declaracao *dec, char **args, int argc) +{ + if (argc != 1) + return -EINVAL; + dec->verbose = 1; + return 0; +} + static void salva(struct declaracao *dec, FILE *f) { fprintf(f, "ano %d\n", dec->ano); @@ -150,6 +158,11 @@ static struct cmd cmd_resumo = { .run = run_resumo, }; +static struct cmd cmd_verbose = { + .name = "verbose", + .run = run_verbose, +}; + static struct cmd cmd_simples = { .name = "simples", .run = run_simples, @@ -176,6 +189,7 @@ int base_cmd_init(void) cmd_add(&cmd_contacorrente); cmd_add(&cmd_dvconta); + cmd_add(&cmd_verbose); cmd_add(&cmd_simples); cmd_add(&cmd_completa); diff --git a/lib/calcula.c b/lib/calcula.c index db260b8..71b94b4 100644 --- a/lib/calcula.c +++ b/lib/calcula.c @@ -23,6 +23,7 @@ #include "cmd.h" #include "rendimento.h" #include "totais.h" +#include "util.h" static const long long dependente2015 = 215652; @@ -39,6 +40,13 @@ long long deducao_dependente(struct declaracao *dec) static long long total_deducao(struct declaracao *dec) { int i; + if (dec->verbose) { + printf("Dedução:\n"); + printf("\tDependentes: "FMT_R"\n", R(totais_get(dec, "DEPENDENTES"))); + printf("\tINSS: "FMT_R"\n", R(totais_get(dec, "INSS"))); + printf("\tPagamentos: "FMT_R"\n", R(totais_get(dec, "PAGAMENTOS"))); + printf("\tReembolsos: -"FMT_R"\n", R(totais_get(dec, "REEMBOLSOS"))); + } return totais_get(dec, "DEPENDENTES") + totais_get(dec, "INSS") + totais_get(dec, "PAGAMENTOS") - @@ -54,6 +62,10 @@ static void total_pago(struct declaracao *dec) dec->pago += rendimento->imposto; dec->retido += rendimento->imposto; } + if (dec->verbose) { + printf("Total pago e retido: "FMT_R" "FMT_R"\n", + R(dec->pago), R(dec->retido)); + } } struct taxtable { @@ -75,11 +87,15 @@ static const long long simples2015 = 1588089; static const long long obrigatoriedade2015 = 2681655; -static long long imposto(struct taxtable *tt, long long tr) +static long long imposto(struct taxtable *tt, long long tr, int verbose) { int i; for (i = 0; tr >= tt[i].base; i++); i--; + if (verbose) { + printf("Aplicando aliquota de %d%%, deduzindo " FMT_R"\n", + tt[i].aliquota / 10, R(tt[i].deducao)); + } return tr * tt[i].aliquota / 10000 - tt[i].deducao; } @@ -96,7 +112,10 @@ static long long imposto_simples(struct declaracao *dec) totais_add(dec, "DESCONTO", td); tr -= td; totais_add(dec, "BASESIMPLES", tr); - return imposto(tt, tr); + if (dec->verbose) { + printf("Desconto simplificado é "FMT_R"\n", R(td)); + } + return imposto(tt, tr, dec->verbose); } static long long imposto_completa(struct declaracao *dec) @@ -112,7 +131,10 @@ static long long imposto_completa(struct declaracao *dec) totais_add(dec, "DEDUCOES", td); tr -= td; totais_add(dec, "BASECOMPLETA", tr); - return imposto(tt, tr); + if (dec->verbose) { + printf("Desconto completa é "FMT_R"\n", R(td)); + } + return imposto(tt, tr, dec->verbose); } int calcula(struct declaracao *dec) @@ -121,11 +143,23 @@ int calcula(struct declaracao *dec) if (dec->ano != 2015) { return -EINVAL; } - if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) + if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) { + if (dec->verbose) { + printf("Declaracao obrigatoria pois rendimento e" + "maior que mínimo para declaracao: " + FMT_R" > "FMT_R"\n", + R(totais_get(dec, "RENDPJ")), + R(obrigatoriedade2015)); + } dec->obrigatoria = 1; + } i_simples = imposto_simples(dec); i_completa = imposto_completa(dec); total_pago(dec); + if (dec->verbose) { + printf("Imposto simplificada e completa: "FMT_R" "FMT_R"\n", + R(i_simples), R(i_completa)); + } if (dec->tipo != FORCA_SIMPLES && (i_simples > i_completa || dec->tipo == FORCA_COMPLETA)) { totais_add(dec, "BASE", totais_get(dec, "BASECOMPLETA")); diff --git a/lib/declaracao.h b/lib/declaracao.h index ce318e6..5948878 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -60,6 +60,7 @@ struct declaracao { struct pmhash *totais; unsigned long hash; unsigned long rhash; + int verbose; }; struct declaracao * declaracao_new(int ano); diff --git a/lib/totais.c b/lib/totais.c index ca79125..5d32d33 100644 --- a/lib/totais.c +++ b/lib/totais.c @@ -21,6 +21,7 @@ #include #include #include "pmhash.h" +#include "util.h" int totais_add(struct declaracao *dec, char *key, long long val) { @@ -39,6 +40,10 @@ int totais_add(struct declaracao *dec, char *key, long long val) goto out_hash; *p = 0; } + if (dec->verbose) { + printf("Somando "FMT_R" a %s: "FMT_R" -> "FMT_R"\n", + R(val), key, R(*p), R(*p + val)); + } *p += val; return 0; out_hash: -- 2.20.1