2 * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
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.
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.
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.
19 #include "pagamento.h"
30 void pagamento_free(void *pointer)
32 struct pagamento *pagamento = pointer;
34 free(pagamento->cnpj);
36 free(pagamento->nome);
40 static int pagamento_cmp(void *p1, void *p2)
42 struct pagamento *r1 = p1;
43 struct pagamento *r2 = p2;
44 /* O pagamento maior vem primeiro. */
45 if (r1->pagamento > r2->pagamento)
47 else if (r1->pagamento < r2->pagamento)
52 static struct pagamento * pagamento_new(char **args)
54 struct pagamento *pagamento;
56 pagamento = malloc(sizeof(*pagamento));
57 pagamento->cnpj = strdup(args[2]);
58 pagamento->nome = strdup(args[3]);
59 /* TODO: consertar set_int para funcionar como set_llong */
60 r += set_int(args, 2, &pagamento->codigo);
61 r += set_llong(args[4], &pagamento->pagamento);
62 r += set_llong(args[5], &pagamento->reembolso);
63 if (!pagamento->cnpj || !pagamento->nome) {
64 pagamento_free(pagamento);
67 if (r < 0 || pagamento->codigo < 0 ||
68 pagamento->pagamento < 0 || pagamento->reembolso < 0) {
69 pagamento_free(pagamento);
75 static int run_pagamento(struct declaracao *dec, char **args, int argc)
77 struct pagamento *pagamento;
81 pagamento = pagamento_new(args);
84 r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
86 pagamento_free(pagamento);
89 r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento);
90 r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
92 pagamento_free(pagamento);
98 void pagamento_salva(struct declaracao *dec, FILE *f)
102 for (i = 0; j = list_get(dec->pagamentos, i); i++)
103 fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n",
104 j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso);
107 static struct cmd cmd_pagamento = {
109 .run = run_pagamento,
112 int pagamento_cmd_init(void)
114 cmd_add(&cmd_pagamento);
118 char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n)
120 struct pagamento *pagamento;
123 for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
124 if (pagamento->codigo == codigo && j++ == n)
129 return pagamento->cnpj;