DROP PROC Rpt_DocFree_Proc
GO
CREATE PROC Rpt_DocFree_Proc
( @SITEID	UD_IDSITE,
  @DOCUMENTFREEID	CHAR(10)
)
AS
BEGIN
	SELECT D.SITEID,A.NAME,D.DOCUMENTFREEID,D.CUSTIDSS,C.CUSTNAME,D.DATEPROCE,D.FREETOTAL 
	FROM (OP_DOCUMENTFREE AS D JOIN CUSTOMER AS C
	ON D.CUSTIDSS=C.CUSTIDSS) JOIN SITE AS A
	ON D.SITEID=A.SITEID WHERE D.SITEID=@SITEID AND DOCUMENTFREEID=@DOCUMENTFREEID

END


SELECT * FROM SITE

SELECT SITEID DOCUMENTFREEID CUSTIDSS             DATEPROCE                   FREETOTAL FROM OP_DOCUMENTFREE


DROP PROC OP_DOCUMENTFREEInsProc
GO
CREATE PROC OP_DOCUMENTFREEInsProc
( @SITEID	UD_IDSITE,
  @DOCUMENTFREEID	CHAR(10),
  @CUSTIDSS	UD_CODCLIENT,
  @DATEPROCE	UD_FECHA,
  @FREETOTAL	UD_MONTO,
  @USERID	UD_USERID
)
AS
BEGIN

	BEGIN TRAN

	INSERT INTO OP_DOCUMENTFREE(SITEID,DOCUMENTFREEID,CUSTIDSS,DATEPROCE,FREETOTAL,USERID)
	VALUES (@SITEID,@DOCUMENTFREEID,@CUSTIDSS,@DATEPROCE,@FREETOTAL,@USERID)

	IF(@@ERROR!=0)
	BEGIN
	        RAISERROR  20000 'Error Insertando en OP_DOCUMENTFREE'
        	ROLLBACK TRAN
        	RETURN(1)	
	END

	COMMIT TRAN
END


GO

DROP PROC OP_DOCUMENTFREEUpdProc
GO
CREATE PROC OP_DOCUMENTFREEUpdProc
( @SITEID	UD_IDSITE,
  @DOCUMENTFREEID	CHAR(10),
  @CUSTIDSS	UD_CODCLIENT,
  @DATEPROCE	UD_FECHA,
  @FREETOTAL	UD_MONTO,
  @USERID	UD_USERID
)
AS
BEGIN

	BEGIN TRAN

	UPDATE OP_DOCUMENTFREE
	SET 	CUSTIDSS=@CUSTIDSS,
		DATEPROCE=@DATEPROCE,
		FREETOTAL=@FREETOTAL,
		USERID=@USERID
	WHERE SITEID=@SITEID AND DOCUMENTFREEID=@DOCUMENTFREEID

	IF(@@ERROR!=0)
	BEGIN
	        RAISERROR  20000 'Error Actualizando en OP_DOCUMENTFREE'
        	ROLLBACK TRAN
        	RETURN(1)	
	END

	COMMIT TRAN
END

DROP PROC OP_DOCUMENTFREEDelProc
GO
CREATE PROC OP_DOCUMENTFREEDelProc
( @SITEID	UD_IDSITE,
  @DOCUMENTFREEID	CHAR(10)
)
AS
BEGIN

	BEGIN TRAN

	DELETE  OP_DOCUMENTFREE
	WHERE SITEID=@SITEID AND DOCUMENTFREEID=@DOCUMENTFREEID

	IF(@@ERROR!=0)
	BEGIN
	        RAISERROR  20000 'Error Eliminando en OP_DOCUMENTFREE'
        	ROLLBACK TRAN
        	RETURN(1)	
	END

	COMMIT TRAN
END

SELECT * FROM   OP_DOCUMENTFREE
