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.

1289 lines
96 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[ws_int_proc_CreateTXN] 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. ws_int_proc_CreateTXN @ACCESSCODE='BRNNP1087',@USERNAME='dhan321',@PASSWORD='dhan123',@FOREX_SESSION_ID='',
  10. @AGENT_TXN_REF_ID='3453453',@REMITTER_ID='1234556',@SENDER_NAME='Dhan Saud',@SENDER_ADDRESS='QTR Test Add',@SENDER_MOBILE='9848700321',
  11. @SENDER_CITY='test',@SENDER_COUNTRY='Qatar',@SENDERS_IDENTITY_TYPE='1302',@SENDER_IDENTITY_NUMBER='124457',@RECEIVER_NAME='Arjun',
  12. @RECEIVER_ADDRESS='KTM',@RECEIVER_CONTACT_NUMBER='9848700324',@RECEIVER_CITY='KTM',@RECEIVER_COUNTRY='Nepal',@OCCUPATION='Student',
  13. @SOURCE_OF_FUND='Salary',@RELATIONSHIP='Brother',@PURPOSE_OF_REMITTANCE='Studying',@COLLECT_AMT='100',@PAYOUTAMT='2900',
  14. @PAYMENTTYPE='C',@BANK_NAME='',@BANK_BRANCH_NAME='',@BANK_ACCOUNT_NUMBER='',@TRNDATE='2016-12-26',@CALC_BY='C'
  15. */
  16. CREATE PROC [dbo].[ws_int_proc_CreateTXN] (
  17. @ACCESSCODE VARCHAR(50),
  18. @USERNAME VARCHAR(50),
  19. @PASSWORD VARCHAR(50),
  20. @FOREX_SESSION_ID VARCHAR(50),
  21. @AGENT_TXN_REF_ID VARCHAR(50) = NULL,
  22. @REMITTER_ID VARCHAR(50),
  23. @SENDER_NAME VARCHAR(50),
  24. @SENDER_ADDRESS VARCHAR(50),
  25. @SENDER_MOBILE VARCHAR(50),
  26. @SENDER_CITY VARCHAR(100),
  27. @SENDER_COUNTRY VARCHAR(50),
  28. @SENDERS_IDENTITY_TYPE VARCHAR(50),
  29. @SENDER_IDENTITY_NUMBER VARCHAR(50),
  30. @RECEIVER_NAME VARCHAR(50),
  31. @RECEIVER_ADDRESS VARCHAR(50),
  32. @RECEIVER_CONTACT_NUMBER VARCHAR(50) = NULL,
  33. @RECEIVER_CITY VARCHAR(50) = NULL,
  34. @RECEIVER_COUNTRY VARCHAR(50),
  35. @OCCUPATION VARCHAR(50) = NULL,
  36. @SOURCE_OF_FUND VARCHAR(50) = NULL,
  37. @RELATIONSHIP VARCHAR(50) = NULL,
  38. @PURPOSE_OF_REMITTANCE VARCHAR(50) = NULL,
  39. @COLLECT_AMT MONEY,
  40. @PAYOUTAMT MONEY,
  41. @PAYMENTTYPE VARCHAR(50),
  42. @BANKID VARCHAR(50) = NULL,
  43. @BANK_NAME VARCHAR(50),
  44. @BANK_BRANCH_NAME VARCHAR(50),
  45. @BANK_ACCOUNT_NUMBER VARCHAR(50) = NULL,
  46. @TRNDATE DATETIME,
  47. @CALC_BY VARCHAR(50)
  48. )
  49. AS
  50. SET NOCOUNT ON;
  51. SET XACT_ABORT ON;
  52. BEGIN TRY
  53. --IF @ACCESSCODE ='IMEQAAJ007'
  54. --SET @CALC_BY = 'C'
  55. DECLARE @apiRequestId INT
  56. INSERT INTO apiRequestLog(
  57. AGENT_CODE,
  58. USER_ID,
  59. PASSWORD,
  60. AGENT_SESSION_ID,
  61. AGENT_TXN_ID,
  62. SENDER_NAME,
  63. SENDER_ADDRESS,
  64. SENDER_MOBILE,
  65. SENDER_CITY,
  66. SENDER_COUNTRY,
  67. SENDER_ID_TYPE,
  68. SENDER_ID_NUMBER,
  69. RECEIVER_NAME,
  70. RECEIVER_ADDRESS,
  71. RECEIVER_CONTACT_NUMBER,
  72. RECEIVER_CITY,
  73. RECEIVER_COUNTRY,
  74. TRANSFER_AMOUNT,
  75. COLLECT_AMT,
  76. PAYOUTAMT,
  77. PAYMENT_MODE,
  78. BANK_ID,
  79. BANK_ACCOUNT_NUMBER,
  80. CALC_BY,
  81. BANK_NAME,
  82. BANK_BRANCH_NAME,
  83. REQUESTED_DATE,
  84. OCCUPATION,
  85. SOURCE_OF_FUND,
  86. RELATIONSHIP,
  87. PURPOSE_OF_REMITTANCE
  88. )
  89. SELECT
  90. @ACCESSCODE,
  91. @USERNAME,
  92. @PASSWORD,
  93. @FOREX_SESSION_ID,
  94. @AGENT_TXN_REF_ID,
  95. @SENDER_NAME,
  96. @SENDER_ADDRESS,
  97. @SENDER_MOBILE,
  98. @SENDER_CITY,
  99. @SENDER_COUNTRY,
  100. @SENDERS_IDENTITY_TYPE,
  101. @SENDER_IDENTITY_NUMBER,
  102. @RECEIVER_NAME,
  103. @RECEIVER_ADDRESS,
  104. @RECEIVER_CONTACT_NUMBER,
  105. @RECEIVER_CITY,
  106. @RECEIVER_COUNTRY,
  107. @COLLECT_AMT,
  108. @COLLECT_AMT,
  109. @PAYOUTAMT,
  110. @PAYMENTTYPE,
  111. @BANKID,
  112. @BANK_ACCOUNT_NUMBER,
  113. @CALC_BY,
  114. @BANK_NAME,
  115. @BANK_BRANCH_NAME,
  116. GETDATE(),
  117. @OCCUPATION,
  118. @SOURCE_OF_FUND,
  119. @RELATIONSHIP,
  120. @PURPOSE_OF_REMITTANCE
  121. SET @apiRequestId = SCOPE_IDENTITY()
  122. SET @SENDER_NAME = UPPER(@SENDER_NAME)
  123. SET @RECEIVER_NAME = UPPER(@RECEIVER_NAME)
  124. DECLARE
  125. @sCountryId INT,@sCountry VARCHAR(50),@collCurr VARCHAR(3), @sSuperAgent INT
  126. ,@sSuperAgentName VARCHAR(200),@pBankName VARCHAR(200),@sAgent INT
  127. ,@sAgentName VARCHAR(200), @sBranch INT,@sBranchName VARCHAR(200)
  128. ,@deliveryMethod VARCHAR(100), @deliveryMethodId INT,@pCountryId INT, @pCurr VARCHAR(3)
  129. ,@pSuperAgent INT,@pSuperAgentName VARCHAR(200), @pAgent INT,@pAgentName VARCHAR(200)
  130. ,@pBranch INT,@pBranchName VARCHAR(200),@pBankBranchName VARCHAR(200)
  131. ,@controlNo VARCHAR(50),@tranId INT
  132. ----# EXTRA THIRDPARTY AGENTS
  133. DECLARE @MTradeMalaysia INT = 9560
  134. SELECT @pSuperAgent = 000,@pSuperAgentName = 'Intl Payout Super Agent',@pCountryId= 151
  135. DECLARE @errorTable TABLE(
  136. AGENT_TXN_REF_ID VARCHAR(150), REFNO VARCHAR(50), AGENT_TXNID INT, COLLECT_AMT MONEY, COLLECT_CURRENCY VARCHAR(3), EXCHANGE_RATE MONEY
  137. ,SERVICE_CHARGE MONEY, PAYOUTAMT MONEY, PAYOUTCURRENCY VARCHAR(3), TXN_DATE VARCHAR(10)
  138. )
  139. --SELECT @agentApiType = agentApiType FROM agentMaster WITH(NOLOCK) WHERE agentCode = @ACCESSCODE
  140. INSERT INTO @errorTable(AGENT_TXN_REF_ID)
  141. SELECT @AGENT_TXN_REF_ID
  142. DECLARE @errCode INT, @autMsg VARCHAR(500), @errorCode VARCHAR(10), @errorMsg VARCHAR(MAX)
  143. EXEC ws_int_proc_checkAuthntication @USERNAME,@PASSWORD,@ACCESSCODE,@errCode OUT, @autMsg OUT
  144. IF (@errCode = 1 )
  145. BEGIN --1002
  146. SELECT @errorCode = '102', @errorMsg = ISNULL(@autMsg, 'Authentication Fail')
  147. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  148. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  149. RETURN
  150. END
  151. IF EXISTS(SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userName = @USERNAME AND forceChangePwd = 'Y')
  152. BEGIN
  153. SELECT @errorCode = '102', @errorMsg = 'You logged on first time,must first change your password and try again!'
  154. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  155. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  156. RETURN
  157. END
  158. -->>------------------VALIDATION-------------------------------
  159. IF @FOREX_SESSION_ID IS NULL
  160. BEGIN --1001
  161. SELECT @errorCode = '102', @errorMsg = 'FOREX SESSION ID Field is Empty'
  162. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  163. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  164. RETURN
  165. END
  166. IF @AGENT_TXN_REF_ID IS NULL
  167. BEGIN
  168. SELECT @errorCode = '102', @errorMsg = 'AGENT TXN REF ID Field is Empty'
  169. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  170. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  171. RETURN;
  172. END
  173. IF @SENDER_NAME IS NULL
  174. BEGIN
  175. SELECT @errorCode = '102', @errorMsg = 'SENDER NAME Field is Empty'
  176. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  177. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  178. RETURN
  179. END
  180. IF @SENDER_COUNTRY IS NULL
  181. BEGIN
  182. SELECT @errorCode = '102', @errorMsg = 'SENDER COUNTRY Field is Empty'
  183. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  184. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  185. RETURN
  186. END
  187. IF @RECEIVER_NAME IS NULL
  188. BEGIN
  189. SELECT @errorCode = '102', @errorMsg = 'RECEIVER NAME Field is Empty'
  190. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  191. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  192. RETURN
  193. END
  194. IF @RECEIVER_ADDRESS IS NULL
  195. BEGIN
  196. SELECT @errorCode = '102', @errorMsg = 'RECEIVER ADDRESS Field is Empty'
  197. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  198. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  199. RETURN
  200. END
  201. IF @RECEIVER_COUNTRY IS NULL
  202. BEGIN
  203. SELECT @errorCode = '102', @errorMsg = 'RECEIVER COUNTRY Field is Empty'
  204. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  205. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  206. RETURN
  207. END
  208. IF @COLLECT_AMT IS NULL AND @PAYOUTAMT IS NULL
  209. BEGIN
  210. SELECT @errorCode = '102', @errorMsg = 'Both COLLECT AMT and PAYOUTAMT Field is Empty'
  211. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  212. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  213. RETURN
  214. END
  215. IF @CALC_BY = 'P'
  216. BEGIN
  217. IF @PAYOUTAMT IS NULL
  218. BEGIN
  219. SET @CALC_BY = 'C'
  220. END
  221. END
  222. IF @CALC_BY = 'C'
  223. BEGIN
  224. IF @COLLECT_AMT IS NULL
  225. BEGIN
  226. SELECT @errorCode = '102', @errorMsg = 'COLLECT AMT Field is Empty'
  227. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  228. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  229. RETURN
  230. END
  231. END
  232. IF ISNUMERIC(@COLLECT_AMT) = 0 AND @COLLECT_AMT IS NOT NULL
  233. BEGIN
  234. SELECT @errorCode = '9001', @errorMsg = 'Technical Error: TRANSFER AMOUNT must be numeric'
  235. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  236. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  237. RETURN
  238. END
  239. IF ISNUMERIC(@REMITTER_ID) = 0 AND @REMITTER_ID IS NOT NULL
  240. BEGIN
  241. SELECT @errorCode = '9001', @errorMsg = 'Technical Error: REMITTER ID must be numeric'
  242. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  243. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  244. RETURN
  245. END
  246. IF ISNUMERIC(@BANKID) = 0 AND @BANKID IS NOT NULL
  247. BEGIN
  248. SELECT @errorCode = '9001', @errorMsg = 'Technical Error: BANK ID must be numeric'
  249. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  250. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  251. RETURN
  252. END
  253. IF @PAYMENTTYPE IS NULL
  254. BEGIN
  255. SELECT @errorCode = '102', @errorMsg = 'PAYMENT TYPE Field is Empty'
  256. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  257. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  258. RETURN
  259. END
  260. IF @CALC_BY IS NULL
  261. BEGIN
  262. SELECT @errorCode = '102', @errorMsg = 'CALC BY Field is Empty'
  263. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  264. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  265. RETURN
  266. END
  267. IF @PAYMENTTYPE NOT IN('C','B')
  268. BEGIN
  269. SELECT @errorCode = '205', @errorMsg = 'Invalid Payment Type, Must be C - Cash Pickup OR B - Account Deposit to Bank'
  270. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  271. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  272. RETURN
  273. END
  274. IF @PAYMENTTYPE = 'B'
  275. BEGIN
  276. IF ISNUMERIC(@BANKID) = 0
  277. BEGIN
  278. SELECT @errorCode = '9001', @errorMsg = 'Provided Bank ID can not perform Account Deposit'
  279. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  280. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  281. RETURN
  282. END
  283. IF @BANK_ACCOUNT_NUMBER IS NULL
  284. BEGIN
  285. SELECT @errorCode = '9001', @errorMsg = 'BANK ACCOUNT NUMBER field is empty'
  286. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  287. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  288. RETURN
  289. END
  290. IF ISNUMERIC(@BANK_ACCOUNT_NUMBER)=0
  291. BEGIN
  292. SELECT @errorCode = '9001', @errorMsg = 'Technical Error: BANK ACCOUNT NUMBER must be numeric'
  293. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  294. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  295. RETURN
  296. END
  297. END
  298. IF @CALC_BY NOT IN('C','P')
  299. BEGIN
  300. SELECT @errorCode = '104', @errorMsg = 'Invalid Parameter CALC BY'
  301. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  302. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  303. RETURN
  304. END
  305. -->>Agent who supplies their own ICN
  306. DECLARE @WORLDCOMFINANCE INT = 4907
  307. --<<End of Agent who supplies their own ICN
  308. -->>Get Sending Agent Details
  309. SELECT
  310. @sCountryId = countryId,
  311. @sBranch = sb.agentId,
  312. @sBranchName = sb.agentName,
  313. @sAgent = sa.agentId,
  314. @sAgentName = sa.agentName,
  315. @sSuperAgent = ssa.agentId,
  316. @sSuperAgentName = ssa.agentName
  317. FROM applicationUsers au WITH (NOLOCK)
  318. LEFT JOIN agentMaster sb WITH (NOLOCK) ON au.agentId = sb.agentId
  319. LEFT JOIN agentMaster sa WITH (NOLOCK) ON sa.agentId = sb.parentId
  320. LEFT JOIN agentMaster ssa WITH (NOLOCK) ON ssa.agentId = sa.parentId
  321. WHERE userName = @USERNAME
  322. AND ISNULL(au.isActive,'N')='Y'
  323. AND ISNULL(sb.isActive,'N')='Y'
  324. AND ISNULL(sa.isActive,'N')='Y'
  325. AND ISNULL(ssa.isActive,'N')='Y'
  326. --SELECT @sCountryId,@sBranch,@sBranchName,@sAgent,@sAgentName,@sSuperAgent,@sSuperAgentName
  327. --RETURN
  328. SELECT TOP 1
  329. @collCurr = CM.currencyCode,
  330. @sCountry = C.countryName
  331. FROM countryCurrency CC WITH (NOLOCK)
  332. INNER JOIN countryMaster C WITH(NOLOCK ) ON C.countryId = CC.countryId
  333. INNER JOIN currencyMaster CM WITH(NOLOCK) ON CC.currencyId = CM.currencyId
  334. WHERE CC.countryId = @sCountryId AND ISNULL(Cc.isDeleted,'N') = 'N'
  335. --<<End of Get Sending Agent Details
  336. DECLARE @Excurr varchar(5)
  337. SELECT @Excurr = cCurrency FROM exRateTreasury(NOLOCK) WHERE CAGENT = @sAgent
  338. IF @Excurr <> @collCurr
  339. SET @collCurr = @Excurr
  340. -->>Get Payout Agent Details--
  341. DECLARE
  342. @pBank INT,@pBankBranch INT
  343. ,@pBankType CHAR(1)
  344. ,@pBankCountry VARCHAR(100) = 'Nepal'
  345. ,@EXTERNALCODE VARCHAR(50)
  346. IF @PAYMENTTYPE = 'B'
  347. BEGIN
  348. DECLARE @parentMapCode VARCHAR(50)
  349. SELECT @pAgent = parentId, @pBankBranchName = agentName FROM dbo.agentMaster WITH(NOLOCK) WHERE agentId = @BANKID
  350. IF NOT EXISTS (SELECT 'X' FROM dbo.agentMaster WITH(NOLOCK) WHERE agentId = @BANKID AND agentType = '2904')
  351. BEGIN
  352. SELECT @errorCode = '102', @errorMsg = 'Invalid BANK ID'
  353. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  354. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  355. RETURN
  356. END
  357. IF EXISTS (SELECT 'X' FROM dbo.agentMaster WITH(NOLOCK) WHERE agentId = @pAgent AND ISNULL(IsIntl, 0) = 0)
  358. BEGIN
  359. SELECT @errorCode = '102', @errorMsg = 'Invalid BANK ID'
  360. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  361. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  362. RETURN
  363. END
  364. SELECT @pBank = @pAgent, @pBankName = agentName FROM dbo.agentMaster WITH(NOLOCK) WHERE agentId = @pAgent
  365. SET @pBranch = @BANKID
  366. SET @pBankBranch = @BANKID
  367. SET @pAgentName = @pBankName
  368. SET @pBranchName = @pBankBranchName
  369. --SELECT TOP 1 @parentMapCode = pam.mapCodeInt, @pBankBranchName = am.agentName FROM agentMaster am WITH(NOLOCK)
  370. --INNER JOIN agentMaster pam WITH(NOLOCK) ON am.parentId = pam.agentId
  371. --WHERE am.mapCodeInt = @BANKID
  372. --SELECT TOP 1
  373. -- @pBank = extBankId, @pBankName = bankName, @EXTERNALCODE = externalCode
  374. --FROM externalBank WITH(NOLOCK)
  375. --WHERE mapCodeInt = @parentMapCode AND ISNULL(isDeleted, 'N') = 'N'
  376. --SELECT
  377. -- @pBankName = ISNULL(@pBankName, @BANK_NAME)
  378. -- ,@pBankType = 'E'
  379. --IF @pBank IS NULL AND ISNUMERIC(@BANKID) = 1
  380. --BEGIN
  381. -- SELECT @pBank = extBankId, @pBankBranch = extBranchId, @pBankBranchName = branchName FROM externalBankBranch WITH(NOLOCK) WHERE extBranchId = @BANKID
  382. -- SELECT @pBankName = bankName, @EXTERNALCODE = externalCode FROM externalBank WITH(NOLOCK) WHERE extBankId = @pBank
  383. --END
  384. --IF @PAYMENTTYPE = 'B'
  385. --BEGIN
  386. -- SELECT TOP 1 @pAgent = internalCode, @pAgentName = bankName FROM externalBank WITH(NOLOCK) WHERE extBankId = @pBank
  387. -- SELECT @pBranchName = ISNULL(@pBankBranchName, @BANK_BRANCH_NAME)
  388. --END
  389. -->>Validate BANK_ID only for Bank Transfer and Account Deposit
  390. IF @pBank IS NULL
  391. BEGIN
  392. SELECT @errorCode = '102', @errorMsg = 'Invalid BANK ID'
  393. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  394. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  395. RETURN
  396. END
  397. END
  398. DECLARE @currDecimal INT
  399. SELECT TOP 1
  400. @currDecimal = CM.countAfterDecimal
  401. ,@pCurr = 'NPR'
  402. FROM countryCurrency CC WITH(NOLOCK)
  403. INNER JOIN currencyMaster CM WITH(NOLOCK) ON CC.currencyId = CM.currencyId
  404. WHERE CC.countryId = @pCountryId AND ISNULL(cc.isDeleted, 'N') = 'N'
  405. IF @pCurr IS NULL
  406. BEGIN
  407. SELECT @errorCode = '102', @errorMsg = 'You are not allowed to send to country ' + @RECEIVER_COUNTRY
  408. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  409. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  410. RETURN
  411. END
  412. --<<End of Get Payout Agent Details
  413. --IF EXISTS (
  414. -- SELECT
  415. -- 'x'
  416. -- FROM exRateCalcHistory WITH(NOLOCK)
  417. -- WHERE FOREX_SESSION_ID = @FOREX_SESSION_ID
  418. -- AND isExpired = 'U'
  419. --)
  420. --BEGIN
  421. -- SELECT
  422. -- 302 CODE
  423. -- ,'Your FOREX SESSION ID is Already used' MESSAGE
  424. -- ,*
  425. -- FROM @errorTable
  426. -- RETURN
  427. --END
  428. --IF EXISTS (
  429. -- SELECT
  430. -- 'x'
  431. -- FROM exRateCalcHistory WITH(NOLOCK)
  432. -- WHERE FOREX_SESSION_ID = @FOREX_SESSION_ID
  433. -- AND (isExpired = 'N' OR isExpired IS NULL)
  434. -- AND createdDate < DATEADD(MINUTE,-5,GETDATE())
  435. --)
  436. --BEGIN
  437. -- SELECT
  438. -- 302 CODE
  439. -- ,'FOREX SESSION ID Expired' MESSAGE
  440. -- ,*
  441. -- FROM @errorTable
  442. -- RETURN
  443. --END
  444. -------------------END ----------------------------------
  445. -->>Manage Payment Type
  446. SELECT
  447. @deliveryMethodId = serviceTypeId,
  448. @deliveryMethod = typeTitle
  449. FROM serviceTypeMaster WITH(NOLOCK)
  450. WHERE ISNULL(isDeleted,'N') = 'N'
  451. AND typeTitle = CASE
  452. WHEN @PAYMENTTYPE = 'C' THEN 'CASH PAYMENT'
  453. WHEN @PAYMENTTYPE = 'B' THEN 'BANK DEPOSIT'
  454. END
  455. -->>Start:Field Validation according to setup
  456. DECLARE @errTable TABLE(errorCode VARCHAR(10), msg VARCHAR(200), id VARCHAR(10))
  457. INSERT INTO @errTable(errorCode, msg, id)
  458. EXEC proc_sendValidation
  459. @agentId = @sAgent
  460. ,@senIdType = @SENDERS_IDENTITY_TYPE
  461. ,@senIdNo = @SENDER_IDENTITY_NUMBER
  462. --,@senIdValidDate = @SENDER_ID_EXPIRE_DATE
  463. --,@senDob = @SENDER_DATE_OF_BIRTH
  464. ,@senAddress = @SENDER_ADDRESS
  465. ,@senCity = @SENDER_CITY
  466. ,@senContact = @SENDER_MOBILE
  467. ,@recAddress = @RECEIVER_ADDRESS
  468. ,@recCity = @RECEIVER_CITY
  469. ,@recContact = @RECEIVER_CONTACT_NUMBER
  470. ,@paymentMethod = @deliveryMethod
  471. ,@deliveryMethodId = @deliveryMethodId
  472. ,@pBank = @pBank
  473. ,@pBankBranchName = @pBankBranchName
  474. ,@accountNo = @BANK_ACCOUNT_NUMBER
  475. ,@pAgent = 99999
  476. ,@pBankType = @pBankType
  477. ,@pCountryId = @pCountryId
  478. ,@sCountryId = @sCountryId
  479. IF NOT EXISTS(SELECT 'X' FROM @errTable WHERE errorCode = '0')
  480. BEGIN
  481. DECLARE @msg VARCHAR(200)
  482. SELECT @msg = msg FROM @errTable
  483. SELECT @errorCode = '1001', @errorMsg = @msg
  484. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  485. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  486. RETURN
  487. END
  488. --<<End:Field Validation according to Setup
  489. DECLARE @collMode VARCHAR(20),@iCollDetailAmt MONEY
  490. SET @collMode = 'Cash'
  491. DECLARE @iServiceCharge MONEY,@tAmt MONEY,@pAmt MONEY,@cAmt MONEY
  492. DECLARE @rowId INT, @place INT
  493. -->>Find Decimal Mask for payout amount rounding
  494. SELECT @place = place, @currDecimal = currDecimal
  495. FROM currencyPayoutRound WITH(NOLOCK) WHERE ISNULL(isDeleted, 'N') = 'N'
  496. AND currency = @pCurr AND tranType IS NULL
  497. -->>Start:Get Exchange Rate Details
  498. DECLARE @customerRate MONEY,@sCurrCostRate MONEY,@sCurrHoMargin MONEY,@sCurrAgentMargin MONEY,@pCurrCostRate MONEY,@pCurrHoMargin MONEY
  499. ,@pCurrAgentMargin MONEY,@agentCrossSettRate MONEY,@treasuryTolerance MONEY,@sharingValue MONEY,@sharingType CHAR(1),@customerPremium MONEY
  500. ,@sAgentComm MONEY,@sAgentCommCurrency VARCHAR(3)
  501. ,@date varchar(20) = cast(getdate() as date)
  502. IF @sAgent = @MTradeMalaysia ----## FOR MTRADE ASIA
  503. BEGIN
  504. SELECT
  505. @customerRate = customerRate
  506. ,@sCurrCostRate = sCurrCostRate
  507. ,@sCurrHoMargin = sCurrHoMargin
  508. ,@sCurrAgentMargin = sCurrAgentMargin
  509. ,@pCurrCostRate = pCurrCostRate
  510. ,@pCurrHoMargin = pCurrHoMargin
  511. ,@pCurrAgentMargin = pCurrAgentMargin
  512. ,@agentCrossSettRate = agentCrossSettRate
  513. ,@treasuryTolerance = treasuryTolerance
  514. ,@customerPremium = customerPremium
  515. ,@sharingValue = sharingValue
  516. ,@sharingType = sharingType
  517. FROM dbo.FNAGetExRate(@sCountryId,@sAgent,@sBranch,@collCurr,@pCountryId,@pAgent,@pCurr,@deliveryMethodId)
  518. --SET @customerRate = 107
  519. IF @customerRate IS NULL
  520. BEGIN
  521. SELECT '102' CODE, 'Ex-Rate Not Defined for Receiving Currency (' + @pCurr + ')' MESSAGE, * FROM @errorTable
  522. RETURN
  523. END
  524. SET @CALC_BY = 'P'
  525. END
  526. ELSE
  527. BEGIN
  528. IF NOT EXISTS ( SELECT 'x' FROM exRateCalcHistory WITH(NOLOCK) WHERE FOREX_SESSION_ID = @FOREX_SESSION_ID AND USER_ID = @USERNAME)
  529. BEGIN
  530. SELECT @errorCode = '302', @errorMsg = 'Invalid FOREX SESSION ID OR Parameter miss matched'
  531. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  532. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  533. RETURN
  534. END
  535. ELSE
  536. BEGIN
  537. SELECT top 1
  538. @customerRate = customerRate
  539. ,@sCurrCostRate = sCurrCostRate
  540. ,@sCurrHoMargin = sCurrHoMargin
  541. ,@sCurrAgentMargin = sCurrAgentMargin
  542. ,@pCurrCostRate = pCurrCostRate
  543. ,@pCurrHoMargin = pCurrHoMargin
  544. ,@pCurrAgentMargin = pCurrAgentMargin
  545. ,@agentCrossSettRate = agentCrossSettRate
  546. ,@treasuryTolerance = treasuryTolerance
  547. ,@customerPremium = customerPremium
  548. ,@sharingValue = sharingValue
  549. ,@sharingType = sharingType
  550. FROM exRateCalcHistory WITH(NOLOCK)
  551. WHERE FOREX_SESSION_ID = @FOREX_SESSION_ID
  552. AND USER_ID = @USERNAME
  553. --AND CreatedDate BETWEEN @date AND @date +' 23:59'
  554. ORDER BY rowId DESC
  555. END
  556. END
  557. IF @customerRate IS NULL
  558. BEGIN
  559. SELECT @errorCode = '102', @errorMsg = 'Transaction cannot be proceed. Exchange Rate not defined'
  560. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  561. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  562. RETURN
  563. END
  564. --<<End:Get Exchange Rate Details
  565. -->>Start:Get tAmt, cAmt, pAmt on the basis of CALCBY
  566. SELECT
  567. @cAmt = CASE WHEN @CALC_BY = 'C' THEN CAST(@COLLECT_AMT AS MONEY) ELSE '0' END
  568. ,@pAmt = CASE WHEN @CALC_BY = 'P' THEN CAST(@PAYOUTAMT AS MONEY) ELSE '0' END
  569. SET @pAmt = dbo.FNARemitRoundForNPR(@pAmt)
  570. DECLARE @scDiscount MONEY
  571. IF ISNULL(@cAmt, 0.00) <> 0.00 AND @CALC_BY = 'C'
  572. BEGIN
  573. SELECT
  574. @iServiceCharge = amount
  575. FROM [dbo].FNAGetServiceCharge(
  576. @sCountryId, @sSuperAgent, @sAgent, @sBranch
  577. ,@pCountryId, NULL, @pAgent, NULL
  578. ,@deliveryMethodId, @cAmt, @collCurr
  579. )
  580. IF @iServiceCharge IS NULL
  581. BEGIN
  582. SELECT @errorCode = '102', @errorMsg = 'Service Charge Not Defined for Receiving Country'
  583. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  584. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  585. RETURN;
  586. END
  587. --Nepal Relief Fund
  588. --IF @pCountryId = 151 AND @sAgent IN (4880)
  589. --BEGIN
  590. -- SET @scDiscount = @iServiceCharge
  591. -- SET @iServiceCharge = 0
  592. --END
  593. SET @tAmt = @cAmt - @iServiceCharge
  594. SET @pAmt = @tAmt * @customerRate
  595. SET @pAmt = dbo.FNARemitRoundForNPR(@pAmt)
  596. /*
  597. SET @currDecimal = ISNULL(@currDecimal, 0)
  598. IF @currDecimal IS NOT NULL
  599. BEGIN
  600. SET @pAmt = ROUND(@pAmt, @currDecimal, 1)
  601. END
  602. ELSE IF @place IS NOT NULL
  603. BEGIN
  604. SET @pAmt = ROUND(@pAmt, -@place, 1)
  605. END
  606. */
  607. END
  608. ELSE
  609. BEGIN
  610. SET @tAmt = @pAmt/(@customerRate)
  611. SELECT
  612. @iServiceCharge = amount
  613. FROM [dbo].FNAGetServiceCharge(
  614. @sCountryId, @sSuperAgent, @sAgent, NULL
  615. ,@pCountryId, NULL, @pAgent, NULL
  616. ,@deliveryMethodId, @tAmt, @collCurr
  617. )
  618. IF @sAgent = @MTradeMalaysia
  619. BEGIN
  620. SELECT
  621. @iServiceCharge = amount
  622. FROM [dbo].FNAGetServiceCharge(
  623. @sCountryId, @sSuperAgent, @sAgent, @sBranch
  624. ,@pCountryId, NULL, @pAgent, NULL
  625. ,@deliveryMethodId, @tAmt, @collCurr
  626. )
  627. END
  628. IF @iServiceCharge IS NULL
  629. BEGIN
  630. SELECT @errorCode = '102', @errorMsg = 'Service Charge Not Defined for Receiving Country'
  631. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  632. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  633. RETURN
  634. END
  635. --IF @pCountryId = 151 AND @sAgent IN (4880)
  636. --BEGIN
  637. -- SET @scDiscount = @iServiceCharge
  638. -- SET @iServiceCharge = 0
  639. --END
  640. SET @cAmt = (@tAmt + @iServiceCharge)
  641. END
  642. --SELECT @errorCode CODE, @iServiceCharge MESSAGE, * FROM @errorTable
  643. --RETURN;
  644. IF ISNULL(@iServiceCharge,0) >= ISNULL(@cAmt,0)
  645. BEGIN
  646. SELECT @errorCode = '102', @errorMsg = 'Sent Amount must be more than Service Charge'
  647. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  648. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  649. RETURN;
  650. END
  651. --<<End:Get tAmt, cAmt, pAmt on the basis of CALCBY
  652. --SET @pAmt = ROUND(@pAmt, 0, 1)
  653. -->>Start:Commission Calculation
  654. DECLARE @sSettlementRate FLOAT, @pSettlementRate FLOAT,@tellerBalance MONEY
  655. SET @sSettlementRate = @sCurrCostRate + @sCurrHoMargin
  656. SET @pSettlementRate = @pCurrCostRate - @pCurrHoMargin
  657. SELECT
  658. @sAgentComm = amount,
  659. @sAgentCommCurrency = commissionCurrency
  660. FROM dbo.FNAGetSendComm(
  661. @sCountryId, @sSuperAgent, @sAgent, @sBranch,
  662. @pCountryId, @pSuperAgent, @pAgent, @pBranch,
  663. @collCurr, @deliveryMethodId, @cAmt, @pAmt, @iServiceCharge, NULL, NULL,
  664. @sSettlementRate, @pSettlementRate
  665. )
  666. --Nepal Relief Fund
  667. --IF @pCountryId = 151 AND @sAgent IN (4880)
  668. --BEGIN
  669. -- SET @sAgentComm = 0
  670. --END
  671. --<<End:Commission Calculation
  672. -->>Start:Verify Agent Send Per Txn
  673. IF EXISTS(
  674. SELECT
  675. 'x'
  676. FROM sendTranLimit
  677. WHERE agentId = @sAgent
  678. AND ISNULL(receivingCountry, ISNULL(@pCountryId, 0)) = ISNULL(@pCountryId, 0)
  679. AND ISNULL(tranType, ISNULL(@deliveryMethodId, 0)) = ISNULL(@deliveryMethodId, 0)
  680. AND currency = @collCurr AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  681. )
  682. BEGIN
  683. IF EXISTS(
  684. SELECT
  685. 'x' FROM sendTranLimit WITH(NOLOCK) WHERE agentId = @sAgent AND receivingCountry = @pCountryId
  686. AND tranType = @deliveryMethodId AND currency = @collCurr
  687. AND @cAmt < minLimitAmt
  688. AND @cAmt > maxLimitAmt
  689. AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N')
  690. BEGIN
  691. SELECT @errorCode = '104', @errorMsg = 'Partner Balance Exceed, Cannot Make a TXN'
  692. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  693. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  694. RETURN
  695. END
  696. IF EXISTS(
  697. SELECT
  698. 'x'
  699. FROM sendTranLimit WITH(NOLOCK)
  700. WHERE agentId = @sAgent AND receivingCountry = @pCountryId
  701. AND tranType IS NULL AND currency = @collCurr
  702. AND @cAmt < minLimitAmt
  703. AND @cAmt > maxLimitAmt
  704. AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  705. )
  706. BEGIN
  707. SELECT @errorCode = '104', @errorMsg = 'Partner Balance Exceed, Cannot Make a TXN'
  708. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  709. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  710. RETURN
  711. END
  712. IF EXISTS(
  713. SELECT
  714. 'x'
  715. FROM sendTranLimit WITH(NOLOCK)
  716. WHERE agentId IS NULL AND receivingCountry = @pCountryId
  717. AND tranType = @deliveryMethodId AND currency = @collCurr
  718. AND @cAmt < minLimitAmt
  719. AND @cAmt > maxLimitAmt
  720. AND ISNULL(isActive, 'N') = 'Y'
  721. AND ISNULL(isDeleted, 'N') = 'N')
  722. BEGIN
  723. SELECT @errorCode = '104', @errorMsg = 'Partner Balance Exceed, Cannot Make a TXN'
  724. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  725. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  726. RETURN
  727. END
  728. IF EXISTS(SELECT 'X' FROM sendTranLimit WITH(NOLOCK) WHERE agentId = @sAgent AND receivingCountry = @pCountryId
  729. AND tranType IS NULL AND currency = @collCurr
  730. AND @cAmt < minLimitAmt
  731. AND @cAmt > maxLimitAmt
  732. AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N')
  733. BEGIN
  734. SELECT @errorCode = '104', @errorMsg = 'Partner Balance Exceed, Cannot Make a TXN'
  735. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  736. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  737. RETURN
  738. END
  739. END
  740. --<<End:Verify Agent Send Per Txn
  741. -->>Start:Verify Payout Per Txn Limit
  742. IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK) WHERE ISNULL(sendingCountry, ISNULL(@sCountryId, 0)) = ISNULL(@sCountryId, 0)
  743. AND countryId = @pCountryId AND agentId = @pAgent AND currency = @pCurr
  744. AND ISNULL(tranType, ISNULL(@deliveryMethodId, 0)) = ISNULL(@deliveryMethodId, 0)
  745. AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  746. )
  747. BEGIN
  748. IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK)
  749. WHERE sendingCountry = @sCountryId AND countryId = @pCountryId AND agentId = @pAgent AND currency = @pCurr
  750. AND tranType = @deliveryMethodId AND @pAmt > maxLimitAmt AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  751. )
  752. BEGIN
  753. SELECT @errorCode = '104', @errorMsg = 'Payout Amount Limit Exceed'
  754. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  755. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  756. RETURN
  757. END
  758. IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK)
  759. WHERE sendingCountry = @sCountryId AND countryId = @pCountryId AND agentId = @pAgent AND currency = @pCurr
  760. AND tranType IS NULL AND @pAmt > maxLimitAmt AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  761. )
  762. BEGIN
  763. SELECT @errorCode = '104', @errorMsg = 'Payout Amount Limit Exceed'
  764. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  765. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  766. RETURN
  767. END
  768. IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK)
  769. WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId = @pAgent AND currency = @pCurr
  770. AND tranType = @deliveryMethodId AND @pAmt > maxLimitAmt AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  771. )
  772. BEGIN
  773. SELECT @errorCode = '104', @errorMsg = 'Payout Amount Limit Exceed'
  774. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  775. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  776. RETURN
  777. END
  778. IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK)
  779. WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId = @pAgent AND currency = @pCurr
  780. AND tranType IS NULL AND @pAmt > maxLimitAmt AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  781. )
  782. BEGIN
  783. SELECT @errorCode = '104', @errorMsg = 'Payout Amount Limit Exceed'
  784. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  785. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  786. RETURN
  787. END
  788. END
  789. IF NOT EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK)
  790. WHERE ISNULL(sendingCountry, ISNULL(@sCountryId, 0)) = ISNULL(@sCountryId, 0)
  791. AND agentId IS NULL AND countryId = @pCountryId AND currency = @pCurr
  792. AND ISNULL(tranType, ISNULL(@deliveryMethodId, 0)) = ISNULL(@deliveryMethodId, 0)
  793. AND maxLimitAmt >= @pAmt AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
  794. )
  795. BEGIN
  796. SELECT @errorCode = '104', @errorMsg = 'Payout Amount Limit Exceed'
  797. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  798. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  799. RETURN
  800. END
  801. -->>End:Verify Payout Per Txn Limit
  802. DECLARE @remarks VARCHAR(500)
  803. IF EXISTS(SELECT TOP 1 'X' FROM thirdPartyAgentTxnIdV2 WITH(NOLOCK) WHERE agentTxnId = @AGENT_TXN_REF_ID AND agentId = @sAgent)
  804. BEGIN
  805. SELECT TOP 1 @controlNo = controlNo FROM remitTran (NOLOCK) WHERE controlNo2 = dbo.FNAEncryptString(@AGENT_TXN_REF_ID) AND sAgent = @sAgent -- AND createdBy = @USERNAME
  806. SELECT @errorCode = '1001', @errorMsg = 'Duplicate Ref ID : ' + @AGENT_TXN_REF_ID + '; ICN : ' + dbo.FNADecryptString(@controlNo)
  807. EXEC ws_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg, @remarks = @remarks
  808. SELECT TOP 1
  809. CODE = @errorCode
  810. ,AGENT_TXN_REF_ID = @AGENT_TXN_REF_ID
  811. ,MESSAGE = @errorMsg
  812. ,REFNO = dbo.FNADecryptString(@controlNo)
  813. --,AGENT_TXNID = 1
  814. ,COLLECT_AMT = cAmt
  815. --,COLLECT_CURRENCY = collCurr
  816. ,EXCHANGE_RATE = customerRate
  817. ,SERVICE_CHARGE = serviceCharge
  818. ,PAYOUTAMT = pAmt
  819. ,PAYOUTCURRENCY = payoutCurr
  820. --,TXN_DATE = createdDate
  821. FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNo
  822. --SELECT
  823. -- CODE = '0'
  824. -- ,AGENT_TXN_REF_ID = @AGENT_TXN_REF_ID
  825. -- ,MESSAGE = 'Transaction created Successfully'--CASE WHEN @AUTHORIZED_REQUIRED='Y' THEN 'Transaction need Authorization' ELSE 'Transaction saved successfully' END
  826. -- ,REFNO = @controlNo
  827. -- --,AGENT_TXNID = 1
  828. -- ,COLLECT_AMT = cAmt
  829. -- --,COLLECT_CURRENCY = collCurr
  830. -- ,EXCHANGE_RATE = customerRate
  831. -- ,SERVICE_CHARGE = serviceCharge
  832. -- ,PAYOUTAMT = pAmt
  833. -- ,PAYOUTCURRENCY = payoutCurr
  834. -- --,TXN_DATE = createdDate
  835. --FROM remitTran WITH(NOLOCK)
  836. --WHERE id = @tranId
  837. RETURN
  838. END
  839. -->>Start:Get Data Compliance for Compliance Checking and suspicious duplicate txn
  840. DECLARE @today VARCHAR(10) = CONVERT(VARCHAR, dbo.FNADateFormatTZ(GETDATE(), @USERNAME), 101)
  841. DECLARE @remitTranTemp TABLE(tranId BIGINT, controlNo VARCHAR(20), cAmt MONEY, receiverName VARCHAR(200), receiverIdType VARCHAR(100), receiverIdNumber VARCHAR(50), dot DATETIME)
  842. INSERT INTO @remitTranTemp(tranId, controlNo, cAmt, receiverName, receiverIdType, receiverIdNumber, dot)
  843. SELECT rt.id, rt.controlNo, rt.cAmt, rt.receiverName, rec.idType, rec.idNumber, rt.createdDateLocal
  844. FROM vwRemitTran rt WITH(NOLOCK)
  845. INNER JOIN vwTranSenders sen WITH(NOLOCK) ON rt.id = sen.tranId
  846. INNER JOIN vwTranReceivers rec WITH(NOLOCK) ON rt.id = rec.tranId
  847. WHERE sen.idNumber = @SENDER_IDENTITY_NUMBER
  848. AND tranStatus <> 'CancelRequest' AND tranStatus <> 'Cancel'
  849. AND (rt.approvedDate BETWEEN @today AND @today + ' 23:59:59'
  850. OR (approvedBy IS NULL AND cancelApprovedBy IS NULL))
  851. IF EXISTS(SELECT 'X' FROM @remitTranTemp WHERE cAmt = @cAmt
  852. AND (receiverName = @RECEIVER_NAME)
  853. AND DATEDIFF(MI, dot, GETDATE()) <= 5
  854. )
  855. BEGIN
  856. SELECT @errorCode = '206', @errorMsg = 'Similar transaction found'
  857. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  858. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  859. RETURN
  860. END
  861. --<<End:Get Data Compliance for Compliance Checking and suspicious duplicate txn
  862. DECLARE @agentFxGain FLOAT
  863. SET @agentFxGain = (@tAmt * (@agentCrossSettRate - @customerRate))/@agentCrossSettRate
  864. -->>Start:OFAC/Compliance Checking
  865. DECLARE @receiverOfacRes VARCHAR(MAX),@ofacRes VARCHAR(MAX),@ofacReason VARCHAR(MAX)
  866. EXEC proc_ofacTracker @flag = 't', @name = @SENDER_NAME, @Result = @ofacRes OUTPUT
  867. EXEC proc_ofacTracker @flag = 't', @name = @RECEIVER_NAME, @Result = @receiverOfacRes OUTPUT
  868. DECLARE @result VARCHAR(MAX)
  869. DECLARE @csMasterId INT, @complianceRes VARCHAR(20), @totalRows INT, @count INT, @compFinalRes VARCHAR(20)
  870. DECLARE @csMasterRec TABLE (rowId INT IDENTITY(1,1), masterId INT)
  871. IF ISNULL(@ofacRes, '') <> ''
  872. BEGIN
  873. SET @ofacReason = 'Matched by sender name'
  874. END
  875. IF ISNULL(@receiverOfacRes, '') <> ''
  876. BEGIN
  877. SET @ofacRes = ISNULL(@ofacRes + ',' + @receiverOfacRes, '' + @receiverOfacRes)
  878. SET @ofacReason = 'Matched by receiver name'
  879. END
  880. IF ISNULL(@ofacRes, '') <> '' AND ISNULL(@receiverOfacRes, '') <> ''
  881. BEGIN
  882. SET @ofacReason = 'Matched by both sender name and receiver name'
  883. END
  884. --INSERT @csMasterRec(masterId)
  885. --SELECT masterId FROM dbo.FNAGetComplianceRuleMaster(@sBranch, @pCountryId, NULL, @pBranch, NULL, NULL, NULL)
  886. --SELECT @totalRows = COUNT(*) FROM @csMasterRec
  887. --IF EXISTS(SELECT 'X' FROM @csMasterRec)
  888. --BEGIN
  889. -- SET @count = 1
  890. -- WHILE(@count <= @totalRows)
  891. -- BEGIN
  892. -- SELECT @csMasterId = masterId FROM @csMasterRec WHERE rowId = @count
  893. -- EXEC ws_int_proc_complianceRuleDetail
  894. -- @user = @USERNAME
  895. -- ,@tranId = @tranId
  896. -- ,@tAmt = @tAmt
  897. -- ,@senId = NULL
  898. -- ,@benId = NULL
  899. -- ,@beneficiaryName = @RECEIVER_NAME
  900. -- ,@beneficiaryMobile = @RECEIVER_CONTACT_NUMBER
  901. -- ,@benAccountNo = @BANK_ACCOUNT_NUMBER
  902. -- ,@masterId = @csMasterId
  903. -- ,@paymentMethod = @deliveryMethodId
  904. -- ,@checkingFor = 'v'
  905. -- ,@agentRefId = @AGENT_TXN_REF_ID
  906. -- ,@result = @complianceRes OUTPUT
  907. -- ,@senderId = @SENDER_IDENTITY_NUMBER
  908. -- ,@senderName = @SENDER_NAME
  909. -- ,@senderMobile = @SENDER_MOBILE
  910. -- SET @compFinalRes = ISNULL(@compFinalRes, '') + ISNULL(@complianceRes, '')
  911. -- SET @count = @count + 1
  912. -- END
  913. --END
  914. --<<End:OFAC/Compliance Checking
  915. -->>Start:Control Number Generation
  916. IF @sAgent = @MTradeMalaysia
  917. BEGIN
  918. IF LEFT(@AGENT_TXN_REF_ID, 4) <> '7001' OR LEN(@AGENT_TXN_REF_ID)<> 11
  919. BEGIN
  920. SELECT @errorCode = '102', @errorMsg = 'Invalid AGENT TXN REF ID. Must start with "7001" and must have 11 digits'
  921. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  922. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  923. RETURN;
  924. END
  925. SET @controlNo = @AGENT_TXN_REF_ID
  926. IF EXISTS(SELECT 'X' FROM controlNoList WITH(NOLOCK) WHERE controlNo = @controlNo)
  927. BEGIN
  928. SELECT @errorCode = '9001', @errorMsg = 'Technical error occurred. Duplicate AGENT TXN REF ID. Please try again'
  929. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  930. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  931. RETURN
  932. END
  933. END
  934. ELSE
  935. BEGIN
  936. SET @controlNo = '788' + LEFT(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR(10)) + '00000000', 8)
  937. --SET @controlNo = '9080' + LEFT(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR(10)) + '0000000000', 7)
  938. IF EXISTS(SELECT 'X' FROM controlNoList WITH(NOLOCK) WHERE controlNo = @controlNo)
  939. BEGIN
  940. SET @controlNo = '788' + LEFT(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR(10)) + '00000000', 8)
  941. IF EXISTS(SELECT 'X' FROM controlNoList WITH(NOLOCK) WHERE controlNo = @controlNo)
  942. BEGIN
  943. SELECT @errorCode = '9001', @errorMsg = 'Technical error occurred. Please try again'
  944. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  945. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  946. RETURN
  947. END
  948. END
  949. END
  950. DECLARE @controlNoEncrypted VARCHAR(50)
  951. SET @controlNoEncrypted = dbo.FNAEncryptString(@controlNo)
  952. --<<End:Control Number Generation
  953. BEGIN
  954. BEGIN TRANSACTION
  955. -->>Start:Data Insert into remitTran, tranSenders, tranReceivers
  956. INSERT INTO remitTran
  957. (
  958. controlNo
  959. ,sCurrCostRate,sCurrHoMargin,sCurrAgentMargin,sAgentSettRate
  960. ,pCurrCostRate,pCurrHoMargin,pCurrAgentMargin
  961. ,agentCrossSettRate,customerRate,treasuryTolerance,customerPremium
  962. ,sharingValue,serviceCharge, agentFxGain, handlingFee
  963. ,sAgentComm,sAgentCommCurrency,sSuperAgentComm,sSuperAgentCommCurrency
  964. ,sCountry,sSuperAgent,sSuperAgentName,sAgent,sAgentName,sBranch,sBranchName
  965. ,pCountry,pSuperAgent,pSuperAgentName,pAgent,pAgentName,pBranch,pBranchName
  966. ,paymentMethod
  967. ,pBank,pBankName,pBankBranch,pBankBranchName,pBankType
  968. ,accountNo,externalBankCode
  969. ,collMode,collCurr,tAmt,cAmt,pAmt,payoutCurr
  970. ,tranStatus
  971. ,payStatus
  972. ,createdDate,createdDateLocal,createdBy
  973. ,approvedDate,approvedDateLocal,approvedBy
  974. ,tranType
  975. ,senderName,receiverName,controlNo2
  976. ,sourceOfFund, relWithSender, purposeOfRemit
  977. )
  978. SELECT
  979. @controlNoEncrypted
  980. ,@sCurrCostRate,@sCurrHoMargin,@sCurrAgentMargin,@sSettlementRate
  981. ,@pCurrCostRate,@pCurrHoMargin,@pCurrAgentMargin
  982. ,@agentCrossSettRate,@customerRate,@treasuryTolerance,@customerPremium
  983. ,@sharingValue,@iServiceCharge,@agentFxGain, ISNULL(@scDiscount, 0)
  984. ,@sAgentComm,@sAgentCommCurrency,0,@sAgentCommCurrency
  985. ,@sCountry,@sSuperAgent,@sSuperAgentName,@sAgent,@sAgentName,@sBranch,@sBranchName
  986. ,@RECEIVER_COUNTRY,@pSuperAgent,@pSuperAgentName,@pAgent,@pAgentName,@pBranch,@pBranchName
  987. ,@deliveryMethod
  988. ,@pBank,@pBankName,@pBankBranch,ISNULL(@pBankBranchName, @BANK_BRANCH_NAME),@pBankType
  989. ,@BANK_ACCOUNT_NUMBER,@EXTERNALCODE
  990. ,@collMode,@collCurr,@tAmt,@cAmt,@pAmt,@pCurr
  991. ,'Payment'
  992. ,'Unpaid'
  993. ,dbo.FNADateFormatTZ(GETDATE(),@USERNAME),GETDATE(),@USERNAME
  994. ,dbo.FNADateFormatTZ(GETDATE(),@USERNAME),GETDATE(),@USERNAME
  995. ,'I'
  996. ,@SENDER_NAME,@RECEIVER_NAME, dbo.FNAEncryptString(@AGENT_TXN_REF_ID)
  997. , @SOURCE_OF_FUND, @RELATIONSHIP, @PURPOSE_OF_REMITTANCE
  998. SET @tranId = SCOPE_IDENTITY()
  999. DECLARE @sFirstName VARCHAR(100),@sMiddleName VARCHAR(100),@sLastName VARCHAR(100),@sLastName2 VARCHAR(100)
  1000. DECLARE @rFirstName VARCHAR(100),@rMiddleName VARCHAR(100),@rLastName VARCHAR(100),@rLastName2 VARCHAR(100)
  1001. SELECT @sFirstName = firstName, @sMiddleName = middleName, @sLastName = lastName1, @sLastName2 = lastName2 FROM dbo.FNASplitName(@SENDER_NAME)
  1002. SELECT @rFirstName = firstName, @rMiddleName = middleName, @rLastName = lastName1, @rLastName2 = lastName2 FROM dbo.FNASplitName(@RECEIVER_NAME)
  1003. DECLARE @memberCode VARCHAR(50),@senderId INT,@sIdTypeId INT
  1004. SELECT @senderId = C.customerId FROM customers C WITH (NOLOCK) INNER JOIN customerIdentity CI WITH (NOLOCK) ON C.customerId = CI.customerId
  1005. INNER JOIN staticDataValue SV WITH (NOLOCK) ON CI.idType = SV.valueId WHERE SV.detailTitle = @SENDERS_IDENTITY_TYPE AND CI.idNumber = @SENDER_IDENTITY_NUMBER
  1006. INSERT INTO tranSenders
  1007. (
  1008. customerId
  1009. ,membershipId
  1010. ,tranId
  1011. ,firstName,middleName,lastName1,lastName2,fullName
  1012. ,country,city,address,homePhone,mobile
  1013. ,idType,idNumber
  1014. --,issuedDate,validDate,dob
  1015. ,occupation
  1016. )
  1017. SELECT
  1018. @senderId
  1019. ,@REMITTER_ID
  1020. ,@tranId
  1021. ,@sFirstName,@sMiddleName,@sLastName,@sLastName2,@SENDER_NAME
  1022. ,@sCountry,@SENDER_CITY,@SENDER_ADDRESS,@SENDER_MOBILE,@SENDER_MOBILE
  1023. ,@SENDERS_IDENTITY_TYPE,@SENDER_IDENTITY_NUMBER
  1024. --,@SENDER_ID_ISSUE_DATE,@SENDER_ID_EXPIRE_DATE,@SENDER_DATE_OF_BIRTH
  1025. ,@OCCUPATION
  1026. INSERT INTO tranReceivers
  1027. (
  1028. tranId
  1029. ,firstName,middleName,lastName1,lastName2,fullName
  1030. ,city,address,homePhone,workPhone,country
  1031. )
  1032. SELECT
  1033. @tranId
  1034. ,@rFirstName,@rMiddleName,@rLastName,@rLastName2,@RECEIVER_NAME
  1035. ,@RECEIVER_CITY,@RECEIVER_ADDRESS,@RECEIVER_CONTACT_NUMBER,@RECEIVER_CONTACT_NUMBER,@RECEIVER_COUNTRY
  1036. -->>End:Data Insert into remitTran, tranSenders, tranReceivers
  1037. INSERT INTO controlNoList(controlNo)
  1038. SELECT @controlNo
  1039. INSERT INTO thirdPartyAgentTxnIdV2(agentTxnId, agentId)
  1040. SELECT @AGENT_TXN_REF_ID, @sAgent
  1041. -->>Start:Verify Compliance
  1042. IF EXISTS(SELECT 'X' FROM remitTranComplianceTemp WITH(NOLOCK) WHERE agentRefId = @AGENT_TXN_REF_ID)
  1043. BEGIN
  1044. INSERT INTO remitTranCompliance(TranId, csDetailTranId, matchTranId)
  1045. SELECT @tranId, csDetailTranId, matchTranId FROM remitTranComplianceTemp WITH(NOLOCK) WHERE agentRefId = @AGENT_TXN_REF_ID
  1046. SET @compFinalRes = 'C'
  1047. END
  1048. IF EXISTS(
  1049. SELECT 'X'
  1050. FROM @remitTranTemp
  1051. WHERE dot BETWEEN CONVERT(VARCHAR, dbo.FNADateFormatTZ(GETDATE(), @USERNAME), 101)
  1052. AND CONVERT(VARCHAR, dbo.FNADateFormatTZ(GETDATE(), @USERNAME), 101) + ' 23:59:59' AND cAmt = @cAmt
  1053. AND (receiverName = @RECEIVER_NAME)
  1054. )
  1055. BEGIN
  1056. INSERT INTO remitTranCompliance(TranId, csDetailTranId, matchTranId, reason)
  1057. SELECT @tranId, 0, tranid, 'Suspected duplicate transaction' FROM @remitTranTemp WHERE cAmt = @cAmt AND (receiverName = @RECEIVER_NAME)
  1058. SET @compFinalRes = 'C'
  1059. END
  1060. IF(ISNULL(@compFinalRes, '') <> '' OR ISNULL(@ofacRes, '') <> '')
  1061. BEGIN
  1062. IF(@ofacRes <> '' AND ISNULL(@compFinalRes, '') = '')
  1063. BEGIN
  1064. INSERT remitTranOfac(TranId, blackListId, reason, flag)
  1065. SELECT @tranId, @ofacRes, @ofacReason, dbo.FNAGetOFAC_Flag(@ofacRes)
  1066. UPDATE remitTran SET
  1067. tranStatus = 'OFAC'
  1068. ,approvedBy = @USERNAME
  1069. ,approvedDate = dbo.FNADateFormatTZ(GETDATE(), @USERNAME)
  1070. ,approvedDateLocal = GETDATE()
  1071. WHERE controlNo = @controlNoEncrypted
  1072. END
  1073. ELSE IF(@compFinalRes <> '' AND ISNULL(@ofacRes, '') = '')
  1074. BEGIN
  1075. UPDATE remitTran SET
  1076. tranStatus = 'Compliance'
  1077. ,approvedBy = @USERNAME
  1078. ,approvedDate = dbo.FNADateFormatTZ(GETDATE(), @USERNAME)
  1079. ,approvedDateLocal = GETDATE()
  1080. WHERE controlNo = @controlNoEncrypted
  1081. END
  1082. ELSE IF(ISNULL(@compFinalRes, '') <> '' AND ISNULL(@ofacRes, '') <> '')
  1083. BEGIN
  1084. INSERT remitTranOfac(TranId, blackListId, reason, flag)
  1085. SELECT @tranId, @ofacRes, @ofacReason, dbo.FNAGetOFAC_Flag(@ofacRes)
  1086. BEGIN
  1087. UPDATE remitTran SET
  1088. tranStatus = 'OFAC/Compliance'
  1089. ,approvedBy = @USERNAME
  1090. ,approvedDate = dbo.FNADateFormatTZ(GETDATE(), @USERNAME)
  1091. ,approvedDateLocal = GETDATE()
  1092. WHERE controlNo = @controlNoEncrypted
  1093. END
  1094. END
  1095. END
  1096. --<<End:Verify Compliance
  1097. UPDATE apiRequestLog SET
  1098. errorCode = '0'
  1099. ,errorMsg = 'Transaction created successfully'
  1100. ,controlNo = @controlNo
  1101. WHERE rowId = @apiRequestId
  1102. UPDATE exRateCalcHistory SET
  1103. isExpired = 'U'
  1104. WHERE @FOREX_SESSION_ID = @FOREX_SESSION_ID
  1105. INSERT INTO PinQueueList(ICN)
  1106. SELECT @controlNoEncrypted
  1107. IF @@TRANCOUNT > 0
  1108. COMMIT TRANSACTION
  1109. SELECT
  1110. CODE = '0'
  1111. ,AGENT_TXN_REF_ID = @AGENT_TXN_REF_ID
  1112. ,MESSAGE = 'Transaction created Successfully'--CASE WHEN @AUTHORIZED_REQUIRED='Y' THEN 'Transaction need Authorization' ELSE 'Transaction saved successfully' END
  1113. ,REFNO = @controlNo
  1114. --,AGENT_TXNID = 1
  1115. ,COLLECT_AMT = cAmt
  1116. --,COLLECT_CURRENCY = collCurr
  1117. ,EXCHANGE_RATE = customerRate
  1118. ,SERVICE_CHARGE = serviceCharge
  1119. ,PAYOUTAMT = pAmt
  1120. ,PAYOUTCURRENCY = payoutCurr
  1121. --,TXN_DATE = createdDate
  1122. FROM remitTran WITH(NOLOCK)
  1123. WHERE id = @tranId
  1124. END
  1125. END TRY
  1126. BEGIN CATCH
  1127. IF @@TRANCOUNT > 0
  1128. ROLLBACK TRAN
  1129. SELECT @errorCode = '9001', @errorMsg = 'Technical Error : ' + ERROR_MESSAGE()
  1130. EXEC ws_int_proc_responseLog @flag = 's', @requestId = @apiRequestId, @errorCode = @errorCode, @errorMsg = @errorMsg
  1131. DECLARE @errorLogId BIGINT
  1132. INSERT INTO Logs (errorPage, errorMsg, errorDetails, createdBy, createdDate)
  1133. SELECT 'API SP Error','Technical Error : ' + ERROR_MESSAGE() MESSAGE,'ws_int_proc_CreateTXN', @USERNAME, GETDATE()
  1134. SET @errorLogId = SCOPE_IDENTITY()
  1135. SELECT '9001' CODE, 'Technical Error occurred, Error Log ID : ' + CAST(@errorLogId AS VARCHAR) MESSAGE, * FROM @errorTable
  1136. END CATCH
  1137. GO