ALTER PROC PROC_AGENT_RECEIVER_SETUP
	  @flag						VARCHAR(50)		= NULL,
      @user						VARCHAR(30)		= NULL,
      @receiverId				VARCHAR(50)		= NULL,
      @customerId				VARCHAR(50)		= NULL,
      @membershipId				VARCHAR(50)		= NULL,
      @firstName				VARCHAR(100)	= NULL,
      @middleName				VARCHAR(100)	= NULL,
      @lastName1				VARCHAR(100)	= NULL,
      @lastName2				VARCHAR(100)	= NULL,
      @country					VARCHAR(200)	= NULL,
      @nativeCountry			VARCHAR(200)	= NULL,
      @address					VARCHAR(500)	= NULL,
      @state					VARCHAR(200)	= NULL,
      @zipCode					VARCHAR(50)		= NULL,
      @city						VARCHAR(100)	= NULL,
      @email					VARCHAR(150)	= NULL,
      @homePhone				VARCHAR(100)	= NULL,
      @workPhone				VARCHAR(100)	= NULL,
      @mobile					VARCHAR(100)	= NULL,
      @relationship				VARCHAR(100)	= NULL,
      @sortBy					VARCHAR(50)		= NULL,
      @sortOrder				VARCHAR(5)		= NULL,
      @pageSize					INT				= NULL,
      @pageNumber				INT				= NULL,
      @receiverType				INT				= NULL,
      @idType					INT				= NULL,
      @idNumber					VARCHAR(25)		= NULL,
      @placeOfIssue				VARCHAR(80)		= NULL,
      @paymentMode				INT				= NULL,
      @bankLocation				VARCHAR(100)	= NULL,
      @payOutPartner			INT				= NULL,
      @bankName					VARCHAR(150)	= NULL,
      @receiverAccountNo		VARCHAR(40)		= NULL,
      @remarks					NVARCHAR(800)	= NULL,
      @purposeOfRemit			VARCHAR(100)	= NULL,
      @fromDate					NVARCHAR(20)	= NULL,
      @toDate					NVARCHAR(20)	= NULL,
	  @otherRelationDesc		VARCHAR(20)		= NULL,
      @RelationshipOther		VARCHAR(100)	= NULL,
      @PurposeOfRemitanceOther	VARCHAR(100)	= NULL,
	  @branchManual 			VARCHAR(100)	= NULL,
	  @loginBranchId			BIGINT			= NULL
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;
	 DECLARE @table VARCHAR(MAX) ,
            @select_field_list VARCHAR(MAX) ,
            @extra_field_list VARCHAR(MAX) ,
            @sql_filter VARCHAR(MAX);
		--,@customerId1			VARCHAR(50)
		DECLARE @PURPOSEID INT = NULL, @RELATION INT = NULL

		SELECT @PURPOSEID = valueId
		FROM STATICDATAVALUE (NOLOCK)
		WHERE detailTitle = @purposeOfRemit
		AND typeID = '3800'

		IF @PURPOSEID IS NULL
			SET @PURPOSEID = @purposeOfRemit

		SELECT @RELATION = valueId
		FROM STATICDATAVALUE (NOLOCK)
		WHERE detailTitle = @relationship
		AND typeID = '2100'

		IF @RELATION IS NULL
			SET @RELATION = @relationship
BEGIN
	IF @flag = 'i-new'
    BEGIN
		DECLARE @customerIdNew BIGINT
		IF NOT EXISTS(SELECT 'X' FROM CUSTOMERMASTER  WITH(NOLOCK) WHERE idnumber = @customerId)
		BEGIN
			SELECT '1' errorCode,''''+cast(@customerId as varchar) +  '''  customerId does not exists.' msg,null id
			RETURN
		END
		ELSE
		BEGIN
			SELECT 	@customerIdNew = customerId from customerMaster where idnumber = @customerId
		END

		SELECT @membershipId = membershipId from  CUSTOMERMASTER  WITH(NOLOCK) WHERE customerId = @customerIdNew
        INSERT  INTO receiverInformation
                      (fullname
						,membershipId ,customerId ,firstName ,middleName ,lastName1 ,lastName2 ,country ,NativeCountry,[address] ,[state] ,zipCode ,city ,email ,
                        homePhone ,workPhone ,mobile ,relationship ,receiverType ,idType ,idNumber ,placeOfIssue ,paymentMode ,bankLocation ,payOutPartner ,
                        bankName ,receiverAccountNo ,remarks ,purposeOfRemit ,createdBy ,createdDate,otherRelationDesc,agentId, relationOther, purposeOther, isCDDIUpdated,
						bankBranchName
		              )
                      SELECT    ISNULL(@firstName,'') + ISNULL(' ' + @middleName,'') +ISNULL(' ' + @lastName1,''),
						@membershipId ,@customerIdNew ,@firstName ,@middleName ,@lastName1 ,@lastName2 ,@country ,@nativeCountry,@address ,@state ,@zipCode ,@city ,@email ,
                              @homePhone ,@workPhone ,@mobile ,@RELATION ,@receiverType ,@idType ,@idNumber ,@placeOfIssue ,@paymentMode ,@bankLocation ,@payOutPartner ,
                              @bankName ,@receiverAccountNo ,@remarks ,@PURPOSEID ,@user ,GETDATE(),@otherRelationDesc,@loginBranchId, @RelationshipOther, @PurposeOfRemitanceOther, 1	,
							  @branchManual
		
		SET @receiverId=SCOPE_IDENTITY();						
              SELECT  '0' errorCode ,'Receiver Successfully added.' msg ,id = @receiverId,extra = cast(@customerIdNew as varchar) + '|' + cast(@membershipId as varchar);
              RETURN; 			
          END;
END

