create PROCEDURE [dbo].[AnularCompras] 
    @CODPROV  VARCHAR(15), 
    @NumeroD  VARCHAR(20) 
AS 
DECLARE @TipoCOM  VARCHAR(2)
DECLARE @TIPO     VARCHAR(2)
DECLARE @CodItem  VARCHAR(15)
DECLARE @CodUbic  VARCHAR(10)
DECLARE @Cantidad  DECIMAL(28,3)
DECLARE @EsUnid   SMALLINT
DECLARE @ExUnidad int
DECLARE @Variable1 decimal(28,2)
DECLARE @Variable2 int 
DECLARE @Variable3 decimal(28,2)
DECLARE @CantEmpaq int
DECLARE @Existen  DECIMAL(28,3)
DECLARE @FechaE   datetime
DECLARE @FechaT   datetime
SET DATEFORMAT YMD
SET @TipoCOM = 'H'
SET @TIPO='H'
SET @FechaE = getdate()
SET @FechaT = Getdate() 

select @FechaE = Fechae from SACOMP where tipoCOM='H' and Numerod=@NumeroD and CodProv=@CODPROV;

IF LEFT (CONVERT (varchar, @FechaE, 112), 6)=(select MAX(mescurso) from saconf) and @TipoCOM='H'
begin
	SELECT @TIPO = tipoCOM from saCOMP where numerod=@numerod and tipoCOM=@TipoCOM AND CODPROV=@CODPROV

	 DECLARE MCURSOR CURSOR FOR
	 SELECT A.CodItem, A.Cantidad, A.CodUbic, A.EsUnid
	 FROM SAITEMCOM as A
	 WHERE A.tipoCOM = @TipoCOM And A.Numerod=@NumeroD AND A.CODPROV=@CODPROV AND EsServ = 0
	 ORDER BY A.NroLinea
	 OPEN Mcursor
	 FETCH NEXT FROM Mcursor INTO @CodItem, @Cantidad, @CodUbic, @EsUnid
	 WHILE (@@FETCH_STATUS<>-1)
	  BEGIN
		IF (@@FETCH_STATUS<>-2)
		   BEGIN
			   -- SET @Existen=@Cantidad*@Signo
			IF @EsUnid=0
                 begin			 
				 UPDATE SAEXIS
				 SET EXISTEN = existen- @Cantidad
				 WHERE codprod = @CodItem and CodUbic=@CodUbic
				 UPDATE SAPROD
				 SET EXISTEN = existen- @Cantidad
				 WHERE codprod = @CodItem 
				 end
             else if @EsUnid=1 begin
				 SELECT @CantEmpaq = CantEmpaq, @ExUnidad= ExUnidad, @Existen=Existen  from saprod where codprod=@CodItem  
				 set @variable1=(@Existen*@CantEmpaq-@ExUnidad-@Cantidad)
				 set @variable2=@variable1/@CantEmpaq
				 set @variable3=@variable1-@variable2*@CantEmpaq
				 UPDATE SAPROD
				 SET EXISTEN = @variable2, ExUnidad= @Variable3
				 WHERE codprod = @CodItem  
				 SELECT @ExUnidad= ExUnidad, @Existen=Existen  from saexis where codprod=@CodItem  and CodUbic=@CodUbic
				 set @variable1=(@Existen*@CantEmpaq-@ExUnidad-@Cantidad)
				 set @variable2=@variable1/@CantEmpaq
				 set @variable3=@variable1-@variable2*@CantEmpaq
				 UPDATE SAEXIS
				 SET EXISTEN = @variable2, ExUnidad= @Variable3
				 WHERE codprod = @CodItem and CodUbic=@CodUbic
			 				
			 end
		   end
		FETCH NEXT
		FROM Mcursor
		INTO @CodItem, @Cantidad, @CodUbic, @EsUnid
	  END
	 CLOSE Mcursor
	 DEALLOCATE Mcursor 

	IF @TIPO ='H' BEGIN
	DELETE FROM SACOMP  
	where numerod=@NumeroD and tipoCOM=@TipoCOM;
	
	DELETE FROM  SAITEMCOM  where numerod=@NumeroD AND CODPROV=@CODPROV and TipoCOM=@TipoCOM	;

	DELETE FROM sataxCOM 
	where numerod=@NumeroD  AND CODPROV=@CODPROV and tipoCOM=@TipoCOM;

	DELETE FROM sataxITC  
	where numerod=@NumeroD  AND CODPROV=@CODPROV and tipoCOM=@TipoCOM;
	
	DELETE FROM SAPVPR
	where numerod=@NumeroD  AND CODPROV=@CODPROV and tipoCOM=@TipoCOM;
	
	DELETE FROM    SAPAGCXP
	WHERE numerod=@NumeroD and TipoCxp='10'



	
	DELETE FROM saacxp  
	where numerod=@NumeroD  AND CODPROV=@CODPROV and tipoCxP IN ('10','41') ;
	
	DELETE FROM saacxp  
	where [document]='RET. IVA DOC.: '+@NumeroD  AND CODPROV=@CODPROV and tipoCxP IN ('81') ;
	end
end


