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.

398 lines
15 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_cancelTranAPI_v2] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_cancelTranAPI_v2]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_cancelTranAPI_v2] 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_cancelTranAPI_v2] (
  12. @flag VARCHAR(50)
  13. ,@controlNo VARCHAR(20) = NULL
  14. ,@user VARCHAR(30) = NULL
  15. ,@agentRefId VARCHAR(50) = NULL
  16. ,@tranId INT = NULL
  17. ,@sCountry INT = NULL
  18. ,@sFirstName VARCHAR(30) = NULL
  19. ,@sMiddleName VARCHAR(30) = NULL
  20. ,@sLastName1 VARCHAR(30) = NULL
  21. ,@sLastName2 VARCHAR(30) = NULL
  22. ,@sMemId VARCHAR(30) = NULL
  23. ,@sId BIGINT = NULL
  24. ,@sTranId VARCHAR(50) = NULL
  25. ,@rCountry INT = NULL
  26. ,@rFirstName VARCHAR(30) = NULL
  27. ,@rMiddleName VARCHAR(30) = NULL
  28. ,@rLastName1 VARCHAR(30) = NULL
  29. ,@rLastName2 VARCHAR(30) = NULL
  30. ,@rMemId VARCHAR(30) = NULL
  31. ,@rId BIGINT = NULL
  32. ,@pCountry INT = NULL
  33. ,@customerId INT = NULL
  34. ,@agentId INT = NULL
  35. ,@senderId INT = NULL
  36. ,@benId INT = NULL
  37. ,@cancelReason VARCHAR(200) = NULL
  38. ,@refund CHAR(1) = NULL
  39. ,@sortBy VARCHAR(50) = NULL
  40. ,@sortOrder VARCHAR(5) = NULL
  41. ,@pageSize INT = NULL
  42. ,@pageNumber INT = NULL
  43. )
  44. AS
  45. DECLARE
  46. @select_field_list VARCHAR(MAX)
  47. ,@extra_field_list VARCHAR(MAX)
  48. ,@table VARCHAR(MAX)
  49. ,@sql_filter VARCHAR(MAX)
  50. ,@sAgent INT
  51. ,@tAmt MONEY
  52. ,@cAmt MONEY
  53. ,@pAmt MONEY
  54. ,@message VARCHAR(200)
  55. ,@payStatus varchar(20)
  56. SET NOCOUNT ON
  57. SET XACT_ABORT ON
  58. SELECT @pageSize = 1000, @pageNumber = 1
  59. DECLARE
  60. @code VARCHAR(50)
  61. ,@userName VARCHAR(50)
  62. ,@password VARCHAR(50)
  63. EXEC proc_GetAPI @user OUTPUT,@code OUTPUT, @userName OUTPUT, @password OUTPUT
  64. DECLARE @tranStatus VARCHAR(20)
  65. DECLARE @chargeToCustomer INT
  66. DECLARE @controlNoEncrypted VARCHAR(20)
  67. SELECT @controlNoEncrypted = dbo.FNAEncryptString(UPPER(LTRIM(RTRIM(@controlNo))))
  68. if right(@controlNo,1) <> 'D'
  69. set @flag ='cancelTxnAdmin'
  70. -->> Direct domestic cancel
  71. IF @flag = 'cancel'
  72. BEGIN
  73. DECLARE @sBranch INT, @pLocation INT, @deliveryMethod VARCHAR(100), @deliveryMethodId INT, @pAgentComm MONEY, @cancelCharge MONEY, @returnAmt MONEY
  74. SELECT
  75. @tranStatus = tranStatus
  76. ,@sBranch = sBranch
  77. ,@sAgent = sAgent
  78. ,@pLocation = pLocation
  79. ,@deliveryMethod = paymentMethod
  80. ,@tAmt = tAmt
  81. ,@cAmt = cAmt
  82. ,@pAmt = pAmt
  83. ,@payStatus = payStatus
  84. FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
  85. IF @user IS NULL
  86. BEGIN
  87. EXEC proc_errorHandler 1, 'Your session has expired. Cannot cancel transaction', NULL
  88. RETURN
  89. END
  90. IF (@tranStatus IS NULL)
  91. BEGIN
  92. EXEC proc_errorHandler 1, 'Transaction not found', @controlNoEncrypted
  93. RETURN
  94. END
  95. IF (@tranStatus = 'Paid')
  96. BEGIN
  97. EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
  98. RETURN
  99. END
  100. IF (@payStatus = 'Post')
  101. BEGIN
  102. EXEC proc_errorHandler 1, 'Transaction has already been POST', @controlNoEncrypted
  103. RETURN
  104. END
  105. IF (@tranStatus = 'Cancel')
  106. BEGIN
  107. EXEC proc_errorHandler 1, 'Transaction already been cancelled', @controlNoEncrypted
  108. RETURN
  109. END
  110. IF (@tranStatus = 'Hold')
  111. BEGIN
  112. EXEC proc_errorHandler 1, 'Transaction is hold. Transaction must be approved for cancellation.', NULL
  113. RETURN
  114. END
  115. DECLARE @id BIGINT, @settlingAgent INT, @ssAgent INT
  116. SELECT @deliveryMethodId = serviceTypeId FROM serviceTypeMaster WITH(NOLOCK) WHERE typeTitle = @deliveryMethod AND ISNULL(isDeleted, 'N') = 'N'
  117. SELECT @pAgentComm = pAgentComm FROM [dbo].FNAGetDomesticPayCommForCancel(@sBranch, @pLocation, @deliveryMethodId, @tAmt)
  118. SET @returnAmt = @tAmt + @pAgentComm
  119. SET @cancelCharge = @cAmt - @returnAmt
  120. BEGIN TRANSACTION
  121. UPDATE remitTran SET
  122. tranStatus = 'Cancel'
  123. ,cancelApprovedBy = @user
  124. ,cancelApprovedDate = dbo.FNAGetDateInNepalTZ()
  125. ,cancelApprovedDateLocal = dbo.FNAGetDateInNepalTZ()
  126. ,cancelReason = @cancelReason
  127. ,refund = @refund
  128. WHERE controlNo = @controlNoEncrypted
  129. SELECT @tranId = id FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
  130. SELECT @message = 'Cancel Request Approved'
  131. EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Cancel Approved'
  132. SELECT @ssAgent = parentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @sAgent
  133. SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @sAgent AND isSettlingAgent = 'Y'
  134. IF @settlingAgent IS NULL
  135. SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @ssAgent AND isSettlingAgent = 'Y'
  136. IF EXISTS (SELECT 'X' FROM remitTran WITH(NOLOCK)
  137. WHERE controlNo = @controlNoEncrypted
  138. AND createdDate BETWEEN CONVERT(VARCHAR,GETDATE(),101) AND CONVERT(VARCHAR,GETDATE(),101) + ' 23:59:59')
  139. BEGIN
  140. UPDATE remitTran SET
  141. cancelCharge = 0
  142. WHERE controlNo = @controlNoEncrypted
  143. SET @chargeToCustomer = 0
  144. END
  145. ELSE
  146. BEGIN
  147. UPDATE remitTran SET
  148. cancelCharge = @cancelCharge
  149. WHERE controlNo = @controlNoEncrypted
  150. SET @chargeToCustomer = 1
  151. END
  152. EXEC FastMoneyPro_Account.dbo.[PROC_REMIT_DATA_UPDATE]
  153. @flag = 'c'
  154. ,@user = @user
  155. ,@controlNo = @controlNo
  156. -- ## Limit Update
  157. EXEC Proc_AgentBalanceUpdate @flag = 'c',@tAmt = @cAmt ,@settlingAgent = @settlingAgent
  158. IF @@TRANCOUNT > 0
  159. COMMIT TRANSACTION
  160. EXEC [proc_errorHandler] 0, 'Transaction Cancelled successfully', @tranId
  161. END
  162. ELSE IF @flag = 'cancelReject'
  163. BEGIN
  164. --EXEC proc_cancelTran @flag = 'cancelReject', @controlNo = ''
  165. BEGIN TRANSACTION
  166. UPDATE remitTran SET
  167. tranStatus = 'Payment'
  168. WHERE controlNo = @controlNoEncrypted
  169. --End-----------------------------------------------------------------------------------------------------------------
  170. --Transaction Log---------------------------
  171. SELECT @tranId = id FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
  172. SELECT @message = 'Cancel Request for this transaction rejected'
  173. EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Cancel Reject'
  174. --------------------------------------------
  175. IF @@TRANCOUNT > 0
  176. COMMIT TRANSACTION
  177. EXEC proc_errorHandler 0, 'Cancel Request rejected successfully', @controlNoEncrypted
  178. select 'a'
  179. REturn
  180. --EXEC proc_addCommentAPI @flag = 'i', @controlNo = @controlNo, @user = @user, @message = @message, @agentRefId = NULL
  181. END
  182. -->> Direct internation cancel
  183. ELSE IF @flag = 'cancelTxnAdmin'
  184. BEGIN
  185. DECLARE @serviceCharge MONEY,@scRefund MONEY,@cancelReason1 VARCHAR(MAX),@canceledAmt MONEY,@createdBy AS VARCHAR(50),@branchId INT,
  186. @isPaidTxn CHAR(1), @sCountryId INT, @pCountryId INT, @pAgent INT, @bonusPoint INT,@holdTranId bigint
  187. SELECT
  188. @tranId = a.id
  189. ,@serviceCharge = a.serviceCharge
  190. ,@tAmt = a.tAmt
  191. ,@cAmt = a.cAmt
  192. ,@createdBy = a.createdBy
  193. ,@tranStatus = a.tranStatus
  194. ,@branchId = a.sBranch
  195. ,@isPaidTxn = CASE WHEN (paidBy IS NOT NULL OR paidDate IS NOT NULL) THEN 'Y' ELSE 'N' END
  196. ,@sCountryId = (SELECT countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = sCountry)
  197. ,@sAgent = sAgent
  198. ,@sBranch = sBranch
  199. ,@pCountryId = (SELECT countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = pCountry)
  200. ,@pAgent = pAgent
  201. ,@bonusPoint = ISNULL(a.bonusPoint, 0)
  202. ,@payStatus = a.payStatus
  203. FROM vwRemitTran a WITH(NOLOCK)
  204. WHERE a.controlNo = @controlNoEncrypted
  205. IF @user IS NULL
  206. BEGIN
  207. EXEC proc_errorHandler 1, 'Your session has expired. Cannot cancel transaction.', NULL
  208. RETURN
  209. END
  210. IF (@tranStatus IS NULL)
  211. BEGIN
  212. EXEC proc_errorHandler 1, 'Transaction not found.', @controlNoEncrypted
  213. RETURN
  214. END
  215. IF (@tranStatus = 'Cancel')
  216. BEGIN
  217. EXEC proc_errorHandler 1, 'Transaction already been cancelled', @controlNoEncrypted
  218. RETURN
  219. END
  220. IF (@payStatus = 'Post')
  221. BEGIN
  222. EXEC proc_errorHandler 1, 'Transaction already been POST.', @controlNoEncrypted
  223. RETURN
  224. END
  225. IF (@tranStatus = 'CANCELLED')
  226. BEGIN
  227. EXEC proc_errorHandler 1, 'Transaction already been cancelled.', @controlNoEncrypted
  228. RETURN
  229. END
  230. IF (@tranStatus = 'Cancel Processing')
  231. BEGIN
  232. EXEC proc_errorHandler 1, 'Transaction already been cancelled.', @controlNoEncrypted
  233. RETURN
  234. END
  235. IF (@tranStatus = 'ModificationRequest')
  236. BEGIN
  237. EXEC proc_errorHandler 1, 'Transaction already been requested for modification.', @controlNoEncrypted
  238. RETURN
  239. END
  240. IF (@tranStatus = 'Lock')
  241. BEGIN
  242. EXEC proc_errorHandler 1, 'Transaction is locked. Please contact HO', @controlNoEncrypted
  243. RETURN
  244. END
  245. IF (@tranStatus = 'Block')
  246. BEGIN
  247. EXEC proc_errorHandler 1, 'Transaction is blocked. Please contact HO', @controlNoEncrypted
  248. RETURN
  249. END
  250. IF (@tranStatus = 'CancelRequest')
  251. BEGIN
  252. EXEC proc_errorHandler 1, 'Transaction already been requested for cancellation', @controlNoEncrypted
  253. RETURN
  254. END
  255. IF EXISTS(SELECT 'x' FROM trancancelrequest WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted AND cancelStatus <> 'Rejected')
  256. BEGIN
  257. EXEC proc_errorHandler 1, 'Transaction is already requested for cancel.', @controlNoEncrypted
  258. RETURN
  259. END
  260. BEGIN TRANSACTION
  261. INSERT INTO tranCancelrequest(tranId,controlNo,cancelReason,cancelStatus,
  262. createdBy,createdDate,tranStatus,approvedBy,approvedDate,scRefund,isScRefund)
  263. SELECT @tranId,@controlNoEncrypted,@cancelReason,'Approved',@user,GETDATE(),@tranStatus,@user,GETDATE(),
  264. CASE WHEN @refund ='D' THEN @cAmt ELSE @tAmt END,@refund
  265. IF @tranStatus NOT LIKE '%HOLD%'
  266. BEGIN
  267. UPDATE remitTran SET
  268. tranStatus = 'Cancel'
  269. ,cancelApprovedBy = @user
  270. ,cancelApprovedDate = GETDATE()
  271. ,cancelApprovedDateLocal = GETDATE()
  272. WHERE controlNo = @controlNoEncrypted
  273. UPDATE FastMoneyPro_Account.dbo.remit_trn_master SET
  274. trn_status = 'Cancel'
  275. ,cancel_date = GETDATE()
  276. WHERE trn_ref_no = @controlNoEncrypted
  277. -- ## Limit Update
  278. EXEC Proc_AgentBalanceUpdate @flag = 'c',@tAmt = @cAmt, @settlingAgent = @sBranch
  279. END
  280. IF @tranStatus LIKE '%HOLD%'
  281. BEGIN
  282. -->> UPDATE REMITTRAN
  283. UPDATE remitTranTemp SET
  284. tranStatus = 'Cancel'
  285. ,cancelApprovedBy = @user
  286. ,cancelApprovedDate = GETDATE()
  287. ,cancelApprovedDateLocal = GETDATE()
  288. WHERE controlNo = @controlNoEncrypted
  289. INSERT INTO cancelTranHistory(
  290. tranId,controlNo,sCurrCostRate,sCurrHoMargin,sCurrSuperAgentMargin,sCurrAgentMargin,pCurrCostRate
  291. ,pCurrHoMargin,pCurrSuperAgentMargin,pCurrAgentMargin,agentCrossSettRate,customerRate,sAgentSettRate,pDateCostRate,agentFxGain
  292. ,treasuryTolerance,customerPremium,schemePremium,sharingValue
  293. --,sharingType
  294. ,serviceCharge,handlingFee,sAgentComm,sAgentCommCurrency
  295. ,sSuperAgentComm,sSuperAgentCommCurrency,pAgentComm,pAgentCommCurrency,pSuperAgentComm,pSuperAgentCommCurrency,promotionCode
  296. ,promotionType,pMessage,sCountry,sSuperAgent,sSuperAgentName,sAgent,sAgentName,sBranch,sBranchName,pCountry,pSuperAgent,pSuperAgentName
  297. ,pAgent,pAgentName,pBranch,pBranchName,paymentMethod,pBank,pBankName,pBankBranch,pBankBranchName,accountNo,externalBankCode,collMode
  298. ,collCurr,tAmt,cAmt,pAmt,payoutCurr,relWithSender,purposeOfRemit,sourceOfFund,tranStatus,payStatus,createdDate,createdDateLocal
  299. ,createdBy,modifiedDate,modifiedDateLocal,modifiedBy,approvedDate,approvedDateLocal,approvedBy,paidDate,paidDateLocal,paidBy
  300. ,cancelRequestDate,cancelRequestDateLocal,cancelRequestBy,cancelReason,refund,cancelCharge,cancelApprovedDate,cancelApprovedDateLocal
  301. ,cancelApprovedBy,blockedDate,blockedBy,lockedDate,lockedDateLocal,lockedBy,payTokenId,tranType,ContNo
  302. ,uploadLogId,voucherNo,controlNo2,pBankType,senderName,receiverName
  303. )
  304. SELECT
  305. id,controlNo,sCurrCostRate,sCurrHoMargin,sCurrSuperAgentMargin,sCurrAgentMargin,pCurrCostRate
  306. ,pCurrHoMargin,pCurrSuperAgentMargin,pCurrAgentMargin,agentCrossSettRate,customerRate,sAgentSettRate,pDateCostRate,agentFxGain
  307. ,treasuryTolerance,customerPremium,schemePremium,sharingValue
  308. --,sharingType
  309. ,serviceCharge,handlingFee,sAgentComm,sAgentCommCurrency
  310. ,sSuperAgentComm,sSuperAgentCommCurrency,pAgentComm,pAgentCommCurrency,pSuperAgentComm,pSuperAgentCommCurrency,promotionCode
  311. ,promotionType,pMessage,sCountry,sSuperAgent,sSuperAgentName,sAgent,sAgentName,sBranch,sBranchName,pCountry,pSuperAgent,pSuperAgentName
  312. ,pAgent,pAgentName,pBranch,pBranchName,paymentMethod,pBank,pBankName,pBankBranch,pBankBranchName,accountNo,externalBankCode,collMode
  313. ,collCurr,tAmt,cAmt,pAmt,payoutCurr,relWithSender,purposeOfRemit,sourceOfFund,tranStatus,payStatus,createdDate,createdDateLocal
  314. ,createdBy,modifiedDate,modifiedDateLocal,modifiedBy,approvedDate,approvedDateLocal,approvedBy,paidDate,paidDateLocal,paidBy
  315. ,cancelRequestDate,cancelRequestDateLocal,cancelRequestBy,@cancelReason1,refund,cancelCharge,dbo.FNADateFormatTZ(GETDATE(), @user),GETDATE()
  316. ,@user,blockedDate,blockedBy,lockedDate,lockedDateLocal,lockedBy,payTokenId,tranType,ContNo
  317. ,uploadLogId,voucherNo,controlNo2,pBankType,senderName,receiverName
  318. FROM remitTranTemp WHERE controlNo = @controlNoEncrypted
  319. INSERT INTO cancelTranSendersHistory
  320. (tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,STATE,district,
  321. zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,occupation,idType,
  322. idNumber,idPlaceOfIssue,issuedDate,validDate,extCustomerId,cwPwd,ttName,isFirstTran,customerRiskPoint,countryRiskPoint,
  323. gender,salary,companyName,address2,dcInfo,ipAddress,notifySms,txnTestQuestion,txnTestAnswer)
  324. SELECT tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,STATE,district,
  325. zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,occupation,idType,
  326. idNumber,idPlaceOfIssue,issuedDate,validDate,extCustomerId,cwPwd,ttName,isFirstTran,customerRiskPoint,countryRiskPoint,
  327. gender,salary,companyName,address2,dcInfo,ipAddress,notifySms,txnTestQuestion,txnTestAnswer
  328. FROM transenderstemp WITH(NOLOCK) WHERE tranId = @tranId
  329. INSERT INTO cancelTranReceiversHistory
  330. (tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,
  331. STATE,district,zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,
  332. occupation,idType,idNumber,idPlaceOfIssue,issuedDate,validDate,idType2,idNumber2,idPlaceOfIssue2,issuedDate2,
  333. validDate2,relationType,relativeName,gender,address2,dcInfo,ipAddress)
  334. SELECT tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,
  335. STATE,district,zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,
  336. occupation,idType,idNumber,idPlaceOfIssue,issuedDate,validDate,idType2,idNumber2,idPlaceOfIssue2,issuedDate2,
  337. validDate2,relationType,relativeName,gender,address2,dcInfo,ipAddress
  338. FROM tranReceiversTemp WITH(NOLOCK) WHERE tranId = @tranId
  339. -- ## Limit Update
  340. UPDATE creditLimitInt SET
  341. todaysSent = ISNULL(todaysSent,0) - @canceledAmt
  342. WHERE agentId = @sAgent
  343. DELETE FROM remitTranTemp WHERE controlNo = @controlNoEncrypted
  344. DELETE FROM tranSendersTemp WHERE tranId = @tranId
  345. DELETE FROM tranReceiversTemp WHERE tranId = @tranId
  346. END
  347. SELECT @message = 'Transaction cancel has been done successfully.'
  348. EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Transaction Cancel Approved'
  349. IF @@TRANCOUNT > 0
  350. COMMIT TRANSACTION
  351. EXEC [proc_errorHandler] 0, 'Transaction cancel has been done successfully', @tranId
  352. END
  353. GO