You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
11 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_sendIntlReceipt] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_sendIntlReceipt]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_sendIntlReceipt] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. CREATE PROC [dbo].[proc_sendIntlReceipt] (
  12. @flag VARCHAR(50)
  13. ,@user VARCHAR(30) = NULL
  14. ,@controlNo VARCHAR(30) = NULL
  15. ,@tranId INT = NULL
  16. ,@msgType CHAR(1) = NULL
  17. ,@sortBy VARCHAR(50) = NULL
  18. ,@sortOrder VARCHAR(5) = NULL
  19. ,@pageSize INT = NULL
  20. ,@pageNumber INT = NULL
  21. )
  22. AS
  23. SET NOCOUNT ON;
  24. DECLARE @controlNoEncrypted VARCHAR(20)
  25. SELECT @controlNoEncrypted = dbo.FNAEncryptString(@controlNo)
  26. IF @flag = 'receipt'
  27. BEGIN
  28. DECLARE @voucherNo VARCHAR(20), @bonusSchemeId int,@holdTranId INT
  29. --6.Message
  30. DECLARE @headMsg NVARCHAR(MAX)
  31. ,@commonMsg NVARCHAR(MAX)
  32. ,@countrySpecificMsg NVARCHAR(MAX)
  33. ,@bonusPointPending MONEY
  34. ,@bonusPoint MONEY
  35. ,@customerId INT
  36. ,@sAgent INT
  37. ,@sCountry VARCHAR(50)
  38. ,@sBranch INT
  39. ,@rCountry VARCHAR(50)
  40. ,@rAgent INT
  41. ,@sUser VARCHAR(50)
  42. ,@sUserFullName VARCHAR(75)
  43. ,@paymentMethod VARCHAR(75)
  44. SELECT
  45. @tranId = rt.id
  46. ,@holdTranId = rt.holdTranId
  47. ,@sCountry = sc.countryId
  48. ,@sAgent = sAgent
  49. ,@sBranch = sBranch
  50. ,@rCountry = rc.countryId
  51. ,@rAgent = pAgent
  52. ,@sUser = rt.createdBy
  53. ,@voucherNo = voucherNo
  54. ,@customerId = sen.customerId
  55. ,@paymentMethod = rt.paymentMethod
  56. FROM vwRemitTran rt WITH(NOLOCK)
  57. LEFT JOIN vwTranSenders sen WITH(NOLOCK) ON rt.id = sen.tranId
  58. INNER JOIN countryMaster sc WITH(NOLOCK) ON rt.sCountry = sc.countryName
  59. LEFT JOIN countryMaster rc WITH(NOLOCK) ON rt.pCountry = rc.countryName
  60. WHERE rt.controlNo = @controlNoEncrypted
  61. SELECT
  62. tranId = ISNULL(trn.holdTranId, trn.id)
  63. ,controlNo = dbo.FNADecryptString(trn.controlNo)
  64. --Sender Information
  65. ,sMemId = sen.membershipId
  66. ,sen.companyName
  67. ,sCustomerId = sen.customerId
  68. ,senderName = sen.firstName + ISNULL( ' ' + sen.middleName, '') + ISNULL( ' ' + sen.lastName1, '') + ISNULL( ' ' + sen.lastName2, '')
  69. ,sCountryName = sen.country
  70. ,sNativeCountry=sen.nativeCountry
  71. ,sAddress = sen.address
  72. ,sContactNo = COALESCE(sen.mobile, sen.homephone, sen.workphone)
  73. ,sIdType = sen.idType
  74. ,sIdNo = sen.idNumber
  75. ,Email = sen.email
  76. --Receiver Information
  77. ,rMemId = rec.membershipId
  78. ,idExpiry = ISNULL(CONVERT(VARCHAR,sen.validDate,101),'-')
  79. ,rCustomerId = rec.customerId
  80. ,receiverName = rec.firstName + ISNULL( ' ' + rec.middleName, '') + ISNULL( ' ' + rec.lastName1, '') + ISNULL( ' ' + rec.lastName2, '')
  81. ,rCountryName = rec.country
  82. ,rAddress = rec.address
  83. ,rContactNo = COALESCE(rec.mobile, rec.homephone, rec.workphone)
  84. ,rIdType = rec.idType
  85. ,rIdNo = rec.idNumber
  86. ,relWithSender = trn.relWithSender
  87. --Sending Agent Information
  88. ,sAgentName = CASE WHEN trn.sAgentName = trn.sBranchName THEN trn.sSuperAgentName ELSE trn.sAgentName END
  89. ,sBranchName = trn.sBranchName
  90. ,sAgentCountry = sa.agentCountry
  91. ,headMsg = sa.headMessage
  92. ,sAgentLocation = sLoc.districtName
  93. ,sAgentAddress = sa.agentAddress
  94. ,agentPhone1 = sa.agentPhone1
  95. --Payout Agent Information
  96. ,pAgentCountry = trn.pCountry
  97. ,trn.sCountry
  98. ,pAgent = trn.pAgentName
  99. ,pAgentDistrict = rec.district
  100. ,pAgentLocation = rec.state
  101. ,sPremium = ISNULL(ROUND((tAmt * schemePremium) / customerRate, 4),0)
  102. ,exRatePremium = ISNULL(schemePremium,0)
  103. ,pPremium = ISNULL((tAmt * schemePremium),0)
  104. ,premiumDisc = 0
  105. ,trn.collMode
  106. ,trn.tAmt
  107. ,trn.serviceCharge
  108. ,handlingFee = ISNULL(trn.handlingFee, 0)
  109. ,netServiceCharge = serviceCharge
  110. ,totalServiceCharge = serviceCharge + ISNULL(handlingFee, 0)
  111. ,perAmt = '1'
  112. ,scAmt2 = serviceCharge - ISNULL(trn.handlingFee, 0)
  113. ,exRate = customerRate + ISNULL(schemePremium, 0)
  114. ,trn.cAmt
  115. ,trn.pAmt
  116. ,trn.paymentMethod
  117. ,paymentMode = CASE trn.paymentMethod WHEN 'Cash Payment' THEN 'Cash Pay' WHEN 'Bank Deposit' THEN 'Bank Transfer' ELSE trn.paymentMethod END
  118. ,stm.category
  119. ,pBankName = ISNULL(trn.pBankName, '[ANY WHERE] - ' + trn.pCountry)
  120. ,trn.pBranchName
  121. ,BankName = trn.pBankName
  122. ,BranchName = trn.pBankBranchName
  123. ,headMsg = sa.headMessage
  124. ,trn.accountNo
  125. ,trn.pCountry
  126. ,relationship = ISNULL(trn.relWithSender, '-')
  127. ,purpose = ISNULL(trn.purposeOfRemit, '-')
  128. ,sourceOfFund = ISNULL(trn.sourceOfFund, '-')
  129. ,occupation = ISNULL(sen.occupation,'-')
  130. ,collMode = trn.collMode
  131. ,trn.collCurr
  132. ,paymentMethod = trn.paymentMethod
  133. ,trn.payoutCurr
  134. ,trn.tranStatus
  135. ,trn.payStatus
  136. ,payoutMsg = ISNULL(trn.pMessage, '-')
  137. ,trn.createdBy
  138. ,createdDate = trn.createdDate
  139. ,trn.approvedBy
  140. ,trn.approvedDate
  141. ,trn.paidBy
  142. ,trn.paidDate
  143. ,trn.payTokenId
  144. ,trn.createdDateLocal
  145. ,SCH.condition [schemeAction]
  146. ,trn.handlingFee [schemeFee]
  147. ,iTelSoftPinNumber = ''
  148. FROM vwRemitTran trn WITH(NOLOCK)
  149. INNER JOIN vwTranSenders sen WITH(NOLOCK) ON trn.id = sen.tranId
  150. INNER JOIN vwTranReceivers rec WITH(NOLOCK) ON trn.id = rec.tranId
  151. LEFT JOIN serviceTypeMaster stm WITH(NOLOCK) ON trn.paymentMethod = stm.typeTitle
  152. LEFT JOIN agentMaster sa WITH(NOLOCK) ON trn.sBranch = sa.agentId
  153. LEFT JOIN agentMaster pa WITH(NOLOCK) ON trn.pBranch = pa.agentId
  154. LEFT JOIN api_districtList sLoc WITH(NOLOCK) ON sa.agentLocation = sLoc.districtCode
  155. LEFT JOIN schemeSetup SCH WITH (NOLOCK) ON SCH.rowId = trn.SchemeId
  156. WHERE trn.controlNo = @controlNoEncrypted
  157. ----------------------------------------------------------------------------------------------
  158. SELECT
  159. @sUserFullName = firstName + ISNULL(' ' + middleName, '') + ISNULL(' ' + lastName, '')
  160. FROM applicationUsers WITH(NOLOCK) WHERE userName = @sUser
  161. SELECT @sCountry = countryId FROM countryMaster WITH(NOLOCK)
  162. WHERE countryName = (SELECT agentCountry FROM agentMaster WHERE agentId = @sAgent)
  163. --Head Message
  164. SELECT @headMsg = headMessage FROM agentMaster WHERE agentId = @sBranch
  165. --print @sAgent
  166. IF @headMsg IS NULL
  167. SELECT @headMsg = agentDetails FROM agentMaster WHERE agentId = @sAgent
  168. IF @headMsg IS NULL
  169. SELECT @headMsg = headMsg FROM [message] WHERE countryId = @sCountry AND ISNULL(isDeleted, 'N') = 'N' AND ISNULL(isActive, 'Inactive') = 'Active'
  170. IF(@headMsg IS NULL)
  171. SELECT @headMsg = headMsg FROM [message] WITH(NOLOCK) WHERE countryId IS NULL AND headMsg IS NOT NULL AND ISNULL(isDeleted, 'N') = 'N' AND ISNULL(isActive, 'Inactive') = 'Active'
  172. --Common Message
  173. SELECT @commonMsg = commonMsg FROM [message] WITH(NOLOCK) WHERE countryId = @sCountry AND ISNULL(isDeleted, 'N') = 'N' AND ISNULL(isActive, 'Inactive') = 'Active'
  174. IF(@commonMsg IS NULL)
  175. SELECT @commonMsg = commonMsg FROM [message] WITH(NOLOCK) WHERE countryId IS NULL AND commonMsg IS NOT NULL AND ISNULL(isDeleted, 'N') = 'N' AND ISNULL(isActive, 'Inactive') = 'Active'
  176. --Country Specific Message
  177. DECLARE @countryMsg TABLE(sCountry INT,sAgent INT,rCountry INT,rAgent INT,countrySpecificMsg NVARCHAR(MAX), paymentType varchar(100))
  178. INSERT INTO @countryMsg(sCountry,sAgent,rCountry,rAgent,countrySpecificMsg, paymentType)
  179. SELECT countryId,agentId,rCountry,rAgent,countrySpecificMsg,typeTitle
  180. FROM [message] M WITH(NOLOCK)
  181. LEFT JOIN serviceTypeMaster S WITH(NOLOCK) ON M.transactionType=S.serviceTypeId
  182. WHERE isnull(rCountry,'') = isnull(@rCountry,'') AND ISNULL(M.isDeleted, 'N') = 'N'
  183. AND ISNULL(M.isActive, 'Inactive') = 'Active' AND (msgType = 'S' OR msgType ='B')
  184. IF @countrySpecificMsg IS NULL
  185. SELECT @countrySpecificMsg = countrySpecificMsg FROM @countryMsg WHERE sCountry = @sCountry
  186. AND ISNULL(sAgent,'0') = ISNULL(@sAgent,ISNULL(sAgent,'0')) AND ISNULL(rCountry,'0') = ISNULL(@rCountry,ISNULL(rCountry,'0'))
  187. AND ISNULL(rAgent,'0')=ISNULL(@rAgent,ISNULL(rAgent,0))
  188. IF @countrySpecificMsg IS NULL
  189. SELECT @countrySpecificMsg = countrySpecificMsg FROM @countryMsg WHERE sCountry = @sCountry AND ISNULL(sAgent,'0') = ISNULL(@sAgent,ISNULL(sAgent,'0')) AND ISNULL(rCountry,'0') = ISNULL(@rCountry,ISNULL(rCountry,'0'))
  190. IF @countrySpecificMsg IS NULL
  191. SELECT @countrySpecificMsg = countrySpecificMsg FROM @countryMsg WHERE sCountry = @sCountry AND ISNULL(sAgent,'0') = ISNULL(@sAgent,ISNULL(sAgent,'0'))
  192. IF @countrySpecificMsg IS NULL
  193. SELECT TOP 1 @countrySpecificMsg = countrySpecificMsg FROM @countryMsg WHERE rCountry = @rCountry AND ISNULL(paymentType,'0') = ISNULL(@paymentMethod,ISNULL(paymentType,'0'))
  194. IF @countrySpecificMsg IS NULL
  195. SELECT TOP 1 @countrySpecificMsg = countrySpecificMsg FROM @countryMsg WHERE rCountry = @rCountry
  196. --print @countrySpecificMsg
  197. IF @bonusSchemeId is not null
  198. SELECT @bonusPointPending= bonusPointPending,
  199. @bonusPoint=isnull(bonusPoint,0) - isnull(Redeemed,0)
  200. FROM customers WITH (NOLOCK)
  201. where isnull(isDeleted,'N') <> 'Y' and customerId = @customerId
  202. SELECT @headMsg AS headMsg,@commonMsg AS commonMsg, @countrySpecificMsg AS countrySpecificMsg, @sUserFullName AS sUserFullName
  203. ,ISNULL(@bonusPointPending,0) bonusPointPending,ISNULL(@bonusPoint,0) bonusPoint,@bonusSchemeId [IsBonusOffer]
  204. --SELECT collMode,ISNULL(amt,0) AMT,collDate,@voucherNo [voucherNo]
  205. -- FROM collectionDetails WITH (NOLOCK)
  206. --WHERE tranId = @tranId
  207. SELECT
  208. bankName = ISNULL(B.bankName, 'Cash')
  209. ,collMode
  210. ,amt = ISNULL(amt, 0)
  211. ,collDate
  212. ,voucherNo = @voucherNo
  213. ,narration
  214. FROM collectionDetails C WITH (NOLOCK)
  215. LEFT JOIN countryBanks B WITH (NOLOCK) ON C.countryBankId = B.countryBankId
  216. WHERE tranId = @holdTranId
  217. END
  218. ELSE IF @flag = 'c' --Check Transaction Control No
  219. BEGIN
  220. DECLARE @tranStatus VARCHAR(20)
  221. SELECT @tranStatus = tranStatus FROM vwRemitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
  222. IF @tranStatus IS NULL
  223. BEGIN
  224. EXEC proc_errorHandler 1, 'Transaction Not Found', @controlNoEncrypted
  225. RETURN
  226. END
  227. --IF @tranStatus = 'Cancel'
  228. --BEGIN
  229. -- EXEC proc_errorHandler 1, 'Transaction Not Found', @controlNoEncrypted
  230. -- RETURN
  231. --END
  232. --IF @tranStatus = 'Block'
  233. --BEGIN
  234. -- EXEC proc_errorHandler 1, 'Transaction is blocked. Please Contact Head Office.', @controlNoEncrypted
  235. -- RETURN
  236. --END
  237. --DECLARE @agentId INT
  238. --SELECT @agentId = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user
  239. --IF @agentId = dbo.FNAGetHOAgentId()
  240. --BEGIN
  241. -- EXEC proc_errorHandler 0, 'Transaction Found', @controlNoEncrypted
  242. -- RETURN
  243. --END
  244. --SET @controlNoEncrypted = dbo.FNADEcryptString(@controlNoEncrypted)
  245. --IF NOT EXISTS(SELECT 'X' FROM vwRemitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted AND
  246. --(sBranch = (SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user)))
  247. --BEGIN
  248. -- EXEC proc_errorHandler 1, 'You are not authorized to view this transaction', @controlNoEncrypted
  249. -- RETURN
  250. --END
  251. EXEC proc_errorHandler 0, 'Transaction Found', @controlNo
  252. END
  253. GO