Se nome do item estiver vazio, utiliza string vazia.
[cascardo/irpf-gui.git] / src / menu.py
1 # coding=utf-8
2 #
3 #   Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 # -*- mode: python; encoding: utf-8; -*-
18
19 import baseui
20 import contribuinte
21 import ocupacoes
22 import rendimentoPJ
23 import sys
24 import form
25 from form import AttrForm
26
27 class OcupacaoForm(form.OptionsForm):
28     def __init__(self, ocupacoes, contribuinte):
29         g = ocupacoes.groups()
30         l = []
31         for i in sorted(g):
32             l.extend(g[i])
33         o = map(lambda x: (x[0], x[3]), l)
34         form.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal"))
35         self.ocupacoes = ocupacoes
36         self.contribuinte = contribuinte
37     def set_value(self, value):
38         form.OptionsForm.set_value(self, value)
39         self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value)
40
41 class ContribuinteForm(form.StringForm):
42     def __init__(self, name, attr, contribuinte):
43         self.contribuinte = contribuinte
44         self.attr = attr
45         form.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
46     def set_value(self, value):
47         form.StringForm.set_value(self, value)
48         self.contribuinte.set_campo_contribuinte(self.attr, value)
49
50 def RendimentoPJ(UI, rend):
51     form = []
52     form.append(AttrForm("Nome", "nomeFontePagadora", rend))
53     form.append(AttrForm("CNPJ", "NIFontePagadora", rend))
54     form.append(AttrForm("Rendimentos", "rendRecebidoPJ", rend))
55     form.append(AttrForm(u"Previdência", "contribuicaoPrevOficial", rend))
56     form.append(AttrForm("Imposto Retido", "impostoRetidoFonte", rend))
57     form.append(AttrForm(u"Décimo Terceiro", "decimoTerceiro", rend))
58     UI.form(form)
59     return True
60
61 def List(UI, L, Edit, display):
62     exit = False
63     while not exit:
64         ls = []
65         for i in L.items:
66             d = i.get_attr(display)
67             if d == None:
68                 d = ""
69             ls.append(d)
70         r = UI.list(ls)
71         if r[1] == None:
72             exit = True
73         elif r[1] == 'add':
74             Edit(UI, L.new_item())
75         elif r[1] == 'edit':
76             Edit(UI, L.items[r[0] - 1])
77         elif r[1] == 'delete':
78             L.remove_item(r[0] - 1)
79     return True
80
81 def RendimentosPJ(UI, contrib):
82     rend = rendimentoPJ.RendimentosPJ(contrib)
83     return List(UI, rend, RendimentoPJ, "nomeFontePagadora")
84
85 def DadosPessoais(UI, contrib):
86     form = []
87     ocup = ocupacoes.Ocupacoes()
88     form.append(ContribuinteForm("Nome", "nome", contrib))
89     form.append(OcupacaoForm(ocup, contrib))
90     for i in contribuinte.contribuinte_attributes:
91         form.append(ContribuinteForm(i, i, contrib))
92     UI.form(form)
93     return True
94
95 def Salvar(UI, contrib):
96     contrib.save()
97
98 def menu(UI, contrib):
99     m = [ "Sair", "Salvar", "Dados Pessoais", "Rendimentos PJ" ]
100     f = [ None, Salvar, DadosPessoais, RendimentosPJ ]
101     exit = False
102     while not exit:
103         r = UI.menu(m)
104         if r <= 0:
105             exit = True
106         else:
107             f[r](UI, contrib)
108
109 def main():
110     ret = False
111     UI = baseui.BaseUI()
112     while ret == False:
113         cpf = UI.get_string("Digite seu CPF: ")
114         try:
115             contrib = contribuinte.Contribuinte(cpf)
116             ret = menu(UI, contrib)
117         except RuntimeError, e:
118             print "CPF invalido"
119
120 if __name__ == '__main__':
121     main()
122
123 # vim:tabstop=4:expandtab:smartindent