DROP PROCEDURE CustomerUpdProc
GO
CREATE PROCEDURE CustomerUpdProc
(
    @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
)
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
    UPDATE CUSTOMER
    SET    	DIAPAGID=@DIAPAGID,
		TERMIDSUM=@TERMIDSUM,
		CUSTID=@CUSTID,
		TAXREGNBR=@TAXREGNBR,
		CUSTNAME=@CUSTNAME,
		ADDR1=@ADDR1,
		ADDR2=@ADDR2,
		PHONE=@PHONE,
		FAX=@FAX,
		CUSTZONEID=@CUSTZONEID,
		COUNTRYID=@COUNTRYID,
                STATEID=@STATEID,
		CITYID=@CITYID,
		CUSTCLASSID=@CUSTCLASSID,
		TAXID1=@TAXID1,
		TAXID2=@TAXID2,
		CURYTYPEID=@CURYTYPEID,
		TERMIDDOC=@TERMIDDOC,
		DATEORIG=@DATEORIG,
		DATEPROC=@DATEPROC,
		DAYGRACE=@DAYGRACE,
                CURYID=@CURYID,
		SALESPERID=@SALESPERID,
		CUSTSTATUSID=@CUSTSTATUSID,
		MTOTOTLIMIT=@MTOTOTLIMIT,
		AFFECT=@AFFECT
    WHERE  CUSTIDSS= @CUSTIDSS                      
    

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

    
    --ELIMINANDO
    DELETE
    FROM OP_CUSTBADGE
    WHERE CUSTIDSS= @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Eliminando en OP_CUSTBADGE'
        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

    --ELIMINANDO
    DELETE
    FROM OP_CUSTCARD
    WHERE CUSTIDSS= @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Eliminando en OP_CUSTCARD'
        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

    --ELIMINANDO
    DELETE
    FROM CUSTWARRANTY
    WHERE CUSTIDSS= @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Eliminando en CUSTWARRANTY'
        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

    --ELIMINANDO
    DELETE
    FROM OP_CUST_LIMIT
    WHERE CUSTIDSS= @CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Eliminando en OP_CUST_LIMIT'
        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


