CREATE PROCEDURE sp_in_actualiza_AnalisisMovi_Articulos
	( 
		@COD_ARTICULO	UD_IDCORPORATIVO,
		@FECHAINI	CHAR(8),
		@FECHAFIN	CHAR(8)

	)
AS
BEGIN
	SET NOCOUNT ON
	DECLARE @ACT_CODARTICULO UD_IDCORPORATIVO
	DECLARE @ACT_DESCRIPCION UD_DESCR_INVENT
	DECLARE @ACT_UNIDAD	 UD_DESCRIPCION
	
	DECLARE @TYPEDOCID 	 UD_TYPEDOC
	DECLARE @BATNBR 	 CHAR (10)
	DECLARE @INTRDATE 	 SMALLDATETIME
	DECLARE @INVTIDSKU 	 UD_IDCORPORATIVO
	DECLARE @C_INGRESOS 	 NUMERIC(10, 3)
	DECLARE @C_SALIDAS 	 NUMERIC(10, 3)
	DECLARE @V_INGRESOS 	 UD_MONTO 
	DECLARE @V_SALIDAS 	 UD_MONTO 
	DECLARE @C_SALDOINICIAL  NUMERIC(10, 3)
	DECLARE @C_SALDO 	 NUMERIC(10, 3)
	DECLARE @V_SALDO 	 UD_MONTO
	DECLARE @VSALIDA	 UD_MONTO
	DECLARE @VINGRESO 	 UD_MONTO
	DECLARE @COSTUNIT 	 UD_COSTO 
	DECLARE @C_ACTUAL 	 UD_MONTO
	DECLARE @V_ACTUAL 	 UD_MONTO
	DECLARE @DATFECHAINI	 UD_FECHA
 	DECLARE @DATFECHAFIN	 UD_FECHA

	SET @C_SALDO  = 0
	SET @V_SALDO  = 0
	SET @C_ACTUAL = 0
	SET @V_ACTUAL = 0
	SET @VSALIDA   = 0
	SET @VINGRESO  = 0	
	
	SET @DATFECHAINI=CAST(@FECHAINI AS SMALLDATETIME)
	SET @DATFECHAFIN=CAST(@FECHAFIN AS SMALLDATETIME)

	BEGIN TRAN
	--INSERTANDO DATOS EN: TEMPORAL_ANALISIS_MOVIMIENTO
	DELETE FROM TEMPORAL_ANALISIS_MOVIMIENTO
	INSERT INTO TEMPORAL_ANALISIS_MOVIMIENTO
	SELECT DISTINCT	TYPEDOCID,				--------SALDO INICIAL
		BATNBR, 
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATE,
	 	DOCTYPEID, 
		[TIPO_CP]= CASE WHEN DOCTYPEID = '19' AND TYPEDOCID = '104' THEN 'SALDO INICIAL' END,
	 	REFNBR AS NBRDOCUMENT,
		QTY AS C_INGRESOS, 
		0 AS [C_SALIDAS],
		0 AS [C_ACTUAL],
		0 AS COSTUNIT,
		(QTY * STDCOSTPEN) AS [V_INGRESOS],
		0 AS [V_SALIDAS],	
		0 AS [V_ACTUAL]
	FROM  	INTRAMOV
	WHERE TYPEDOCID = '104' AND DOCTYPEID = '19' AND 
	INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN AND 
	INVTIDSKU = @COD_ARTICULO 
	UNION ALL
	SELECT DISTINCT	TYPEDOCID,				--------------SALIDAS
		BATNBR, 
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATE,
	 	DOCTYPEID, 
		[TIPO_CP]= CASE WHEN DOCTYPEID = '01' AND TYPEDOCID = '101' THEN 'FTC'
			   	WHEN DOCTYPEID = '03' AND TYPEDOCID = '101' THEN 'BTC'
				WHEN DOCTYPEID = '01' AND TYPEDOCID = '301' THEN 'FTV'
			   	WHEN DOCTYPEID = '03' AND TYPEDOCID = '301' THEN 'BTV'
			   	WHEN DOCTYPEID = '90' AND TYPEDOCID = '301' THEN 'VLE'	
			   	WHEN DOCTYPEID = '12' AND TYPEDOCID = '301' THEN 'TKV' END,
	 	REFNBR AS NBRDOCUMENT,
		0 AS [C_INGRESOS], 
		(QTY) AS C_SALIDAS,
		0 AS [C_ACTUAL],
		0 AS COSTUNIT,
		0 AS [V_INGRESOS],
		((QTY * STDCOSTPEN)) AS [V_SALIDAS],	
		0 AS [V_ACTUAL]
	FROM  	INTRAMOV
	WHERE SWTINOUT = 0 and TYPEDOCID <> '104' AND  
	INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN AND 
	INVTIDSKU = @COD_ARTICULO 
	UNION ALL
	SELECT DISTINCT	TYPEDOCID,				--------------INGRESOS
		BATNBR,
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATE,
	 	DOCTYPEID, 
		[TIPO_CP]= CASE WHEN DOCTYPEID = '01' AND TYPEDOCID = '101' THEN 'FTC'
			   	WHEN DOCTYPEID = '03' AND TYPEDOCID = '101' THEN 'BTC'
				WHEN DOCTYPEID = '01' AND TYPEDOCID = '301' THEN 'FTV'
			   	WHEN DOCTYPEID = '03' AND TYPEDOCID = '301' THEN 'BTV'
			   	WHEN DOCTYPEID = '90' AND TYPEDOCID = '301' THEN 'VLE'	
			   	WHEN DOCTYPEID = '12' AND TYPEDOCID = '301' THEN 'TKV' END,
	 	REFNBR AS NBRDOCUMENT,
		QTY AS [C_INGRESOS], 
		0 AS C_SALIDAS,
		0 AS [C_ACTUAL],
		0 AS COSTUNIT,
		(QTY * STDCOSTPEN) AS [V_INGRESOS],
		0 AS [V_SALIDAS],	
		0 AS [V_ACTUAL]
	FROM  	INTRAMOV
	WHERE SWTINOUT = 1 and TYPEDOCID <> '104' AND  
	INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN AND 
	INVTIDSKU = @COD_ARTICULO 
	ORDER BY INTRDATE,REFNBR,BATNBR,TYPEDOCID

	--CALCULANDO Y ACTUALIZANDO LOS SALDOS DE LOS MOVIMIENTOS
	DECLARE  MICURSOR CURSOR FOR
		SELECT 	TYPEDOCID,
			BATNBR, 
			INTRDATE,
			INVTIDSKU,
			C_INGRESOS, 
			C_SALIDAS,
			C_ACTUAL,
			V_INGRESOS,
			V_SALIDAS,
			V_ACTUAL
		FROM TEMPORAL_ANALISIS_MOVIMIENTO 
		ORDER BY INTRDATE,REFNBR,BATNBR,TYPEDOCID
	OPEN MICURSOR
	FETCH NEXT FROM MICURSOR INTO @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @C_ACTUAL, @V_INGRESOS, @V_SALIDAS, @V_ACTUAL
	SET @C_SALDO = @C_INGRESOS
	SET @V_SALDO = @V_INGRESOS
	WHILE @@FETCH_STATUS = 0    
		BEGIN	
			--ACTUALIZANDO DESCRIPCION Y UNIDAD DE PRODUCTOS
			SELECT 	@ACT_CODARTICULO = IN_INVENTORY.INVTIDSKU, 
				@ACT_DESCRIPCION = IN_INVENTORY.DESCR, 
				@ACT_UNIDAD = IN_INVENTORYUNITMED.DESCR
			FROM    IN_INVENTORY INNER JOIN IN_INVENTORYUNITMED ON
				IN_INVENTORY.STKUNITID = IN_INVENTORYUNITMED.STKUNITID
			WHERE   IN_INVENTORY.INVTIDSKU = @COD_ARTICULO	
			
			UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
			       DESCRIPCION = @ACT_DESCRIPCION,
			       UNIDAD = @ACT_UNIDAD
		
			IF (@@ERROR!=0)
		     	   BEGIN
		           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
		           	ROLLBACK TRAN
	             		RETURN(1)
    		     	   END			
			
			IF @TYPEDOCID = '104'	-----SALDO INICIAL
			   BEGIN
				SET @C_SALDO = @C_INGRESOS
				SET @V_SALDO = @V_INGRESOS
				SET @COSTUNIT = (@V_SALDO/@C_SALDO)

				UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
			       	       C_ACTUAL  = @C_SALDO,
				       V_ACTUAL  = @V_SALDO,
				       COSTUNIT  = @COSTUNIT
				WHERE  BATNBR    = @BATNBR    AND
			      	       INVTIDSKU = @INVTIDSKU				

				IF (@@ERROR!=0)
			     	   BEGIN
			           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
			           	ROLLBACK TRAN
		             		RETURN(1)
	    		     	   END			
			   END			

			IF @TYPEDOCID <> '104'	-----SALIDAS
		   	   BEGIN								
				IF @C_INGRESOS = 0  AND @C_SALIDAS > 0
				   BEGIN
					-----CALCULANDO SALDO CANTIDADES
					SET @C_SALDO = @C_SALDO - @C_SALIDAS  --CANTIDADES
					SET @V_SALDO = @V_SALDO - @V_SALIDAS  --MONTOS
					SET @COSTUNIT = (@V_SALDO/@C_SALDO)
					SET @VSALIDA = (@C_SALIDAS * @COSTUNIT)

					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
				       	       C_ACTUAL  = @C_SALDO,
					       COSTUNIT  = @COSTUNIT,
					       V_SALIDAS = @VSALIDA	
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

					IF (@@ERROR!=0)
			     	      	    BEGIN
				           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
				           	ROLLBACK TRAN
			             		RETURN(1)
	    			     	    END	
				   END

				IF @V_INGRESOS = 0  AND @V_SALIDAS > 0
				   BEGIN	
					-----CALCULANDO SALDO MONTOS
					SET @V_SALDO = (@V_SALDO - @VSALIDA) + @V_SALIDAS
					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 
					       V_ACTUAL  = @V_SALDO
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

					IF (@@ERROR!=0)
			     	      	    BEGIN
				           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
				           	ROLLBACK TRAN
			             		RETURN(1)
	    			     	    END
				   END
			   END

			IF @TYPEDOCID = '101'	-----INGRESOS
		   	   BEGIN				
			   	IF @C_INGRESOS > 0 AND @C_SALIDAS =  0				 	
			           BEGIN
					-----CALCULANDO SALDO CANTIDADES
					SET @C_SALDO = @C_SALDO + @C_INGRESOS  --CANTIDADES
					SET @V_SALDO = @V_SALDO + @V_INGRESOS  --MONTOS
					SET @COSTUNIT = (@V_SALDO/@C_SALDO)
					SET @VINGRESO = (@C_INGRESOS * @COSTUNIT)

					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
				       	       C_ACTUAL  = @C_SALDO,
					       COSTUNIT  = @COSTUNIT,
					       V_INGRESOS = @VINGRESO	
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

					IF (@@ERROR!=0)
			     	      	    BEGIN
				           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
				           	ROLLBACK TRAN
			             		RETURN(1)
	    			     	    END	
				   END

			   	IF @V_INGRESOS > 0 AND @V_SALIDAS =  0
				   BEGIN	
					-----CALCULANDO SALDO MONTOS
					SET @V_SALDO = (@V_SALDO + @VINGRESO) - @V_INGRESOS
					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 
					       V_ACTUAL  = @V_SALDO
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

					IF (@@ERROR!=0)
			     	      	    BEGIN
				           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
				           	ROLLBACK TRAN
			             		RETURN(1)
	    			     	    END
				   END
			   END	

			FETCH NEXT FROM MICURSOR INTO @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @C_ACTUAL, @V_INGRESOS, @V_SALIDAS, @V_ACTUAL			   
		END
	CLOSE MICURSOR    
	DEALLOCATE MICURSOR
	
	--RECOGIENDO INFORMACION PARA EL REPORTE : TEMPORAL_ANALISIS_MOVIMIENTO
	SELECT 	INVTIDSKU,
		DESCRIPCION,
		UNIDAD,
		CAST(INTRDATE AS SMALLDATETIME) AS FECHA,
		[TIPO_CP], 
		REFNBR AS NUM_CP,
		C_INGRESOS AS [CANT. INGRESO],
		C_SALIDAS,
		C_ACTUAL,
		COSTUNIT,
		V_INGRESOS,
		V_SALIDAS,
		V_ACTUAL
	FROM 	TEMPORAL_ANALISIS_MOVIMIENTO 
	ORDER BY INTRDATE,REFNBR,BATNBR,TYPEDOCID

	COMMIT TRAN
	SET NOCOUNT OFF
END