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.

302 lines
24 KiB

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