From 8c59ccf495b2aa94469fe25d4e1efe224f3a9aea Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 3 May 2018 15:55:26 -0300 Subject: [PATCH] Tenta ordernar pagamentos --- lib/declaracao.h | 1 + lib/list.c | 19 +++++++++++++++++ lib/list.h | 1 + lib/pagamento.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/declaracao.h b/lib/declaracao.h index 0dc2c17..8a27e9c 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -39,6 +39,7 @@ struct declaracao { struct list *carne; struct list *isentos; struct list *pagamentos; + struct list *pagamentos_cnpj; struct list *bens; struct list *dependentes; struct contribuinte contribuinte; diff --git a/lib/list.c b/lib/list.c index 8cba3f3..57d24ba 100644 --- a/lib/list.c +++ b/lib/list.c @@ -128,3 +128,22 @@ int list_size(struct list *list) { return list->len; } + +/* Selection sort */ +int list_sort(struct list *l, sort_function_t *fn) +{ + int i, j; + for (i = 0; i < l->len; i++) { + int max = i; + void *tmp; + /* Find index of max value */ + for (j = i; j < l->len; j++) { + if (fn(l->items[j].val, l->items[max].val) < 0) + max = j; + } + /* Swap items */ + tmp = l->items[max].val; + l->items[max].val = l->items[i].val; + l->items[i].val = tmp; + } +} diff --git a/lib/list.h b/lib/list.h index 8521518..eb34475 100644 --- a/lib/list.h +++ b/lib/list.h @@ -30,5 +30,6 @@ int list_insert_ordered(struct list **list, void *val, sort_function_t *fn); void * list_get(struct list *list, int pos); void list_free(struct list *list, free_function_t *ifree); int list_size(struct list *list); +int list_sort(struct list *list, sort_function_t *fn); #endif diff --git a/lib/pagamento.c b/lib/pagamento.c index 7dc0e95..b37390d 100644 --- a/lib/pagamento.c +++ b/lib/pagamento.c @@ -148,13 +148,64 @@ int pagamento_cmd_init(void) return 0; } +static struct pagamento * novo_pagamento_cnpj(struct pagamento *pagamento) +{ + struct pagamento *cnpj; + cnpj = malloc(sizeof(*pagamento)); + cnpj->codigo = pagamento->codigo; + cnpj->cnpj = strdup(pagamento->cnpj); + cnpj->nome = strdup(pagamento->nome); + cnpj->pagamento = 0; + return cnpj; +} + +static struct pagamento * procura_pagamento_cnpj(struct declaracao *dec, char *cnpj) +{ + int i; + struct pagamento *pagamento; + for (i = 0; (pagamento = list_get(dec->pagamentos_cnpj, i)); i++) { + if (!strcmp(pagamento->cnpj, cnpj)) { + return pagamento; + } + } + return NULL; +} + +static void agrupa_ordena_pagamentos(struct declaracao *dec) +{ + int i; + struct pagamento *pagamento; + struct pagamento *cnpj; + struct pagamento *j; + dec->pagamentos_cnpj = list_new(); + for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { + cnpj = procura_pagamento_cnpj(dec, pagamento->cnpj); + if (!cnpj) { + cnpj = novo_pagamento_cnpj(pagamento); + cnpj->pagamento = (pagamento->pagamento); + list_add(&dec->pagamentos_cnpj, cnpj); + } else { + cnpj->pagamento += (pagamento->pagamento); + } + } + list_sort(dec->pagamentos_cnpj, pagamento_cmp); + for (i = 0; (j = list_get(dec->pagamentos_cnpj, i)); i++) { + if (dec->verbose) + printf("pagamento %d \"%s\" \"%s\" %lld\n", + j->codigo, j->cnpj, j->nome, j->pagamento); + } +} + char * pagamento_cnpj_ordenado_cond(struct declaracao *dec, int (*cond)(struct pagamento *), int n) { struct pagamento *pagamento; int i; int j = 0; - for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { + if (!dec->pagamentos_cnpj) { + agrupa_ordena_pagamentos(dec); + } + for (i = 0; (pagamento = list_get(dec->pagamentos_cnpj, i)); i++) { if (cond(pagamento) && j++ == n) break; } -- 2.20.1