DROP PROC OP_AGREACLIENTECSTORE
GO
CREATE PROC OP_AGREACLIENTECSTORE
     @custidss         UD_CODCLIENT,
      @taxregnbr        UD_CODCLIENT                = NULL,
      @CUSTNAME         UD_NOMBRE                   = NULL,
      @addr1            UD_DIRECCION                = NULL,
    @dateproc         datetime,
    @USERID           UD_USERID

AS

DECLARE    @custid           UD_CODCLIENT
DECLARE    @TAXID1           UD_TAXID
DECLARE    @CURYTYPEID       UD_CURYTYPEID
DECLARE    @CURYID           UD_CURYID

SELECT @custid= custid, @TAXID1= TAXID,@CURYTYPEID=CURYTYPEIDSALES, @CURYID=CURYORIG FROM SETUP

    BEGIN TRAN

SELECT custidss FROM CUSTOMER WHERE custidss=@custidss

IF( @@ROWCOUNT=0 )
    INSERT INTO CUSTOMER(custidss,
                         TERMIDSUM,
                         custid,
                         taxregnbr,
                         CUSTNAME,
                         addr1,
                         CUSTCLASSID,
                         TAXID1,
                         CURYTYPEID,
                         TERMIDDOC,
                         dateproc,
                         DAYGRACE,
                         CURYID,
                         USERID,
                         CUSTSTATUSID,
                         MTOTOTLIMIT,
                         TOTCURRBAL,
                         AFFECT )
    VALUES(@custidss,
           '00',
           @custid,
           @taxregnbr,
           @CUSTNAME,
           @addr1,
           '00',
           @TAXID1,
           @CURYTYPEID,
           '00',
           @dateproc,
           0,
           @CURYID,
           @USERID,
           'A',0,0,0 )


ELSE
    UPDATE  CUSTOMER 
		SET taxregnbr=@taxregnbr,
                    CUSTNAME=@CUSTNAME,
                    addr1=@addr1 
    		    WHERE custidss=@custidss
                     
    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'SP_AGREGAR CLIENTE CSTORE: ERROR ACTUALIZANDO DATOS CLIENTE'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

GO

EXEC OP_AGREACLIENTECSTORE '111111','AA222222','BB33333','DD44444444','20021230','SYSADMIN'

SELECT * FROM CUSTOMER




