59fbb66341dde0f7e927da0d93bb4a493e26115c
[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 21:
45         case 26:
46                 r += totais_add(dec, "MEDICAS", pagamento->pagamento - pagamento->reembolso);
47                 break;
48         case 36:
49                 r += totais_add(dec, "PREVIDENCIA", pagamento->pagamento - pagamento->reembolso);
50                 break;
51         }
52         return r;
53 }
54
55 void pagamento_free(void *pointer)
56 {
57         struct pagamento *pagamento = pointer;
58         if (pagamento->cnpj)
59                 free(pagamento->cnpj);
60         if (pagamento->nome)
61                 free(pagamento->nome);
62         free(pagamento);
63 }
64
65 static int pagamento_cmp(void *p1, void *p2)
66 {
67         struct pagamento *r1 = p1;
68         struct pagamento *r2 = p2;
69         /* O pagamento maior vem primeiro. */
70         if (r1->pagamento > r2->pagamento)
71                 return -1;
72         else if (r1->pagamento < r2->pagamento)
73                 return 1;
74         return 0;
75 }
76
77 static struct pagamento * pagamento_new(char **args, int argc)
78 {
79         struct pagamento *pagamento;
80         int r = 0;
81         pagamento = malloc(sizeof(*pagamento));
82         r += set_int(args[1], &pagamento->codigo);
83         r += set_string(args[2], &pagamento->cnpj);
84         r += set_string(args[3], &pagamento->nome);
85         r += set_llong(args[4], &pagamento->pagamento);
86         r += set_llong(args[5], &pagamento->reembolso);
87         if (r < 0 || pagamento->codigo < 0 ||
88             pagamento->pagamento < 0 || pagamento->reembolso < 0) {
89                 pagamento_free(pagamento);
90                 return NULL;
91         }
92         if (argc == 7) {
93                 r = set_int(args[6], &pagamento->dependente);
94         } else {
95                 pagamento->dependente = 0;
96         }
97         if (r < 0 || pagamento->dependente < 0) {
98                 pagamento_free(pagamento);
99                 return NULL;
100         }
101         return pagamento;
102 }
103
104 static int run_pagamento(struct declaracao *dec, char **args, int argc)
105 {
106         struct pagamento *pagamento;
107         int r;
108         if (argc != 6 && argc != 7)
109                 return -EINVAL;
110         pagamento = pagamento_new(args, argc);
111         if (!pagamento)
112                 return -ENOMEM;
113         if (pagamento->dependente > list_size(dec->dependentes)) {
114                 pagamento_free(pagamento);
115                 return -EINVAL;
116         }
117         r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
118         if (r < 0) {
119                 pagamento_free(pagamento);
120                 return r;
121         }
122         r = pagamento_totais_update(dec, pagamento);
123         if (r) {
124                 pagamento_free(pagamento);
125                 return r;
126         }
127         return 0;
128 }
129
130 void pagamento_salva(struct declaracao *dec, FILE *f)
131 {
132         int i;
133         struct pagamento *j;
134         for (i = 0; j = list_get(dec->pagamentos, i); i++)
135                 fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld %d\n",
136                         j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso, j->dependente);
137 }
138
139 static struct cmd cmd_pagamento = {
140         .name = "pagamento",
141         .run = run_pagamento,
142 };
143
144 int pagamento_cmd_init(void)
145 {
146         cmd_add(&cmd_pagamento);
147         return 0;
148 }
149
150 char * pagamento_cnpj_ordenado_cond(struct declaracao *dec,
151                                     int (*cond)(struct pagamento *), int n)
152 {
153         struct pagamento *pagamento;
154         int i;
155         int j = 0;
156         for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
157                 if (cond(pagamento) && j++ == n)
158                         break;
159         }
160         if (!pagamento)
161                 return "";
162         return pagamento->cnpj;
163 }
164
165 static int always(struct pagamento *pagamento)
166 {
167         return pagamento != NULL;
168 }
169
170 char * pagamento_cnpj_ordenado(struct declaracao *dec, int n)
171 {
172         return pagamento_cnpj_ordenado_cond(dec, always, n);
173 }
174
175 static int pagamento_medico(struct pagamento *pagamento)
176 {
177         switch (pagamento->codigo) {
178         case 10:
179         case 21:
180         case 26:
181                 return 1;
182         default:
183                 return 0;
184         }
185         return 0;
186 }
187
188 int pagamento_instrucao(struct pagamento *pagamento)
189 {
190         switch (pagamento->codigo) {
191         case 1:
192                 return 1;
193         default:
194                 return 0;
195         }
196         return 0;
197 }
198
199 char * medico_cnpj_ordenado(struct declaracao *dec, int n)
200 {
201         return pagamento_cnpj_ordenado_cond(dec, pagamento_medico, n);
202 }
203
204 static int pagamento_inss(struct pagamento *pagamento)
205 {
206         return pagamento->codigo == 36;
207 }
208
209 char *inss_cnpj_ordenado(struct declaracao *dec, int n)
210 {
211         return pagamento_cnpj_ordenado_cond(dec, pagamento_inss, n);
212 }