
IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'dbo.SP_GENERAR_REMITO') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE dbo.SP_GENERAR_REMITO_Verminal
END
GO
CREATE PROCEDURE [dbo].SP_GENERAR_REMITO_Verminal @ID_TRANS INT
AS 
DECLARE @Mensaje			VARCHAR(100)
BEGIN TRY
BEGIN TRAN


IF ((SELECT Id_Estado_cbte FROM CBTES WHERE ID_TRANS = @ID_TRANS) = 3) 
	BEGIN
	    SET @Mensaje = 'El cbte no debe estar en Estado Anulado'
		ROLLBACK
		SELECT	 1099		    	AS ErrorNumber 
				,0					AS ErrorSeverity  
				,0					AS ErrorState 
				,0					AS ErrorProcedure  
				,0					AS ErrorLine		
				,@Mensaje			AS ErrorMessage
		RETURN 1
	END

   UPDATE CBTES SET ID_ESTADO_CBTE = 18 WHERE ID_TRANS =  @ID_TRANS

		DECLARE @trans_nueva INTEGER
		DECLARE @NRO_CBTE INTEGER
	    declare @desc varchar(50)
		declare @fecha_mov datetime
		declare @user smallint
		declare @id_cond_vta  smallint
        declare @id_tipo_cbte varchar(5)
		Declare @Id_Estado_Cbte_Rever smallint

		SET @id_cond_vta = (SELECT id_cond_vta from cbtes where id_trans = @ID_TRANS) 
		SET @trans_nueva = (SELECT max(id_trans) from transacciones ) + 1
		SET @desc = (SELECT descripcion from transacciones where id_trans = @ID_TRANS) 
		SET @fecha_mov = (SELECT fecha_mov from transacciones where id_trans = @ID_TRANS) 
		SET @user = (SELECT id_usuario from transacciones where id_trans = @ID_TRANS) 
		SET @id_tipo_cbte = 'REM'

		SET @Id_Estado_Cbte_Rever = (SELECT Isnull(Id_Estado_cbte,1)  From cbtes_Tipos where Id_Tipo_Cbte = @id_tipo_cbte) 
		SET @NRO_CBTE =  (SELECT Prox_Nro  From cbtes_Tipos where Id_Tipo_Cbte = @id_tipo_cbte)  
		Update cbtes_Tipos SET Prox_Nro = @NRO_CBTE + 1 WHERE Id_Tipo_Cbte = @id_tipo_cbte

        ----------------------------- Primero los insert ----------------------------------
        INSERT INTO TRANSACCIONES
			(id_trans,descripcion,fecha_mov,id_usuario)
			values (@trans_nueva,@desc,@fecha_mov,@user)

               SELECT * INTO #CBTES_aux_sp FROM CBTES WHERE ID_TRANS = @ID_TRANS

               SELECT * INTO #CBTES_ITEMS_aux_sp FROM CBTES_ITEMS WHERE ID_TRANS = @ID_TRANS


               -----------------------------Actualizo id_trans -----------------------------------------------

		update #CBTES_aux_sp set id_trans = @trans_nueva , id_tipo_cbte = @id_tipo_cbte, Id_Estado_Cbte = @Id_Estado_Cbte_Rever
		               ,Fecha = Getdate(), Periodo_Iva = Getdate(), Periodo = Getdate(), Fecha_Venc = Getdate()
					   ,Nro_Auto_Trib = '',Cai_Vence = NULL
					   ,Comprobante_Nro = @NRO_CBTE
                       where id_trans = @ID_TRANS

		update #CBTES_ITEMS_aux_sp set id_trans = @trans_nueva  where id_trans = @ID_TRANS

							  
   -----------------------------agrego -----------------------------------------------

               INSERT INTO CBTES
                      SELECT * FROM  #CBTES_aux_sp WHERE ID_TRANS = @trans_nueva    

               INSERT INTO CBTES_ITEMS
                      SELECT * FROM #CBTES_ITEMS_aux_sp WHERE ID_TRANS = @trans_nueva 

   ----------------------------- update -----------------------------------------------
   
       UPDATE CBTES SET ID_ESTADO_CBTE = 16,Remito_Nro = @NRO_CBTE  WHERE ID_TRANS =  @ID_TRANS

   ----------------------------- borro -----------------------------------------------

               DROP TABLE #CBTES_aux_sp 
               DROP TABLE #CBTES_ITEMS_aux_sp 



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

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH;   




-- exec CP_CBTE_ANULAR_REVERSION 37362