
DECLARE @NBRSERIES numeric(3,0)
DECLARE @NBRDOC NUMERIC(7,0)
DECLARE @CHAR_NBRDOCUMENT CHAR(10)

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

SET @NBRSERIES= CAST( SUBSTRING(@CHAR_NBRDOCUMENT ,1,3) AS NUMERIC(3,0) )
SET @NBRDOC =	CAST( SUBSTRING(@CHAR_NBRDOCUMENT ,4,7) AS NUMERIC(7,0) )

SELECT @NBRSERIES,@NBRDOC



IF( ='A')	--SI ES FACTURACION AUTOMATICA (TIENDA)
BEGIN 
	IF ()		--SI ES TICKET FACTURA
		BEGIN
			update OP_SALESPOINTSETTING 
			set NBRTICKFAC=NBRTICKFAC+1,BATNBRSALES = BATNBRSALES+1  
			where SALESPOINT=@SALESPOINT
		
			 IF (@@error!=0)
			    BEGIN
			        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al Grabar Transaccion incrementando correlativo'
			        ROLLBACK TRAN
			        RETURN(1)    
			    END
		END
	ELSE
		BEGIN

			--SI ES TICKET BOLETA
			update OP_SALESPOINTSETTING 
			set 	NBRTICKBOL  =NBRTICKBOL  +1,BATNBRSALES = BATNBRSALES+1
				where SALESPOINT=@SALESPOINT

			 IF (@@error!=0)
			    BEGIN
			        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al Grabar Transaccion incrementando correlativo'
			        ROLLBACK TRAN
			        RETURN(1)    
			    END
		END
ELSE	--ES FACTURACION MANUAL
	BEGIN	
		update OP_DOCSERIES set NBRDOC =@NBRDOC +1
			where DOCTYPEID=@DOCTYPEID AND  NBRSERIES=@NBRSERIES AND  SITEID=@SITEID

		 IF (@@error!=0)
		    BEGIN
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al Grabar Transaccion incrementando correlativo'
		        ROLLBACK TRAN
		        RETURN(1)    
		    END

		update OP_SALESPOINTSETTING set BATNBRSALES = BATNBRSALES+1  
		where SALESPOINT=@SALESPOINT

		 IF (@@error!=0)
		    BEGIN
		        RAISERROR  20000 'OP_GRABARVENTASCTORE: Error al Grabar Transaccion incrementando correlativo'
		        ROLLBACK TRAN
		        RETURN(1)    
		    END

	END
END



