Nova função para ordenação de pagamentos médicos.
[cascardo/declara.git] / lib / pagamento.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "pagamento.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "cmd.h"
26 #include "list.h"
27 #include "util.h"
28 #include "totais.h"
29
30 void pagamento_free(void *pointer)
31 {
32         struct pagamento *pagamento = pointer;
33         if (pagamento->cnpj)
34                 free(pagamento->cnpj);
35         if (pagamento->nome)
36                 free(pagamento->nome);
37         free(pagamento);
38 }
39
40 static int pagamento_cmp(void *p1, void *p2)
41 {
42         struct pagamento *r1 = p1;
43         struct pagamento *r2 = p2;
44         /* O pagamento maior vem primeiro. */
45         if (r1->pagamento > r2->pagamento)
46                 return -1;
47         else if (r1->pagamento < r2->pagamento)
48                 return 1;
49         return 0;
50 }
51
52 static struct pagamento * pagamento_new(char **args)
53 {
54         struct pagamento *pagamento;
55         int r = 0;
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);
65                 return NULL;
66         }
67         if (r < 0 || pagamento->codigo < 0 ||
68             pagamento->pagamento < 0 || pagamento->reembolso < 0) {
69                 pagamento_free(pagamento);
70                 return NULL;
71         }
72         return pagamento;
73 }
74
75 static int run_pagamento(struct declaracao *dec, char **args, int argc)
76 {
77         struct pagamento *pagamento;
78         int r;
79         if (argc != 6)
80                 return -EINVAL;
81         pagamento = pagamento_new(args);
82         if (!pagamento)
83                 return -ENOMEM;
84         r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
85         if (r < 0) {
86                 pagamento_free(pagamento);
87                 return r;
88         }
89         r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento);
90         r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
91         r += totais_add(dec, "REEMBOLSOS", pagamento->reembolso);
92         r += totais_add(dec, "REEMBOLSOSTIT", pagamento->reembolso);
93         if (r) {
94                 pagamento_free(pagamento);
95                 return r;
96         }
97         return 0;
98 }
99
100 void pagamento_salva(struct declaracao *dec, FILE *f)
101 {
102         int i;
103         struct pagamento *j;
104         for (i = 0; j = list_get(dec->pagamentos, i); i++)
105                 fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n",
106                         j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso);
107 }
108
109 static struct cmd cmd_pagamento = {
110         .name = "pagamento",
111         .run = run_pagamento,
112 };
113
114 int pagamento_cmd_init(void)
115 {
116         cmd_add(&cmd_pagamento);
117         return 0;
118 }
119
120 char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n)
121 {
122         struct pagamento *pagamento;
123         int i;
124         int j = 0;
125         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
126                 if (pagamento->codigo == codigo && j++ == n)
127                         break;
128         }
129         if (!pagamento)
130                 return "";
131         return pagamento->cnpj;
132 }
133
134 static int pagamento_medico(int codigo)
135 {
136         switch (codigo) {
137         case 10:
138         case 21:
139         case 26:
140                 return 1;
141         default:
142                 return 0;
143         }
144         return 0;
145 }
146
147 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
148 {
149         struct pagamento *pagamento;
150         int i;
151         int j = 0;
152         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
153                 if (pagamento_medico(pagamento->codigo) && j++ == n)
154                         break;
155         }
156         if (!pagamento)
157                 return "";
158         return pagamento->cnpj;
159 }