IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'dbo.CPJ_Cbtes_Reten_Agregar') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE dbo.CPJ_Cbtes_Reten_Agregar
END
GO

CREATE PROCEDURE dbo.CPJ_Cbtes_Reten_Agregar	@pJson VARCHAR(8000)
AS
SET NOCOUNT ON

/*Variables recibidas por parametro a traves del JSON*/
DECLARE @pFlagUsarTrans		     	TINYINT
DECLARE @pModoDebug			    	TINYINT
DECLARE @pId_Usuario			    SMALLINT
DECLARE @pId_Trans					INT
DECLARE @pNro						DECIMAL(16,0)
DECLARE @pId_Tipo_Reten				VARCHAR(10)
DECLARE @pImporte					DECIMAL(13,2)
DECLARE @pObservaciones				VARCHAR(8000)
DECLARE @pImp_Pago_Acum				DECIMAL(12,2)
DECLARE @pImp_No_Imponible			DECIMAL(12,2)
DECLARE @pImp_Base_Imponible		DECIMAL(12,2)
DECLARE @pPorc						DECIMAL(7,4)
DECLARE @pImp_Determinado			DECIMAL(12,2)
DECLARE @pImp_Reten_Ant				DECIMAL(12,2)
DECLARE @pFecha						DATETIME

SET @pFlagUsarTrans				= ISNULL(CONVERT(TINYINT, dbo.CF_JSON_VALUE(@pJson,'FlagUsarTrans')), 0)
SET @pModoDebug					= ISNULL(CONVERT(TINYINT, dbo.CF_JSON_VALUE(@pJson,'ModoDebug')), 0)
SET @pId_Usuario				= ISNULL(CONVERT(SMALLINT, dbo.CF_JSON_VALUE(@pJson,'Id_Usuario')), 0)
SET @pId_Trans					= ISNULL(CONVERT(INT, dbo.CF_JSON_VALUE(@pJson,'Id_Trans')), 0)
SET @pNro						= ISNULL(CONVERT(DECIMAL(16,0), dbo.CF_JSON_VALUE(@pJson,'Nro')), 0)
SET @pId_Tipo_Reten				= ISNULL(CONVERT(VARCHAR(10), dbo.CF_JSON_VALUE(@pJson,'Id_Tipo_Reten')), '')
SET @pImporte					= ISNULL(CONVERT(DECIMAL(13,2), dbo.CF_JSON_VALUE(@pJson,'Importe')), 0)
SET @pObservaciones				= ISNULL(CONVERT(VARCHAR(8000), dbo.CF_JSON_VALUE(@pJson,'Observaciones')), '')
SET @pImp_Pago_Acum				= ISNULL(CONVERT(DECIMAL(12,2), dbo.CF_JSON_VALUE(@pJson,'Imp_Pago_Acum')), 0)
SET @pImp_No_Imponible			= ISNULL(CONVERT(DECIMAL(12,2), dbo.CF_JSON_VALUE(@pJson,'Imp_No_Imponible')), 0)
SET @pImp_Base_Imponible		= ISNULL(CONVERT(DECIMAL(12,2), dbo.CF_JSON_VALUE(@pJson,'Imp_Base_Imponible')), 0)
SET @pPorc						= ISNULL(CONVERT(DECIMAL(7,4), dbo.CF_JSON_VALUE(@pJson,'Porc')), 0)
SET @pImp_Determinado			= ISNULL(CONVERT(DECIMAL(12,2), dbo.CF_JSON_VALUE(@pJson,'Imp_Determinado')), 0)
SET @pImp_Reten_Ant				= ISNULL(CONVERT(DECIMAL(12,2), dbo.CF_JSON_VALUE(@pJson,'Imp_Reten_Ant')), 0)
SET @pFecha						= ISNULL(CONVERT(DATETIME, dbo.CF_JSON_VALUE(@pJson,'Fecha')), '19000101')
/*FIN Variables recibidas por parametro a traves del JSON*/

/*Variables Locales*/

/*FIN Variables Locales*/

/*Variable de Retorno del Stored*/
DECLARE @rtaErrorStored			INT
DECLARE @rtaMsjStored			VARCHAR(4000)
/*FIN Variable de Retorono del Stored*/

BEGIN TRY
	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Ingreso a Stored Procedure: CPJ_Cbtes_Reten_Agregar'
		PRINT 'Parametros: '
		PRINT '@pJson: ' + @pJson
	END

	/*=============================================================================================================================*/

	IF(@pId_Usuario = 0)
	BEGIN
		SET @pId_Usuario = -1
	END

	IF(@pId_Trans = 0)											RAISERROR ('Id_Trans can not be zero.', 16,  1) 
	IF(@pNro = 0)												RAISERROR ('Nro can not be zero.', 16,  1) 
	IF(@pId_Tipo_Reten = '')									RAISERROR ('Id_Tipo_Reten can not be zero.', 16,  1) 
	IF(@pFecha = '19000101')									RAISERROR ('Fecha can not be blank.', 16,  1)
	
	/*=============================================================================================================================*/
	
	INSERT INTO Cbtes_Reten
	(Id_Trans,				Nro,					Id_Tipo_Reten,
	Importe,				Observaciones,			Imp_Pago_Acum,
	Imp_No_Imponible,		Imp_Base_Imponible,		Porc,
	Imp_Determinado,		Imp_Reten_Ant,			Fecha)
	VALUES
	(@pId_Trans,			@pNro,					@pId_Tipo_Reten,
	@pImporte,				@pObservaciones,		@pImp_Pago_Acum,
	@pImp_No_Imponible,		@pImp_Base_Imponible,	@pPorc,
	@pImp_Determinado,		@pImp_Reten_Ant,		@pFecha)
	
	/*===================================================================*/
	
	IF @pFlagUsarTrans <> 0 
	BEGIN
		COMMIT
	END

	/*===================================================================*/	

	SET @rtaMsjStored		=	'{' +
								dbo.CF_Json_ParsearVariable('id_trans', @pId_Trans, 'INT')  + ',' +
								dbo.CF_Json_ParsearVariable('nro',@pNro, 'DECIMAL(16,0)')  + ',' +
								dbo.CF_Json_ParsearVariable('id_tipo_reten', @pId_Tipo_Reten, 'VARCHAR(10)') + 
								'}'

	SELECT	 0				AS ErrorNumber 
			,0				AS ErrorSeverity  
			,0				AS ErrorState 
			,0				AS ErrorProcedure  
			,0				AS ErrorLine		
			,@rtaMsjStored	AS ErrorMessage

	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Fin Stored Procedure: CPJ_Cbtes_Reten_Agregar'
	END
END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo

	RETURN 9999
END CATCH

RETURN 0

/*
EXEC dbo.CPJ_Cbtes_Reten_Agregar		'{"FlagUsarTrans":"0",
										"ModoDebug":"0",
										"Id_Usuario":"-1",
										"Id_Trans":"1203931",
										"Nro":"1",
										"Id_Tipo_Reten":"PERIBRU",
										"Importe":"1421.28",
										"Observaciones":"",
										"Imp_Pago_Acum":"0",
										"Imp_No_Imponible":"0",
										"Porc":"0",
										"Imp_Determinado":"0",
										"Imp_Reten_Ant":"0",
										"Fecha":"2021-04-20T00:00:00"
										}'
*/