ALTER proc proc_customerInfo
	@flag			VARCHAR(30)	,
	@customerId		VARCHAR(20)	=	NULL,
	@user			VARCHAR(20)	=	NULL,
	@membershipId	VARCHAR(20) =	NULL,  
	@receiverId		BIGINT		=	NULL,  
	@rowId			BIGINT		=	NULL 
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
IF @flag = 'getCustomerAndReceiverInfo'
BEGIN
	DECLARE   @customerIdNew BIGINT
	select @customerIdNew = CUSTOMERID FROM CUSTOMERMASTER WHERE idnumber =@customerId
	IF cast(@customerIdNew  as varchar) IS NULL
	BEGIN
		SELECT '1' Error_Code,'Customer Does not exists' Msg,@CUSTOMERID id
		RETURN
	END
	SELECT fullname,membershipId FROM customerMaster WHERE CUSTOMERID = @customerIdNew
	--get bank and branch details
	SELECT * INTO #receiverId
	from 
	(
	SELECT receiverId from receiverinformation
	WHERE customerid = @customerIdNew
	)x

	
	SELECT * INTO #transactionDetails
	from 
	(
	SELECT  ROW_NUMBER() OVER( PARTITION BY REC.CUSTOMERID ORDER BY REC.id desc) rowId
			 ,CASE WHEN R.PCOUNTRY IN ('Nepal', 'Vietnam') THEN pBankBranchName
									ELSE CAST(A.BRANCH_ID AS VARCHAR) + '|' + A.BRANCH_NAME + ' - ' + A.BRANCH_CODE1 END pBranchName
			 ,R.pBankName
			 ,R.paymentMethod
			 ,R.accountno
			 ,REC.customerid receiverId
	FROM REMITTRAN R(NOLOCK)
	INNER JOIN TRANRECEIVERS REC(NOLOCK) ON REC.TRANID = R.ID
	LEFT JOIN API_BANK_BRANCH_LIST A(NOLOCK) ON A.BRANCH_ID = R.PBANKBRANCH
	LEFT JOIN PAYER_BANK_DETAILS AP(NOLOCK) ON AP.PAYER_ID = R.PayerId
	WHERE 1=1
	--AND R.PCOUNTRY = 'INDIA'
	AND REC.CUSTOMERID in (select receiverId from #receiverId)
	)x
	where rowId = 1

	select  ROW_NUMBER() OVER(ORDER BY RI.RECEIVERID) SN, 
			isnull(firstname,'') + isnull(' '+middleName,'') + isnull(' ' + lastname1,'') [fullname],
			isnull(address,'-') address,
			isnull(mobile,'-') mobile,
			ISNULL(TD.paymentMethod,case when ri.paymentMode = '1' then 'Cash Payment' 
										when ri.paymentMode = '2' then 'Bank Deposit'
										else 'Mobile Wallet' END) PaymentMethod,
			ISNULL(TD.pBankName,ISNULL(ABL.BANK_NAME,'-')) bankName,
			ISNULL(TD.pBranchName,CASE WHEN RI.COUNTRY IN ('Nepal', 'Vietnam') THEN bankBranchName
									ELSE CAST(A.BRANCH_ID AS VARCHAR) + '|' + A.BRANCH_NAME + ' - ' + A.BRANCH_CODE1 END) branchName,
			ISNULL(TD.accountNo,ISNULL(RI.receiverAccountNo,'-')) accountNo
	 from receiverinformation ri(nolock)
	LEFT JOIN #transactionDetails TD  ON TD.RECEIVERID = RI.RECEIVERID
	LEFT JOIN API_BANK_LIST ABL (NOLOCK) ON ABL.BANK_ID = RI.payOutPartner
	LEFT JOIN API_BANK_BRANCH_LIST A(NOLOCK) ON cast(A.BRANCH_ID as varchar) = RI.BANKLOCATION
	WHERE RI.CUSTOMERID = @customerIdNew
END
IF @flag = 'details-fromMembershipId'  
BEGIN  
  SELECT @customerId = CUSTOMERID  FROM CUSTOMERMASTER WHERE MEMBERSHIPID = @membershipId   
  SELECT *    
   FROM (SELECT fileName,   
		fileType,   
		documentType = detailTitle,  
		ROW_NUMBER()OVER(PARTITION BY SV.detailTitle ORDER BY CD.createdDate DESC)rn  
		FROM customerDocument CD(NOLOCK)  
		INNER JOIN STATICDATAVALUE SV(NOLOCK) ON SV.valueId = CD.documentType  
		WHERE ISNULL(isDeleted, 'N') = 'N'  
		AND customerId = @customerId  
		AND valueId IN (11054, 11055, 11056, 11057)  
		)X WHERE rn=1  
  
        SELECT    
                       cm.customerId,  
                       cm.createdDate,  
					   customerType = TYP.detailTitle,  
                       cm.fullName ,  
						CM.membershipId,  
                       cmb.countryName AS [country] ,  
						cm.zipcode,  
                       email,  
                       sdg.detailTitle AS [gender] ,  
                       cmn.countryName AS [nativeCountry] ,  
					   [address] = ' '+ ISNULL(substring(cm.zipcode,1,3),'') + '-' +  ISNULL(substring(cm.zipcode,4,7),'') + isnull(', ' + csm.stateName,'')+ISNULL('-'+ detail.state_japanese,'') +  isnull(', ' +cm.city,'')+ISNULL('-'+ detail.city_japanese,'') +  isnull(', ' + cm.street,'')+ISNULL('-'+ detail.street_japanese,'') + +isnull(', ' +cm.ADDITIONALADDRESS,''),
                       cm.city ,  
                       COALESCE(cm.telNo,cm.homePhone) telNo,  
                       cm.mobile ,  
                       sdo.detailTitle AS [occupation] ,  
						cm.occupationOther,  
                       sdi.detailTitle AS [idType] ,  
                       cm.idType AS [idTypeCode] ,  
                       UPPER(cm.idNumber) idNumber,  
                       CONVERT(VARCHAR(10), dob, 121) AS [dob] ,  
                       CONVERT(VARCHAR(10), idIssueDate, 121) AS [idIssueDate] ,  
                       CONVERT(VARCHAR(10), idExpiryDate, 121) AS [idExpiryDate] ,  
						sdv.detailDesc visaStatus,  
						sdv1.detailDesc employeeBusinessType,  
						cm.nameOfEmployeer,  
						cm.SSNNO,  
						sdv2.detailDesc sourceOfFund,  
						cm.monthlyIncome,  
						case cm.remittanceAllowed WHEN 1 THEN 'Yes' ELSE 'No' END remittanceAllowed,  
						CASE cm.onlineUser WHEN 'Y' THEN 'Yes' ELSE 'No' END onlineUser,  
						cm.remarks  
               FROM    customerMaster cm ( NOLOCK )  
				 LEFT JOIN staticDataValue TYP(NOLOCK) ON TYP.valueId = cm.customerType  
				 LEFT JOIN staticDataValue sdg ( NOLOCK ) ON sdg.valueId = cm.gender  
				 LEFT JOIN dbo.countryMaster cmb ( NOLOCK ) ON cmb.countryId = cm.country  
				 LEFT JOIN dbo.countryMaster cmn ( NOLOCK ) ON cmn.countryId = cm.nativeCountry  
				 LEFT JOIN staticDataValue sdo ( NOLOCK ) ON sdo.valueId = cm.occupation  
				 LEFT JOIN staticDataValue sdi ( NOLOCK ) ON sdi.valueId = cm.idType  
				 LEFT JOIN countryStateMaster CSM (NOLOCK) ON CSM.stateId = CAST(cm.state AS VARCHAR)  
				 LEFT JOIN dbo.staticDataValue sdv (NOLOCK) ON sdv.valueId = cm.visaStatus  
				 LEFT JOIN dbo.staticDataValue sdv1 (NOLOCK) ON sdv1.valueId = cm.employeeBusinessType  
				 LEFT JOIN dbo.staticDataValue sdv2 (NOLOCK) ON sdv2.valueId = cm.sourceOfFund  
				 LEFT JOIN tbl_japan_address_detail detail (NOLOCK) ON detail.row_id = cm.district
               WHERE   customerId = @customerId;  
  
   SELECT			ri.receiverId ,  
				  ri.fullName customerName,  
                   ri.customerId ,  
                   ri.firstName ,  
                   COM.membershipId ,  
                   ri.middleName ,  
                   ri.lastName1 ,  
                   ri.lastName2 ,  
                   ri.country,  
                   ri.address ,  
                   ri.state ,  
                   ri.zipCode ,  
                   cm.countryId ,  
       cm1.countryname [NativeCountry],  
                   ri.city ,  
                   ri.email ,  
                   ri.homePhone ,  
                   ri.workPhone ,  
                   ri.mobile ,  
       SDV.detailTitle relationship,  
       ri.otherRelationDesc,  
        
       SDV1.detailTitle purposeOfRemit,  
       SDV2.detailTitle receiverType,  
                   SDV3.detailTitle idType ,  
                   ri.idNumber ,  
                   ri.placeOfIssue ,  
                   CASE WHEN ri.paymentmode = '1' THEN 'Cash Payment' ELSE 'Bank Deposit' END paymentMode ,  
                   ISNULL(ABBL.BRANCH_NAME ,'-')  bankBranchName ,  
                   ISNULL(CASE WHEN ri.country = 'NEPAL' THEN ISNULL(ABL.BANK_NAME,'ANY WHERE') ELSE  ISNULL(ABL.BANK_NAME ,'-') END,'-') payOutPartner ,  
                   ISNULL(ri.bankName,'-') bankName,  
                   ISNULL(ri.receiverAccountNo,'-') receiverAccountNo,  
                   ISNULL(ri.remarks,'-') remarks,  
                   COM.createdDate,  
       ri.purposeOther [otherPurpose]  
           FROM    receiverInformation ri WITH ( NOLOCK )  
                   LEFT JOIN countryMaster cm WITH ( NOLOCK ) ON ri.country = cm.countryName  
     LEFT JOIN dbo.customerMaster COM(NOLOCK) ON com.customerId=ri.customerId  
     LEFT JOIN countrymaster CM1 (NOLOCK) ON CM1.COUNTRYID = ri.nativecountry  
     LEFT JOIN STATICDATAVALUE SDV (NOLOCK) ON SDV.VALUEID = RI.RELATIONSHIP  
     LEFT JOIN STATICDATAVALUE SDV1 (NOLOCK) ON SDV1.VALUEID = RI.PURPOSEOFREMIT  
     LEFT JOIN STATICDATAVALUE SDV2 (NOLOCK) ON SDV2.VALUEID = RI.RECEIVERTYPE  
     LEFT JOIN STATICDATAVALUE SDV3 (NOLOCK) ON SDV3.VALUEID = RI.idtype  
     LEFT JOIN API_BANK_BRANCH_LIST ABBL (NOLOCK) ON cast(ABBL.BRANCH_ID as varchar) = RI.BANKLOCATION  
     LEFT JOIN API_BANK_LIST ABL (NOLOCK) ON ABL.BANK_ID = RI.PAYOUTPARTNER  
     LEFT JOIN dbo.customerMaster (NOLOCK) cmm ON cmm.customerId = ri.customerId  
           WHERE   receiverId = @receiverId ;  
  
    select cmm.FullName custFullname,  
     upper(smq.idnumber) idNumber,  
     receiverFullName,  
     receiverAddress,  
     receiverMobileNumber,  
     accountNumber,  
     cAmt,  
     tAmt,  
     serviceCharge,  
     purpose.detailTitle purposeOfRemit,  
     relation.detailTitle relationship,  
     cm.countryName pcountry,  
     CASE WHEN smq.pModeId = '1' THEN 'Cash Payment' ELSE 'Bank Deposit' END pMode,   
     BANK_NAME = pBankName,  
     BRANCH_NAME = ISNULL(pBranchName, BranchManual),  
     smq.otherPurpose,  
     smq.otherRelation  
   FROM send_money_request smq (NOLOCK)   
   INNER JOIN customermaster cmm (NOLOCK) ON cmm.idnumber = smq.idNumber  
   LEFT JOIN  STATICDATAVALUE purpose (NOLOCK) ON purpose.valueId = smq.purposeOfRemittance  
   LEFT JOIN  STATICDATAVALUE relation (NOLOCK) ON relation.valueId = smq.relationShip  
   LEFT JOIN COUNTRYMASTER CM (NOLOCK) ON CM.COUNTRYID = SMQ.pCountryId  
    where rowId = @rowId  
     END;  
END
