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