ALTER PROCEDURE sp_in_actualiza_AnalisisMovi_Articulos
	( 
		@cod_articulo UD_IDCORPORATIVO,
		@FECHAINI	CHAR(8),
		@FECHAFIN	CHAR(8)

	)
as 
	--permite actualizar los saldos de Analisis de Movimiento de articulos asi como l precio promedio
	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 @COSTUNIT UD_COSTO 

	DECLARE @DATFECHAINI	UD_FECHA
 	DECLARE @DATFECHAFIN	UD_FECHA
	
	SET @DATFECHAINI=CAST(@FECHAINI AS SMALLDATETIME)
	SET @DATFECHAFIN=CAST(@FECHAFIN AS SMALLDATETIME)

	SET @C_INGRESOS = 0
	SET @C_SALIDAS = 0
	SET @V_INGRESOS = 0
	SET @V_SALIDAS = 0
	SET @C_SALDOINICIAL = 0
	SET @C_SALDO = 0
	SET @V_SALDO = 0
	SET @COSTUNIT = 0

	begin tran
	--INSERTANDO DATOS EN: temporal_analisis_Movimiento
	DELETE FROM temporal_analisis_Movimiento
	INSERT INTO temporal_analisis_Movimiento
	SELECT 	TYPEDOCID,
		BATNBR, 
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATEPROCE AS INTRDATE,
	 	DOCTYPEID, 
	 	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 
	INTRDATEPROCE BETWEEN @DATFECHAINI AND @DATFECHAFIN
	union all
	SELECT 	TYPEDOCID,
		BATNBR, 
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATEPROCE AS INTRDATE,
	 	DOCTYPEID, 
	 	REFNBR AS NBRDOCUMENT,
		0 AS [C_INGRESOS], 
		(QTY*-1) AS C_SALIDAS,
		0 AS [C_ACTUAL],
		0 AS COSTUNIT,
		0 AS [V_INGRESOS],
		((QTY * STDCOSTPEN)*-1) AS [V_SALIDAS],	
		0 AS [V_ACTUAL]
	FROM  	INTRAMOV
	WHERE SWTINOUT = 0 and TYPEDOCID <> '104' AND  
	INTRDATEPROCE BETWEEN @DATFECHAINI AND @DATFECHAFIN
	union all
	SELECT 	TYPEDOCID,
		BATNBR,
		INVTIDSKU,
	 	' ' AS [DESCRIPCION],
	 	' ' AS [UNIDAD],
	 	INTRDATEPROCE AS INTRDATE,
	 	DOCTYPEID, 
	 	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  
	INTRDATEPROCE BETWEEN @DATFECHAINI AND @DATFECHAFIN
	--CALCULANDO Y ACTUALIZANDO LOS SALDOS DE LOS MOVIMIENTOS
	declare  micursor cursor for
		select 	TYPEDOCID,
			BATNBR, 
			INTRDATE,
			INVTIDSKU,
			C_INGRESOS, 
			C_SALIDAS,
			V_INGRESOS,
			V_SALIDAS
		from temporal_analisis_Movimiento 
		WHERE INVTIDSKU = @cod_articulo
		ORDER BY INTRDATE
	open micursor
	fetch next from micursor into @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @V_INGRESOS, @V_SALIDAS
	SET @C_SALDO = @C_INGRESOS
	SET @V_SALDO = @V_INGRESOS
	WHILE @@FETCH_STATUS = 0    
		begin	
			--actualizando descrpcion 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
			where  INVTIDSKU = @act_codArticulo
		
			IF (@@error!=0)
		     	   BEGIN
		           	RAISERROR  20000 'No se puede actualizar temporal_analisis_Movimiento'
		           	ROLLBACK TRAN
	                   	RETURN(1)
    		     	   END			
			
			
			if @TYPEDOCID <> '104'
			   begin
				--ingresos
			   	if @C_INGRESOS > 0 AND @C_SALIDAS =  0
			           begin
          				SET @C_SALDO = @C_SALDO + @C_INGRESOS --CANTIDADES
					SET @V_SALDO = @V_SALDO	+ @V_INGRESOS --MONTOS		
				   end

				--salidas
				IF @C_INGRESOS = 0  AND @C_SALIDAS < 0
				   begin
          			  	SET @C_SALDO = @C_SALDO + @C_SALIDAS  --podria ser
					SET @V_SALDO = @V_SALDO + @V_SALIDAS
				   end
				
				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	 
			   fetch next from micursor into @TYPEDOCID, @BATNBR, @INTRDATE ,@INVTIDSKU, @C_INGRESOS, @C_SALIDAS, @V_INGRESOS, @V_SALIDAS
			   
		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]= CASE WHEN DOCTYPEID = '19' AND TYPEDOCID = '104' THEN 'SALDO INICIAL'
			   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 NUM_CP,
		C_INGRESOS AS [CANT. INGRESO],
		C_SALIDAS = CASE WHEN C_SALIDAS > 0 THEN C_SALIDAS ELSE C_SALIDAS * -1 END,
		C_ACTUAL = CASE WHEN C_ACTUAL > 0 THEN C_ACTUAL ELSE C_ACTUAL * -1 END,
		COSTUNIT,
		V_INGRESOS,
		V_SALIDAS = CASE WHEN V_SALIDAS > 0 THEN V_SALIDAS ELSE V_SALIDAS * -1 END,
		V_ACTUAL = CASE WHEN V_ACTUAL > 0 THEN V_ACTUAL ELSE V_ACTUAL * -1 END
	FROM 	temporal_analisis_Movimiento 
	WHERE 	INVTIDSKU = @cod_articulo 
	ORDER BY INTRDATE,REFNBR,BATNBR,TYPEDOCID

	commit tran
	SET NOCOUNT OFF
--	return(0)
end
