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.

277 lines
9.4 KiB

1 year ago
  1. --EXEC PROC_THUNES_TXN_UPDATE @Flag='u',@User ='system',@GmeControlNo='80151368349',@WingCode='66420240',@ThunesCode='201020480'
  2. --SELECT * FROM dbo.remitTran(NOLOCK) WHERE controlNo=dbo.FNAEncryptString('66420240')
  3. --SELECT * FROM FastMoneyPro_Account.dbo.tran_master(NOLOCK) WHERE field1='66420240'
  4. --EXEC APPROVE_ONHOLD_TXN @flag='s-by-controlNo', @ControlNo2='80115870376',@user='admin'
  5. ALTER PROC APPROVE_ONHOLD_TXN
  6. (
  7. @Flag VARCHAR(100)
  8. ,@User VARCHAR(100)
  9. ,@ControlNo2 VARCHAR(100) = NULL
  10. ,@ControlNo VARCHAR(100) = NULL
  11. ,@ContNo VARCHAR(100) = NULL
  12. ,@tranId VARCHAR(100) = NULL
  13. ,@remarks VARCHAR(300) = NULL
  14. ,@searchBy VARCHAR(30) = NULL
  15. )
  16. AS
  17. BEGIN TRY
  18. SET NOCOUNT ON;
  19. SET XACT_ABORT ON;
  20. DECLARE @ControlNo2Encrypted VARCHAR(20)
  21. ,@ControlNoEncrypted VARCHAR(20)
  22. ,@id BIGINT
  23. ,@sBranch INT;
  24. SELECT @ControlNo2Encrypted = dbo.FNAEncryptString(@ControlNo2)
  25. SELECT @ControlNoEncrypted = dbo.FNAEncryptString(@ControlNo)
  26. IF @Flag = 's-by-controlNo'
  27. BEGIN
  28. IF @searchBy IS NOT NULL AND @searchBy='TranId'
  29. BEGIN
  30. SELECT @ControlNo2Encrypted=controlNo, @ControlNo2= dbo.FNADecryptString(controlNo) FROM dbo.remitTranTemp(NOLOCK) WHERE id = @tranId
  31. IF ISNULL(@ControlNo2Encrypted,'') = ''
  32. BEGIN
  33. UPDATE remitTranTemp SET controlNo = DBO.FNAEncryptString(id) WHERE id = @tranId
  34. SELECT @ControlNo2Encrypted = DBO.FNAEncryptString(@tranId),@ControlNo2 = @tranId
  35. END
  36. END
  37. DECLARE @tranStatus VARCHAR(20) = NULL
  38. SELECT @sBranch = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user
  39. SELECT @tranStatus = tranStatus FROM remitTranTemp WITH(NOLOCK) WHERE controlNo = @ControlNo2Encrypted
  40. IF (@tranStatus IS NOT NULL)
  41. BEGIN
  42. INSERT INTO tranViewHistory(
  43. controlNumber
  44. ,tranViewType
  45. ,agentId
  46. ,createdBy
  47. ,createdDate
  48. )
  49. SELECT
  50. @controlNoEncrypted
  51. ,'A'
  52. ,@sBranch
  53. ,@user
  54. ,GETDATE()
  55. END
  56. ELSE
  57. BEGIN
  58. EXEC proc_errorHandler 1000, 'No Transaction Found for approve/rejection', @ControlNo2Encrypted
  59. RETURN
  60. END
  61. IF (@tranStatus = 'CancelRequest')
  62. BEGIN
  63. EXEC proc_errorHandler 1, 'Transaction has been requested for cancel', @ControlNo2Encrypted
  64. RETURN
  65. END
  66. IF (@tranStatus = 'Payment')
  67. BEGIN
  68. EXEC proc_errorHandler 1, 'Transaction already been approved and ready for payment', @ControlNo2Encrypted
  69. RETURN
  70. END
  71. IF (@tranStatus = 'Paid')
  72. BEGIN
  73. EXEC proc_errorHandler 1, 'Transaction is already been paid', @ControlNo2Encrypted
  74. RETURN
  75. END
  76. IF (@tranStatus = 'Cancel')
  77. BEGIN
  78. EXEC proc_errorHandler 1, 'Transaction is cancelled', @ControlNo2Encrypted
  79. RETURN
  80. END
  81. IF (@tranStatus = 'Lock')
  82. BEGIN
  83. EXEC proc_errorHandler 1, 'Transaction is locked. Please Contact HO', @ControlNo2Encrypted
  84. RETURN
  85. END
  86. EXEC proc_errorHandler 0, 'Transaction Found', @ControlNo2Encrypted
  87. declare @table varchar(max), @sql VARCHAR(MAX);
  88. SET @table = '
  89. SELECT DISTINCT
  90. ControlNo = dbo.fnaDecryptstring(trn.controlNo)
  91. ,trn.id
  92. ,branch = am.agentName
  93. ,country = trn.pCountry
  94. ,senderId = sen.customerId
  95. ,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')
  96. ,receiverId = rec.customerId
  97. ,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
  98. ,amt = CAST(trn.cAmt AS DECIMAL(18, 2))
  99. ,txnDate = CAST(trn.createdDate AS DATE)
  100. ,tranType = CASE WHEN sRouteId=''w'' THEN ''WALLET'' WHEN sRouteId=''a'' THEN ''AUTO-DEBIT'' ELSE ''-'' END
  101. ,txncreatedBy = trn.createdBy
  102. ,CASE WHEN trn.pAgent=1100 OR trn.pAgent = 1043 THEN 1 ELSE 0 END isThirdPartyTran
  103. FROM remitTranTemp trn WITH(NOLOCK)
  104. INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId
  105. INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
  106. INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
  107. WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND
  108. trn.payStatus = ''Unpaid'' AND
  109. trn.approvedBy IS NULL
  110. '
  111. IF @ControlNo2 IS NOT NULL
  112. SET @table = @table + ' AND trn.controlNo = '''+@ControlNo2Encrypted+''''
  113. SET @sql = '
  114. SELECT
  115. * , STUFF((SELECT '''' + US.voucherNo +'' - ''+ CONVERT(VARCHAR(11),US.voucherDate,6) +'' - ''+ CAST(US.voucherAmt AS VARCHAR)+'' || ''
  116. FROM bankCollectionVoucherDetail US
  117. WHERE US.tempTranId = x.id
  118. FOR XML PATH('''')), 1, 1, '''') [voucherDetail]
  119. FROM (
  120. ' + @table + '
  121. ) x
  122. WHERE 1 = 1 '
  123. --PRINT @sql
  124. EXEC (@sql)
  125. RETURN
  126. END
  127. ELSE IF @Flag='u'
  128. BEGIN
  129. IF @ControlNo IS NULL
  130. BEGIN
  131. SELECT @ControlNoEncrypted = dbo.FNAEncryptString(@ControlNo2)
  132. SET @ControlNo = @ControlNo2
  133. END
  134. BEGIN TRANSACTION
  135. /*temp table check*/
  136. IF EXISTS(SELECT 'X' from remitTrantemp(NOLOCK) where controlNo=@ControlNo2Encrypted AND tranStatus='Hold' AND payStatus='Unpaid')
  137. BEGIN
  138. IF EXISTS(SELECT 'X' FROM remitTran(nolock) where controlNo = @ControlNoEncrypted
  139. --OR EXISTS(select 'x' from PinQueueList(nolock) where icn = @ControlNoEncrypted)
  140. --OR EXISTS(SELECT 'X' FROM FastMoneyPro_Account.dbo.tran_master (nolock) where field1=@ControlNo
  141. )
  142. BEGIN
  143. DECLARE @newControlNo VARCHAR(20);
  144. SET @newControlNo = CONCAT(@controlNo,'A');
  145. UPDATE remitTran SET controlNo=dbo.FNAEncryptString(@newControlNo) WHERE controlNo = @ControlNoEncrypted
  146. UPDATE PinQueueList SET icn=dbo.FNAEncryptString(@newControlNo) WHERE icn = @ControlNoEncrypted
  147. UPDATE FastMoneyPro_Account.dbo.tran_master SET field1= @newControlNo WHERE field1 = @controlNo AND tran_type='T'
  148. IF EXISTS(SELECT TOP 1 'A' FROM FastMoneyPro_Account.dbo.tran_master_post(NOLOCK) WHERE field1 = @controlNo AND tran_type='T')
  149. UPDATE FastMoneyPro_Account.dbo.tran_master_post SET field1= @newControlNo WHERE field1 = @controlNo AND tran_type='T'
  150. END
  151. UPDATE remitTrantemp SET
  152. controlNo = @ControlNoEncrypted --wing code
  153. ,contNo = @ContNo--thunes code
  154. ,controlNo2 = @ControlNo2Encrypted
  155. ,tranStatus = 'Payment'
  156. ,payStatus = 'Post'
  157. WHERE controlNo=@ControlNo2Encrypted AND id=@tranId
  158. EXEC [proc_ApproveHoldedTXN] @flag='approve',@user=@user,@id = @tranId
  159. UPDATE remitTran SET
  160. postedBy=@User,
  161. postedDate=GETDATE(),
  162. postedDateLocal=GETDATE()
  163. WHERE controlNo=@ControlNoEncrypted
  164. END
  165. /*main table */
  166. --IF EXISTS(SELECT 'X' from remitTran(NOLOCK) where controlNo=@ControlNo2Encrypted AND tranStatus='Hold' AND payStatus='Unpaid')
  167. --BEGIN
  168. -- IF EXISTS (SELECT 'X' from FastMoneyPro_Account.dbo.tran_master (nolock) where field1=@ControlNo2)
  169. -- BEGIN
  170. -- UPDATE remitTran SET
  171. -- controlNo = @ControlNoEncrypted --wing code
  172. -- ,contNo = @ContNo --thunes code
  173. -- ,controlNo2 = @ControlNo2Encrypted
  174. -- ,tranStatus = 'Payment'
  175. -- ,payStatus = 'Post'
  176. -- WHERE controlNo = @ControlNo2Encrypted AND id = @tranId
  177. -- UPDATE FastMoneyPro_Account.dbo.tran_master
  178. -- SET field1 = @ControlNo
  179. -- WHERE field1 = @ControlNo2
  180. -- END
  181. --END
  182. COMMIT TRANSACTION
  183. IF @@TRANCOUNT = 0
  184. BEGIN
  185. SELECT 0 errorCode,'Txn Update sucessfully' Msg, @id
  186. RETURN
  187. END
  188. ELSE
  189. BEGIN
  190. SELECT 1 errorCode,'Failed to Update Transaction' Msg, @id
  191. RETURN
  192. END
  193. END
  194. ELSE IF @Flag='reject'
  195. BEGIN
  196. DECLARE @TxnType VARCHAR(2)= NULL
  197. SELECT
  198. @Id = id
  199. ,@TxnType = sRouteId
  200. FROM remitTrantemp(NOLOCK) WHERE controlNo= @ControlNo2Encrypted
  201. EXEC [proc_ApproveHoldedTXN] @flag='reject',@user=@User ,@id = @Id, @remarks=@remarks
  202. RETURN
  203. --IF @TxnType='a'
  204. --BEGIN
  205. -- SELECT '0' ErrorCode, 'You cannot reject the Autodebit transaction. First approve then use cancel menu to cancel the Transaction.' Msg, NULL Id
  206. -- RETURN
  207. --END
  208. END
  209. ELSE IF @Flag='onHoldRejectReceipt'
  210. BEGIN
  211. DECLARE @AccName NVARCHAR(100),@AccNo VARCHAR(30),@BankName NVARCHAR(100),@bankCode VARCHAR(5),@srouteId CHAR(1), @customerId BIGINT , @tAmt MONEY
  212. SELECT @controlNoEncrypted = DBO.fnaDecryptstring(Controlno),@tranStatus= tranStatus,@srouteId = sRouteId FROM dbo.cancelTranHistory(NOLOCK) WHERE tranId = @tranId
  213. IF ISNULL(@srouteId,'') = 'A'
  214. BEGIN
  215. SELECT @AccName = accountName,@AccNo = accountNum,@BankName = bankName ,@bankCode = bankCodeStd,@customerId = customerId
  216. FROM DBO.[FNA_KFTC_CUST_DETAILBY_TXN](@tranId)
  217. END
  218. SELECT
  219. controlNo = dbo.FNADecryptString(controlNo)
  220. ,'0' AS ERRORCODE
  221. ,postedBy = trn.sBranchName
  222. ,createdDate
  223. ,cancelDate = cancelApprovedDate
  224. ,sender = sen.firstName + ISNULL(' ' + sen.middleName, '') + ISNULL(' ' + sen.lastName1, '') + ISNULL(' ' + sen.lastName2, '')
  225. ,receiver = rec.firstName + ISNULL(' ' + rec.middleName, '') + ISNULL(' ' + rec.lastName1, '') + ISNULL(' ' + rec.lastName2, '')
  226. ,rContactNo = rec.mobile
  227. ,trn.collCurr
  228. ,trn.cAmt
  229. ,trn.serviceCharge
  230. ,trn.pAmt
  231. ,trn.cancelCharge
  232. --,returnAmt = trn.cAmt - ISNULL(trn.cancelCharge,0)
  233. ,returnAmt = trn.cAmt
  234. ,@AccName AccName,@AccNo AccNo,@BankName BankName,@bankCode bankCode,@customerId customerId
  235. FROM dbo.cancelTranHistory trn WITH(NOLOCK)
  236. INNER JOIN dbo.cancelTranSendersHistory sen WITH(NOLOCK) ON trn.tranId = sen.tranId
  237. INNER JOIN dbo.cancelTranReceiversHistory rec WITH(NOLOCK) ON trn.tranId = rec.tranId
  238. WHERE trn.tranId = @tranId
  239. END
  240. END TRY
  241. BEGIN CATCH
  242. IF @@TRANCOUNT > 0
  243. ROLLBACK TRANSACTION
  244. SELECT 1 error_code, ERROR_MESSAGE() mes, NULL id
  245. END CATCH