QUERY_TEST = "SELECT 1 AS ok;"

QUERY_SUCURSALES = """
SELECT DISTINCT
    LTRIM(RTRIM(s.NombreSucursal)) AS NombreSucursal
FROM dbo.AA_Sucursales s
WHERE s.NombreSucursal IS NOT NULL
ORDER BY LTRIM(RTRIM(s.NombreSucursal));
"""

QUERY_VENTAS_MINMAX = """
SELECT 
    MIN([Fecha]) AS fecha_min,
    MAX([Fecha]) AS fecha_max
FROM dbo.[RE_RemisionesLinea];
"""

QUERY_FOLIOS_AGG = """
SELECT
    v.Folio,
    v.Estacion,
    MIN(v.Fecha)       AS Fecha,
    MIN(v.HoraCaptura) AS HoraCaptura,
    SUM(CAST(v.Cant AS DECIMAL(18,4)) * CAST(v.PU AS DECIMAL(18,4))) AS ventas_subtotal,

    MIN(v.[Desc])      AS [Desc],
    MIN(v.Impto)       AS Impto,
    MIN(v.ImptoAdic)   AS ImptoAdic,
    MIN(v.Comision)    AS Comision,
    MIN(v.MotivoDev)   AS MotivoDev,

    MAX(s.NombreSucursal) AS NombreSucursal,
    MAX(s.Zona)           AS Zona,
    MAX(s.Ciudad)         AS Ciudad,
    MAX(s.Estado)         AS Estado
FROM dbo.RE_RemisionesLinea v
LEFT JOIN dbo.AA_EstacionesSucursal es ON es.Estacion = v.Estacion
LEFT JOIN dbo.AA_Sucursales s ON s.Sucursal = es.Sucursal
WHERE v.Fecha >= ? AND v.Fecha < ?
GROUP BY v.Folio, v.Estacion
"""

QUERY_LINEAS_SUCURSAL = """
SELECT
    v.Folio,
    v.Estacion,
    v.Fecha,
    v.HoraCaptura,
    CAST(v.Cant AS DECIMAL(18,4)) AS Cant,
    CAST(v.PU   AS DECIMAL(18,4)) AS PU,

    v.Cod,
    p.Descripcion AS Platillo,
    p.Clasif      AS Clasif,
    c.Descripcion AS Clasificacion_Platillo,

    v.[Desc],
    v.Impto,
    v.ImptoAdic,
    v.Comision,
    v.MotivoDev,

    LTRIM(RTRIM(s.NombreSucursal)) AS NombreSucursal
FROM dbo.RE_RemisionesLinea v
LEFT JOIN dbo.RE_Platillos p
    ON p.Cod = v.Cod
LEFT JOIN dbo.RE_Clasificacion c
    ON RIGHT('000' + LTRIM(RTRIM(p.Clasif)), 3) = RIGHT('000' + LTRIM(RTRIM(c.Clasif)), 3)
LEFT JOIN dbo.AA_EstacionesSucursal es
    ON es.Estacion = v.Estacion
LEFT JOIN dbo.AA_Sucursales s
    ON s.Sucursal = es.Sucursal
WHERE v.Fecha >= ? AND v.Fecha < ?
ORDER BY v.Fecha, v.Folio;
"""

QUERY_LINEAS = """
SELECT
    v.Folio,
    v.Estacion,
    v.Fecha,
    v.HoraCaptura,
    CAST(v.Cant AS DECIMAL(18,4)) AS Cant,
    CAST(v.PU   AS DECIMAL(18,4)) AS PU,

    v.Cod,
    p.Descripcion AS Platillo,
    p.Clasif      AS Clasif,

    c.Descripcion AS Clasificacion_Platillo,

    v.[Desc],
    v.Impto,
    v.ImptoAdic,
    v.Comision,
    v.MotivoDev,

    s.NombreSucursal
FROM dbo.RE_RemisionesLinea v
LEFT JOIN dbo.RE_Platillos p
    ON p.Cod = v.Cod
LEFT JOIN dbo.RE_Clasificacion c
    ON RIGHT('000' + LTRIM(RTRIM(p.Clasif)), 3)
     = RIGHT('000' + LTRIM(RTRIM(c.Clasif)), 3)
LEFT JOIN dbo.AA_EstacionesSucursal es
    ON es.Estacion = v.Estacion
LEFT JOIN dbo.AA_Sucursales s
    ON s.Sucursal = es.Sucursal
WHERE v.Fecha >= ? AND v.Fecha < ?
ORDER BY v.Fecha, v.Folio;
"""
