DROP PROCEDURE CustomerInsProc
GO
CREATE PROCEDURE CustomerInsProc
(
    @CUSTIDSS         UD_CODCLIENT,
    @DIAPAGID         char(10)                    = NULL,
    @TERMIDSUM        char(2),
    @CUSTID           UD_CODCLIENT,
    @TAXREGNBR        UD_CODCLIENT                = NULL,
    @CUSTNAME         UD_NOMBRE                   = NULL,
    @ADDR1            UD_DIRECCION                = NULL,
    @ADDR2            UD_DIRECCION                = NULL,
    @PHONE            char(10)                    = NULL,
    @FAX              char(10)                    = NULL,
    @CUSTZONEID       char(2)                     = NULL,
    @COUNTRYID        char(3)                     = NULL,
    @STATEID          char(2)                     = NULL,
    @CITYID           char(3)                     = NULL,
    @CUSTCLASSID      char(2),
    @TAXID1           UD_TAXID,
    @TAXID2           UD_TAXID                    = NULL,
    @CURYTYPEID       UD_CURYTYPEID,
    @TERMIDDOC        char(2),
    @DATEORIG         datetime                    = NULL,
    @DATEPROC         datetime,
    @DAYGRACE         int,
    @CURYID           UD_CURYID                   = NULL,
    @SALESPERID       UD_SALESPERSONID            = NULL,
    @USERID           UD_USERID,
    @CUSTSTATUSID     CHAR(1),
    @CORRNBR          int,
    @MTOTOTLIMIT      UD_MONTO,
    @AFFECT	      BIT,			
    @USER1            UD_USER1                    = NULL,
    @USER2            UD_USER2                    = NULL,
    @USER3            UD_USER3                    = NULL,
    @USER4            UD_USER4                    = NULL,
    @USER5            UD_USER5                    = NULL,
    @USER6            UD_USER6                    = NULL,
    @USER7            UD_USER7                    = NULL,
    @USER8            UD_USER8                    = NULL,
    @USER9            UD_USER9                    = NULL)
AS
BEGIN
    BEGIN TRAN
    
    IF(@CITYID='') SET @CITYID=NULL
    IF(@COUNTRYID='') SET @COUNTRYID=NULL
    IF(@CUSTZONEID='') SET @CUSTZONEID=NULL
    IF(@STATEID='') SET @STATEID=NULL
    IF(@CURYID='') SET @CURYID=NULL
    IF(@DIAPAGID='') SET @DIAPAGID=NULL
    IF(@SALESPERID='') SET @SALESPERID=NULL
    IF(@TAXID2='') SET @TAXID2=NULL

    -- GRABANDO EN TABLA DE CLEINTES	
    INSERT INTO CUSTOMER(CUSTIDSS,DIAPAGID,TERMIDSUM,CUSTID,TAXREGNBR,CUSTNAME,ADDR1,ADDR2,PHONE,FAX,CUSTZONEID,COUNTRYID,
                         STATEID,CITYID,CUSTCLASSID,TAXID1,TAXID2,CURYTYPEID,TERMIDDOC,DATEORIG,DATEPROC,DAYGRACE,
                         CURYID,SALESPERID,USERID,CUSTSTATUSID,MTOTOTLIMIT,TOTCURRBAL,AFFECT,USER1,USER2,USER3,USER4,USER5,USER6,USER7,USER8,USER9 
                         )
    VALUES(@CUSTIDSS,@DIAPAGID,@TERMIDSUM,@CUSTID,@TAXREGNBR,@CUSTNAME,@ADDR1,@ADDR2,@PHONE,@FAX,@CUSTZONEID,@COUNTRYID,
           @STATEID,@CITYID,@CUSTCLASSID,@TAXID1,@TAXID2,@CURYTYPEID,@TERMIDDOC,@DATEORIG,@DATEPROC,@DAYGRACE,
           @CURYID,@SALESPERID,@USERID,@CUSTSTATUSID,@MTOTOTLIMIT,0,@AFFECT,@USER1,@USER2,@USER3,@USER4,@USER5,@USER6,@USER7,@USER8,@USER9 
           )

    IF (@@error!=0 OR @@rowcount=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en Customer'
        ROLLBACK TRAN
        RETURN(1)
    
    END

	
    --GRABANDO EN TABLA DE PLACAS DE CLIENTES
    INSERT 	INTO 	OP_CUSTBADGE(CUSTIDSS,BADGE)
		SELECT 	@CUSTIDSS,BADGE
		FROM	TMP_OP_CUSTBADGE
		WHERE	CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en OP_CUSTBADGE'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE TARJETAS DE CLIENTES
    INSERT 	INTO 	OP_CUSTCARD(CUSTIDSS,CARDPASSWORD,EXPIREDATE,DESCRCUSTCARD,USERID)
		SELECT 	@CUSTIDSS,CARDPASSWORD,EXPIREDATE,DESCRCUSTCARD,@USERID
		FROM	TMP_OP_CUSTCARD
		WHERE	CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en OP_CUSTCARD'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE GARANTIAS DE CLIENTES
    INSERT 	INTO 	CUSTWARRANTY(CUSTIDSS,CUSTWARRANTYTYPEID,TOTGAR,DESCRGAR,EXPIREDATE,CURYID,USERID)
		SELECT 	@CUSTIDSS,CUSTWARRANTYTYPEID,TOTGAR,DESCRGAR,EXPIREDATE,CURYID,@USERID
		FROM	TMP_CUSTWARRANTY
		WHERE	CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en CUSTWARRANTY'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE CREDITOS DE CLIENTES
    INSERT 	INTO 	OP_CUST_LIMIT(CUSTIDSS,SITEID,PORCENTLIMITED,CREDLIMITED,CURRBAL,PORCENTSURPLUS,MTOSURPLUS,USERID)
		SELECT 	@CUSTIDSS,SITEID,PORCENTLIMITED,CREDLIMITED,CURRBAL,PORCENTSURPLUS,MTOSURPLUS,@USERID
		FROM	TMP_OP_CUST_LIMIT
		WHERE	CORRNBR=@CORRNBR

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en OP_CUST_LIMIT'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END


