File size: 5,345 Bytes
f0a1298
8a2fdb3
4a19aa3
 
567b716
 
8a2fdb3
4a19aa3
567b716
 
 
 
4a19aa3
 
 
 
 
 
 
 
567b716
 
 
 
 
 
4a19aa3
567b716
4a19aa3
 
567b716
4a19aa3
 
567b716
4a19aa3
8a2fdb3
 
 
4a19aa3
 
 
 
 
 
 
 
 
 
 
567b716
 
4a19aa3
567b716
4a19aa3
 
 
 
 
 
 
 
 
567b716
 
 
 
 
d234aa2
567b716
 
 
 
 
 
 
 
 
 
 
 
 
d234aa2
567b716
 
4a19aa3
567b716
4a19aa3
d234aa2
567b716
 
 
 
4a19aa3
567b716
4a19aa3
fa92e71
 
 
 
 
 
 
 
567b716
 
fa92e71
 
 
1df3e04
567b716
fa92e71
567b716
 
fa92e71
567b716
 
1df3e04
567b716
 
1df3e04
567b716
 
 
 
4a19aa3
 
6fcf1fa
209230e
3b2d1c9
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import gradio as gr
from fpdf import FPDF
import os
import tempfile
import matplotlib.pyplot as plt
import numpy as np

# Função para calcular o ROI
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
    # Desconto sobre o aluguel (custos + administração + impostos)
    aluguel_mensal_com_desconto = aluguel_mensal * (1 - desconto_aluguel / 100)
    
    # 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 para o primeiro ano
    total_pago_1_ano = parcela_mensal * 12 + investimento_proprio
    lucro_1_ano = aluguel_mensal_com_desconto * 12 - total_pago_1_ano
    roi_1_ano = (lucro_1_ano / total_pago_1_ano) * 100

    # Calcular ROI geral
    total_pago = parcela_mensal * numero_parcelas + investimento_proprio
    lucro = aluguel_mensal_com_desconto * numero_parcelas
    roi = (lucro - total_pago) / total_pago * 100

    return roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro

# Função para gerar o PDF
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, 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 do 1º Ano: {roi_1_ano:.2f}%", ln=True)
    pdf.cell(200, 10, txt=f"ROI Geral: {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 do 1º Ano: R$ {lucro_1_ano:,.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 gerar o gráfico
def gerar_grafico(roi_1_ano, roi):
    # Gerar gráfico
    anos = [1, 20]
    roi_values = [roi_1_ano, roi]
    
    plt.figure(figsize=(6, 4))
    plt.bar(anos, roi_values, color='blue', alpha=0.7)
    plt.title('ROI ao Longo do Tempo')
    plt.xlabel('Ano')
    plt.ylabel('ROI (%)')
    plt.xticks(anos, ['1º Ano', 'Geral'])
    
    # Salvar gráfico em um arquivo temporário
    temp_dir = tempfile.gettempdir()
    grafico_path = os.path.join(temp_dir, "grafico_roi.png")
    plt.savefig(grafico_path)
    
    return grafico_path

# Função para o botão "Gerar ROI e PDF"
def calcular_gerar_roi_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
    # Calcular ROI
    roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel)
    
    # Gerar o PDF com os dados
    pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro)
    
    # Gerar gráfico
    grafico_path = gerar_grafico(roi_1_ano, roi)
    
    return grafico_path, 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)
    desconto_aluguel = gr.Number(label="Desconto sobre o Aluguel (%)", value=10)

    # Resultado do cálculo ROI
    roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3)
    
    # Botões
    gerar_button = gr.Button("Gerar ROI e PDF")
    
    # Exibição do gráfico
    grafico_output = gr.Image(label="Gráfico de ROI")
    
    # Exibição do link de download
    pdf_download_button = gr.File(label="Baixar PDF", visible=False)
    
    # Funções de callback para os botões
    gerar_button.click(fn=calcular_gerar_roi_pdf, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel], outputs=[grafico_output, pdf_download_button])
    
    # Estilo e descrição
    iface.title = "Cálculo de ROI de Aluguel"
    iface.description = "Insira os valores abaixo para calcular o ROI do seu imóvel e gerar o PDF com os resultados."

# Rodar a interface Gradio
iface.launch(share=True)