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


CREATE PROCEDURE [dbo].[CP_CtrlPresup_Generar] 
@Fecha		CHAR(8),
@Id_Centro_Costo CHAR(10),
@Importe	DECIMAL(16,2)
AS 

DECLARE @Id_Tipo_Cbte	CHAR(4)
DECLARE @Nombre			VARCHAR(100)
DECLARE @NRO_CBTE		INT
DECLARE @trans_nueva	INT
DECLARE @Mensaje		VARCHAR(100)
DECLARE @Id_Moneda		CHAR(3)

BEGIN TRY
	BEGIN TRAN
	SET @Id_Tipo_Cbte = dbo.CF_xParam('Tipo_Cbte_CtrlPresup')
	IF ISNULL(@Id_Tipo_Cbte,'') = ''
	BEGIN
		SELECT	 1001				AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,'Parámetro Tipo_Cbte_CtrlPresup sin definir'		AS ErrorMessage
		ROLLBACK
		RETURN
	END


	SET @Id_Moneda    = dbo.CF_xParam('Moneda_Nacional')
	IF ISNULL(@Id_Tipo_Cbte,'') = ''
	BEGIN
		SELECT	 1002				AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,'Parámetro Moneda_Nacional sin definir'		AS ErrorMessage
		ROLLBACK
		RETURN
	END


	----------------------------------------Anulo lo anterior
	UPDATE Cbtes SET Id_Estado_Cbte = 3 WHERE Id_Centro_Costo = @Id_Centro_Costo
										AND DATEPART(yyyy,Fecha) =  CAST(SUBSTRING(@Fecha,1,4) AS INT)
										AND DATEPART(mm,Fecha) =  CAST(SUBSTRING(@Fecha,5,2) AS INT)
										AND Id_Tipo_Cbte = @Id_Tipo_Cbte
    ----------------------------------------------------------------------------------


	SET @Nombre = (SELECT rtrim(Descripcion) FROM Centro_Costos WHERE Id_Centro_Costo = @Id_Centro_Costo)

	SET @trans_nueva = (SELECT max(id_trans) from transacciones ) + 1

	/*===================================================================*/
	INSERT INTO Transacciones	(id_trans,		descripcion,	fecha_mov) 
					VALUES		(@trans_nueva,	@Nombre,			GETDATE())

	/*===================================================================*/
	SET @NRO_CBTE = (SELECT Prox_Nro from Cbtes_Tipos WHERE Id_Tipo_Cbte = @Id_Tipo_Cbte) 

	UPDATE Cbtes_Tipos SET Prox_Nro = Prox_Nro+1 WHERE Id_Tipo_Cbte = @Id_Tipo_Cbte

	/*===================================================================*/
	INSERT INTO Cbtes	(Id_Cond_Vta,		Id_trans,		Id_Sucursal,
						Cbte_Grupo,			Fecha_Venc ,    id_cliente,
						id_estado_Cbte,		id_tipo_cbte,	comprobante_nro,
						fecha,				periodo_iva,	periodo,
						imp_total,			saldo,			imp_gravado,
						destinatario,		lote,			ID_MONEDA,
						Id_Cond_Pago,		Cotizacion,		Id_Centro_Costo)
			   VALUES	('',				@trans_nueva,	1,
						0,					@FECHA,			0,
						0,					@Id_Tipo_cbte,	@NRO_CBTE,
						@FECHA,				@FECHA,			@FECHA,
						@Importe,			0,				@Importe,
						@NOMBRE,			'',				@Id_Moneda,
						10,					1,				@Id_Centro_Costo)

	/*===================================================================*/
	INSERT INTO Clientes_CtaCte (Id_Trans,		Nro_Cuota,		Id_Empresa,
								Id_Cliente,		Fecha,			Fecha_Vence,
								Descripcion,	Importe,		Id_Estado_Cbte,
								Observacion,	Id_Trans_Origen,Nro_Cuota_Origen,
								Id_Moneda,		Saldo,			Nro_FacCre)
					VALUES		(@trans_nueva,	1,				NULL,
								0,	@Fecha,			@Fecha,
								dbo.CF_Descrip_Cbte(@trans_nueva),@Importe ,1,
								'',				0,				0,
								@Id_Moneda,			0,				0)


	 
	COMMIT

	SELECT	 0				AS ErrorNumber 
			,0				AS ErrorSeverity  
			,0				AS ErrorState 
			,0				AS ErrorProcedure  
			,0				AS ErrorLine		
			,@Mensaje		AS ErrorMessage
			,@trans_nueva	AS Id_Trans

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH;   
