CREATE PROC SP_RPT_VENTAS_CLIENTE_DOC(
@CADENA_CLIENTE VARCHAR(255) = '20530537855', --PROBANDO CON AVICOLA PATTY
@MONEDA VARCHAR(3) = 'PEN',
@FECHA_INICIAL VARCHAR(8)='20081201',
@FECHA_FINAL VARCHAR(8) = '20081231',
@TODOSESTADO INT =0
)
AS
DECLARE @CUSTIDSS VARCHAR(20)
DECLARE @CADENA CHAR(8000)
DECLARE @CAD CHAR(8000)

--SELECT TOP 1 * FROM CUSTOMER



IF @MONEDA = 'PEN'
BEGIN
	IF @TODOSESTADO = 1
		BEGIN
		SET @CAD="SELECT S.DOCTYPEID,DT.DESCR AS TIPO,DT.MINDES," +
		" CASE LEFT(S.NBRDOCUMENT,3) WHEN '102' THEN '002' + '-' + RIGHT(S.NBRDOCUMENT,7)" +
		" ELSE RIGHT( '000' + LEFT(RIGHT(S.NBRDOCUMENT,10),LEN(S.NBRDOCUMENT) - 7) ,3) + '-' + RIGHT(S.NBRDOCUMENT,7) END " + 
		" AS NRO_DOCUMENTO," +
		" S.NBRDOCUMENT,S.DATEDOC, " +
		"S.SUBTOTALPEN AS VALOR_VENTA," +
		"S.TAXIGVPEN AS IGV, " +
		"S.TOTALPEN  AS TOTAL, " +
		" S.TAXREGNBR AS RUC,S.CUSTNAME AS CLIENTE " +
		" FROM OP_SALES S (NOLOCK) INNER JOIN DOCTYPE DT (NOLOCK) ON S.DOCTYPEID = DT.DOCTYPEID" + 
		" WHERE S.DOCSTATUSID <> 'AN'  AND LEFT(S.NBRDOCUMENT,3) <> '761' AND DT.DOCTYPEID IN ('01','12')" +
		 " AND S.DATEDOC BETWEEN '" + LTRIM(RTRIM(@FECHA_INICIAL)) + "' AND '" + LTRIM(RTRIM(@FECHA_FINAL)) + "'" + 
		 " ORDER BY S.DATEDOC,DT.DESCR,NRO_DOCUMENTO"
		END
	ELSE
		BEGIN
		SET @CAD="SELECT S.DOCTYPEID,DT.DESCR AS TIPO,DT.MINDES," +
		" CASE LEFT(S.NBRDOCUMENT,3) WHEN '102' THEN '002' + '-' + RIGHT(S.NBRDOCUMENT,7)" +
		" ELSE RIGHT( '000' + LEFT(RIGHT(S.NBRDOCUMENT,10),LEN(S.NBRDOCUMENT) - 7) ,3) + '-' + RIGHT(S.NBRDOCUMENT,7) END " + 
		" AS NRO_DOCUMENTO," +
		" S.NBRDOCUMENT,S.DATEDOC, " +
		"S.SUBTOTALPEN AS VALOR_VENTA," +
		"S.TAXIGVPEN AS IGV, " +
		"S.TOTALPEN  AS TOTAL, " +
		" S.TAXREGNBR AS RUC,S.CUSTNAME AS CLIENTE " +
		" FROM OP_SALES S (NOLOCK) INNER JOIN DOCTYPE DT (NOLOCK) ON S.DOCTYPEID = DT.DOCTYPEID" + 
		" WHERE S.DOCSTATUSID <> 'AN'  AND LEFT(S.NBRDOCUMENT,3) <> '761' AND DT.DOCTYPEID IN ('01','12')" +
		 " AND S.DATEDOC BETWEEN '" + LTRIM(RTRIM(@FECHA_INICIAL)) + "' AND '" + LTRIM(RTRIM(@FECHA_FINAL)) + "'" + 
		" AND  S.CUSTIDSS IN (" + LTRIM(RTRIM(@CADENA_CLIENTE)) + ")" +
		 " ORDER BY S.DATEDOC,DT.DESCR,NRO_DOCUMENTO"
		END
END

ELSE
BEGIN
	IF @TODOSESTADO = 1 
	BEGIN
	SET @CAD="SELECT S.DOCTYPEID,DT.DESCR AS TIPO,DT.MINDES," +
	" CASE LEFT(S.NBRDOCUMENT,3) WHEN '102' THEN '002' + '-' + RIGHT(S.NBRDOCUMENT,7)" +
	" ELSE RIGHT( '000' + LEFT(RIGHT(S.NBRDOCUMENT,10),LEN(S.NBRDOCUMENT) - 7) ,3) + '-' + RIGHT(S.NBRDOCUMENT,7) END " + 
	" AS NRO_DOCUMENTO," +
	" S.NBRDOCUMENT,S.DATEDOC, " +
	"S.SUBTOTALUSD AS VALOR_VENTA," +
	"S.TAXIGVUSD AS IGV, " +
	"S.TOTALUSD  AS TOTAL, " +
	" S.TAXREGNBR AS RUC,S.CUSTNAME AS CLIENTE " +
	" FROM OP_SALES S (NOLOCK) INNER JOIN DOCTYPE DT (NOLOCK) ON S.DOCTYPEID = DT.DOCTYPEID" + 
	" WHERE S.DOCSTATUSID <> 'AN'  AND LEFT(S.NBRDOCUMENT,3) <> '761' AND DT.DOCTYPEID IN ('01','12')" +
	 " AND S.DATEDOC BETWEEN '" + LTRIM(RTRIM(@FECHA_INICIAL)) + "' AND '" + LTRIM(RTRIM(@FECHA_FINAL)) + "'" +
	 " ORDER BY S.DATEDOC,DT.DESCR,NRO_DOCUMENTO"
	END
	ELSE
	BEGIN
	SET @CAD="SELECT S.DOCTYPEID,DT.DESCR AS TIPO,DT.MINDES," +
	" CASE LEFT(S.NBRDOCUMENT,3) WHEN '102' THEN '002' + '-' + RIGHT(S.NBRDOCUMENT,7)" +
	" ELSE RIGHT( '000' + LEFT(RIGHT(S.NBRDOCUMENT,10),LEN(S.NBRDOCUMENT) - 7) ,3) + '-' + RIGHT(S.NBRDOCUMENT,7) END " + 
	" AS NRO_DOCUMENTO," +
	" S.NBRDOCUMENT,S.DATEDOC, " +
	"S.SUBTOTALUSD AS VALOR_VENTA," +
	"S.TAXIGVUSD AS IGV, " +
	"S.TOTALUSD  AS TOTAL, " +
	" S.TAXREGNBR AS RUC,S.CUSTNAME AS CLIENTE " +
	" FROM OP_SALES S (NOLOCK) INNER JOIN DOCTYPE DT (NOLOCK) ON S.DOCTYPEID = DT.DOCTYPEID" + 
	" WHERE S.DOCSTATUSID <> 'AN'  AND LEFT(S.NBRDOCUMENT,3) <> '761' AND DT.DOCTYPEID IN ('01','12')" +
	 " AND S.DATEDOC BETWEEN '" + LTRIM(RTRIM(@FECHA_INICIAL)) + "' AND '" + LTRIM(RTRIM(@FECHA_FINAL)) + "'" +
	" AND  S.CUSTIDSS IN (" + LTRIM(RTRIM(@CADENA_CLIENTE)) + ")" +
	 " ORDER BY S.DATEDOC,DT.DESCR,NRO_DOCUMENTO"
	
	END

END

--CONVERT(SMALLDATETIME,'20081201',3)
--SET @CAD = @CAD +   " AND CONVERT(DATETIME,LEFT(S.DATEDOC,11),3) BETWEEN " + CONVERT(SMALLDATETIME,@FECHA_INICIAL,3) + " AND " + CONVERT(SMALLDATETIME,@FECHA_FINAL,3)
--SET @CAD = @CAD +   " AND S.DATEDOC BETWEEN " + LTRIM(RTRIM(@FECHA_INICIAL)) + " AND " + LTRIM(RTRIM(@FECHA_FINAL))
--SET @CADENA = @CADENA +   " AND S.DATEDOC BETWEEN " + CONVERT(DATETIME,LTRIM(RTRIM(@FECHA_INICIAL)),3) + " AND " + CONVERT(DATETIME,LTRIM(RTRIM(@FECHA_FINAL)) ,3)

--IF @TODOSESTADO = 0 SET @CAD = @CAD + " AND  S.CUSTIDSS IN (" + @CADENA_CLIENTE + ")"
--PRINT @CAD

EXEC (@CAD)
GO

