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