SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

--DROP procedure sp_registrar
CREATE PROCEDURE sp_registrar_Picking @ID_TRANS INT
      AS 

	  BEGIN TRAN
		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)


						
               ----------------------------- Primero los insert ----------------------------------
		 SET @trans_nueva = (SELECT max(id_trans) from transacciones ) + 1
         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 = 'HPIC'
                       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 

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

               DROP TABLE #CBTES_aux_sp  
 
               DROP TABLE #CBTES_ITEMS_aux_sp  

        IF (@@ERROR = 0)
		     COMMIT TRAN
	     ELSE
		     ROLLBACK
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

