Adiciona comando pagamento.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 2 Aug 2015 01:20:40 +0000 (22:20 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 2 Aug 2015 01:20:40 +0000 (22:20 -0300)
Cria uma lista de pagamentos. Algumas opções ainda não são suportadas,
como NIT para empregado/a doméstico/a.

Makefile.am
declara.c
declaracao.c
declaracao.h
pagamento.c [new file with mode: 0644]
pagamento.h [new file with mode: 0644]

index 8e86311..b0bea0d 100644 (file)
@@ -9,7 +9,8 @@ declara_SOURCES = declara.c declaracao.c declaracao.h \
        list.c list.h \
        util.c util.h \
        contribuinte.c contribuinte.h \
-       rendimento.c rendimento.h
+       rendimento.c rendimento.h \
+       pagamento.c pagamento.h
 
 check_PROGRAMS = listtest
 listtest_SOURCES = listtest.c list.c list.h
index fd1d534..8db644a 100644 (file)
--- a/declara.c
+++ b/declara.c
@@ -28,6 +28,7 @@
 #include "base.h"
 #include "contribuinte.h"
 #include "rendimento.h"
+#include "pagamento.h"
 #include "calcula.h"
 #include "gera.h"
 
@@ -90,6 +91,7 @@ int main(int argc, char **argv)
        base_cmd_init();
        contribuinte_cmd_init();
        rendimento_cmd_init();
+       pagamento_cmd_init();
        calcula_cmd_init();
        gera_cmd_init();
 
index ec519c9..53ff1e4 100644 (file)
@@ -21,6 +21,7 @@
 #include <string.h>
 #include "list.h"
 #include "rendimento.h"
+#include "pagamento.h"
 #include "pmhash.h"
 
 struct declaracao * declaracao_new(int ano)
@@ -34,6 +35,9 @@ struct declaracao * declaracao_new(int ano)
        dec->rendimento = list_new();
        if (!dec->rendimento)
                goto out_rendimento;
+       dec->pagamentos = list_new();
+       if (!dec->pagamentos)
+               goto out_pagamentos;
        dec->totais = pmhash_new();
        if (!dec->totais)
                goto out_totais;
@@ -43,6 +47,8 @@ struct declaracao * declaracao_new(int ano)
        dec->tipo = SIMPLES;
        return dec;
 out_totais:
+       list_free(dec->pagamentos, pagamento_free);
+out_pagamentos:
        list_free(dec->rendimento, rendimento_free);
 out_rendimento:
        free(dec);
@@ -68,6 +74,7 @@ void declaracao_free(struct declaracao *dec)
        if (dec->dvconta)
                free(dec->dvconta);
        list_free(dec->rendimento, rendimento_free);
+       list_free(dec->pagamentos, pagamento_free);
        pmhash_del(dec->totais);
        free(dec);
 }
index a5cb0b7..1694661 100644 (file)
@@ -32,6 +32,7 @@ struct declaracao {
        char *cpf;
        char *nome;
        struct list *rendimento;
+       struct list *pagamentos;
        struct contribuinte contribuinte;
        long long pago;
        long long retido;
diff --git a/pagamento.c b/pagamento.c
new file mode 100644 (file)
index 0000000..26cb3c0
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ *  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;
+}
diff --git a/pagamento.h b/pagamento.h
new file mode 100644 (file)
index 0000000..235daca
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  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