Persano commited on
Commit
d4b6c8b
·
verified ·
1 Parent(s): 2c3b871

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -97
app.py CHANGED
@@ -8,103 +8,163 @@ from datetime import date, timedelta
8
 
9
  app = Flask(__name__)
10
 
11
- # HTML_FORM vem aqui (omitido neste trecho por brevidade)
12
- HTML_FORM = """(HTML completo da pergunta anterior)"""
13
-
14
- # Funções auxiliares
15
-
16
- def calcular_valor_por_m2(tamanho, acabamento):
17
- tabela = {
18
- "simples": 1000,
19
- "medio": 1500,
20
- "luxo": 2000
21
- }
22
- fator_tamanho = {
23
- "pequeno": 1,
24
- "medio": 1.5,
25
- "grande": 2
26
- }
27
- return tabela[acabamento] * fator_tamanho[tamanho]
28
-
29
- def calcular_orcamento(form):
30
- detalhes = []
31
- total = 0
32
- metros_totais = float(form.get("dimensao_obra", 0))
33
-
34
- # Cozinha
35
- cozinha_tamanho = form.get("cozinha_tamanho")
36
- cozinha_acabamento = form.get("cozinha_acabamento")
37
- preco_cozinha = calcular_valor_por_m2(cozinha_tamanho, cozinha_acabamento)
38
- valor_cozinha = preco_cozinha * 5 # Cozinha = 5m² fixo
39
- detalhes.append(f"Cozinha ({cozinha_tamanho}, {cozinha_acabamento}): R$ {valor_cozinha:.2f}")
40
- total += valor_cozinha
41
-
42
- # Banheiros
43
- num_banheiros = int(form.get("banheiros", 0))
44
- for i in range(1, num_banheiros + 1):
45
- tam = form.get(f"banheiro_{i}_tamanho")
46
- acb = form.get(f"banheiro_{i}_acabamento")
47
- preco = calcular_valor_por_m2(tam, acb)
48
- m2 = 3 if tam == "pequeno" else 5 if tam == "medio" else 7
49
- valor = preco * m2
50
- detalhes.append(f"Banheiro {i} ({tam}, {acb}): R$ {valor:.2f}")
51
- total += valor
52
-
53
- # Serviços adicionais (hidráulica, elétrica, pintura = 10% cada)
54
- servicos = request.form.getlist("servicos")
55
- for s in ["hidraulica", "eletrica", "pintura"]:
56
- if s in servicos:
57
- valor = total * 0.1
58
- total += valor
59
- detalhes.append(f"{s.capitalize()}: R$ {valor:.2f}")
60
-
61
- # Outros serviços fixos
62
- servicos_fixos = ["ar_condicionado", "fechamento_sacada", "gesso"]
63
- precos_fixos = {
64
- "ar_condicionado": 1500,
65
- "fechamento_sacada": 3000,
66
- "gesso": 1000
67
- }
68
- for s in servicos_fixos:
69
- if s in servicos:
70
- valor = precos_fixos[s]
71
- total += valor
72
- detalhes.append(f"{s.replace('_', ' ').capitalize()}: R$ {valor:.2f}")
73
-
74
- return total, detalhes
75
-
76
- def gerar_pdf(form, total, detalhes):
77
- pdf = FPDF()
78
- pdf.add_page()
79
- pdf.set_font("Arial", size=12)
80
- pdf.cell(200, 10, txt="Contrato de Reforma", ln=True, align="C")
81
- pdf.cell(200, 10, txt=f"Contratante: {form.get('nome_contratante')}", ln=True)
82
- pdf.cell(200, 10, txt=f"Endereço da Obra: {form.get('endereco_obra')}", ln=True)
83
- pdf.cell(200, 10, txt=f"Data: {date.today().strftime('%d/%m/%Y')}", ln=True)
84
- pdf.cell(200, 10, txt="Serviços contratados:", ln=True)
85
- for item in detalhes:
86
- pdf.multi_cell(0, 10, txt=f"- {item}")
87
- pdf.cell(200, 10, txt=f"Valor Total: R$ {total:.2f}", ln=True)
88
- pdf.cell(200, 10, txt="Assinatura do Contratante: __________________________", ln=True)
89
-
90
- tmp_dir = tempfile.mkdtemp()
91
- caminho_pdf = os.path.join(tmp_dir, "contrato.pdf")
92
- pdf.output(caminho_pdf)
93
- return caminho_pdf
94
-
95
- # Rotas
96
-
97
- @app.route("/", methods=["GET", "POST"])
98
- def index():
99
- if request.method == "POST":
100
- if request.form.get("acao") == "Calcular Orçamento":
101
- total, detalhes = calcular_orcamento(request.form)
102
- return render_template_string(HTML_FORM, total=total, detalhes=detalhes, request=request)
103
- elif request.form.get("acao") == "Gerar Contrato PDF":
104
- total, detalhes = calcular_orcamento(request.form)
105
- caminho_pdf = gerar_pdf(request.form, total, detalhes)
106
- return send_file(caminho_pdf, as_attachment=True)
107
- return render_template_string(HTML_FORM)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  if __name__ == "__main__":
110
  app.run(host="0.0.0.0", port=7860)
 
8
 
9
  app = Flask(__name__)
10
 
11
+ # HTML_FORM vem aqui (atualizado com estilo moderno)
12
+ HTML_FORM = """
13
+ <!DOCTYPE html>
14
+ <html>
15
+ <head>
16
+ <title>Orçamento de Reforma</title>
17
+ <style>
18
+ body {
19
+ font-family: 'Segoe UI', sans-serif;
20
+ background-color: #f4f6f8;
21
+ margin: 0;
22
+ padding: 2rem;
23
+ }
24
+ h2, h3 {
25
+ color: #333;
26
+ }
27
+ form {
28
+ background: #fff;
29
+ padding: 2rem;
30
+ border-radius: 12px;
31
+ box-shadow: 0 0 12px rgba(0,0,0,0.05);
32
+ max-width: 700px;
33
+ margin: auto;
34
+ }
35
+ label {
36
+ display: block;
37
+ margin-top: 1rem;
38
+ font-weight: bold;
39
+ }
40
+ input[type="text"],
41
+ input[type="number"],
42
+ select {
43
+ width: 100%;
44
+ padding: 0.5rem;
45
+ margin-top: 0.3rem;
46
+ border: 1px solid #ccc;
47
+ border-radius: 8px;
48
+ }
49
+ button {
50
+ background-color: #007bff;
51
+ color: white;
52
+ padding: 0.75rem 1.5rem;
53
+ border: none;
54
+ border-radius: 8px;
55
+ cursor: pointer;
56
+ margin-top: 1rem;
57
+ margin-right: 1rem;
58
+ }
59
+ button:hover {
60
+ background-color: #0056b3;
61
+ }
62
+ .checkbox-group {
63
+ display: grid;
64
+ grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
65
+ gap: 0.5rem;
66
+ margin-top: 1rem;
67
+ }
68
+ .checkbox-group label {
69
+ font-weight: normal;
70
+ }
71
+ #banheiros-container h4 {
72
+ margin-top: 1rem;
73
+ }
74
+ ul {
75
+ background: #fff;
76
+ max-width: 700px;
77
+ margin: 2rem auto;
78
+ padding: 1rem;
79
+ border-radius: 12px;
80
+ box-shadow: 0 0 12px rgba(0,0,0,0.05);
81
+ }
82
+ </style>
83
+ </head>
84
+ <body>
85
+ <h2 style="text-align:center;">Formulário de Orçamento de Reforma</h2>
86
+ <form method="post">
87
+ <label>Nome do Contratante:</label>
88
+ <input type="text" name="nome_contratante" required>
89
+
90
+ <label>Endereço da Obra:</label>
91
+ <input type="text" name="endereco_obra" required>
92
+
93
+ <label>Dimensão da obra (m²):</label>
94
+ <input type="number" step="0.1" name="dimensao_obra" required>
95
+
96
+ <h3>Cozinha</h3>
97
+ <label>Tamanho:</label>
98
+ <select name="cozinha_tamanho">
99
+ <option value="pequeno">Pequeno</option>
100
+ <option value="medio">Médio</option>
101
+ <option value="grande">Grande</option>
102
+ </select>
103
+ <label>Acabamento:</label>
104
+ <select name="cozinha_acabamento">
105
+ <option value="simples">Simples</option>
106
+ <option value="medio">Médio</option>
107
+ <option value="luxo">Luxo</option>
108
+ </select>
109
+
110
+ <h3>Banheiros</h3>
111
+ <label>Quantidade:</label>
112
+ <input type="number" name="banheiros" min="0" max="5" value="1" required>
113
+ <div id="banheiros-container"></div>
114
+
115
+ <h3>Serviços</h3>
116
+ <div class="checkbox-group">
117
+ <label><input type="checkbox" name="servicos" value="hidraulica"> Hidráulica</label>
118
+ <label><input type="checkbox" name="servicos" value="eletrica"> Elétrica</label>
119
+ <label><input type="checkbox" name="servicos" value="pintura"> Pintura</label>
120
+ <label><input type="checkbox" name="servicos" value="ar_condicionado"> Ar Condicionado</label>
121
+ <label><input type="checkbox" name="servicos" value="fechamento_sacada"> Fechamento de Sacada</label>
122
+ <label><input type="checkbox" name="servicos" value="gesso"> Gesso</label>
123
+ </div>
124
+
125
+ <button type="submit" name="acao" value="Calcular Orçamento">Calcular Orçamento</button>
126
+ <button type="submit" name="acao" value="Gerar Contrato PDF">Gerar Contrato PDF</button>
127
+ </form>
128
+
129
+ {% if total %}
130
+ <ul>
131
+ <h3>Resumo do Orçamento</h3>
132
+ {% for item in detalhes %}
133
+ <li>{{ item }}</li>
134
+ {% endfor %}
135
+ <p><strong>Total: R$ {{ total }}</strong></p>
136
+ </ul>
137
+ {% endif %}
138
+
139
+ <script>
140
+ function gerarBanheiros() {
141
+ const container = document.getElementById("banheiros-container");
142
+ const qtd = parseInt(document.querySelector("input[name='banheiros']").value);
143
+ container.innerHTML = "";
144
+ for (let i = 1; i <= qtd; i++) {
145
+ container.innerHTML += `
146
+ <h4>Banheiro ${i}</h4>
147
+ <label>Tamanho:</label>
148
+ <select name="banheiro_${i}_tamanho">
149
+ <option value="pequeno">Pequeno</option>
150
+ <option value="medio">Médio</option>
151
+ <option value="grande">Grande</option>
152
+ </select>
153
+ <label>Acabamento:</label>
154
+ <select name="banheiro_${i}_acabamento">
155
+ <option value="simples">Simples</option>
156
+ <option value="medio">Médio</option>
157
+ <option value="luxo">Luxo</option>
158
+ </select>
159
+ `;
160
+ }
161
+ }
162
+ window.onload = gerarBanheiros;
163
+ document.querySelector("input[name='banheiros']").addEventListener("input", gerarBanheiros);
164
+ </script>
165
+ </body>
166
+ </html>
167
+ """
168
 
169
  if __name__ == "__main__":
170
  app.run(host="0.0.0.0", port=7860)