ALTER   PROCEDURE [dbo].[sp_conComprobante_ListarDetalles]
(
@fecini AS DATE = '01-01-1900',
@fecfin AS DATE = '01-01-2050',
@clasificacion AS INT = 0,
@origen AS CHAR(3) = 'ALL', --EQUIVALE AL CAMPO SISTEMA 
@cuenta AS VARCHAR(10) = '',
@ctacte AS INT = 0,
@unineg AS INT = 0,
@item AS INT = 0
)
AS
     DECLARE @tipfol INT = (SELECT  ISNULL(foliacion,0) FROM conParametros)
    
     SELECT top(800000)
			YEAR(C.fecha) AS periodo, 

			CASE C.tipo
			WHEN 1 THEN 'INGR' 
			WHEN 2 THEN 'EGRE'
			WHEN 3 THEN 'TRAS'
			ELSE 'N/A'
			END AS tipo, 

			C.numero,
			CONVERT(VARCHAR(10), C.fecha, 103) as fecha, 
			
			CASE C.clasificacion
			WHEN 1 THEN 'GRAL'
			WHEN 2 THEN 'IFRS'
			WHEN 3 THEN 'TRIB'
			ELSE 'N/A'
			END AS clasificacion,
		
			CASE C.glosa
			WHEN '' THEN 'N/A'
			ELSE C.glosa
			END AS comentario,
			
			C.sistema AS origen,
			C.linea,  
			C.cuenta, 
            ISNULL(P.descripcion, 'S/D') AS descripcion, 
            C.debe, 
            C.haber, 
            
			CASE C.glosa_linea
			WHEN '' THEN 'N/A'
			ELSE C.glosa_linea
			END AS glosa_linea,
			
            CONVERT(VARCHAR,C.uni_negocio) + '-' + ISNULL(conUnidadNegocio.descripcion, 'S/D') AS unineg, 
			CONVERT(VARCHAR, C.item) + '-' + ISNULL(conItem.descripcion, 'S/D') AS item, 
            C.ctacte,
			conCtacte.digcte,
			conCtacte.nomcte,
			C.num_docto,
			COALESCE(venDocumentosSii.Desdocsii, 'N/A') AS tipo_docto,
			CONVERT(VARCHAR,C.vencimiento,103) AS vencimiento
     FROM conComprobantes AS C
          INNER JOIN conPlanCuentas AS P ON C.cuenta = P.cuenta AND C.periodo = P.periodo
          LEFT OUTER JOIN conItem ON C.item = conItem.codigo
          LEFT OUTER JOIN conUnidadNegocio ON C.uni_negocio = conUnidadNegocio.idUniNeg
		  LEFT OUTER JOIN venDocumentossii ON C.tipo_docto=venDocumentosSii.codsii
          LEFT OUTER JOIN conCtacte ON C.ctacte = conCtacte.rutcte
   WHERE 
		(C.Fecha BETWEEN @fecini AND @fecfin)
	AND (C.clasificacion = @clasificacion OR @clasificacion=0 )
	AND (C.sistema = @origen OR @origen = 'ALL')
	AND (C.cuenta = @cuenta OR @cuenta = '')
	AND (C.ctacte = @ctacte OR @ctacte = 0)
	AND (C.uni_negocio = @unineg OR @unineg = 0)
	AND (C.item = @item OR @item = 0)

    ORDER BY C.fecha desc;