b042796b37718b2bd846c055ec791cb32800e5dd
[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             ls.append(i.get_attr(display))
67         r = UI.list(ls)
68         if r[1] == None:
69             exit = True
70         elif r[1] == 'add':
71             Edit(UI, L.new_item())
72         elif r[1] == 'edit':
73             Edit(UI, L.items[r[0] - 1])
74         elif r[1] == 'delete':
75             L.remove_item(r[0] - 1)
76     return True
77
78 def RendimentosPJ(UI, contrib):
79     rend = rendimentoPJ.RendimentosPJ(contrib)
80     return List(UI, rend, RendimentoPJ, "nomeFontePagadora")
81
82 def DadosPessoais(UI, contrib):
83     form = []
84     ocup = ocupacoes.Ocupacoes()
85     form.append(ContribuinteForm("Nome", "nome", contrib))
86     form.append(OcupacaoForm(ocup, contrib))
87     for i in contribuinte.contribuinte_attributes:
88         form.append(ContribuinteForm(i, i, contrib))
89     UI.form(form)
90     return True
91
92 def Salvar(UI, contrib):
93     contrib.save()
94
95 def menu(UI, contrib):
96     m = [ "Sair", "Salvar", "Dados Pessoais", "Rendimentos PJ" ]
97     f = [ None, Salvar, DadosPessoais, RendimentosPJ ]
98     exit = False
99     while not exit:
100         r = UI.menu(m)
101         if r <= 0:
102             exit = True
103         else:
104             f[r](UI, contrib)
105
106 def main():
107     ret = False
108     UI = baseui.BaseUI()
109     while ret == False:
110         cpf = UI.get_string("Digite seu CPF: ")
111         try:
112             contrib = contribuinte.Contribuinte(cpf)
113             ret = menu(UI, contrib)
114         except RuntimeError, e:
115             print "CPF invalido"
116
117 if __name__ == '__main__':
118     main()
119
120 # vim:tabstop=4:expandtab:smartindent