/* PROCEDIMIENTO QUE GRABA EL DETALLE DE UN MOVIMIENTO */

DROP PROCEDURE IN_GrabaDetalleMovimientoProc 
GO
CREATE PROCEDURE IN_GrabaDetalleMovimientoProc
(@CORRNBR int,
@BATNBR1 char(10),
@VENDIDSS UD_IDVENDOR,
@CURYID char(3),
@DOCSTATUSID char(2),
@INTRDATE1	smalldatetime,
@INTRDATEPROCE1	smalldatetime
)
AS
BEGIN    
    DECLARE   @SITEID            UD_IDSITE
    DECLARE   @BATNBR            char(10)
    DECLARE   @TYPEDOCID         UD_TYPEMOVEID
    DECLARE   @SEQUENCE          smallint
    DECLARE   @INVTIDSKU         UD_IDCORPORATIVO
    DECLARE   @INVTIDBARR        UD_CODIGOBARRA
    DECLARE   @QTY               UD_STOCK
    DECLARE   @TAXIGV            numeric(5, 2)
    DECLARE   @STDCOSTPEN        UD_COSTO
    DECLARE   @STDCOSTUSD        UD_COSTO
    DECLARE   @STKFISI           bit
    DECLARE   @INTRDATE          smalldatetime
    DECLARE   @INTRDATEPROCE     smalldatetime
    DECLARE   @RLSED             bit
    DECLARE   @TAXISC		 numeric(5,2)
    DECLARE   @STDCOSTPENOLD	 UD_COSTO
    DECLARE   @STDCOSTUSDOLD	 UD_COSTO
    DECLARE   @USER1             UD_USER1
    DECLARE   @USER2             UD_USER2
    DECLARE   @USER3             UD_USER3
    DECLARE   @USER4             UD_USER5
    DECLARE   @USER5             UD_USER5
    DECLARE   @USER6             UD_USER6
    DECLARE   @USER7             UD_USER7
    DECLARE   @USER8             UD_USER8
    DECLARE   @USER9             UD_USER9
    DECLARE   @ERROR             integer

   --VARIABLE PARA IN_VENDORINVT
    DECLARE   @MONEDABASE		char(3)    -- almacena la moneda base
    DECLARE   @CAMPO  		char(10)  --para distinguir el campo de costo a tomar 

    SET @BATNBR=@BATNBR1
    SET @INTRDATE=@INTRDATE1
    SET @INTRDATEPROCE=@INTRDATEPROCE1   
    SET @MONEDABASE =(SELECT CURYORIG FROM SETUP) 

/****** VARIABLES QUE VIENEN DEL SETUP PARA EL REDONDEO ********************/ 
	DECLARE @ND_COSTOSETUP Tinyint
	DECLARE @ND_STOCKSETUP Tinyint

	SET @ND_COSTOSETUP=(Select Floatcost from setup)
 	SET @ND_STOCKSETUP=(Select Floatqty from setup)	
/*******************************************************************************************************/
    	-- VERIFICAR LA TOMA DE INVENTARIO
   	SET @STKFISI=(SELECT STKFISI FROM IN_SETUP)

/*******************************************************************************************************/

    DECLARE INTRAITEM cursor 


    FOR SELECT SITEID, @BATNBR,TYPEDOCID, SEQUENCE, INVTIDSKU, 
               INVTIDBARR, QTY, TAXIGV, STDCOSTPEN, STDCOSTUSD, 
               @STKFISI, @INTRDATE, @INTRDATEPROCE, RLSED, TAXISC, STDCOSTPENOLD, STDCOSTUSDOLD, 
               USER1, USER2, USER3, USER4, USER5, USER6, 
               USER7, USER8, USER9
        FROM TMP_IN_INTRAITEM WHERE CORRNBR=@CORRNBR

    OPEN INTRAITEM
    FETCH NEXT FROM INTRAITEM INTO  @SITEID, @BATNBR, @TYPEDOCID, @SEQUENCE, @INVTIDSKU,
                      		    @INVTIDBARR, @QTY, @TAXIGV, @STDCOSTPEN, @STDCOSTUSD,
                      		    @STKFISI, @INTRDATE, @INTRDATEPROCE, @RLSED, @TAXISC, @STDCOSTPENOLD, @STDCOSTUSDOLD, @USER1,
				    @USER2, @USER3, @USER4, @USER5, @USER6,
		      		    @USER7, @USER8, @USER9  
      IF (@@FETCH_STATUS!=0)
          BEGIN       
            CLOSE INTRAITEM
            DEALLOCATE INTRAITEM
            RAISERROR 20000  'No tiene registros en Detalle'
            RETURN(1)  
          END        
     

      WHILE @@FETCH_STATUS=0
        BEGIN    

          INSERT INTO IN_INTRAITEM(SITEID,BATNBR,TYPEDOCID,SEQUENCE,INVTIDSKU,INVTIDBARR,QTY,
                                   TAXIGV,STDCOSTPEN,STDCOSTUSD,STKFISI,INTRDATE,INTRDATEPROCE,
                                   RLSED,TAXISC,STDCOSTPENOLD,STDCOSTUSDOLD,USER1,USER2,USER3,USER4,USER5,USER6,USER7,USER8,USER9,DOCSTATUSID 
                                   )
          VALUES(@SITEID,@BATNBR,@TYPEDOCID,@SEQUENCE,@INVTIDSKU,@INVTIDBARR,@QTY,@TAXIGV,@STDCOSTPEN,
                 @STDCOSTUSD,@STKFISI,@INTRDATE,@INTRDATEPROCE,@RLSED,@TAXISC,@STDCOSTPENOLD,@STDCOSTUSDOLD,@USER1,@USER2,@USER3,@USER4,
                 @USER5,@USER6,@USER7,@USER8,@USER9,@DOCSTATUSID
                 )


          SET @ERROR=@@error
     
          IF (@ERROR!=0)
          BEGIN       
            CLOSE INTRAITEM
            DEALLOCATE INTRAITEM
            RETURN(1)  
          END         
             

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

    	  FETCH NEXT FROM INTRAITEM INTO  @SITEID, @BATNBR, @TYPEDOCID, @SEQUENCE, @INVTIDSKU,
                      		    @INVTIDBARR, @QTY, @TAXIGV, @STDCOSTPEN, @STDCOSTUSD,
                      		    @STKFISI, @INTRDATE, @INTRDATEPROCE, @RLSED, @TAXISC, @STDCOSTPENOLD, @STDCOSTUSDOLD, @USER1,
				    @USER2, @USER3, @USER4, @USER5, @USER6,
		      		    @USER7, @USER8, @USER9

        
      END /* Termina while */



  CLOSE INTRAITEM
  DEALLOCATE INTRAITEM
  RETURN (0)

END


