/* PROCEDIMIENTO PARA GRABAR UN INGRESO O UNA SALIDA, LLAMA AL PROCEDIMIENTO
   IN_GrabaDetalleMovimientoProc */
DROP PROCEDURE IN_GrabarMovimientoProc
GO
CREATE PROCEDURE IN_GrabarMovimientoProc
(
    @SITEID            UD_IDSITE,
    @TYPEDOCID         UD_TYPEMOVEID,
    @VENDIDSS          UD_IDVENDOR,
    @DOCTYPEID         UD_DOCTYPEID,
    @DOCSTATUSID       char(2),
    @REFNBR            char(15),
    @CURYRATE          numeric(6, 4),
    @TOCURYID          char(3),
    @FROMCURYID        char(3),
    @SITEPROCES        UD_IDSITE,
    @NBRORDER          char(15)                 = NULL,
    @DATENBRORDER      smalldatetime            = NULL,
    @USERIDORDER       UD_USERID                = NULL,
    @INTRDATE          smalldatetime,
    @INTRDATEPROCE     smalldatetime,
    @SITEDEST          UD_IDSITE,
    @INTRTOTPEN        UD_MONTO,
    @INTRTAXPEN        UD_IMPUESTO,
    @INTRTOTUSD        UD_MONTO,
    @INTRTAXUSD        UD_IMPUESTO,
    @PERPOST           char(6),
    @RLSED             bit,
    @DISASSEMBLE       bit,
    @INTRSUBTOTPEN     UD_MONTO,
    @INTRSUBTOTUSD     UD_MONTO,
    @USERID	       UD_USERID,
    @USER1             UD_USER1                 = NULL,
    @USER2             UD_USER2                 = NULL,
    @USER3             UD_USER3                 = NULL,
    @USER4             UD_USER5                 = NULL,
    @USER5             UD_USER5                 = NULL,
    @USER6             UD_USER6                 = NULL,
    @USER7             UD_USER7                 = NULL,
    @USER8             UD_USER8                 = NULL,
    @USER9             UD_USER9                 = NULL,
    @CORRNBR           int,
    @COMMENT1	       CHAR(100),
    @COMMENT2	       CHAR(100),
    @COMMENT3	       CHAR(100),
    @BAT 	CHAR(10) =' '	 OUTPUT	 )
AS
BEGIN
   
   DECLARE @BATNBR char(10)
   DECLARE @BATNBR_NEXT char(10)
   DECLARE @NUM_BATNBR NUMERIC(10,0)
   DECLARE @ERROR Integer
   DECLARE @TSTAMP timestamp
   DECLARE @ROWCOUNT Integer
   SELECT @ERROR=0   


    BEGIN TRAN
   
   --set TRANSACTION  isolation LEVEL read committed 

   SELECT @BATNBR=BATNBRTYPEDOC from in_setup
-- SELECT @TSTAMP=TIMESTAMP from in_setup
   SELECT @NUM_BATNBR=cast(@BATNBR AS NUMERIC(10,0))+1
   SELECT @BATNBR_NEXT =replicate('0',10-len(@NUM_BATNBR))+cast(@NUM_BATNBR as char(10))


  /*** ACTUALIZACION DEL CORRELATIVO DE TIPO DE MOVIMIENTO ***/
    	UPDATE IN_SETUP
    	SET    BATNBRTYPEDOC=@BATNBR_NEXT    
	WHERE  BATNBRTYPEDOC=@BATNBR

     IF (@@error!=0 OR @@ROWCOUNT=0 )
    BEGIN
        ROLLBACK TRAN
        RETURN(1)
    
    END
    

    INSERT INTO IN_INTRAHEAD(SITEID,BATNBR,TYPEDOCID,VENDIDSS,DOCTYPEID,DOCSTATUSID,
                             REFNBR,CURYRATE,TOCURYID,FROMCURYID,SITEPROCES,NBRORDER,
                             DATENBRORDER,USERIDORDER,INTRDATE,INTRDATEPROCE,SITEDEST,
                             INTRTOTPEN,INTRTAXPEN,INTRTOTUSD,INTRTAXUSD,PERPOST,
                             RLSED,DISASSEMBLE,INTRSUBTOTPEN,INTRSUBTOTUSD,USERID,USER1,USER2,USER3,USER4,USER5,USER6,USER7,USER8,USER9 
                             )
    VALUES(@SITEID,@BATNBR,@TYPEDOCID,@VENDIDSS,@DOCTYPEID,@DOCSTATUSID,@REFNBR,@CURYRATE,
           @TOCURYID,@FROMCURYID,@SITEPROCES,@NBRORDER,@DATENBRORDER,@USERIDORDER,@INTRDATE,
           @INTRDATEPROCE,@SITEDEST,@INTRTOTPEN,@INTRTAXPEN,@INTRTOTUSD,@INTRTAXUSD,@PERPOST,
	   @RLSED,@DISASSEMBLE,@INTRSUBTOTPEN,@INTRSUBTOTUSD,@USERID,@USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9 
           )
 
    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'IN_INTRAHEADInsProc: Cannot insert because primary key value not found in IN_INTRAHEAD '
        ROLLBACK TRAN
        RETURN(1)
    
    END

    /*** Llamada al store de grabado de detalle ******/
    SET EXEC @ERROR=IN_GrabaDetalleMovimientoProc @CORRNBR,@BATNBR,@VENDIDSS,@FROMCURYID,@DOCSTATUSID,@INTRDATE, @INTRDATEPROCE

    IF (@ERROR!=0)
    BEGIN
       ROLLBACK TRAN
       RETURN(1)               
    END 

    -- Insertando los comentarios en Tabla COMMENT
    	
    INSERT INTO COMMENT(SITEID,TYPEDOCID,DOCTYPEID,BATNBR,DATEPROCE,COMMENT1,COMMENT2,COMMENT3)
    VALUES (@SITEID,@TYPEDOCID,@DOCTYPEID,@BATNBR,@INTRDATEPROCE,@COMMENT1,@COMMENT2,@COMMENT3)		

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'No se puede insertar en tabla COMMENT '
        ROLLBACK TRAN
        RETURN(1)
    
    END

   SET @BAT=@BATNBR
   SELECT @BATNBR as NUMEROMOVIMIENTO
    COMMIT TRAN
    RETURN(0)
END








