SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO

--EXEC [proc_GetDetailForDllTxn] @SenderId=88,@ReceiverId=170

ALTER PROCEDURE [dbo].[proc_GetDetailForDllTxn] 
@SenderId			INT,
@ReceiverId			INT = NULL

AS
	SET NOCOUNT ON;
	SET XACT_ABORT ON;

IF NOT EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @SenderId AND approvedDate IS NOT NULL AND isActive = 'Y')
BEGIN
	EXEC proc_errorHandler '1','Invalid customer found for operation',null
	RETURN
END
IF NOT EXISTS(SELECT 'A' FROM receiverInformation(NOLOCK) WHERE customerId = @SenderId AND receiverId = @ReceiverId)
BEGIN
	EXEC proc_errorHandler '1','Invalid receiver found for operation',null
	RETURN
END

IF NOT EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @SenderId AND sourceOfFund IS NULL)
	UPDATE customerMaster SET sourceOfFund = 'Family maintenance' WHERE customerId = @SenderId

IF NOT EXISTS(SELECT 'A' FROM receiverInformation(NOLOCK) WHERE customerId = @SenderId AND receiverId = @ReceiverId  AND purposeOfRemit IS NULL)
	UPDATE receiverInformation SET purposeOfRemit = 'Family maintenance' WHERE customerId = @SenderId AND receiverId = @ReceiverId

IF NOT EXISTS(SELECT 'A' FROM receiverInformation(NOLOCK) WHERE customerId = @SenderId AND receiverId = @ReceiverId  AND city IS NULL)
	UPDATE receiverInformation SET city = LEFT(ISNULL(address,'any city'),30) WHERE customerId = @SenderId AND receiverId = @ReceiverId


SELECT TOP 1 nativeCountry AS ScountryId,ISNULL(sourceOfFund,'Family maintenance') AS SourceOfFund,sourceOfFund AS SourceOfFundId , 1 AS ROWID, address AS SenderAddress
INTO #SENDER
FROM customerMaster(NOLOCK) WHERE customerId = @SenderId


SELECT TOP 1 FirstName AS FirstName,MiddleName AS MiddleName,LastName = lastName1+ISNULL(lastName2,''),NativeCountry = R.country,C.countryId AS PcountryId
		,R.mobile AS MobileNo,R.city AS City,R.address AS [Address],DBO.FNAGetState(R.state,R.country) AS StateId,DBO.FNAGetDistrict(R.district,R.country) AS DistrictId
		,ISNULL(R.relationship,'Friend') AS RelWithSender,R.relationship AS RelWithSenderId,ISNULL(R.purposeOfRemit,'Family maintenance') AS PurposeOfRemittanceId,ISNULL(R.purposeOfRemit,'Family maintenance') AS PurposeOfRemittance , 1 AS ROWID
INTO #RECEIVER
FROM receiverInformation(NOLOCK) R
INNER JOIN countryMaster(NOLOCK) C ON C.countryName = R.country 
WHERE customerId = @SenderId AND receiverId = @ReceiverId 

SELECT 0 as ErrorCode ,* FROM #SENDER S
INNER JOIN #RECEIVER R ON R.ROWID = S.ROWID



GO

