|
import gradio as gr |
|
from fpdf import FPDF |
|
import os |
|
import tempfile |
|
|
|
|
|
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros): |
|
|
|
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 = parcela_mensal * numero_parcelas + investimento_proprio |
|
lucro = aluguel_mensal * numero_parcelas |
|
roi = (lucro - total_pago) / total_pago * 100 |
|
|
|
return roi, parcela_mensal, lucro |
|
|
|
|
|
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, 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 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) |
|
|
|
|
|
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 calcular_gerar_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros): |
|
|
|
roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros) |
|
|
|
|
|
return f"ROI Calculado: {roi:.2f}%\nParcela Mensal: R$ {parcela_mensal:,.2f}\nLucro Total: R$ {lucro:,.2f}" |
|
|
|
|
|
def gerar_pdf_arquivo(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros): |
|
|
|
roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros) |
|
|
|
|
|
pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro) |
|
|
|
return 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) |
|
|
|
|
|
roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3) |
|
|
|
|
|
gerar_roi_button = gr.Button("Gerar ROI") |
|
gerar_pdf_button = gr.Button("Gerar PDF") |
|
|
|
|
|
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")) |
|
|
|
|
|
iface.title = "Cálculo de ROI de Aluguel" |
|
iface.description = "Insira os valores abaixo e calcule o retorno sobre investimento (ROI) de aluguel." |
|
|
|
|
|
pdf_file_output.update(visible=True) |
|
|
|
|
|
iface.launch(share=True) |
|
|
|
|
|
|