USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[mobile_proc_online_customerMaster_V1]    Script Date: 10/10/2023 9:49:53 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[mobile_proc_online_customerMaster_V1] (
	--EXEC mobile_proc_online_customerMaster_V1 @flag='detail',@customerId='3'
	@flag VARCHAR(20)
	,@customerId VARCHAR(100) = NULL
	,@username VARCHAR(100) = NULL
	,@postalCode VARCHAR(100) = NULL
	,@address1 NVARCHAR(200) = NULL
	,@address2 NVARCHAR(200) = NULL
	,@city VARCHAR(100) = NULL
	,@idType VARCHAR(100) = NULL
	,@idTypeNumber VARCHAR(100) = NULL
	,@idIssuingCountry VARCHAR(5) = NULL
	,@idStartDate VARCHAR(20) = NULL
	,@idEndDate VARCHAR(20) = NULL
	)
AS
	;

SET NOCOUNT ON;
SET XACT_ABORT ON;

BEGIN
	IF @flag = 'detail'
	BEGIN
		--ID TYPE
		SELECT valueId AS id
			,detailTitle AS [text]
			,isBackRequired = CASE sv.valueId
				WHEN '10997'
					THEN 0
				ELSE 1
				END
		INTO #ID_TYPE
		FROM countryIdType CID WITH (NOLOCK)
		INNER JOIN staticDataValue SV WITH (NOLOCK) ON CID.IdTypeId = SV.valueId
		WHERE ISNULL(SV.ISActive, 'Y') = 'Y'
			AND ISNULL(isDeleted, 'N') <> 'Y'
			AND COUNTRYID = 233

		IF EXISTS (
				SELECT 'x'
				FROM dbo.customerMasterTemp(NOLOCK)
				WHERE customerId = @customerId
				)
		BEGIN
			SELECT TOP 1 cmt.firstName
				,fullName
				,gender = CASE 
					WHEN gender = 97
						THEN 'M'
					WHEN gender = 98
						THEN 'F'
					ELSE NULL
					END
				,idType = CASE 
					WHEN ID.ID = '11402'
						THEN CMT.otherIdNumber
					ELSE CAST(ID.id AS VARCHAR)
					END
				,CONVERT(VARCHAR(10), dob, 120) AS dob
				,email AS email
				,mobile
				,city
				,ISNULL(zipCode, postalCode)
				,address address1
				,ISNULL(city, additionalAddress) address2
				,nativeCountry
				,cm.countryName 
				,idNumber 
				,cmt.idIssueDate
				,cmt.idExpiryDate
				,idIssueCountry
				,KycStatus = ISNULL(verificationCode, 'NOT_COMPLETED')
				,KycStatusMsg = CASE verificationCode
					WHEN 'NOT_COMPLETED'
						THEN 'Not Completed'
					WHEN 'PROCESSING'
						THEN 'ID Document Submission is in Processing'
					WHEN 'COMPLETED'
						THEN 'KYC Completed'
					ELSE 'NOT COMPLETED'
					END
					
			FROM dbo.customerMasterTEMP(NOLOCK) cmt
			LEFT JOIN #ID_TYPE ID ON ID.id = cmt.idType
			LEFT JOIN dbo.countryMaster(NOLOCK) cm ON cm.countryId = cmt.nativeCountry
			WHERE customerId = @customerId
		END
		ELSE
		BEGIN
			SELECT TOP 1 cmt.firstName
				,fullName
				,gender = CASE 
					WHEN gender = 97
						THEN 'M'
					WHEN gender = 98
						THEN 'F'
					ELSE NULL
					END
				,idType = CASE 
					WHEN ID.ID = '11402'
						THEN CMT.otherIdNumber
					ELSE CAST(ID.id AS VARCHAR)
					END
				,CONVERT(VARCHAR(10), dob, 120) AS dob
				,email AS email
				,mobile
				,city
			     ,zipCode
				,address  address1
				,additionalAddress address2
				,nativeCountry
				,cm.countryName 
				,idNumber 
				,CONVERT(VARCHAR(10), cmt.idIssueDate, 120) as  idIssueDate
				,CONVERT(VARCHAR(10), cmt.idExpiryDate, 120) as idExpiryDate
				,cmt.idType
				,icm.countryName as ICountryName
				,KycStatus = ISNULL(verificationCode, 'NOT_COMPLETED')
				,KycStatusMsg = CASE verificationCode
					WHEN 'NOT_COMPLETED'
						THEN 'Not Completed'
					WHEN 'PROCESSING'
						THEN 'ID Document Submission is in Processing'
					WHEN 'COMPLETED'
						THEN 'KYC Completed'
					ELSE 'NOT COMPLETED'
					END
					,'0' errorCode 
					,'Success' msg
			FROM dbo.customerMaster(NOLOCK) cmt
			LEFT JOIN #ID_TYPE ID ON ID.id = cmt.idType
			LEFT JOIN dbo.countryMaster(NOLOCK) cm ON cm.countryId = cmt.nativeCountry
			LEFT JOIN dbo.countryMaster(NOLOCK) icm On icm.countryId= cmt.idIssueCountry
			WHERE customerId = @customerId
		END
	END

	IF @flag = 'update-id-Kyc'
	BEGIN
		IF EXISTS (
				SELECT 'X'
				FROM dbo.customerMasterTemp(NOLOCK)
				WHERE customerId = @customerId
				)
		BEGIN
			UPDATE dbo.customerMasterTemp
			SET idType = ISNULL(@idType, idType)
				,idNumber = ISNULL(@idTypeNumber, idNumber)
				,idExpiryDate = ISNULL(@idEndDate, idExpiryDate)
				,idIssueDate = ISNULL(@idStartDate, idIssueDate)
				,idIssueCountry = ISNULL(@idIssuingCountry, idIssueCountry)
			WHERE customerId = @customerId

			SELECT '0' ErrorCode
			,'Customer update successfully.' Msg
			,@customerId id
			return

		END
		ELSE
		BEGIN
			UPDATE dbo.customerMaster
			SET idType = ISNULL(@idType, idType)
				,idNumber = ISNULL(@idTypeNumber, idNumber)
				,idExpiryDate = ISNULL(@idEndDate, idExpiryDate)
				,idIssueDate = ISNULL(@idStartDate, idIssueDate)
				,idIssueCountry = ISNULL(@idIssuingCountry, idIssueCountry)
			WHERE customerId = @customerId
	
			SELECT '0' ErrorCode
			,'Customer updated successfully.' Msg
			,@customerId id
		return
		END
	END
	IF @flag = 'update-address'
	BEGIN
	IF EXISTS (
				SELECT 'X'
				FROM dbo.customerMasterTemp(NOLOCK)
				WHERE customerId = @customerId
				)
		BEGIN
			UPDATE dbo.customerMasterTemp
			SET zipCode = ISNULL(@postalCode, zipCode)
				,address = ISNULL(@address1, address)
				,ADDITIONALADDRESS = ISNULL(@address2, ADDITIONALADDRESS)
				,city = ISNULL(@city, city)
			WHERE customerId = @customerId
	
		SELECT '0' ErrorCode
			,'Customer update successfully.' Msg
			,@customerId id
			return
		
		END
		ELSE
		--print 'update'
		BEGIN
			UPDATE dbo.customerMaster
			SET zipCode = ISNULL(@postalCode, zipCode)
				,address = ISNULL(@address1, address)
				,ADDITIONALADDRESS = ISNULL(@address2, ADDITIONALADDRESS)
				,city = ISNULL(@city, city)
			WHERE customerId = @customerId
		
	SELECT '0' ErrorCode
			,'Customer update successfully.' Msg
			,@customerId id
			return
	 
		

		
		END

	END
END
