

ALTER PROC proc_sendRequest

		@flag							VARCHAR(20)		
		,@user							VARCHAR(20)		=	NULL
		,@membershipId					VARCHAR(20)		=	NULL
		,@idNumber						VARCHAR(20)		=	NULL
		,@receiverFullName				varchar(200)	=	NULL
		,@receiveraddress 				varchar(100)	=	NULL
		,@receiverMobileNumber			varchar(20)		=	NULL
		,@pCountryId 					BIGINT			=	NULL
		,@pModeId 						BIGINT			=	NULL
		,@pagentId 						BIGINT			=	NULL
		,@branchId 						BIGINT			=	NULL
		,@branchManual 					VARCHAR(100)	=	NULL
		,@accountNumber 				varchar(50)		=	NULL
		,@cAmt 							MONEY			=	NULL
		,@tamt 							MONEY			=	NULL
		,@serviceCharge 				MONEY			=	NULL
		,@purposeOfRemittanceId			BIGINT			=	NULL
		,@otherPurposeOfRemittance		VARCHAR(100)	=	NULL
		,@relationShipId				BIGINT			=	NULL
		,@otherRelation					VARCHAR(100)	=	NULL
		,@referralName					VARCHAR(50)		=	NULL
		,@benefeciaryId					BIGINT			=	NULL
		,@bankName						VARCHAR(100)	=	NULL
		,@branchName					VARCHAR(100)	=	NULL
		,@collMode						VARCHAR(30)		=	NULL
		,@loginUser						VARCHAR(30)		=	NULL
AS
SET NOCOUNT ON ;
SET XACT_ABORT ON;
BEGIN
	IF @flag = 'sendRequest'
	BEGIN
		DECLARE @CUSTOMERID BIGINT, @rowId BIGINT
		IF NOT EXISTS (select 1 from customermaster where idnumber = @idNumber AND ISNULL(isActive, 'Y') = 'Y')
		BEGIN
			SELECT '1' ERROR_CODE, 'Customer with idNumber '''+@idNumber+''' does not exixts' Msg,@idNumber id
			RETURN
		END
		select @CUSTOMERID = CUSTOMERID,@membershipId = membershipId FROM CUSTOMERMASTER WHERE idNumber = @idNumber AND ISNULL(ISACTIVE, 'Y') = 'Y'
		--IF NOT EXISTS (SELECT 1 from receiverinformation where fullname = LTRIM(RTRIM(@receiverFullName))  and customerid = @CUSTOMERID)
		--BEGIN
		--	SELECT '1' ERROR_CODE, 'Receiver with name '''+@receiverFullName+''' does not exist of customer with membershipId '''+@membershipId+'''' Msg,@membershipId id
		--	RETURN
		--END
		--ALTER TABLE send_money_request ADD BranchManual VARCHAR(50)
		INSERT INTO send_money_request(IdNumber,MembershipId,ReceiverFullName,Receiveraddress
		,ReceiverMobileNumber,purposeOfRemittance,otherPurpose,relationShip,otherRelation,referralName
		,PCountryId,PmodeId,PagentId,BranchId,AccountNumber,CAmt,Tamt,ServiceCharge,createdby,createddate,BranchManual
		,pBankName, pBranchName, collMode,loginUser
		)
		values
		(
		@idNumber,@membershipId,@receiverFullName	,@receiveraddress
		,@receiverMobileNumber,@purposeOfRemittanceId,@otherPurposeOfRemittance,@relationShipId,@otherRelation,@referralName			
		,@pCountryId ,@pModeId,@pagentId ,@branchId ,@accountNumber ,@cAmt ,@tamt ,@serviceCharge,@user,Getdate(),@branchManual,
		@bankName, @branchName, @collMode,@loginUser
		)

		SET @rowId = @@IDENTITY

		SELECT '0' ERROR_CODE,'Request Send Successfully' Msg,@rowId id, cast(@CUSTOMERID as varchar) + '|' + cast(@membershipId as varchar) EXTRA
	END
	IF @flag = 'validateData'
	BEGIN
		SELECT @CUSTOMERID = CUSTOMERID 
		FROM CUSTOMERMASTER (NOLOCK)
		WHERE idNumber = @idNumber 
		AND ISNULL(ISACTIVE, 'Y') = 'Y'
		AND ISNULL(ISDELETED, 'N') = 'N'
		AND APPROVEDDATE IS NOT NULL

		IF @CUSTOMERID IS NULL
		BEGIN
			SELECT '1' errorCode, 'Customer with idNumber '+ISNULL(@idNumber, '')+' does not exixts or is not approved!' Msg,@idNumber id
			RETURN
		END

		--CHECKING CUSTOMER KYC STATUS
		DECLARE @kycStatus INT, @MSG VARCHAR(150)
		
		SELECT @kycStatus = kycStatus
		FROM TBL_CUSTOMER_KYC (NOLOCK) 
		WHERE CUSTOMERID = @CUSTOMERID
		AND ISDELETED = 0
		--AND kycStatus=11044
		ORDER BY KYC_DATE 

		
		IF ISNULL(@kycStatus, 0) <> 11044
		BEGIN
			IF @kycStatus IS NOT NULL
				SELECT @MSG = 'KYC for selected customer is not completed, it is in status:' + detailTitle FROM staticDataValue (NOLOCK) WHERE valueId = @kycStatus
			ELSE 
				SELECT @MSG = 'Please complete KYC status first'

			SELECT '1' ErrCode, @MSG Msg,NULL id
			RETURN
		END

		SELECT FULLNAME = RI.firstName + ISNULL(' '+RI.middleName, '') + ISNULL(' '+RI.lastName1, '') , RI.RECEIVERID, errorCode = 0,CM.FULLNAME CUSTOMERNAME,CM.MEMBERSHIPID
		FROM RECEIVERINFORMATION ri(NOLOCK) 
		INNER JOIN CUSTOMERMASTER CM (NOLOCK) ON CM.CUSTOMERID = RI.CUSTOMERID
		WHERE RI.CUSTOMERID = @CUSTOMERID
	END
	IF @flag = 'receiver-data'
	BEGIN
		SELECT FULLNAME, address, mobile, country, paymentMode, bankLocation, payOutPartner, city
				, homePhone, idType, idNumber, placeOfIssue, purposeOfRemit, relationship, relationOther, purposeOther, errorCode = 0
		FROM RECEIVERINFORMATION (NOLOCK)
		WHERE receiverId = @benefeciaryId
	END
END

