from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

# Asegurar directorio de salida
os.makedirs("generated", exist_ok=True)
pdf_path = "generated/plantilla_cotizacion_paneles_solares.pdf"

# Configuración del documento
doc = SimpleDocTemplate(
    pdf_path, 
    pagesize=letter,
    rightMargin=40, leftMargin=40, topMargin=40, bottomMargin=40,
    title="Plantilla de Cotización de Paneles Solares"
)

styles = getSampleStyleSheet()
story = []

# Estilos personalizados
style_title = ParagraphStyle('DocTitle', parent=styles['Heading1'], fontSize=24, textColor=colors.HexColor("#1A365D"), spaceAfter=6)
style_subtitle = ParagraphStyle('DocSub', parent=styles['Normal'], fontSize=10, textColor=colors.HexColor("#4A5568"), spaceAfter=15)
style_h2 = ParagraphStyle('SectionHeader', parent=styles['Heading2'], fontSize=14, textColor=colors.HexColor("#2B6CB0"), spaceBefore=12, spaceAfter=6)
style_body = ParagraphStyle('BodyTextCustom', parent=styles['Normal'], fontSize=10, textColor=colors.HexColor("#2D3748"), leading=14)
style_th = ParagraphStyle('TableHead', parent=styles['Normal'], fontSize=10, textColor=colors.white, fontName="Helvetica-Bold")
style_td = ParagraphStyle('TableData', parent=styles['Normal'], fontSize=9, textColor=colors.HexColor("#2D3748"))

# Encabezado principal
story.append(Paragraph("<b>PROPUESTA TÉCNICO-COMERCIAL</b>", style_title))
story.append(Paragraph("Sistemas Energéticos Fotovoltaicos | Folio: SOL-2026-001", style_subtitle))
story.append(Spacer(1, 10))

# Información de las partes (Tabla)
info_data = [
    [Paragraph("<b>EMPRESA EMISORA:</b>", style_body), Paragraph("<b>PREPARADO PARA:</b>", style_body)],
    [Paragraph("[Nombre de tu Empresa Soluciones Solares]<br/>RFC / Tax ID: [Tu RFC]<br/>Contacto: contacto@tuempresa.com<br/>Teléfono: +52 55 XXXX XXXX", style_body),
     Paragraph("[Nombre del Cliente / Razón Social]<br/>Dirección del Proyecto: [Dirección Completa]<br/>Tarifa Eléctrica: [Ej. PDBT / GDMTO / Residencial]<br/>Fecha de Emisión: 25 de Agosto, 2026", style_body)]
]
info_table = Table(info_data, colWidths=[266, 266])
info_table.setStyle(TableStyle([
    ('VALIGN', (0,0), (-1,-1), 'TOP'),
    ('BACKGROUND', (0,0), (-1,-1), colors.HexColor("#F7FAFC")),
    ('PADDING', (0,0), (-1,-1), 10),
    ('LINEBELOW', (0,0), (-1,-1), 1, colors.HexColor("#E2E8F0")),
]))
story.append(info_table)
story.append(Spacer(1, 15))

# Especificaciones Técnicas
story.append(Paragraph("1. Resumen del Sistema Propuesto", style_h2))
tech_data = [
    [Paragraph("<b>Componente / Concepto</b>", style_th), Paragraph("<b>Detalle Técnico</b>", style_th), Paragraph("<b>Cantidad</b>", style_th)],
    [Paragraph("Paneles Fotovoltaicos", style_td), Paragraph("Módulos Monocristalinos PERC de [550W] o similar", style_td), Paragraph("[10] Piezas", style_td)],
    [Paragraph("Inversor / Microinversores", style_td), Paragraph("Inversor Central Interconectado [5 kW] con monitoreo WiFi", style_td), Paragraph("[1] Unidad", style_td)],
    [Paragraph("Estructura de Montaje", style_td), Paragraph("Aluminio anodizado AL6005-T5 para techo [Inclinado/Plano]", style_td), Paragraph("[1] Lote", style_td)],
    [Paragraph("Protecciones y Cableado", style_td), Paragraph("Tablero AC/DC, supresor de picos, cable solar fotovoltaico", style_td), Paragraph("[1] Lote", style_td)],
    [Paragraph("Mano de Obra y Trámites", style_td), Paragraph("Instalación certificada y gestión de interconexión ante CFE/proveedor", style_td), Paragraph("Incluido", style_td)],
]
tech_table = Table(tech_data, colWidths=[150, 282, 100])
tech_table.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,0), colors.HexColor("#2B6CB0")),
    ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
    ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.white, colors.HexColor("#F7FAFC")]),
    ('PADDING', (0,0), (-1,-1), 6),
    ('GRID', (0,0), (-1,-1), 0.5, colors.HexColor("#E2E8F0")),
]))
story.append(tech_table)
story.append(Spacer(1, 15))

# Análisis de Retorno Financiero Corto
story.append(Paragraph("2. Proyección Financiera Estimada", style_h2))
fin_data = [
    [Paragraph("Potencia Total Instalada", style_td), Paragraph("<b>[5.5 kWp]</b>", style_td), Paragraph("Generación Mensual Promedio", style_td), Paragraph("<b>[680 kWh]</b>", style_td)],
    [Paragraph("Ahorro Bimestral Estimado", style_td), Paragraph("<b>[$ X,XXX MXN]</b>", style_td), Paragraph("Retorno de Inversión (ROI)", style_td), Paragraph("<b>[3.5 a 4.5 Años]</b>", style_td)]
]
fin_table = Table(fin_data, colWidths=[140, 126, 140, 126])
fin_table.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,-1), colors.HexColor("#EDF2F7")),
    ('PADDING', (0,0), (-1,-1), 6),
    ('GRID', (0,0), (-1,-1), 0.5, colors.HexColor("#CBD5E0")),
]))
story.append(fin_table)
story.append(Spacer(1, 15))

# Desglose Económico
story.append(Paragraph("3. Presupuesto Económico", style_h2))
price_data = [
    [Paragraph("<b>Descripción del Servicio / Paquete</b>", style_th), Paragraph("<b>Monto</b>", style_th)],
    [Paragraph("Suministro total, ingeniería, instalación y trámites de interconexión", style_td), Paragraph("$ 00,000.00", style_td)],
    [Paragraph("<b>Subtotal</b>", style_td), Paragraph("$ 00,000.00", style_td)],
    [Paragraph("<b>IVA (16%)</b>", style_td), Paragraph("$ 0,000.00", style_td)],
    [Paragraph("<b>TOTAL NETO INVERSIÓN</b>", style_td), Paragraph("<b>$ 00,000.00 MXN</b>", style_td)],
]
price_table = Table(price_data, colWidths=[382, 150])
price_table.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,0), colors.HexColor("#1A365D")),
    ('ALIGN', (1,0), (1,-1), 'RIGHT'),
    ('PADDING', (0,0), (-1,-1), 6),
    ('BACKGROUND', (0,2), (-1,-1), colors.HexColor("#F7FAFC")),
    ('LINEABOVE', (0,2), (-1,2), 1, colors.HexColor("#A0AEC0")),
    ('GRID', (0,0), (-1,-1), 0.5, colors.HexColor("#E2E8F0")),
]))
story.append(price_table)
story.append(Spacer(1, 20))

# Términos y Condiciones
story.append(Paragraph("4. Términos y Condiciones", style_h2))
condiciones = """
• <b>Vigencia de la cotización:</b> [15] días naturales debido a la fluctuación cambiaria de los componentes.<br/>
• <b>Forma de pago:</b> [60%] de anticipo al firmar contrato, [30%] a la entrega de equipos en sitio, [10%] contra instalación y encendido de prueba.<br/>
• <b>Garantías:</b> [25 años] de rendimiento en paneles fotovoltaicos, [5 a 12 años] en inversor central, [1 año] en mano de obra por instalación.<br/>
• <b>Exclusión:</b> No incluye refuerzos estructurales en techos dañados ni modificaciones complejas en la acometida eléctrica existente si esta no cumple con la norma vigente.
"""
story.append(Paragraph(condiciones, style_body))

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: