SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


ALTER  PROC RESU_TIENDA_CIERREZ_PROC
( 
  @DATEPROCE	UD_FECHA,
  @PTOVTA	UD_PTOVTA,
  @RESUMEN	INT
)
AS
BEGIN

	DECLARE @DOCTYPEID    UD_DOCTYPEID
	SELECT @DOCTYPEID=DOCTYPEIDTICKET FROM OP_SETUP

	IF (@RESUMEN=1) -- POR CATEGORIA
	BEGIN 
		SELECT I.CLASSID,CA.DESCR,SUM(D.TOTALPEN) AS TOTALPEN  ,SUM(D.QTY) AS CANTIDAD
		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 IN_INVENTORY AS I
		ON D.INVTIDSKU=I.INVTIDSKU) JOIN IN_CATEGORY AS CA
		ON I.CLASSID=CA.CLASSID
        	WHERE C.DATEPROCESALES=@DATEPROCE AND C.SALESPOINT=@PTOVTA AND C.DOCTYPEID=@DOCTYPEID AND D.DOCSTATUSID<>'AN'
		GROUP BY I.CLASSID,CA.DESCR
		ORDER BY I.CLASSID
	END 

	IF (@RESUMEN=2) -- POR PRODUCTO
	BEGIN 
		SELECT D.INVTIDSKU,D.DESCRINVENTORY,SUM(D.TOTALPEN) AS TOTALPEN  ,SUM(D.QTY) AS CANTIDAD
		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
        	WHERE C.DATEPROCESALES=@DATEPROCE AND C.SALESPOINT=@PTOVTA AND C.DOCTYPEID=@DOCTYPEID AND D.DOCSTATUSID<>'AN'
		GROUP BY INVTIDSKU,DESCRINVENTORY
		ORDER BY INVTIDSKU
	END

	IF (@RESUMEN=3) -- POR VENDEDOR
	BEGIN 
		SELECT C.SALESPERID,V.SALESPERNAME,SUM(D.TOTALPEN) AS TOTALPEN  ,SUM(D.QTY) AS CANTIDAD
		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 OP_SALESPERSON AS V
		ON C.SALESPERID=V.SALESPERID 
        	WHERE C.DATEPROCESALES=@DATEPROCE AND C.SALESPOINT=@PTOVTA AND C.DOCTYPEID=@DOCTYPEID AND D.DOCSTATUSID<>'AN'
		GROUP BY C.SALESPERID,V.SALESPERNAME
		ORDER BY C.SALESPERID
	END 

END 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

