DROP PROCEDURE Copiar_Temporal_A_OP_DEPOSITSITEDETProc
GO
CREATE PROCEDURE Copiar_Temporal_A_OP_DEPOSITSITEDETProc
(	@BATNBRDEPOSITSITE	UD_CORRELATIVOS,
	@SITEID		UD_IDSITE,
	@TOTALPEN	UD_MONTO,
	@TOTALUSD	UD_MONTO,
	@CORRNBR	INT
)
AS
BEGIN
	BEGIN TRAN

	DELETE 
	FROM OP_DEPOSITSITEDET 	
	WHERE BATNBRDEPOSITSITE=@BATNBRDEPOSITSITE AND SITEID=@SITEID 

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Eliminando en OP_DEPOSITSITEDET'
		RETURN(1)
	END


        INSERT 	INTO 	OP_DEPOSITSITEDET(SITEID,BATNBRDEPOSITSITE,DATEPROCE,SEQUENCE,CUSTIDSS,DEPOSITPASSWORD,DATEDEPOSIT,CURYID,CURYRATE,TOTALPEN,TOTALUSD,DEBEHABER,STATUS,USERID)
	SELECT 	SITEID,@BATNBRDEPOSITSITE,DATEPROCE,SEQUENCE,CUSTIDSS,DEPOSITPASSWORD,DATEDEPOSIT,CURYID,CURYRATE,TOTALPEN,TOTALUSD,DEBEHABER,STATUS,USERID
	FROM	TMP_OP_DEPOSITSITEDET
	WHERE CORRNBR=@CORRNBR AND SITEID=@SITEID 

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Insertando en TMP_OP_DEPOSITSITEDET'
		RETURN(1)
	END

	UPDATE 	OP_DEPOSITSITE
	SET	TOTALPEN=@TOTALPEN,
		TOTALUSD=@TOTALUSD
	WHERE	BATNBRDEPOSITSITE=@BATNBRDEPOSITSITE AND SITEID=@SITEID

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Actualizando en OP_DEPOSITSITE'
		RETURN(1)
	END

	COMMIT TRAN 
	RETURN(0)
END 

