
    i                         S SK Jr  S SKrS rg)    )get_connectionNc            	         Sn Sn [        5       nU(       d%  [        S5        / U(       a  UR                  5         $ $ UR                  5       nUR	                  U 5        UR
                   Vs/ s H  o3S   PM	     nnUR                  5        Vs/ s H  n[        [        XE5      5      PM     nnUU(       a  UR                  5         $ $ s  snf s  snf ! [        R                   a2  n[        SU 35        / s SnAU(       a  UR                  5         $ $ SnAf[         a2  n[        SU 35        / s SnAU(       a  UR                  5         $ $ SnAff = f! U(       a  UR                  5         f f = f)u   
Obtiene la lista completa de tareas de cotización desde la BD.
Esta función es llamada por el endpoint en CotizFormQueryIng.py
u  
    SELECT 
        -- 🟦 Identificadores principales
        Q_SpQ_QuotationTasks.TaskID                               AS [NroTarea],
        Q_SpQ_QuotationTasks.Status                               AS [Estatus],
        Q_OpportunityCRM.CRM_OpportunityNumber                   AS [Oportunidad],
        Q_QuotationType.FrontES                                   AS [TipoFormulario],

        -- 🟩 Fechas clave (Con FORMATO)
        Q_SpQ_FormsHead.FormID                                 AS [NroFormulario],
        FORMAT(Q_SpQ_FormsHead.CreatedAt, 'dd-MM-yyyy HH:mm')      AS [FechaCreacion],

        -- 🟨 Información del cliente (CRM)
        Q_OpportunityCRM.CRM_ContactName                          AS [Contacto],

        -- 🟧 Información del vendedor (desde Profiles)
        Profiles.FirstName + ' ' + 
        ISNULL(Profiles.MiddleName, '') + ' ' + 
        Profiles.LastName + ' ' + 
        ISNULL(Profiles.SecondLastName, '')                       AS [Vendedor],
        Profiles.Email                                            AS [CorreoVendedor],
        Profiles.ContactPhone                                     AS [TelefonoVendedor],

        -- 🟥 Información de costeo (puede estar vacía si no se ha generado)
        Q_CostingHead.CostingNum                                  AS [NroCosteo],
        Q_CostingHead.CaseCost                                    AS [CasoCosteo],
        Q_CostingHead.DirectCost                                  AS [CostoDirecto],
        Q_CostingHead.IndirectPercent                             AS [PctIndirecto],
        Q_CostingHead.IndirectAmount                              AS [MontoIndirecto],
        Q_CostingHead.FinancePercent                              AS [PctFinanciamiento],
        Q_CostingHead.FinanceAmount                               AS [MontoFinanciamiento],
        Q_CostingHead.UtilityPercent                              AS [PctUtilidad],
        Q_CostingHead.UtilityAmount                               AS [MontoUtilidad],
        Q_CostingHead.OperationPercent                            AS [PctGtosOp],
        Q_CostingHead.OperationAmount                             AS [MontoGtosOp],
        Q_CostingHead.OvercostFactor                              AS [FactorSobrecosto],
        Q_CostingHead.SalePriceMin                                AS [PrecioVentaMin],
        Q_CostingHead.DiscountMaxPercent                          AS [PctDescMax],
        Q_CostingHead.DiscountMaxAmount                           AS [MontoDescuento],
        Q_CostingHead.SalePriceList                               AS [PrecioLista],

        -- 🟪 Tiempo de ejecución formateado
        LTRIM(RTRIM(CONCAT(
            ISNULL(CAST(Q_CostingHead.RunTimeNumber AS NVARCHAR(10)), ''),
            ' ',
            ISNULL(Q_CostingHead.RunTimeType, '')
        ))) AS [TiempoEjecucion]

    FROM Q_SpQ_QuotationTasks
    INNER JOIN Q_SpQ_FormsHead
        ON Q_SpQ_QuotationTasks.FormID = Q_SpQ_FormsHead.FormID
    INNER JOIN Q_QuotationType
        ON Q_SpQ_FormsHead.QuotationTypeID = Q_QuotationType.QuotationTypeID
    INNER JOIN Q_OpportunityCRM
        ON Q_SpQ_QuotationTasks.CRM_OpportunityID = Q_OpportunityCRM.CRM_OpportunityID
    LEFT JOIN Q_CostingHead
        ON Q_SpQ_QuotationTasks.CostingID = Q_CostingHead.CostingID
    LEFT JOIN Profiles
        ON Q_SpQ_QuotationTasks.SellerUserID = Profiles.UserID
    WHERE 
        Q_SpQ_QuotationTasks.Active = 1
    ORDER BY 
        Q_SpQ_QuotationTasks.Status,
        Q_SpQ_FormsHead.CreatedAt DESC;
    Nu@   Error SQL: No se pudo establecer conexión con la base de datos.r   z8[Error SQL pyodbc] Error obteniendo la lista de tareas: z,[Error inesperado] Error en get_tasks_list: )r   printclosecursorexecutedescriptionfetchalldictzippyodbcError	Exception)queryconnr   columncolumnsrowresultses           ~C:\Users\victor.barrera\Documents\proyectos\elepV3\Elep\src\Consultas_SQL\Operaciones\Ingenieria\Cotiz\CotizFormQueryIngSQL.pyget_tasks_listr      sG   @ED DTU. JJL + u ,2+=+=>+=!9+=> 7=oo6GH6Gs4G)*6GH JJL ! ? I << HLM	 JJL   <QC@A	JJL 
 JJL sj   C /C +C9C C+C 
C E%D4E5E EE.E/E EE E.)Consultas_SQL.conexionr   r   r        r   <module>r      s    2 fr   