--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "pagamento.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "cmd.h"
+#include "list.h"
+#include "util.h"
+#include "totais.h"
+
+void pagamento_free(void *pointer)
+{
+ struct pagamento *pagamento = pointer;
+ if (pagamento->cnpj)
+ free(pagamento->cnpj);
+ if (pagamento->nome)
+ free(pagamento->nome);
+ free(pagamento);
+}
+
+static int pagamento_cmp(void *p1, void *p2)
+{
+ struct pagamento *r1 = p1;
+ struct pagamento *r2 = p2;
+ /* O pagamento maior vem primeiro. */
+ if (r1->pagamento > r2->pagamento)
+ return -1;
+ else if (r1->pagamento < r2->pagamento)
+ return 1;
+ return 0;
+}
+
+static struct pagamento * pagamento_new(char **args)
+{
+ struct pagamento *pagamento;
+ int r = 0;
+ pagamento = malloc(sizeof(*pagamento));
+ pagamento->cnpj = strdup(args[2]);
+ pagamento->nome = strdup(args[3]);
+ /* TODO: consertar set_int para funcionar como set_llong */
+ r += set_int(args, 2, &pagamento->codigo);
+ r += set_llong(args[4], &pagamento->pagamento);
+ r += set_llong(args[5], &pagamento->reembolso);
+ if (!pagamento->cnpj || !pagamento->nome) {
+ pagamento_free(pagamento);
+ return NULL;
+ }
+ if (r < 0 || pagamento->codigo < 0 ||
+ pagamento->pagamento < 0 || pagamento->reembolso < 0) {
+ pagamento_free(pagamento);
+ return NULL;
+ }
+ return pagamento;
+}
+
+static int run_pagamento(struct declaracao *dec, char **args, int argc)
+{
+ struct pagamento *pagamento;
+ int r;
+ if (argc != 6)
+ return -EINVAL;
+ pagamento = pagamento_new(args);
+ if (!pagamento)
+ return -ENOMEM;
+ r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp);
+ if (r < 0) {
+ pagamento_free(pagamento);
+ return r;
+ }
+ r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento);
+ r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento);
+ if (r) {
+ pagamento_free(pagamento);
+ return r;
+ }
+ return 0;
+}
+
+void pagamento_salva(struct declaracao *dec, FILE *f)
+{
+ int i;
+ struct pagamento *j;
+ for (i = 0; j = list_get(dec->pagamentos, i); i++)
+ fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n",
+ j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso);
+}
+
+static struct cmd cmd_pagamento = {
+ .name = "pagamento",
+ .run = run_pagamento,
+};
+
+int pagamento_cmd_init(void)
+{
+ cmd_add(&cmd_pagamento);
+ return 0;
+}
+
+char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n)
+{
+ struct pagamento *pagamento;
+ int i;
+ int j = 0;
+ for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) {
+ if (pagamento->codigo == codigo && j++ == n)
+ break;
+ }
+ if (!pagamento)
+ return "";
+ return pagamento->cnpj;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _PAGAMENTO_H
+#define _PAGAMENTO_H
+
+#include <stdio.h>
+#include "declaracao.h"
+
+struct pagamento {
+ int codigo;
+ char *cnpj;
+ char *nome;
+ long long pagamento;
+ long long reembolso;
+};
+
+void pagamento_salva(struct declaracao *dec, FILE *f);
+void pagamento_free(void *pointer);
+
+int pagamento_cmd_init(void);
+
+char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n);
+
+#endif