drop proc op_eliminarventa_proc
go
create proc op_eliminarventa_proc
@DOCTYPEID      UD_DOCTYPEID,    
@NBRDOCUMENT    UD_CORRELATIVOS, 
@SITEID         UD_IDSITE,
@DOCSTATUS CHAR(2),
@TYPEPAYMENTID CHAR(2),
@custidss ud_codclient,
@montopen ud_monto=0,
@montousd ud_monto=0
as
declare @qty ud_stock
declare @INVTIDSKU ud_idcorporativo
declare @batnbr char(10)
DECLARE @NUMEROPAGOS INT
declare @NUMEROREFERENCIAS int
declare @CURYID ud_curyid
declare @monedaorigen ud_curyid
declare @TYPEPAYMENTIDREF UD_TYPEPAYMENTID
declare @stkitem bit
DECLARE @DOCTYPEIDNC UD_DOCTYPEID
DECLARE @DOCTYPEIDND UD_DOCTYPEID

SELECT @DOCTYPEIDND=@DOCTYPEIDND,@DOCTYPEIDNC= DOCTYPEIDNC from op_setup

SET @batnbr = CAST( @NBRDOCUMENT   AS CHAR(10) )

IF(@TYPEPAYMENTID='06')	--SI EL DOCUMENTO ES DE CREDITO 
BEGIN
	--saber si el documento tiene pagos parciales o totales
	SET @NUMEROPAGOS =(select count(*) from ar_ccdocdet 
				WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID and DOCSTATUSID<>'AN' )

	IF(@NUMEROPAGOS>0)
	BEGIN
		select 3 AS ERROR
		RETURN(3)	--TIENE PAGOS PARCIALES	
	END
	
	SET @NUMEROREFERENCIAS =(select count(*) from OP_DOCREFERENCE
				WHERE DOCTYPEIDREF = @DOCTYPEID  and NBRDOCUMENTREF=@NBRDOCUMENT and  SITEID=@SITEID)
	
	IF(@NUMEROREFERENCIAS >0)
	BEGIN
		select 4 AS ERROR
		RETURN(4)	--TIENE REFERENCIAS NOTAS DE CREDITO O DEBITO
	END
END

begin tran

if(@DOCSTATUS<>'AN') --SI EL DOCUMENTO NO ESTA ANULADO REVERTIR STOCK
BEGIN
	--declarar cursor para recorrer el detalle de los movimientos
	declare CURSORDETALLEVENTA CURSOR for 
			select INVTIDSKU,qty,stkitem
			from op_salesdet 
			WHERE DOCTYPEID = @DOCTYPEID  and NBRDOCUMENT = @NBRDOCUMENT and  SITEID=@SITEID and docstatusid<>'AN'

	OPEN CURSORDETALLEVENTA 

	FETCH NEXT FROM CURSORDETALLEVENTA 
		INTO @INVTIDSKU,@QTY,@stkitem

	--recorrer el detalle de tarjetas'
	WHILE @@FETCH_STATUS = 0
	BEGIN
	--------------------------------------------------------------------------------------------------
	if( @stkitem=1)		--si es producto inventariable
	begin
		--revertir stock
		IF( @DOCTYPEIDNC =@DOCTYPEID )	--SI EL DOCUMENTO ES UNA NOTA DE CREDITO DISMINUIR STOCK
			UPDATE IN_INVENTORYDAT SET QTYAVAIL=QTYAVAIL - @QTY  
		          WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU 
		ELSE
			UPDATE IN_INVENTORYDAT SET QTYAVAIL=QTYAVAIL + @QTY  
		          WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU 
          	
			
		IF (@@ERROR!=0)
		BEGIN   
			CLOSE CURSORDETALLEVENTA
			DEALLOCATE CURSORDETALLEVENTA
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al actualizar stock'
	        	ROLLBACK TRAN
			select 1 AS ERROR
			RETURN(1)  
		END 
		--------------------------------------------------------------------------------------------------
	end	--fin de si es producto inventariable
		FETCH NEXT FROM CURSORDETALLEVENTA 
		INTO @INVTIDSKU,@QTY,@stkitem

	END -- FIN DEL BUCLE DEL CURSOR

	CLOSE CURSORDETALLEVENTA
	DEALLOCATE CURSORDETALLEVENTA
	-----------------------------------------------------------------------------------
	--borrar de intramov
	delete intramov 
	WHERE DOCTYPEID = @DOCTYPEID  and REFNbr = @NBRDOCUMENT and  SITEID=@SITEID
		IF (@@ERROR!=0)
	BEGIN   
		CLOSE CURSORDETALLEVENTA
		DEALLOCATE CURSORDETALLEVENTA
	        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al actualizar intramov'
        	ROLLBACK TRAN
		select 1 AS ERROR
		RETURN(1)  
	END 
	----------------------------------------------------------------------------------------
	--SI EL DOCUMENTO ES UNA NOTA DE CREDITO o nota de debito
	IF( @DOCTYPEIDNC =@DOCTYPEID or @DOCTYPEIDND =@DOCTYPEID )	
	begin

		SET @TYPEPAYMENTIDREF=( SELECT TOP 1 TYPEPAYMENTIDREF FROM OP_DOCREFERENCE 
			WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID )

		DELETE OP_DOCREFERENCE 	--borrar referecias
			WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID

		IF (@@ERROR!=0)
		BEGIN   
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al eliminar referencias del documento'
		       	ROLLBACK TRAN
			select 1 AS ERROR
			RETURN(1)  
		END 
	end

	-------SI EL DOCUMENTO ES DE CREDITO 
	IF(@TYPEPAYMENTID='06' OR (@TYPEPAYMENTIDREF='06' AND ( @DOCTYPEIDNC =@DOCTYPEID or @DOCTYPEIDND =@DOCTYPEID) ) )	
	BEGIN
		--borrar cuenta corriente

		set @CURYID = (select CURYID from customer where custidss=@custidss)
		set @monedaorigen =(select CURYORIG from setup)

		delete ar_ccdoc
			WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID 
	

		IF (@@ERROR!=0)
		BEGIN   
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al anular cta.cte.'
		       	ROLLBACK TRAN
			select 1 AS ERROR
			RETURN(1)  
		END 
		if( @DOCTYPEIDNC =@DOCTYPEID )	--si el nota de credito aumentar limite
			begin
				if(@monedaorigen=@CURYID )
					UPDATE OP_CUST_LIMIT SET CURRBAL = CURRBAL + @montopen WHERE custidss = @custidss 
				else
					UPDATE OP_CUST_LIMIT SET CURRBAL = CURRBAL + @montousd WHERE custidss = @custidss 
			end
		else
			begin
				if(@monedaorigen=@CURYID )
					UPDATE OP_CUST_LIMIT SET CURRBAL = CURRBAL - @montopen WHERE custidss = @custidss 
				else
					UPDATE OP_CUST_LIMIT SET CURRBAL = CURRBAL - @montousd WHERE custidss = @custidss 
		end	

		IF (@@ERROR!=0)
		BEGIN   
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al anular cta.cte.'
		       	ROLLBACK TRAN
			select 1 AS ERROR
			RETURN(1)  
		END 
	END
--------------------------------------------------------------------------------------------------
END	--FIN DEL IF DE ESTADO ANULADO
	
--------------------------------------------------------------------------------------------------
--Borrar op_salescard
delete op_salescard
WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID

IF (@@ERROR!=0)
BEGIN   
        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al eliminar Movimientos de Tarjetas'
       	ROLLBACK TRAN
	select 1 AS ERROR
	RETURN(1)  
END 
--------------------------------------------------------------------------------------------------
--borrar op_salesdet
delete op_salesdet
	WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID 

IF (@@ERROR!=0)
BEGIN   
        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al eliminar Detalle de Venta'
       	ROLLBACK TRAN
	select 1 AS ERROR
	RETURN(1)  
END 
--------------------------------------------------------------------------------------------------
--borrar op_sales
delete op_sales
	WHERE DOCTYPEID = @DOCTYPEID  and @NBRDOCUMENT=NBRDOCUMENT and  SITEID=@SITEID

IF (@@ERROR!=0)
BEGIN   
        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al eliminar Venta'
       	ROLLBACK TRAN
	select 1 AS ERROR
	RETURN(1)  
END 

COMMIT TRAN   
select 0 AS ERROR
RETURN (0)  
go