4ba0d31e05e8c7ea1f890a50c925775c9cd7c208
[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
26 class OcupacaoForm(form.OptionsForm):
27     def __init__(self, ocupacoes, contribuinte):
28         g = ocupacoes.groups()
29         l = []
30         for i in sorted(g):
31             l.extend(g[i])
32         o = map(lambda x: (x[0], x[3]), l)
33         form.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal"))
34         self.ocupacoes = ocupacoes
35         self.contribuinte = contribuinte
36     def set_value(self, value):
37         form.OptionsForm.set_value(self, value)
38         self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value)
39
40 class ContribuinteForm(form.StringForm):
41     def __init__(self, name, attr, contribuinte):
42         self.contribuinte = contribuinte
43         self.attr = attr
44         form.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
45     def set_value(self, value):
46         form.StringForm.set_value(self, value)
47         self.contribuinte.set_campo_contribuinte(self.attr, value)
48
49 def List(UI, L, display):
50     exit = False
51     while not exit:
52         ls = []
53         for i in L.items:
54             d = i.get_attr(display)
55             if d == None:
56                 d = ""
57             ls.append(d)
58         r = UI.list(ls)
59         if r[1] == None:
60             exit = True
61         elif r[1] == 'add':
62             UI.form(L.form(L.new_item()))
63         elif r[1] == 'edit':
64             UI.form(L.form(L.items[r[0] - 1]))
65         elif r[1] == 'delete':
66             L.remove_item(r[0] - 1)
67     return True
68
69 def RendimentosPJ(UI, contrib):
70     rend = rendimentoPJ.RendimentosPJ(contrib)
71     return List(UI, rend, "nomeFontePagadora")
72
73 def DadosPessoais(UI, contrib):
74     form = []
75     ocup = ocupacoes.Ocupacoes()
76     form.append(ContribuinteForm("Nome", "nome", contrib))
77     form.append(OcupacaoForm(ocup, contrib))
78     for i in contribuinte.contribuinte_attributes:
79         form.append(ContribuinteForm(i, i, contrib))
80     UI.form(form)
81     return True
82
83 def Salvar(UI, contrib):
84     contrib.save()
85
86 def menu(UI, contrib):
87     m = [ "Sair", "Salvar", "Dados Pessoais", "Rendimentos PJ" ]
88     f = [ None, Salvar, DadosPessoais, RendimentosPJ ]
89     exit = False
90     while not exit:
91         r = UI.menu(m)
92         if r <= 0:
93             exit = True
94         else:
95             f[r](UI, contrib)
96
97 def main():
98     ret = False
99     UI = baseui.BaseUI()
100     while ret == False:
101         cpf = UI.get_string("Digite seu CPF: ")
102         try:
103             contrib = contribuinte.Contribuinte(cpf)
104             ret = menu(UI, contrib)
105         except RuntimeError, e:
106             print "CPF invalido"
107
108 if __name__ == '__main__':
109     main()
110
111 # vim:tabstop=4:expandtab:smartindent