ALTER 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 	 DECIMAL(11,4) 
	DECLARE @C_SALIDAS 	 DECIMAL(11,4)
	DECLARE @V_INGRESOS 	 DECIMAL(13,7) 
	DECLARE @V_SALIDAS 	 DECIMAL(13,7)
	DECLARE @C_SALDO 	 DECIMAL(11,4)
	DECLARE @V_SALDO 	 DECIMAL(13,7)
	DECLARE @COSTUNIT 	 DECIMAL(10,6) 
	DECLARE @COSTPROM 	 DECIMAL(10,6) 
	DECLARE @V_COSTPROM 	 DECIMAL(10,6) 
	DECLARE @C_ACTUAL 	 DECIMAL(11,4)
	DECLARE @V_ACTUAL 	 DECIMAL(13,7)
	DECLARE @VSALIDA	 DECIMAL(13,7)
	DECLARE @VINGRESO 	 DECIMAL(13,7)
	DECLARE @COSTPROMSLDINI  DECIMAL(10,6)
	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 @COSTPROM  = 0
	SET @V_COSTPROM = 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],
		STDCOSTPEN AS COSTUNIT,
		STDCOSTPEN AS COSTPROM, 
		(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 COSTPROM,
		0 AS [V_INGRESOS],
		0 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],
		STDCOSTPEN AS COSTUNIT,
		STDCOSTPEN AS COSTPROM,
		0 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,TYPEDOCID,DOCTYPEID,REFNBR
	---------------------------------------------------------

	--CALCULANDO Y ACTUALIZANDO LOS SALDOS DE LOS MOVIMIENTOS
	DECLARE  MICURSOR CURSOR FOR
		SELECT 	TYPEDOCID,
			BATNBR, 
			INTRDATE,
			INVTIDSKU,
			C_INGRESOS, 
			C_SALIDAS,
			C_ACTUAL,
			COSTPROM,
			V_INGRESOS,
			V_SALIDAS,
			V_ACTUAL
		FROM TEMPORAL_ANALISIS_MOVIMIENTO 
		ORDER BY INTRDATE,TYPEDOCID,DOCTYPEID,REFNBR

	OPEN MICURSOR
	FETCH NEXT FROM MICURSOR INTO @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @C_ACTUAL, @COSTPROM, @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

				UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
			       	       C_ACTUAL  = @C_SALDO,
				       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			

				SET @V_COSTPROM     = @COSTPROM
				SET @COSTPROMSLDINI = @COSTPROM
			   END			

			IF @TYPEDOCID = '101'	-----INGRESOS
		   	   BEGIN				
			   	IF @C_INGRESOS > 0 AND @C_SALIDAS =  0				 	
			           BEGIN
					----------------------------CALCULANDO SALDO CANTIDADES DE INGRESOS----------------------------
					SET @V_COSTPROM = @COSTPROM
					SET @C_SALDO = @C_SALDO + @C_INGRESOS  --CANTIDADES
					SET @V_SALDO = @V_SALDO + @V_INGRESOS  --MONTOS

					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 
				       	       C_ACTUAL  = @C_SALDO,
					       COSTPROM  = @V_COSTPROM 
					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 @C_INGRESOS > 0 AND @V_SALIDAS =  0
				   BEGIN	
					------------------------------CALCULANDO VALOR DE INGRESOS------------------------------
					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET   
					       V_INGRESOS = C_INGRESOS * COSTPROM						
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

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

					---------------------CALCULANDO COSTO UNITARIO Y MONTO SALDOS DE CANTIDADES---------------------
					SET @VINGRESO = (@C_INGRESOS * @V_COSTPROM)
					SET @V_SALDO = (@V_SALDO + @VINGRESO) - @V_INGRESOS

					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 					       
					       COSTUNIT = V_INGRESOS / C_INGRESOS,
					       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
				   SET @V_COSTPROM = @V_COSTPROM
			   END	


			IF @TYPEDOCID <> '104'	-----SALIDAS
		   	   BEGIN								
				IF @C_INGRESOS = 0  AND @C_SALIDAS > 0
				   BEGIN
					----------------------------CALCULANDO SALDO CANTIDADES DE SALIDAS----------------------------
					--PRINT 'COSTO PROMEDIO DE SALDO INICIAL : ' + CONVERT(VARCHAR(20),@COSTPROMSLDINI)
					--PRINT 'COSTO PROMEDIO SALIDAS : ' + CONVERT(VARCHAR(20),@V_COSTPROM)

					IF @COSTPROMSLDINI <> @V_COSTPROM 					
					   BEGIN
						SET @C_SALDO = @C_SALDO - @C_SALIDAS  --CANTIDADES
						SET @V_SALDO = @V_SALDO - @V_SALIDAS  --MONTOS
						
						UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
					       	       C_ACTUAL  = @C_SALDO,
						       COSTPROM  = @V_COSTPROM	
						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
					ELSE
					   BEGIN
						SET @C_SALDO = @C_SALDO - @C_SALIDAS  --CANTIDADES
						SET @V_SALDO = @V_SALDO - @V_SALIDAS  --MONTOS
						
						UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
					       	       C_ACTUAL  = @C_SALDO,
						       COSTPROM  = @COSTPROMSLDINI	
						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 @V_INGRESOS = 0  AND @C_SALIDAS > 0
				   BEGIN	
					------------------------------CALCULANDO VALOR DE SALIDAS------------------------------
					UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 
					       V_SALIDAS = C_SALIDAS * COSTPROM						
					WHERE  BATNBR    = @BATNBR    AND
				      	       INVTIDSKU = @INVTIDSKU

					IF (@@ERROR!=0)
			     	      	    BEGIN
				           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
				           	ROLLBACK TRAN
			             		---RETURN(1)
	    			     	    END
					------------------------------CALCULANDO COSTO UNITARIO Y MONTO SALDOS DE SALIDAS------------------------------
					IF @COSTPROMSLDINI <> @V_COSTPROM 					
					   BEGIN
						SET @VSALIDA = (@C_SALIDAS * @V_COSTPROM)
						SET @V_SALDO = (@V_SALDO - @VSALIDA) + @V_SALIDAS	
						
						UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 					       
						       V_ACTUAL  = @V_SALDO, 
						       COSTUNIT = V_ACTUAL / C_ACTUAL
						WHERE  BATNBR    = @BATNBR    AND
					      	       INVTIDSKU = @INVTIDSKU
	
						IF (@@ERROR!=0)
				     	      	    BEGIN
					           	RAISERROR  20000 'No se puede actualizar TEMPORAL_ANALISIS_MOVIMIENTO'
					           	ROLLBACK TRAN
				             		---RETURN(1)
		    			     	    END
						--------------------------------------------------------------------
						UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 
						       COSTUNIT = V_ACTUAL / C_ACTUAL
						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
					ELSE
					    BEGIN
						SET @VSALIDA = (@C_SALIDAS * @COSTPROMSLDINI)
						SET @V_SALDO = (@V_SALDO - @VSALIDA) + @V_SALIDAS	
						
						UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET 					       
						       COSTUNIT = V_SALIDAS / C_SALIDAS,
						       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
				   SET @V_COSTPROM = @V_COSTPROM		
			   END


			FETCH NEXT FROM MICURSOR INTO @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @C_ACTUAL, @COSTPROM, @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,TYPEDOCID,DOCTYPEID,REFNBR

	COMMIT TRAN
	SET NOCOUNT OFF
END
