DROP PROCEDURE ReporteVentas_Vendedor_Proc
GO
CREATE PROCEDURE ReporteVentas_Vendedor_Proc
(
 @FECHAINI	CHAR(15),
 @FECHAFIN	CHAR(15),
 @MONEDA	UD_CURYID,
 @TODOS_DOC	TINYINT=1,	-- (1) TODOS LOD DOC EN @MONEDA, (0) SOLO LOS DOC EN @MONEDA
 @ALMACENES	VARCHAR(255)='',-- CADENA DE ALMACENES
 @TODOS_ALM	TINYINT=1,--TODOS LOS ALMACENES
 @CRITERIOS	VARCHAR(255)='', --solo los elementos que cumplan la condicion
 @TODOS_CON	TINYINT=1 --TODOS LOS elementos del tipo de condicion
)
AS
BEGIN
   DECLARE  @DATFECHAINI SMALLDATETIME
   DECLARE  @DATFECHAFIN SMALLDATETIME
      
      
--  SET @FECHAINI='20021201'
--  SET @FECHAFIN='20021218'

       
        SET @DATFECHAINI=CAST(RTRIM(LTRIM(@FECHAINI)) AS SMALLDATETIME)
        SET @DATFECHAFIN=CAST(RTRIM(LTRIM(@FECHAFIN)) AS SMALLDATETIME)
         
        

	DECLARE @CADENA	VARCHAR(8000)
	DECLARE @MONEDABASE UD_CURYID	
	
	SET @MONEDABASE=(SELECT CURYORIG FROM SETUP)        --OBTENGO EL CODIGO DE LA MONEDA BASE
  

	IF (@MONEDA=@MONEDABASE) 
		BEGIN
			SET @CADENA="SELECT D.SITEID,S.NAME,C.DATEPROCESALES,C.SALESPERID,V.SALESPERNAME,D.NBRTURN,D.INVTIDSKU,D.DESCRINVENTORY,D.QTY,D.TOTALPEN as Total " +
			    	    "FROM ((OP_SALES AS C JOIN OP_SALESDET AS D " +
			    	    "ON C.DOCTYPEID=D.DOCTYPEID AND C.NBRDOCUMENT=D.NBRDOCUMENT AND C.SITEID=D.SITEID) JOIN SITE AS S " +
			    	    "ON D.SITEID=S.SITEID) JOIN OP_SALESPERSON AS V " +
			    	    "ON C.SALESPERID=V.SALESPERID " +
	        	            "WHERE D.DOCSTATUSID<>'AN' AND D.DATEPROCESALES BETWEEN '" + RTRIM(LTRIM(CAST(@DATFECHAINI AS CHAR(100)))) + "' AND '" + RTRIM(LTRIM(CAST(@DATFECHAFIN AS CHAR(100)))) + "'"
		END
	ELSE
		BEGIN
			SET @CADENA="SELECT D.SITEID,S.NAME,C.DATEPROCESALES,C.SALESPERID,V.SALESPERNAME,D.NBRTURN,D.INVTIDSKU,D.DESCRINVENTORY,D.QTY,D.TOTALUSD as Total " +
			    	    "FROM ((OP_SALES AS C JOIN OP_SALESDET AS D " +
			    	    "ON C.DOCTYPEID=D.DOCTYPEID AND C.NBRDOCUMENT=D.NBRDOCUMENT AND C.SITEID=D.SITEID) JOIN SITE AS S " +
			    	    "ON D.SITEID=S.SITEID) JOIN OP_SALESPERSON AS V " +
			    	    "ON C.SALESPERID=V.SALESPERID " +
	        	            "WHERE D.DOCSTATUSID<>'AN' AND D.DATEPROCESALES BETWEEN '" + RTRIM(LTRIM(CAST(@DATFECHAINI AS CHAR(100)))) + "' AND '" + RTRIM(LTRIM(CAST(@DATFECHAFIN AS CHAR(100)))) + "'"
		END


	IF (@TODOS_ALM=0)
              	SET @CADENA=@CADENA + " AND D.SITEID IN (" + @ALMACENES + ")" 


	IF (@TODOS_CON=0)
		SET @CADENA=@CADENA + " AND C.SALESPERID IN (" + @CRITERIOS + ")"

	--SELECT 	@CADENA		
	EXEC (@CADENA)	


END 
GO


ReporteVentas_Vendedor_Proc '20030326','20030326','PEN'



SELECT D.SITEID,S.NAME,C.DATEPROCESALES,C.SALESPERID,V.SALESPERNAME,D.NBRTURN,D.INVTIDSKU,D.DESCRINVENTORY,D.QTY,D.TOTALPEN,D.TOTALUSD
FROM ((OP_SALES AS C JOIN OP_SALESDET AS D
ON C.DOCTYPEID=D.DOCTYPEID AND C.NBRDOCUMENT=D.NBRDOCUMENT AND C.SITEID=D.SITEID) JOIN SITE AS S
ON D.SITEID=S.SITEID) JOIN OP_SALESPERSON AS V
ON C.SALESPERID=V.SALESPERID
WHERE D.DOCSTATUSID<>'AN' AND D.DATEPROCESALES='20030326' 