File size: 4,337 Bytes
f0a1298
8a2fdb3
4a19aa3
 
8a2fdb3
4a19aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a2fdb3
 
 
4a19aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d234aa2
 
 
 
 
 
 
 
 
 
4a19aa3
 
 
d234aa2
4a19aa3
 
d234aa2
4a19aa3
fa92e71
 
 
 
 
 
 
 
 
 
 
 
1df3e04
fa92e71
 
 
 
 
1df3e04
fa92e71
 
 
 
1df3e04
 
 
 
4a19aa3
 
6fcf1fa
209230e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
from fpdf import FPDF
import os
import tempfile

# Função para calcular o ROI
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
    # Calcular valor total do financiamento
    valor_financiado = valor_imovel - investimento_proprio
    
    # Calcular parcelas mensais do financiamento
    juros_mensal = (taxa_juros / 100) / 12
    numero_parcelas = tempo_anos * 12
    parcela_mensal = valor_financiado * (juros_mensal * (1 + juros_mensal) ** numero_parcelas) / ((1 + juros_mensal) ** numero_parcelas - 1)
    
    # Calcular ROI
    total_pago = parcela_mensal * numero_parcelas + investimento_proprio
    lucro = aluguel_mensal * numero_parcelas
    roi = (lucro - total_pago) / total_pago * 100

    return roi, parcela_mensal, lucro

# Função para gerar o PDF
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro):
    # Criar um PDF com o cálculo do ROI
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    
    # Título
    pdf.cell(200, 10, txt="Cálculo de ROI de Aluguel", ln=True, align='C')
    pdf.ln(10)
    
    # Adicionar os dados no PDF
    pdf.cell(200, 10, txt=f"Valor do Imóvel: R$ {valor_imovel:,.2f}", ln=True)
    pdf.cell(200, 10, txt=f"Investimento Próprio: R$ {investimento_proprio:,.2f}", ln=True)
    pdf.cell(200, 10, txt=f"Aluguel Mensal: R$ {aluguel_mensal:,.2f}", ln=True)
    pdf.cell(200, 10, txt=f"Tempo (anos): {tempo_anos} anos", ln=True)
    pdf.cell(200, 10, txt=f"Taxa de Juros: {taxa_juros}%", ln=True)
    pdf.cell(200, 10, txt=f"ROI Calculado: {roi:.2f}%", ln=True)
    pdf.cell(200, 10, txt=f"Parcela Mensal do Financiamento: R$ {parcela_mensal:,.2f}", ln=True)
    pdf.cell(200, 10, txt=f"Lucro Total: R$ {lucro:,.2f}", ln=True)
    
    # Salvar o PDF em um arquivo temporário
    temp_dir = tempfile.gettempdir()
    pdf_file_path = os.path.join(temp_dir, "resultado_roi.pdf")
    pdf.output(pdf_file_path)

    return pdf_file_path

# Função para o botão "Gerar ROI"
def calcular_gerar_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
    # Calcular ROI
    roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros)
    
    # Exibir resultado
    return f"ROI Calculado: {roi:.2f}%\nParcela Mensal: R$ {parcela_mensal:,.2f}\nLucro Total: R$ {lucro:,.2f}"

# Função para o botão "Gerar PDF"
def gerar_pdf_arquivo(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
    # Calcular ROI
    roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros)
    
    # Gerar o PDF com os dados
    pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro)
    
    return pdf_path

# Interface Gradio com gr.Blocks
with gr.Blocks() as iface:
    # Inputs para os dados
    valor_imovel = gr.Number(label="Valor do Imóvel (R$)", value=500000)
    investimento_proprio = gr.Number(label="Investimento Próprio (R$)", value=100000)
    aluguel_mensal = gr.Number(label="Aluguel Mensal (R$)", value=2500)
    tempo_anos = gr.Number(label="Tempo (anos)", value=20)
    taxa_juros = gr.Number(label="Taxa de Juros (%)", value=8)
    
    # Resultado do cálculo ROI
    roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3)
    
    # Botões
    gerar_roi_button = gr.Button("Gerar ROI")
    gerar_pdf_button = gr.Button("Gerar PDF")
    
    # Funções de callback para os botões
    gerar_roi_button.click(fn=calcular_gerar_roi, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros], outputs=roi_resultado)
    pdf_file_output = gerar_pdf_button.click(fn=gerar_pdf_arquivo, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros], outputs=gr.File(label="Baixar PDF"))
    
    # Configuração do tema e título
    iface.title = "Cálculo de ROI de Aluguel"
    iface.description = "Insira os valores abaixo e calcule o retorno sobre investimento (ROI) de aluguel."
    
    # Criar link de download como botão
    pdf_file_output.update(visible=True)
    
# Rodar a interface Gradio
iface.launch(share=True)