Interface para Rendimentos PJ
[cascardo/irpf-gui.git] / src / menu.py
1 # -*- mode: python; encoding: 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
18 import baseui
19 import contribuinte
20 import ocupacoes
21 import rendimentoPJ
22 import sys
23
24 class OcupacaoForm(baseui.OptionsForm):
25     def __init__(self, ocupacoes, contribuinte):
26         g = ocupacoes.groups()
27         l = []
28         for i in sorted(g):
29             l.extend(g[i])
30         o = map(lambda x: (x[0], x[3]), l)
31         baseui.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal"))
32         self.ocupacoes = ocupacoes
33         self.contribuinte = contribuinte
34     def set_value(self, value):
35         baseui.OptionsForm.set_value(self, value)
36         self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value)
37
38 class ContribuinteForm(baseui.StringForm):
39     def __init__(self, name, attr, contribuinte):
40         self.contribuinte = contribuinte
41         self.attr = attr
42         baseui.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
43     def set_value(self, value):
44         baseui.StringForm.set_value(self, value)
45         self.contribuinte.set_campo_contribuinte(self.attr, value)
46
47 class AttrForm(baseui.StringForm):
48     def __init__(self, name, attr, element):
49         self.element = element
50         self.attr = attr
51         baseui.StringForm.__init__(self, name, self.element.get_attr(self.attr))
52     def set_value(self, value):
53         baseui.StringForm.set_value(self, value)
54         self.element.set_attr(self.attr, value)
55
56 def RendimentoPJ(UI, rend):
57     form = []
58     form.append(AttrForm("Nome", "nomeFontePagadora", rend))
59     form.append(AttrForm("CNPJ", "NIFontePagadora", rend))
60     form.append(AttrForm("Rendimentos", "rendRecebidoPJ", rend))
61     form.append(AttrForm(u"Previdência", "contribuicaoPrevOficial", rend))
62     form.append(AttrForm("Imposto Retido", "impostoRetidoFonte", rend))
63     form.append(AttrForm(u"Décimo Terceiro", "decimoTerceiro", rend))
64     UI.form(form)
65     return True
66
67 def List(UI, L, Edit, display):
68     exit = False
69     while not exit:
70         ls = []
71         for i in L.items:
72             ls.append(i.get_attr(display))
73         r = UI.list(ls)
74         if r[1] == None:
75             exit = True
76         elif r[1] == 'add':
77             Edit(UI, L.new_item())
78         elif r[1] == 'edit':
79             Edit(UI, L.items[r[0] - 1])
80         elif r[1] == 'delete':
81             L.remove_item(r[0] - 1)
82     return True
83
84 def RendimentosPJ(UI, contrib):
85     rend = rendimentoPJ.RendimentosPJ(contrib)
86     return List(UI, rend, RendimentoPJ, "nomeFontePagadora")
87
88 def DadosPessoais(UI, contrib):
89     form = []
90     ocup = ocupacoes.Ocupacoes()
91     form.append(ContribuinteForm("Nome", "nome", contrib))
92     form.append(OcupacaoForm(ocup, contrib))
93     for i in contribuinte.contribuinte_attributes:
94         form.append(ContribuinteForm(i, i, contrib))
95     UI.form(form)
96     return True
97
98 def menu(UI, contrib):
99     m = [ "Sair", "Dados Pessoais", "Rendimentos PJ" ]
100     f = [ None, 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