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 static int pagamento_totais_update(struct declaracao *dec, struct pagamento *pagamento)
33 r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento);
34 r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
35 r += totais_add(dec, "REEMBOLSOS", pagamento->reembolso);
36 r += totais_add(dec, "REEMBOLSOSTIT", pagamento->reembolso);
37 switch (pagamento->codigo) {
41 r += totais_add(dec, "MEDICAS", pagamento->pagamento - pagamento->reembolso);
44 r += totais_add(dec, "PREVIDENCIA", pagamento->pagamento - pagamento->reembolso);
50 void pagamento_free(void *pointer)
52 struct pagamento *pagamento = pointer;
54 free(pagamento->cnpj);
56 free(pagamento->nome);
60 static int pagamento_cmp(void *p1, void *p2)
62 struct pagamento *r1 = p1;
63 struct pagamento *r2 = p2;
64 /* O pagamento maior vem primeiro. */
65 if (r1->pagamento > r2->pagamento)
67 else if (r1->pagamento < r2->pagamento)
72 static struct pagamento * pagamento_new(char **args)
74 struct pagamento *pagamento;
76 pagamento = malloc(sizeof(*pagamento));
77 pagamento->cnpj = strdup(args[2]);
78 pagamento->nome = strdup(args[3]);
79 /* TODO: consertar set_int para funcionar como set_llong */
80 r += set_int(args, 2, &pagamento->codigo);
81 r += set_llong(args[4], &pagamento->pagamento);
82 r += set_llong(args[5], &pagamento->reembolso);
83 if (!pagamento->cnpj || !pagamento->nome) {
84 pagamento_free(pagamento);
87 if (r < 0 || pagamento->codigo < 0 ||
88 pagamento->pagamento < 0 || pagamento->reembolso < 0) {
89 pagamento_free(pagamento);
95 static int run_pagamento(struct declaracao *dec, char **args, int argc)
97 struct pagamento *pagamento;
101 pagamento = pagamento_new(args);
104 r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
106 pagamento_free(pagamento);
109 r = pagamento_totais_update(dec, pagamento);
111 pagamento_free(pagamento);
117 void pagamento_salva(struct declaracao *dec, FILE *f)
121 for (i = 0; j = list_get(dec->pagamentos, i); i++)
122 fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n",
123 j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso);
126 static struct cmd cmd_pagamento = {
128 .run = run_pagamento,
131 int pagamento_cmd_init(void)
133 cmd_add(&cmd_pagamento);
137 char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n)
139 struct pagamento *pagamento;
142 for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
143 if (pagamento->codigo == codigo && j++ == n)
148 return pagamento->cnpj;
151 static int pagamento_medico(int codigo)
164 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
166 struct pagamento *pagamento;
169 for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
170 if (pagamento_medico(pagamento->codigo) && j++ == n)
175 return pagamento->cnpj;