DROP PROCEDURE IN_ActualizaKitComponentProc
GO
CREATE PROCEDURE IN_ActualizaKitComponentProc
(@KITID1	UD_KITID,
 @CORRNBR1	INT
)
AS
BEGIN
	DECLARE	@CORRNBR	INT									
	DECLARE	@KITID          UD_KITID
	DECLARE	@INVTIDSKU      UD_IDCORPORATIVO
	DECLARE	@SEQUENCE       tinyint
	DECLARE	@STDCOSTPEN     UD_COSTO
	DECLARE	@STDCOSTUSD     UD_COSTO
	DECLARE	@QTY            numeric(9, 3)
	
	
	SET @KITID=@KITID1
	SET @CORRNBR=@CORRNBR1

	BEGIN TRAN

	--ELIMINO DATOS DE LA TABLA DETALLE DE FORMULAS (IN_KITCOMPONENT) PARA LUEGO ACTUALIZARLA
	DELETE
	FROM		IN_KITCOMPONENT
	WHERE	KITID=@KITID

	IF (@@error!=0)
    	BEGIN
        	ROLLBACK TRAN
        	RETURN(1) 
	END
	-----------------------------------------------------------------------

	--PROCEDE A INSERTAR LOS DATOS EN LA TABLA DETALLE DE FORMULA	
	DECLARE CUR_KITCOMPONENT CURSOR 
	FOR	SELECT 	KITID, INVTIDSKU, SEQUENCE, STDCOSTPEN, STDCOSTUSD,QTY  
        		FROM   	TMP_IN_KITCOMPONENT
		WHERE 	KITID=@KITID AND CORRNBR=@CORRNBR 

    	OPEN CUR_KITCOMPONENT
    	FETCH NEXT	FROM 	CUR_KITCOMPONENT
		 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
                      		

      	WHILE @@FETCH_STATUS=0
	 BEGIN
	
		INSERT INTO IN_KITCOMPONENT(KITID,INVTIDSKU,SEQUENCE,STDCOSTPEN,STDCOSTUSD,QTY)
    		VALUES(@KITID,@INVTIDSKU,@SEQUENCE,@STDCOSTPEN,@STDCOSTUSD,@QTY)

		IF (@@ERROR!=0)
          		BEGIN       
            			CLOSE CUR_KITCOMPONENT
            			DEALLOCATE CUR_KITCOMPONENT
	    		ROLLBACK
            			RETURN(1)  
          		END

	   	FETCH NEXT	FROM 	CUR_KITCOMPONENT
			 	INTO  	@KITID, @INVTIDSKU, @SEQUENCE, @STDCOSTPEN, @STDCOSTUSD,@QTY
	END

	--ELIMINO DATOS DE LA TABLA TEMPORAL DETALLE DE FORMULAS (TMP_IN_KITCOMPONENT) PARA LUEGO ACTUALIZARLA
	DELETE
	FROM		TMP_IN_KITCOMPONENT
	WHERE	KITID=@KITID

	IF (@@error!=0)
    	BEGIN
        	ROLLBACK TRAN
        	RETURN(1) 
	END


  CLOSE CUR_KITCOMPONENT
  DEALLOCATE CUR_KITCOMPONENT   
  COMMIT TRAN
  RETURN (0)         
END


