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, "REEMBOLSOS", pagamento->reembolso);
35 if (pagamento->dependente) {
36 r += totais_add(dec, "PAGAMENTOSDEP", pagamento->pagamento);
37 r += totais_add(dec, "REEMBOLSOSDEP", pagamento->reembolso);
39 r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
40 r += totais_add(dec, "REEMBOLSOSTIT", pagamento->reembolso);
42 switch (pagamento->codigo) {
47 r += totais_add(dec, "MEDICAS", pagamento->pagamento - pagamento->reembolso);
50 r += totais_add(dec, "PREVIDENCIA", pagamento->pagamento - pagamento->reembolso);
56 void pagamento_free(void *pointer)
58 struct pagamento *pagamento = pointer;
60 free(pagamento->cnpj);
62 free(pagamento->nome);
66 static int pagamento_cmp(void *p1, void *p2)
68 struct pagamento *r1 = p1;
69 struct pagamento *r2 = p2;
70 /* O pagamento maior vem primeiro. */
71 if (r1->pagamento > r2->pagamento)
73 else if (r1->pagamento < r2->pagamento)
78 static struct pagamento * pagamento_new(char **args, int argc)
80 struct pagamento *pagamento;
82 pagamento = malloc(sizeof(*pagamento));
83 r += set_int(args[1], &pagamento->codigo);
84 r += set_string(args[2], &pagamento->cnpj);
85 r += set_string(args[3], &pagamento->nome);
86 r += set_llong(args[4], &pagamento->pagamento);
87 r += set_llong(args[5], &pagamento->reembolso);
88 if (r < 0 || pagamento->codigo < 0 ||
89 pagamento->pagamento < 0 || pagamento->reembolso < 0) {
90 pagamento_free(pagamento);
94 r = set_int(args[6], &pagamento->dependente);
96 pagamento->dependente = 0;
98 if (r < 0 || pagamento->dependente < 0) {
99 pagamento_free(pagamento);
105 static int run_pagamento(struct declaracao *dec, char **args, int argc)
107 struct pagamento *pagamento;
109 if (argc != 6 && argc != 7)
111 pagamento = pagamento_new(args, argc);
114 if (pagamento->dependente > list_size(dec->dependentes)) {
115 pagamento_free(pagamento);
118 r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
120 pagamento_free(pagamento);
123 r = pagamento_totais_update(dec, pagamento);
125 pagamento_free(pagamento);
131 void pagamento_salva(struct declaracao *dec, FILE *f)
135 for (i = 0; j = list_get(dec->pagamentos, i); i++)
136 fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld %d\n",
137 j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso, j->dependente);
140 static struct cmd cmd_pagamento = {
142 .run = run_pagamento,
145 int pagamento_cmd_init(void)
147 cmd_add(&cmd_pagamento);
151 char * pagamento_cnpj_ordenado_cond(struct declaracao *dec,
152 int (*cond)(struct pagamento *), int n)
154 struct pagamento *pagamento;
157 for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
158 if (cond(pagamento) && j++ == n)
163 return pagamento->cnpj;
166 static int always(struct pagamento *pagamento)
168 return pagamento != NULL;
171 char * pagamento_cnpj_ordenado(struct declaracao *dec, int n)
173 return pagamento_cnpj_ordenado_cond(dec, always, n);
176 static int pagamento_medico(struct pagamento *pagamento)
178 switch (pagamento->codigo) {
190 int pagamento_instrucao(struct pagamento *pagamento)
192 switch (pagamento->codigo) {
201 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
203 return pagamento_cnpj_ordenado_cond(dec, pagamento_medico, n);
206 static int pagamento_inss(struct pagamento *pagamento)
208 return pagamento->codigo == 36;
211 char *inss_cnpj_ordenado(struct declaracao *dec, int n)
213 return pagamento_cnpj_ordenado_cond(dec, pagamento_inss, n);