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.

552 lines
31 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_instantCashPay] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /*
  9. EXEC proc_instantCashPay @flag='restore', @user='dipesh', @pBranch='5517', @rowId='33',
  10. @pBranchName='RAHAT MULTIPURPOSE CO-OPERATIVE SOCIETY LTD - BAGLUNG'
  11. */
  12. CREATE procEDURE [dbo].[proc_instantCashPay]
  13. @flag VARCHAR(50)
  14. ,@sortBy VARCHAR(50) = NULL
  15. ,@sortOrder VARCHAR(5) = NULL
  16. ,@pageSize INT = NULL
  17. ,@pageNumber INT = NULL
  18. ,@user VARCHAR(50) = NULL
  19. ,@rowId INT = NULL
  20. ,@xpin VARCHAR(50) = NULL
  21. ,@agentName VARCHAR(200) = NULL
  22. ,@pBranch INT = NULL
  23. ,@pBranchName VARCHAR(200) = NULL
  24. AS
  25. SET NOCOUNT ON;
  26. SET XACT_ABORT ON;
  27. BEGIN TRY
  28. DECLARE
  29. @xpinEnc VARCHAR(50)
  30. ,@sql VARCHAR(MAX)
  31. ,@table VARCHAR(MAX)
  32. ,@select_field_list VARCHAR(MAX)
  33. ,@extra_field_list VARCHAR(MAX)
  34. ,@sql_filter VARCHAR(MAX)
  35. ,@controlNoEncrypted VARCHAR(50)
  36. ,@tranId BIGINT
  37. IF @flag = 's'
  38. BEGIN
  39. --SELECT * FROM [ThirdPaymentRemitTran]
  40. IF @sortBy IS NULL
  41. SET @sortBy = 'provider'
  42. SET @table = '
  43. (
  44. SELECT
  45. rowId = rowId
  46. ,provider = ''INSTANT CASH''
  47. ,agentName = am.agentName
  48. ,xpin = dbo.fnadecryptstring(ic.ictc_number)
  49. ,customer = ic.remitter_Name
  50. ,beneficiary = ic.Beneficiary_Name
  51. ,customerAddress = ic.remitter_Address
  52. ,beneficiaryAddress = ic.Beneficiary_Address
  53. ,payoutAmount = floor(ic.Paying_Amount)
  54. ,payoutDate = ic.createdDate
  55. FROM icPayHistory ic WITH(NOLOCK)
  56. LEFT JOIN agentMaster am WITH(NOLOCK) ON am.agentId = ic.pBranch
  57. WHERE 1 = 1
  58. '
  59. IF @xpin IS NOT NULL
  60. BEGIN
  61. SET @xpinEnc = dbo.fnaencryptstring(@xpin)
  62. SET @table = @table + ' AND ic.ictc_number = ''' + @xpinEnc + ''''
  63. select @pBranch = pBranch,@user = createdBy from icPayHistory with(nolock)
  64. where ictc_number = @xpinEnc
  65. if @pBranch is null and @user is not null
  66. begin
  67. select @pBranch = agentId from applicationUsers with(nolock)
  68. where userName = @user
  69. update icPayHistory set pBranch = @pBranch
  70. where ictc_number = @xpinEnc
  71. end
  72. END
  73. IF @agentName IS NOT NULL
  74. SET @table = @table + ' AND am.agentName LIKE ''' + @agentName + '%'''
  75. SET @table = @table + '
  76. ) x '
  77. print(@table)
  78. SET @sql_filter = ''
  79. SET @select_field_list ='
  80. rowId
  81. ,provider
  82. ,agentName
  83. ,xpin
  84. ,customer
  85. ,beneficiary
  86. ,customerAddress
  87. ,beneficiaryAddress
  88. ,payoutAmount
  89. ,payoutDate
  90. '
  91. EXEC dbo.proc_paging
  92. @table
  93. ,@sql_filter
  94. ,@select_field_list
  95. ,@extra_field_list
  96. ,@sortBy
  97. ,@sortOrder
  98. ,@pageSize
  99. ,@pageNumber
  100. RETURN
  101. END
  102. IF @flag = 'a'
  103. BEGIN
  104. SELECT TOP 1
  105. rowId = rowId
  106. ,[controlNo] = dbo.fnadecryptstring(ic.ictc_number)
  107. ,[sCountry] = ic.Originating_Country
  108. ,[sName] = Remitter_Name
  109. ,[sAddress] = Remitter_Address
  110. ,[sCity] = ''
  111. ,[sIdType] = Remitter_IDType
  112. ,[sIdNumber] = Remitter_IDDtl
  113. ,[sMobile] = ''
  114. ,[sAgentName] = 'Instant Cash'
  115. ,[rCountry] = 'Nepal'
  116. ,[rName] = Beneficiary_Name
  117. ,[rAddress] = Beneficiary_Address
  118. ,[rCity] = Beneficiary_City
  119. ,[rPhone] = Beneficiary_TelNo
  120. ,[rIdType] = Expected_BenefID
  121. ,[rIdNumber] = Expected_BenefID
  122. ,[pAmt] = floor(ic.Paying_Amount)
  123. ,[pCurr] = 'NPR'
  124. ,[pBranch] = am.agentName
  125. ,[branchId] = am.agentId
  126. ,[pUser] = ic.createdBy
  127. ,[transactionMode] = 'Cash Payment'
  128. ,[PlaceOfIssue] = rIdPlaceOfIssue
  129. ,[rRelativeName] = relativeName
  130. ,[RelationType] = relationType
  131. ,[rContactNo] = rContactNo
  132. FROM icPayHistory ic WITH(NOLOCK)
  133. INNER JOIN agentMaster am WITH(NOLOCK) ON ic.pBranch = am.agentId
  134. WHERE ic.rowid = @rowId ORDER BY rowid DESC
  135. RETURN
  136. END
  137. IF @flag = 'restore'
  138. BEGIN
  139. IF NOT EXISTS(
  140. SELECT 'x' FROM icPayHistory WITH(NOLOCK)
  141. WHERE recordStatus IN('READYTOPAY', 'PAYERROR', 'PAID')
  142. AND rowid = @rowid )
  143. BEGIN
  144. EXEC proc_errorHandler 1, 'Transaction Not Found!', @rowid
  145. RETURN
  146. END
  147. DECLARE
  148. @ICTC_Number VARCHAR(100) = NULL
  149. ,@Agent_OrderNumber VARCHAR(100) = NULL
  150. ,@Remitter_Name VARCHAR(100) = NULL
  151. ,@Remitter_Address VARCHAR(100) = NULL
  152. ,@Remitter_IDType VARCHAR(100) = NULL
  153. ,@Remitter_IDDtl VARCHAR(100) = NULL
  154. ,@Originating_Country VARCHAR(100) = NULL
  155. ,@Delivery_Mode VARCHAR(100) = NULL
  156. ,@Paying_Amount VARCHAR(100) = NULL
  157. ,@PayingAgent_CommShare VARCHAR(100) = NULL
  158. ,@Paying_Currency VARCHAR(100) = NULL
  159. ,@Paying_Agent VARCHAR(100) = NULL
  160. ,@Paying_AgentName VARCHAR(100) = NULL
  161. ,@Beneficiary_Name VARCHAR(100) = NULL
  162. ,@Beneficiary_Address VARCHAR(100) = NULL
  163. ,@Beneficiary_City VARCHAR(100) = NULL
  164. ,@Destination_Country VARCHAR(100) = NULL
  165. ,@Beneficiary_TelNo VARCHAR(100) = NULL
  166. ,@Beneficiary_MobileNo VARCHAR(100) = NULL
  167. ,@Expected_BenefID VARCHAR(100) = NULL
  168. ,@Bank_Address VARCHAR(100) = NULL
  169. ,@Bank_Account_Number VARCHAR(100) = NULL
  170. ,@Bank_Name VARCHAR(100) = NULL
  171. ,@Purpose_Remit VARCHAR(100) = NULL
  172. ,@Message_PayeeBranch VARCHAR(100) = NULL
  173. ,@Bank_BranchCode VARCHAR(100) = NULL
  174. ,@Settlement_Rate VARCHAR(100) = NULL
  175. ,@PrinSettlement_Amount VARCHAR(100) = NULL
  176. ,@Transaction_SentDate VARCHAR(100) = NULL
  177. ,@payConfirmationNo VARCHAR(100) = NULL
  178. ,@apiStatus VARCHAR(100) = NULL
  179. ,@payResponseCode VARCHAR(20) = NULL
  180. ,@payResponseMsg VARCHAR(100) = NULL
  181. ,@recordStatus VARCHAR(50) = NULL
  182. ,@tranPayProcess VARCHAR(20) = NULL
  183. ,@createdDate DATETIME = NULL
  184. ,@createdBy VARCHAR(30) = NULL
  185. ,@paidDate DATETIME = NULL
  186. ,@paidBy VARCHAR(30) = NULL
  187. ,@rIdType VARCHAR(30) = NULL
  188. ,@rIdNumber VARCHAR(30) = NULL
  189. ,@rIdPlaceOfIssue VARCHAR(50) = NULL
  190. ,@rValidDate DATETIME = NULL
  191. ,@rDob DATETIME = NULL
  192. ,@rAddress VARCHAR(100) = NULL
  193. ,@rOccupation VARCHAR(100) = NULL
  194. ,@rContactNo VARCHAR(50) = NULL
  195. ,@rCity VARCHAR(100) = NULL
  196. ,@rNativeCountry VARCHAR(100) = NULL
  197. ,@relationType VARCHAR(50) = NULL
  198. ,@relativeName VARCHAR(100) = NULL
  199. ,@pAgent INT = NULL
  200. ,@pAgentName VARCHAR(100) = NULL
  201. ,@sCountry VARCHAR(100) = NULL
  202. ,@provider VARCHAR(100) = NULL
  203. ,@remarks VARCHAR(500) = NULL
  204. ,@pSuperAgent INT
  205. ,@pSuperAgentName VARCHAR(100)
  206. ,@pCountry VARCHAR(100)
  207. ,@pState VARCHAR(100)
  208. ,@pDistrict VARCHAR(100)
  209. ,@pLocation INT
  210. ,@pAgentComm MONEY
  211. ,@pAgentCommCurrency VARCHAR(3)
  212. ,@pSuperAgentComm MONEY
  213. ,@pSuperAgentCommCurrency VARCHAR(3)
  214. ,@sAgent INT
  215. ,@sAgentName VARCHAR(100)
  216. ,@sBranch INT
  217. ,@sBranchName VARCHAR(100)
  218. ,@sSuperAgent INT
  219. ,@sSuperAgentName VARCHAR(100)
  220. ,@sAgentMapCode INT = 33200000
  221. ,@sBranchMapCode INT = 33200100
  222. ,@bankName VARCHAR(100) = NULL
  223. ,@purposeOfRemit VARCHAR(100) = NULL
  224. ,@pBankBranch VARCHAR(100) = NULL
  225. ,@sAgentSettRate VARCHAR(100) = NULL
  226. ,@agentType INT
  227. ,@payoutMethod VARCHAR(50)
  228. ,@cAmt MONEY
  229. ,@beneIdNo INT
  230. ,@customerRate MONEY
  231. ,@payoutCurr VARCHAR(50)
  232. ,@collCurr VARCHAR(50)
  233. ,@MapCodeIntBranch VARCHAR(50)
  234. ,@MapCodeIntAgent VARCHAR(50)
  235. ,@MapAgentName VARCHAR(50)
  236. ,@companyId INT = 16
  237. ,@controlNo VARCHAR(50)
  238. SELECT
  239. @ICTC_Number = ic.ICTC_Number
  240. ,@Agent_OrderNumber = ic.Agent_OrderNumber
  241. ,@Remitter_Name = ic.Remitter_Name
  242. ,@Remitter_Address = ic.Remitter_Address
  243. ,@Remitter_IDType = ic.Remitter_IDType
  244. ,@Remitter_IDDtl = ic.Remitter_IDDtl
  245. ,@Originating_Country = ic.Originating_Country
  246. ,@Delivery_Mode = ic.Delivery_Mode
  247. ,@Paying_Amount = ic.Paying_Amount
  248. ,@PayingAgent_CommShare = ic.PayingAgent_CommShare
  249. ,@Paying_Currency = ic.Paying_Currency
  250. ,@Paying_Agent = ic.Paying_Agent
  251. ,@Paying_AgentName = ic.Paying_AgentName
  252. ,@Beneficiary_Name = ic.Beneficiary_Name
  253. ,@Beneficiary_Address = ic.rAddress
  254. ,@Beneficiary_City = ic.rCity
  255. ,@Destination_Country = ic.Destination_Country
  256. ,@Beneficiary_TelNo = ic.rContactNo
  257. ,@Beneficiary_MobileNo = ic.Beneficiary_MobileNo
  258. ,@Expected_BenefID = ic.Expected_BenefID
  259. ,@Bank_Address = ic.Bank_Address
  260. ,@Bank_Account_Number = ic.Bank_Account_Number
  261. ,@Bank_Name = ic.Bank_Name
  262. ,@Purpose_Remit = ic.Purpose_Remit
  263. ,@Message_PayeeBranch = ic.Message_PayeeBranch
  264. ,@Bank_BranchCode = ic.Bank_BranchCode
  265. ,@Settlement_Rate = ic.Settlement_Rate
  266. ,@PrinSettlement_Amount = ic.PrinSettlement_Amount
  267. ,@Transaction_SentDate = ic.Transaction_SentDate
  268. ,@rIdType = ic.rIdType
  269. ,@rIdNumber = ic.rIdNumber
  270. ,@rValidDate = ic.rValidDate
  271. ,@rDob = ic.rDob
  272. ,@rOccupation = ic.rOccupation
  273. ,@rNativeCountry = ic.nativeCountry
  274. ,@pBranch = pBranch
  275. ,@pBranchName = pb.agentName
  276. FROM icPayHistory ic WITH(NOLOCK)
  277. LEFT JOIN agentMaster pb WITH(NOLOCK) ON ic.pBranch = pb.agentId
  278. WHERE rowId = @rowId
  279. --## Check if controlno exist in remittran.
  280. IF EXISTS( SELECT 'x' FROM remitTran WITH(NOLOCK) WHERE controlNo = @ICTC_Number)
  281. BEGIN
  282. DECLARE @status VARCHAR(100),@msg VARCHAR(100)
  283. SELECT
  284. @agentName = sAgentName
  285. ,@status = payStatus
  286. FROM remitTran WITH(NOLOCK) WHERE controlNo = @ICTC_Number
  287. SET @msg = 'This transaction belongs to ' + @agentName + ' and is in status: ' + @status
  288. EXEC proc_errorHandler 1,@msg,NULL
  289. RETURN
  290. END
  291. --## Set paying agent details.
  292. SELECT
  293. @pAgent = parentId,
  294. @pBranchName = agentName,
  295. @agentType = agentType,
  296. @pCountry = agentCountry,
  297. @pState = agentState,
  298. @pDistrict = agentDistrict,
  299. @pLocation = agentLocation,
  300. @MapCodeIntBranch=mapCodeInt
  301. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pBranch
  302. IF @agentType = 2903
  303. SET @pAgent = @pBranch
  304. SELECT @sBranch = bm.agentId,
  305. @sAgent = am.agentId,
  306. @sBranchName = bm.agentName,
  307. @sAgentName = am.agentName
  308. FROM agentMaster bm WITH(NOLOCK) inner join agentMaster am with(nolock) on bm.parentId = am.agentId
  309. WHERE bm.agentId = 4817
  310. SELECT @sSuperAgent = '4641',
  311. @sSuperAgentName = 'INTERNATIONAL AGENTS'
  312. --## 2. Find Payout Agent Details
  313. SELECT @pSuperAgent = parentId
  314. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pAgent
  315. SELECT @pSuperAgentname = agentNAME
  316. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pSuperAgent
  317. --## 3. Find Commission
  318. DECLARE @sCountryId INT, @deliveryMethodId INT, @pCommCheck MONEY
  319. SELECT @sCountryId = countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = @sCountry
  320. if @sCountryId is null
  321. select @sCountryId = agentCountryId from agentMaster with(nolock) where agentId = @sBranch
  322. SET @payoutMethod = 'Cash Payment'
  323. DECLARE @pCountryId INT = NULL
  324. SELECT @pCountryId = countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = @pCountry
  325. SELECT @deliveryMethodId = serviceTypeId FROM serviceTypeMaster WITH(NOLOCK)
  326. WHERE typeTitle = @payoutMethod AND ISNULL(isDeleted, 'N') = 'N'
  327. SELECT @pSuperAgentComm = 0, @pSuperAgentCommCurrency = 'NPR'
  328. SELECT @pAgentComm = ISNULL(amount, 0),
  329. @pCommCheck = amount,
  330. @pAgentCommCurrency = commissionCurrency,
  331. @pCommCheck = amount
  332. FROM dbo.FNAGetPayComm(@sBranch, @sCountryId, NULL, @pSuperAgent, 151,
  333. @pLocation, @pBranch, 'NPR', @deliveryMethodId, NULL, @Paying_Amount, NULL, NULL, NULL)
  334. IF @pCommCheck IS NULL
  335. BEGIN
  336. EXEC proc_errorHandler 1, 'Payout Commission not defined', @pBranch
  337. RETURN
  338. END
  339. BEGIN TRANSACTION
  340. BEGIN
  341. INSERT INTO remitTran (
  342. [controlNo]
  343. ,[senderName]
  344. ,[sCountry]
  345. ,[sSuperAgent]
  346. ,[sSuperAgentName]
  347. ,[paymentMethod]
  348. ,[cAmt]
  349. ,[pAmt]
  350. ,[tAmt]
  351. ,[pAgentComm]
  352. ,[payoutCurr]
  353. ,[pAgent]
  354. ,[pAgentName]
  355. ,[pSuperAgent]
  356. ,[pSuperAgentName]
  357. ,[receiverName]
  358. ,[pCountry]
  359. ,[pBranch]
  360. ,[pBranchName]
  361. ,[pState]
  362. ,[pDistrict]
  363. ,[pLocation]
  364. ,[purposeofRemit]
  365. ,[pMessage]
  366. ,[createdDate]
  367. ,[createdDateLocal]
  368. ,[createdBy]
  369. ,[approvedDate]
  370. ,[approvedDateLocal]
  371. ,[approvedBy]
  372. ,[paidBy]
  373. ,[paidDate]
  374. ,[paidDateLocal]
  375. --## hardcoded parameters
  376. ,[tranStatus]
  377. ,[payStatus]
  378. ,[collCurr]
  379. ,[tranType]
  380. ,[sAgent]
  381. ,[sAgentName]
  382. ,[sBranch]
  383. ,[sBranchName]
  384. ,sCurrCostRate
  385. )
  386. SELECT
  387. @ICTC_Number
  388. ,@Remitter_Name
  389. ,@Originating_Country
  390. ,@sSuperAgent
  391. ,@sSuperAgentName
  392. ,'Cash Payment'
  393. ,ROUND(@Paying_Amount,0)
  394. ,ROUND(@Paying_Amount,0)
  395. ,ROUND(@Paying_Amount,0)
  396. ,@pAgentComm
  397. ,'NPR'
  398. ,@pAgent
  399. ,@pAgentName
  400. ,@pSuperAgent
  401. ,@pSuperAgentName
  402. ,@Beneficiary_Name
  403. ,@pCountry
  404. ,@pBranch
  405. ,@pBranchName
  406. ,@pState
  407. ,@pDistrict
  408. ,@pLocation
  409. ,@purposeOfRemit
  410. ,@remarks
  411. ,GETDATE()
  412. ,GETDATE()
  413. ,'SWIFT:API'
  414. ,GETDATE()
  415. ,GETDATE()
  416. ,'SWIFT:API'
  417. ,@user
  418. ,dbo.FNAGetDateInNepalTZ()
  419. ,dbo.FNAGetDateInNepalTZ()
  420. --## HardCoded Parameters
  421. ,'Paid'
  422. ,'Paid'
  423. ,'NPR'
  424. ,'I'
  425. ,@sAgent
  426. ,@sAgentName
  427. ,@sBranch
  428. ,@sBranchName
  429. ,'1'
  430. SET @tranId = SCOPE_IDENTITY()
  431. -- ## insert to TranSenders
  432. INSERT INTO tranSenders (
  433. tranId
  434. ,firstName
  435. ,country
  436. ,[address]
  437. ,homePhone
  438. )
  439. SELECT
  440. @tranId
  441. ,@Remitter_Name
  442. ,@Originating_Country
  443. ,@Remitter_Address
  444. ,''
  445. -- ## insert to TranReceivers
  446. INSERT INTO tranReceivers (
  447. tranId
  448. ,firstName
  449. ,country
  450. ,city
  451. ,[address]
  452. ,homePhone
  453. ,mobile
  454. ,idType
  455. ,idNumber
  456. ,dob
  457. ,occupation
  458. ,validDate
  459. )
  460. SELECT
  461. @tranId
  462. ,@Beneficiary_Name
  463. ,left(@Destination_Country,100)
  464. ,left(@Beneficiary_City,150)
  465. ,left(@Beneficiary_Address,500)
  466. ,left(@Beneficiary_TelNo,100)
  467. ,left(@Beneficiary_MobileNo,100)
  468. ,left(@rIdType,50)
  469. ,left(@rIdNumber,50)
  470. ,@rDob
  471. ,@rOccupation
  472. ,@rValidDate
  473. -- ## Updating Data in icPayHistory table by paid status
  474. UPDATE icPayHistory SET
  475. recordStatus = 'PAID'
  476. ,tranPayProcess = CASE WHEN @flag = 'Pay' THEN 'REGULAR' ELSE 'RESTORED' END
  477. ,payResponseCode = @payResponseCode
  478. ,payResponseMsg = @payResponseMsg
  479. WHERE rowId = @rowId
  480. END
  481. IF @@TRANCOUNT > 0
  482. COMMIT TRANSACTION
  483. SET @msg = 'Transaction has been restored successfully'
  484. EXEC proc_pushToAc @flag= 'i', @controlNoEncrypted = @ICTC_Number
  485. SET @controlNo = dbo.fnadecryptstring(@ICTC_Number)
  486. EXEC [proc_errorHandler] 0, @msg, @controlNo
  487. RETURN
  488. END
  489. END TRY
  490. BEGIN CATCH
  491. IF @@TRANCOUNT > 0
  492. ROLLBACK TRANSACTION
  493. DECLARE @errorMessage VARCHAR(MAX)
  494. SET @errorMessage = ERROR_MESSAGE()
  495. EXEC proc_errorHandler 1, @errorMessage, NULL
  496. END CATCH
  497. GO