Tenta ordernar pagamentos
[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 static int pagamento_totais_update(struct declaracao *dec, struct pagamento *pagamento)
31 {
32         int r;
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);
38         } else {
39                 r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
40                 r += totais_add(dec, "REEMBOLSOSTIT", pagamento->reembolso);
41         }
42         switch (pagamento->codigo) {
43         case 10:
44         case 12:
45         case 21:
46         case 26:
47                 r += totais_add(dec, "MEDICAS", pagamento->pagamento - pagamento->reembolso);
48                 break;
49         case 36:
50                 r += totais_add(dec, "PREVIDENCIA", pagamento->pagamento - pagamento->reembolso);
51                 break;
52         }
53         return r;
54 }
55
56 void pagamento_free(void *pointer)
57 {
58         struct pagamento *pagamento = pointer;
59         if (pagamento->cnpj)
60                 free(pagamento->cnpj);
61         if (pagamento->nome)
62                 free(pagamento->nome);
63         free(pagamento);
64 }
65
66 static int pagamento_cmp(void *p1, void *p2)
67 {
68         struct pagamento *r1 = p1;
69         struct pagamento *r2 = p2;
70         /* O pagamento maior vem primeiro. */
71         if (r1->pagamento > r2->pagamento)
72                 return -1;
73         else if (r1->pagamento < r2->pagamento)
74                 return 1;
75         return 0;
76 }
77
78 static struct pagamento * pagamento_new(char **args, int argc)
79 {
80         struct pagamento *pagamento;
81         int r = 0;
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);
91                 return NULL;
92         }
93         if (argc == 7) {
94                 r = set_int(args[6], &pagamento->dependente);
95         } else {
96                 pagamento->dependente = 0;
97         }
98         if (r < 0 || pagamento->dependente < 0) {
99                 pagamento_free(pagamento);
100                 return NULL;
101         }
102         return pagamento;
103 }
104
105 static int run_pagamento(struct declaracao *dec, char **args, int argc)
106 {
107         struct pagamento *pagamento;
108         int r;
109         if (argc != 6 && argc != 7)
110                 return -EINVAL;
111         pagamento = pagamento_new(args, argc);
112         if (!pagamento)
113                 return -ENOMEM;
114         if (pagamento->dependente > list_size(dec->dependentes)) {
115                 pagamento_free(pagamento);
116                 return -EINVAL;
117         }
118         r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
119         if (r < 0) {
120                 pagamento_free(pagamento);
121                 return r;
122         }
123         r = pagamento_totais_update(dec, pagamento);
124         if (r) {
125                 pagamento_free(pagamento);
126                 return r;
127         }
128         return 0;
129 }
130
131 void pagamento_salva(struct declaracao *dec, FILE *f)
132 {
133         int i;
134         struct pagamento *j;
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);
138 }
139
140 static struct cmd cmd_pagamento = {
141         .name = "pagamento",
142         .run = run_pagamento,
143 };
144
145 int pagamento_cmd_init(void)
146 {
147         cmd_add(&cmd_pagamento);
148         return 0;
149 }
150
151 static struct pagamento * novo_pagamento_cnpj(struct pagamento *pagamento)
152 {
153         struct pagamento *cnpj;
154         cnpj = malloc(sizeof(*pagamento));
155         cnpj->codigo = pagamento->codigo;
156         cnpj->cnpj = strdup(pagamento->cnpj);
157         cnpj->nome = strdup(pagamento->nome);
158         cnpj->pagamento = 0;
159         return cnpj;
160 }
161
162 static struct pagamento * procura_pagamento_cnpj(struct declaracao *dec, char *cnpj)
163 {
164         int i;
165         struct pagamento *pagamento;
166         for (i = 0; (pagamento = list_get(dec->pagamentos_cnpj, i)); i++) {
167                 if (!strcmp(pagamento->cnpj, cnpj)) {
168                         return pagamento;
169                 }
170         }
171         return NULL;
172 }
173
174 static void agrupa_ordena_pagamentos(struct declaracao *dec)
175 {
176         int i;
177         struct pagamento *pagamento;
178         struct pagamento *cnpj;
179         struct pagamento *j;
180         dec->pagamentos_cnpj = list_new();
181         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
182                 cnpj = procura_pagamento_cnpj(dec, pagamento->cnpj);
183                 if (!cnpj) {
184                         cnpj = novo_pagamento_cnpj(pagamento);
185                         cnpj->pagamento = (pagamento->pagamento);
186                         list_add(&dec->pagamentos_cnpj, cnpj);
187                 } else {
188                         cnpj->pagamento += (pagamento->pagamento);
189                 }
190         }
191         list_sort(dec->pagamentos_cnpj, pagamento_cmp);
192         for (i = 0; (j = list_get(dec->pagamentos_cnpj, i)); i++) {
193                 if (dec->verbose)
194                         printf("pagamento %d \"%s\" \"%s\" %lld\n",
195                                 j->codigo, j->cnpj, j->nome, j->pagamento);
196         }
197 }
198
199 char * pagamento_cnpj_ordenado_cond(struct declaracao *dec,
200                                     int (*cond)(struct pagamento *), int n)
201 {
202         struct pagamento *pagamento;
203         int i;
204         int j = 0;
205         if (!dec->pagamentos_cnpj) {
206                 agrupa_ordena_pagamentos(dec);
207         }
208         for (i = 0; (pagamento = list_get(dec->pagamentos_cnpj, i)); i++) {
209                 if (cond(pagamento) && j++ == n)
210                         break;
211         }
212         if (!pagamento)
213                 return "";
214         return pagamento->cnpj;
215 }
216
217 static int always(struct pagamento *pagamento)
218 {
219         return pagamento != NULL;
220 }
221
222 char * pagamento_cnpj_ordenado(struct declaracao *dec, int n)
223 {
224         return pagamento_cnpj_ordenado_cond(dec, always, n);
225 }
226
227 static int pagamento_medico(struct pagamento *pagamento)
228 {
229         switch (pagamento->codigo) {
230         case 10:
231         case 12:
232         case 21:
233         case 26:
234                 return 1;
235         default:
236                 return 0;
237         }
238         return 0;
239 }
240
241 int pagamento_instrucao(struct pagamento *pagamento)
242 {
243         switch (pagamento->codigo) {
244         case 1:
245                 return 1;
246         default:
247                 return 0;
248         }
249         return 0;
250 }
251
252 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
253 {
254         return pagamento_cnpj_ordenado_cond(dec, pagamento_medico, n);
255 }
256
257 static int pagamento_inss(struct pagamento *pagamento)
258 {
259         return pagamento->codigo == 36;
260 }
261
262 char *inss_cnpj_ordenado(struct declaracao *dec, int n)
263 {
264         return pagamento_cnpj_ordenado_cond(dec, pagamento_inss, n);
265 }