SET NOCOUNT ON
DECLARE @COD_ARTICULO UD_IDCORPORATIVO
DECLARE @FECHAINI	CHAR(8)
DECLARE @FECHAFIN	CHAR(8)
DECLARE @DATFECHAINI	UD_FECHA
DECLARE @DATFECHAFIN	UD_FECHA

--VARIABLES DE MICURSOR
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 UD_MONTO
DECLARE @V_SALDO UD_MONTO
DECLARE @COSTUNIT UD_COSTO 

SET @COD_ARTICULO = '1'
SET @FECHAINI	  = '20070601'
SET @FECHAFIN	  = '20070610'
	
SET @DATFECHAINI  = CAST(@FECHAINI AS SMALLDATETIME)
SET @DATFECHAFIN  = CAST(@FECHAFIN AS SMALLDATETIME)
SET @COSTUNIT = 0
SET @V_SALDO = 0
SET @C_SALDO = 0

BEGIN TRAN
DELETE FROM TEMPORAL_ANALISIS_MOVIMIENTO
INSERT INTO TEMPORAL_ANALISIS_MOVIMIENTO
SELECT  INTRAMOV.TYPEDOCID,  -----SALDO INICIAL
        INTRAMOV.BATNBR, 
        INTRAMOV.INVTIDSKU,
        IN_INVENTORY.DESCR AS DESCRIPCION,
        IN_INVENTORYUNITMED.DESCR AS UNIDAD,
        TIPO_CP = CASE WHEN INTRAMOV.DOCTYPEID = '19' AND INTRAMOV.TYPEDOCID = '104' THEN 'SALDO INICIAL' END,
 	INTRAMOV.INTRDATE,
 	INTRAMOV.DOCTYPEID, 
 	INTRAMOV.REFNBR,
	INTRAMOV.QTY AS C_INGRESOS, 
	0 AS [C_SALIDAS],
	0 AS [C_ACTUAL],
	0 AS COSTUNIT,
	(INTRAMOV.QTY * INTRAMOV.STDCOSTPEN) AS [V_INGRESOS],
	0 AS [V_SALIDAS],	
	0 AS [V_ACTUAL] 
FROM INTRAMOV INNER JOIN IN_INVENTORY ON INTRAMOV.INVTIDSKU = IN_INVENTORY.INVTIDSKU 
INNER JOIN IN_INVENTORYUNITMED ON IN_INVENTORY.STKUNITID = IN_INVENTORYUNITMED.STKUNITID
WHERE INTRAMOV.DOCTYPEID = '19' AND INTRAMOV.TYPEDOCID = '104'  AND INTRAMOV.INVTIDSKU = @COD_ARTICULO AND 
INTRAMOV.INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN
UNION ALL 
SELECT  INTRAMOV.TYPEDOCID,  -----SALIDAS
        INTRAMOV.BATNBR, 
        INTRAMOV.INVTIDSKU,
        IN_INVENTORY.DESCR AS DESCRIPCION,
        IN_INVENTORYUNITMED.DESCR AS UNIDAD,
        TIPO_CP  = CASE WHEN INTRAMOV.DOCTYPEID = '01' AND INTRAMOV.TYPEDOCID = '101' THEN 'FTC'
			WHEN INTRAMOV.DOCTYPEID = '03' AND INTRAMOV.TYPEDOCID = '101' THEN 'BTC'
			WHEN INTRAMOV.DOCTYPEID = '01' AND INTRAMOV.TYPEDOCID = '301' THEN 'FTV'
			WHEN INTRAMOV.DOCTYPEID = '03' AND INTRAMOV.TYPEDOCID = '301' THEN 'BTV'
			WHEN INTRAMOV.DOCTYPEID = '90' AND INTRAMOV.TYPEDOCID = '301' THEN 'VLE'	
			WHEN INTRAMOV.DOCTYPEID = '12' AND INTRAMOV.TYPEDOCID = '301' THEN 'TKV' END, 
 	INTRAMOV.INTRDATE,
 	INTRAMOV.DOCTYPEID, 
 	INTRAMOV.REFNBR,
	0 AS C_INGRESOS, 
	INTRAMOV.QTY AS [C_SALIDAS],
	0 AS [C_ACTUAL],
	0 AS COSTUNIT,
	0 AS [V_INGRESOS],
	((INTRAMOV.QTY * INTRAMOV.STDCOSTPEN)) AS [V_SALIDAS],	
	0 AS [V_ACTUAL] 
FROM INTRAMOV INNER JOIN IN_INVENTORY ON INTRAMOV.INVTIDSKU = IN_INVENTORY.INVTIDSKU 
INNER JOIN IN_INVENTORYUNITMED ON IN_INVENTORY.STKUNITID = IN_INVENTORYUNITMED.STKUNITID
WHERE INTRAMOV.SWTINOUT = 0 AND 
      INTRAMOV.TYPEDOCID <> '104' AND 
      INTRAMOV.INVTIDSKU = @COD_ARTICULO AND 
      INTRAMOV.INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN
UNION ALL 
SELECT  INTRAMOV.TYPEDOCID,   -----INGRESOS
        INTRAMOV.BATNBR, 
        INTRAMOV.INVTIDSKU,
        IN_INVENTORY.DESCR AS DESCRIPCION,
        IN_INVENTORYUNITMED.DESCR AS UNIDAD,
        TIPO_CP  = CASE WHEN INTRAMOV.DOCTYPEID = '01' AND INTRAMOV.TYPEDOCID = '101' THEN 'FTC'
			WHEN INTRAMOV.DOCTYPEID = '03' AND INTRAMOV.TYPEDOCID = '101' THEN 'BTC'
			WHEN INTRAMOV.DOCTYPEID = '01' AND INTRAMOV.TYPEDOCID = '301' THEN 'FTV'
			WHEN INTRAMOV.DOCTYPEID = '03' AND INTRAMOV.TYPEDOCID = '301' THEN 'BTV'
			WHEN INTRAMOV.DOCTYPEID = '90' AND INTRAMOV.TYPEDOCID = '301' THEN 'VLE'	
			WHEN INTRAMOV.DOCTYPEID = '12' AND INTRAMOV.TYPEDOCID = '301' THEN 'TKV' END, 
 	INTRAMOV.INTRDATE,
 	INTRAMOV.DOCTYPEID, 
 	INTRAMOV.REFNBR,
	INTRAMOV.QTY AS C_INGRESOS, 
	0 AS [C_SALIDAS],
	0 AS [C_ACTUAL],
	0 AS COSTUNIT,
	((INTRAMOV.QTY * INTRAMOV.STDCOSTPEN)) AS [V_INGRESOS],
	0 AS [V_SALIDAS],	
	0 AS [V_ACTUAL] 
FROM INTRAMOV 
INNER JOIN IN_INVENTORY ON INTRAMOV.INVTIDSKU = IN_INVENTORY.INVTIDSKU 
INNER JOIN IN_INVENTORYUNITMED ON IN_INVENTORY.STKUNITID = IN_INVENTORYUNITMED.STKUNITID
WHERE INTRAMOV.SWTINOUT = 1 AND 
      INTRAMOV.TYPEDOCID <> '104' AND 
      INTRAMOV.INVTIDSKU = @COD_ARTICULO AND 
      INTRAMOV.INTRDATE BETWEEN @DATFECHAINI AND @DATFECHAFIN
ORDER BY INTRAMOV.INTRDATE
	COMMIT TRAN
	SET NOCOUNT OFF


---------------------------------------------

SET NOCOUNT ON
DECLARE @COD_ARTICULO UD_IDCORPORATIVO
DECLARE @FECHAINI	CHAR(8)
DECLARE @FECHAFIN	CHAR(8)
DECLARE @DATFECHAINI	UD_FECHA
DECLARE @DATFECHAFIN	UD_FECHA

--VARIABLES DE MICURSOR
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 UD_MONTO
DECLARE @V_SALDO UD_MONTO
DECLARE @C_ACTUAL UD_MONTO
DECLARE @V_ACTUAL UD_MONTO
DECLARE @COSTUNIT UD_COSTO 
DECLARE @V_CANTINGRESOINICIAL UD_MONTO
DECLARE @V_VALINGRESOINICIAL UD_MONTO


SET @COD_ARTICULO = '1'
SET @FECHAINI	  = '20070601'
SET @FECHAFIN	  = '20070610'
	
SET @DATFECHAINI  = CAST(@FECHAINI AS SMALLDATETIME)
SET @DATFECHAFIN  = CAST(@FECHAFIN AS SMALLDATETIME)
SET @COSTUNIT = 0
SET @V_SALDO = 0
SET @C_SALDO = 0
SET @V_CANTINGRESOINICIAL = 0
SET @V_VALINGRESOINICIAL = 0
SET @C_ACTUAL = 0
SET @V_ACTUAL = 0

BEGIN TRAN

	--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 
		WHERE INVTIDSKU = @COD_ARTICULO
		ORDER BY INTRDATE
	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_ACTUAL
	SET @V_SALDO = @V_ACTUAL

	WHILE @@FETCH_STATUS = 0    
		BEGIN	
			IF @TYPEDOCID = '104'		-----------------SALDO INICIAL
			   BEGIN
				UPDATE TEMPORAL_ANALISIS_MOVIMIENTO SET
			       	       C_ACTUAL = @C_INGRESOS, 
				       V_ACTUAL = @V_INGRESOS	
				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 @C_SALDO = @C_INGRESOS
				SET @V_SALDO = @V_INGRESOS
			   END	

			IF @TYPEDOCID <> '104'		-----------------SALIDAS
			   BEGIN
				--SALIDAS
				IF @C_INGRESOS = 0  AND @C_SALIDAS > 0
				   BEGIN
          			 	SET @C_SALDO = @C_SALDO - @C_SALIDAS  --CANTIDADES
					SET @V_SALDO = @V_SALDO - @V_SALIDAS  --MONTOS
					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
			   END

		        IF @TYPEDOCID = '101'		-----------------INGRESOS
			   BEGIN	
			   	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
				   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,@C_ACTUAL, @V_INGRESOS, @V_SALIDAS, @V_ACTUAL
		END
	CLOSE MICURSOR    
	DEALLOCATE MICURSOR
	--RETURN(0)
	SELECT 	* FROM TEMPORAL_ANALISIS_MOVIMIENTO ORDER BY INTRDATE,TYPEDOCID
	COMMIT TRAN
	SET NOCOUNT OFF
--------------------------------------------------------------
