|
import gradio as gr |
|
from fpdf import FPDF |
|
import os |
|
import tempfile |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
|
|
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel): |
|
|
|
aluguel_mensal_com_desconto = aluguel_mensal * (1 - desconto_aluguel / 100) |
|
|
|
|
|
valor_financiado = valor_imovel - investimento_proprio |
|
|
|
|
|
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) |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro): |
|
|
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
|
|
|
|
pdf.cell(200, 10, txt="Cálculo de ROI de Aluguel", ln=True, align='C') |
|
pdf.ln(10) |
|
|
|
|
|
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) |
|
|
|
|
|
temp_dir = tempfile.gettempdir() |
|
pdf_file_path = os.path.join(temp_dir, "resultado_roi.pdf") |
|
pdf.output(pdf_file_path) |
|
|
|
return pdf_file_path |
|
|
|
|
|
def gerar_grafico(roi_1_ano, roi): |
|
|
|
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']) |
|
|
|
|
|
temp_dir = tempfile.gettempdir() |
|
grafico_path = os.path.join(temp_dir, "grafico_roi.png") |
|
plt.savefig(grafico_path) |
|
|
|
return grafico_path |
|
|
|
|
|
def calcular_gerar_roi_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel): |
|
|
|
roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel) |
|
|
|
|
|
pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro) |
|
|
|
|
|
grafico_path = gerar_grafico(roi_1_ano, roi) |
|
|
|
return grafico_path, pdf_path |
|
|
|
|
|
with gr.Blocks() as iface: |
|
|
|
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) |
|
|
|
|
|
roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3) |
|
|
|
|
|
gerar_button = gr.Button("Gerar ROI e PDF") |
|
|
|
|
|
grafico_output = gr.Image(label="Gráfico de ROI") |
|
|
|
|
|
pdf_download_button = gr.File(label="Baixar PDF", visible=False) |
|
|
|
|
|
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]) |
|
|
|
|
|
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." |
|
|
|
|
|
iface.launch(share=True) |
|
|
|
|
|
|
|
|