Funções para lista de pagamentos ordenados.
[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, "PAGAMENTOSTIT", pagamento->pagamento);
35         r += totais_add(dec, "REEMBOLSOS", pagamento->reembolso);
36         r += totais_add(dec, "REEMBOLSOSTIT", pagamento->reembolso);
37         switch (pagamento->codigo) {
38         case 10:
39         case 21:
40         case 26:
41                 r += totais_add(dec, "MEDICAS", pagamento->pagamento - pagamento->reembolso);
42                 break;
43         case 36:
44                 r += totais_add(dec, "PREVIDENCIA", pagamento->pagamento - pagamento->reembolso);
45                 break;
46         }
47         return r;
48 }
49
50 void pagamento_free(void *pointer)
51 {
52         struct pagamento *pagamento = pointer;
53         if (pagamento->cnpj)
54                 free(pagamento->cnpj);
55         if (pagamento->nome)
56                 free(pagamento->nome);
57         free(pagamento);
58 }
59
60 static int pagamento_cmp(void *p1, void *p2)
61 {
62         struct pagamento *r1 = p1;
63         struct pagamento *r2 = p2;
64         /* O pagamento maior vem primeiro. */
65         if (r1->pagamento > r2->pagamento)
66                 return -1;
67         else if (r1->pagamento < r2->pagamento)
68                 return 1;
69         return 0;
70 }
71
72 static struct pagamento * pagamento_new(char **args)
73 {
74         struct pagamento *pagamento;
75         int r = 0;
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);
85                 return NULL;
86         }
87         if (r < 0 || pagamento->codigo < 0 ||
88             pagamento->pagamento < 0 || pagamento->reembolso < 0) {
89                 pagamento_free(pagamento);
90                 return NULL;
91         }
92         return pagamento;
93 }
94
95 static int run_pagamento(struct declaracao *dec, char **args, int argc)
96 {
97         struct pagamento *pagamento;
98         int r;
99         if (argc != 6)
100                 return -EINVAL;
101         pagamento = pagamento_new(args);
102         if (!pagamento)
103                 return -ENOMEM;
104         r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
105         if (r < 0) {
106                 pagamento_free(pagamento);
107                 return r;
108         }
109         r = pagamento_totais_update(dec, pagamento);
110         if (r) {
111                 pagamento_free(pagamento);
112                 return r;
113         }
114         return 0;
115 }
116
117 void pagamento_salva(struct declaracao *dec, FILE *f)
118 {
119         int i;
120         struct pagamento *j;
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);
124 }
125
126 static struct cmd cmd_pagamento = {
127         .name = "pagamento",
128         .run = run_pagamento,
129 };
130
131 int pagamento_cmd_init(void)
132 {
133         cmd_add(&cmd_pagamento);
134         return 0;
135 }
136
137 char * pagamento_cnpj_ordenado_cond(struct declaracao *dec,
138                                     int (*cond)(struct pagamento *), int n)
139 {
140         struct pagamento *pagamento;
141         int i;
142         int j = 0;
143         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
144                 if (cond(pagamento) && j++ == n)
145                         break;
146         }
147         if (!pagamento)
148                 return "";
149         return pagamento->cnpj;
150 }
151
152 static int always(struct pagamento *pagamento)
153 {
154         return pagamento != NULL;
155 }
156
157 char * pagamento_cnpj_ordenado(struct declaracao *dec, int n)
158 {
159         return pagamento_cnpj_ordenado_cond(dec, always, n);
160 }
161
162 static int pagamento_medico(struct pagamento *pagamento)
163 {
164         switch (pagamento->codigo) {
165         case 10:
166         case 21:
167         case 26:
168                 return 1;
169         default:
170                 return 0;
171         }
172         return 0;
173 }
174
175 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
176 {
177         return pagamento_cnpj_ordenado_cond(dec, pagamento_medico, n);
178 }
179
180 static int pagamento_inss(struct pagamento *pagamento)
181 {
182         return pagamento->codigo == 36;
183 }
184
185 char *inss_cnpj_ordenado(struct declaracao *dec, int n)
186 {
187         return pagamento_cnpj_ordenado_cond(dec, pagamento_inss, n);
188 }