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.

458 lines
19 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_complianceRuleDetail] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_complianceRuleDetail]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_complianceRuleDetail] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. /*
  12. declare @complienceMessage varchar(1000)
  13. EXEC [proc_complianceRuleDetail]
  14. @flag= 'sender-limit',@user= 'orji@gme.com',@sIdType= 'Passport',@sIdNo= 'A09506060'
  15. ,@cAmt= '900000.00',@cAmtUSD= '800',@customerId= '136509',@pCountryId= 151,@deliveryMethod= 1,@message= @complienceMessage OUTPUT
  16. select @complienceMessage
  17. */
  18. CREATE PROC [dbo].[proc_complianceRuleDetail]
  19. @flag VARCHAR(30) = 'core'
  20. ,@user VARCHAR(50)
  21. ,@pCountryId INT
  22. ,@deliveryMethod INT
  23. ,@cAmt MONEY = NULL
  24. ,@cAmtUSD INT
  25. ,@customerId VARCHAR(20)
  26. ,@receiverName VARCHAR(150) = NULL
  27. ,@sIdNo VARCHAR(50) = NULL
  28. ,@sIdType VARCHAR(50) = NULL
  29. ,@receiverMobile VARCHAR(25) = NULL
  30. ,@message VARCHAR(1000) = NULL OUTPUT
  31. ,@shortMessage VARCHAR(100) = NULL OUTPUT
  32. ,@errCode TINYINT = NULL OUTPUT
  33. ,@ruleId INT = NULL OUTPUT
  34. AS
  35. SET NOCOUNT ON
  36. /*
  37. 1> Get the data for per txn, monthly txn and yearly txn limit amount
  38. 2> Check for per txn limit, and return with proper error message if limit amount exceeded
  39. 3> Check for monthly txn limit, and return with proper error message if limit amount exceeded
  40. 4> Check for yearly txn limit, and return with proper error message if limit amount exceeded
  41. 5> Return success message and code if no issue with compliance rule
  42. */
  43. BEGIN TRY
  44. DECLARE @perTxnLimitAmt MONEY ,@limitAmt DECIMAL(10,2)
  45. ,@comRuleId INT ,@csMasterId INT
  46. ,@YearStart DATE ,@YearEnd DATE
  47. ,@MonthStart DATE ,@MonthEnd DATE
  48. ,@ruleType CHAR(1) ,@customerType VARCHAR(20)
  49. ,@txnLimitCount INT ,@txnCount INT
  50. ,@fdate DATE ,@tDate DATE = GETDATE()+1
  51. ,@period INT
  52. ,@comRuleId_ws BIGINT ,@limitAmt_ws MONEY
  53. ,@ruleType_ws CHAR(1)
  54. SELECT @YearStart = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
  55. ,@YearEnd = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)
  56. ,@MonthStart = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
  57. ,@MonthEnd = DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
  58. SELECT @customerType = customerType
  59. FROM customerMaster(NOLOCK)
  60. WHERE customerId = @customerId
  61. IF @flag = 'core'
  62. BEGIN
  63. IF OBJECT_ID('tempdb..#tempC') IS NOT NULL
  64. DROP TABLE #tempC
  65. ---- aggregate rule
  66. SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
  67. ,condition ,trancount ,amount
  68. ,period ,CD.nextAction AS nextAction ,csDetailId
  69. INTO #tempC
  70. FROM dbo.csDetail CD(NOLOCK)
  71. INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId
  72. WHERE CD.condition = 4600
  73. --AND ISNULL(CM.rCountry,'') =ISNULL(@pCountryId,ISNULL (CM.rCountry,''))
  74. --AND ISNULL(CD.paymentMode,'') = ISNULL(@deliveryMethod,ISNULL(CD.paymentMode,''))
  75. AND ISNULL(CD.isActive, 'Y') = 'Y'
  76. AND ISNULL(CD.isDeleted, 'N') = 'N'
  77. AND ISNULL(CD.isEnable, 'Y') = 'Y'
  78. AND ISNULL(CM.isActive, 'Y') = 'Y'
  79. AND ISNULL(CM.isDeleted, 'N') = 'N'
  80. order by period
  81. DECLARE @totalRowC INT
  82. DECLARE @counterC INT = 1
  83. SELECT @totalRowC = COUNT('X') FROM #tempC
  84. CREATE TABLE #tempTran(id BIGINT, tAmt MONEY, tAmtUSD MONEY, sIdType VARCHAR(50),sIdNo VARCHAR(50),approvedDate DATETIME,createdDate DATETIME,tranStatus VARCHAR(20))
  85. CREATE TABLE #tempTranR(id BIGINT, tAmt MONEY, tAmtUSD MONEY, approvedDate DATETIME, createdDate DATETIME, tranStatus VARCHAR(20),receiverName VARCHAR(15))
  86. DECLARE
  87. @sql VARCHAR(MAX)
  88. ,@sumTxnAmt MONEY
  89. ,@sumTxnAmtUSD MONEY
  90. -- Get the record of 365 days into temp table
  91. INSERT INTO #tempTran(id,tAmt,tAmtUSD,sIdType,sIdNo,approvedDate,createdDate,tranStatus)
  92. SELECT r.id,r.tAmt, r.tAmt/(r.sCurrCostRate + ISNULL(r.sCurrHoMargin, 0)) ,s.idType,s.idNumber,r.approvedDate,r.createdDate,r.tranStatus
  93. FROM vwRemitTran R(nolock)
  94. INNER JOIN vwtranSenders S(nolock) ON R.ID = S.tranId
  95. WHERE r.tranStatus <> 'Cancel'
  96. AND S.customerId = @customerId
  97. AND r.approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)
  98. WHILE(@counterC <= @totalRowC)
  99. BEGIN
  100. SELECT @limitAmt = amount
  101. ,@period = ISNULL(PERIOD,0)
  102. ,@ruleType = nextAction
  103. ,@comRuleId_ws = csDetailId
  104. ,@comRuleId = csDetailId
  105. FROM #tempC WHERE sno=@counterC
  106. SET @fdate = GETDATE()+1 - @period
  107. SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END
  108. IF @customerType = '11048' ----EASY REMIT CUSTOMER
  109. BEGIN
  110. IF(SELECT dbo.CheckCustomerPerDayAmt(@customerId,@cAmt)) = 1
  111. BEGIN
  112. SET @ruleType_ws = 'B'
  113. SET @message = 'The transaction is
  114. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType_ws = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
  115. (<b>'+CAST(@sumTxnAmt AS VARCHAR)+' KRW</b>) and by doing this transaction (<b>'+CAST(@cAmt AS VARCHAR)+' KRW</b>)
  116. <b>per day transaction</b> Limit (<b>'+CAST(@limitAmt_ws AS VARCHAR)+' KRW</b>) is exceeded.'
  117. --SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
  118. SELECT @errCode = CASE WHEN @ruleType_ws = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId_ws, @shortMessage = 'Easy remit Per day limit exceeded(c).'
  119. RETURN
  120. END
  121. END
  122. /*per txn limit*/
  123. IF @cAmtUSD > @limitAmt AND @period = 0
  124. BEGIN
  125. SET @message = 'The transaction is
  126. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because the transaction
  127. amount (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>), is exceeded as <b>per transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>).'
  128. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = 'Per txn limit exceeded(c).'
  129. RETURN
  130. END
  131. /*Per day txn limit */
  132. SELECT @sumTxnAmt = SUM(ISNULL(tAmt,0)),
  133. @sumTxnAmtUSD = SUM(ISNULL(tAmtUSD, 0))
  134. FROM #tempTran
  135. WHERE approvedDate BETWEEN @fdate AND @tDate
  136. IF (ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt
  137. BEGIN
  138. SET @message = 'The transaction is
  139. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
  140. (<b>'+CAST(@sumTxnAmtUSD AS VARCHAR)+' USD</b>) and by doing this transaction (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>)
  141. <b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.'
  142. SET @shortMessage = 'Customer ' + CASE WHEN @period = 1 THEN 'Per day ' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' limit exceeded(c).'
  143. --SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
  144. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
  145. RETURN
  146. END
  147. SET @counterC = @counterC + 1
  148. END
  149. IF ISNULL(@receiverName, '') = ''
  150. BEGIN
  151. SELECT @errCode = 0, @message = 'Success', @ruleId = 0
  152. RETURN
  153. END
  154. -- ## Start of multiple sender same Reciever txn limit
  155. INSERT INTO #tempTranR(id,tAmt, tAmtUSD,receiverName,approvedDate,createdDate,tranStatus)
  156. SELECT rt.id,tAmt, tAmt/(sCurrCostRate + ISNULL(sCurrHoMargin, 0)),tr.firstName,approvedDate,createdDate,tranStatus
  157. FROM vwRemitTran rt WITH(NOLOCK)
  158. INNER JOIN dbo.vwTranReceivers tr WITH(NOLOCK) ON tr.tranId = rt.id
  159. WHERE tr.fullName = @receiverName AND TR.mobile = @receiverMobile AND tranStatus <> 'CANCEL'
  160. AND approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)
  161. IF OBJECT_ID('tempdb..#tempCR') IS NOT NULL
  162. DROP TABLE #tempCR
  163. SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
  164. ,condition ,trancount ,amount
  165. ,period ,nextAction ,csDetailId
  166. INTO #tempCR FROM dbo.csDetail CD(NOLOCK)
  167. INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId
  168. WHERE CD.condition = 4603
  169. AND ISNULL(CD.isActive, 'Y') = 'Y'
  170. AND ISNULL(CD.isDeleted, 'N') = 'N'
  171. AND ISNULL(CD.isEnable, 'Y') = 'Y'
  172. AND ISNULL(CM.isActive, 'Y') = 'Y'
  173. AND ISNULL(CM.isDeleted, 'N') = 'N'
  174. ORDER BY period
  175. DECLARE @totalRowCR INT
  176. DECLARE @counterCR INT = 1
  177. SELECT @totalRowCR = COUNT('X') FROM #tempCR
  178. WHILE(@counterCR<= @totalRowCR)
  179. BEGIN
  180. SELECT @limitAmt = amount
  181. ,@period = ISNULL(PERIOD,0)
  182. ,@ruleType = nextAction
  183. ,@txnLimitCount = ISNULL(trancount,0)
  184. FROM #tempCR WHERE sno=@counterCR
  185. SET @fdate = GETDATE()+1 - @period
  186. SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END
  187. /*per day limit*/
  188. SELECT @sumTxnAmt = SUM(ISNULL(tAmt,0)),
  189. @sumTxnAmtUSD = SUM(ISNULL(tAmtUSD, 0)),
  190. @txnCount = COUNT('x')
  191. FROM #tempTranR
  192. WHERE approvedDate BETWEEN CAST(@fdate AS DATE) AND @tDate
  193. /*BASED ON TRANSACTION LIMIT*/
  194. IF ISNULL(@txnLimitCount,0) = 0 AND ((ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt)
  195. BEGIN
  196. SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
  197. <b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.(' + CAST((@sumTxnAmtUSD + @cAmtUSD) AS VARCHAR) + ' USD)'
  198. SET @shortMessage = 'Customer ' + CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' limit exceeded for same receiver(c).'
  199. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
  200. RETURN
  201. END
  202. /*BASED ON TRANSACTION COUNT*/
  203. IF @txnLimitCount >0 AND (ISNULL(@txnCount,0)) >= @txnLimitCount
  204. BEGIN
  205. SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
  206. <b>'''+CAST(@period AS VARCHAR)+''' day no of '''+CAST(@txnLimitCount AS VARCHAR)+''' transactions</b> Limit (<b>'+CAST(@txnCount AS VARCHAR)+' </b>) is exceeded.(' + CAST(@txnLimitCount as VARCHAr) + ' )'
  207. SET @shortMessage = CASE WHEN @period = 1 THEN 'Per ' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+'transactions limit exceeded for same receiver(c).'
  208. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
  209. RETURN
  210. END
  211. SET @counterCR = @counterCR + 1
  212. END
  213. --Return success message if there is no complaince matched txn
  214. SELECT @errCode = 0, @message = 'Success', @ruleId = 0
  215. RETURN
  216. END
  217. ELSE IF @flag = 'sender-limit'
  218. BEGIN
  219. DECLARE @returnStatus BIT = 0
  220. IF OBJECT_ID('tempdb..#temp') IS NOT NULL
  221. DROP TABLE #temp
  222. SELECT *
  223. INTO #temp
  224. FROM(
  225. SELECT condition ,trancount ,amount
  226. ,CD.period ,CD.nextAction AS nextAction ,csDetailId
  227. ,CASE WHEN DATEADD(DAY,- CD.period,@tDate) < @YearStart THEN @YearStart ELSE DATEADD(DAY,- CD.period,@tDate) END fdate
  228. FROM dbo.csDetail CD(NOLOCK)
  229. INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId
  230. AND CD.condition = 4600
  231. AND ISNULL(CD.isActive, 'Y') = 'Y'
  232. AND ISNULL(CD.isDeleted, 'N') = 'N'
  233. AND ISNULL(CD.isEnable, 'Y') = 'Y'
  234. AND ISNULL(CM.isActive, 'Y') = 'Y'
  235. AND ISNULL(CM.isDeleted, 'N') = 'N'
  236. )tb
  237. OUTER APPLY
  238. (SELECT SUM(R.tAmt) sumTxnAmt ,SUM(r.tAmt/(r.sCurrCostRate + ISNULL(r.sCurrHoMargin, 0)))sumTxnAmtUSD
  239. FROM vwRemitTran R(nolock)
  240. INNER JOIN vwtranSenders S(nolock) ON R.ID = S.tranId
  241. AND r.tranStatus <> 'Cancel'
  242. AND S.customerId = @customerId
  243. AND r.approvedDate BETWEEN tb.fdate AND @tDate)s
  244. IF @customerType = '11048' AND dbo.CheckCustomerPerDayAmt(@customerId,@cAmt) = 1
  245. BEGIN
  246. SELECT TOP 1 @limitAmt = amount
  247. ,@period = ISNULL(PERIOD,0)
  248. ,@ruleType = nextAction
  249. ,@comRuleId_ws = csDetailId
  250. ,@comRuleId = csDetailId
  251. FROM #temp t
  252. ORDER BY period
  253. SET @ruleType_ws = 'B'
  254. SET @message = 'The transaction is
  255. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType_ws = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
  256. (<b>'+CAST(@sumTxnAmt AS VARCHAR)+' KRW</b>) and by doing this transaction (<b>'+CAST(@cAmt AS VARCHAR)+' KRW</b>)
  257. <b>per day transaction</b> Limit (<b>'+CAST(@limitAmt_ws AS VARCHAR)+' KRW</b>) is exceeded.'
  258. --SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
  259. SELECT @errCode = 1 , @message = @message, @ruleId = @comRuleId_ws, @shortMessage = 'Easy remit Per day limit exceeded(c).'
  260. RETURN
  261. END
  262. --select * from #temp
  263. --IF EXISTS(SELECT TOP 1 1 FROM #temp T WHERE @cAmtUSD > T.amount AND T.period = 0)
  264. --BEGIN
  265. SELECT TOP 1 @limitAmt = amount
  266. ,@period = ISNULL(PERIOD,0)
  267. ,@ruleType = nextAction
  268. ,@comRuleId_ws = csDetailId
  269. ,@comRuleId = csDetailId
  270. ,@returnStatus = 1
  271. FROM #temp t
  272. WHERE @cAmtUSD > T.amount AND T.period = 0
  273. SET @message = 'The transaction is
  274. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because the transaction
  275. amount (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>), is exceeded as <b>per transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>).'
  276. IF @returnStatus = 1
  277. BEGIN
  278. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = 'Per txn limit exceeded(c).'
  279. RETURN
  280. END
  281. SET @returnStatus = 0
  282. --IF EXISTS(SELECT TOP 1 1 FROM #temp T WHERE ISNULL(T.sumTxnAmt,0) + @cAmtUSD > T.amount AND T.period <> 0)
  283. --BEGIN
  284. SELECT TOP 1 @limitAmt = amount
  285. ,@period = ISNULL(PERIOD,0)
  286. ,@ruleType = nextAction
  287. ,@comRuleId_ws = csDetailId
  288. ,@comRuleId = csDetailId
  289. ,@sumTxnAmtUSD = ISNULL(T.sumTxnAmtUSD,0)
  290. ,@returnStatus = 1
  291. FROM #temp t
  292. WHERE ISNULL(T.sumTxnAmtUSD,0) + @cAmtUSD > T.amount AND t.period <> 0
  293. ORDER BY t.period
  294. SET @message = 'The transaction is
  295. <b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
  296. (<b>'+CAST(@sumTxnAmtUSD AS VARCHAR)+' USD</b>) and by doing this transaction (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>)
  297. <b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.'
  298. IF @returnStatus = 1
  299. BEGIN
  300. --SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
  301. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = ''''+CAST(@period AS VARCHAR)+''' day limit exceeded(c).'
  302. RETURN
  303. END
  304. SELECT @errCode = 0, @message = 'Success', @ruleId = 0
  305. RETURN
  306. END
  307. ELSE IF @flag = 'receiver-limit'
  308. BEGIN
  309. CREATE TABLE #tempTransactionR(id BIGINT, tAmt MONEY, tAmtUSD MONEY, approvedDate DATETIME, createdDate DATETIME, tranStatus VARCHAR(20),receiverName VARCHAR(50))
  310. IF OBJECT_ID('tempdb..#tempR') IS NOT NULL
  311. DROP TABLE #tempR
  312. -- ## Start of multiple sender same Reciever txn limit
  313. INSERT INTO #tempTransactionR(id,tAmt, tAmtUSD,receiverName,approvedDate,createdDate,tranStatus)
  314. SELECT rt.id,tAmt, tAmt/(sCurrCostRate + ISNULL(sCurrHoMargin, 0)),tr.firstName,approvedDate,createdDate,tranStatus
  315. FROM vwRemitTran rt WITH(NOLOCK)
  316. INNER JOIN dbo.vwTranReceivers tr WITH(NOLOCK) ON tr.tranId = rt.id
  317. WHERE tr.fullName = @receiverName AND TR.mobile = @receiverMobile AND rt.tranStatus <> 'CANCEL'
  318. AND approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)
  319. SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
  320. ,condition ,trancount ,amount
  321. ,period ,nextAction ,csDetailId
  322. INTO #tempR FROM dbo.csDetail CD(NOLOCK)
  323. INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId
  324. WHERE condition = 4603
  325. --AND ISNULL(CM.rCountry,'') =ISNULL(@pCountryId,ISNULL (CM.rCountry,''))
  326. --AND ISNULL(CD.paymentMode,'') = ISNULL(@deliveryMethod,ISNULL(CD.paymentMode,''))
  327. AND ISNULL(CD.isActive,'Y')='Y'
  328. AND ISNULL(CD.isDeleted, 'N') = 'N'
  329. AND ISNULL(CD.isEnable, 'Y') = 'Y'
  330. AND ISNULL(CM.isActive, 'Y') = 'Y'
  331. AND ISNULL(CM.isDeleted, 'N') = 'N'
  332. ORDER BY period
  333. DECLARE @totalRowR INT
  334. DECLARE @counterR INT = 1
  335. SELECT @totalRowR = COUNT('X') FROM #tempR
  336. WHILE(@counterR<=@totalRowR)
  337. BEGIN
  338. SELECT @limitAmt = amount
  339. ,@period = ISNULL(PERIOD,0)
  340. ,@ruleType = nextAction
  341. ,@txnLimitCount = ISNULL(trancount,0)
  342. ,@comRuleId = csDetailId
  343. FROM #tempR WHERE sno = @counterR
  344. SET @fdate = GETDATE() + 1 - @period
  345. SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END
  346. /*per day limit*/
  347. SELECT @sumTxnAmt = SUM(ISNULL(tAmt,0)),
  348. @sumTxnAmtUSD = SUM(ISNULL(tAmtUSD, 0)),
  349. @txnCount = COUNT('x')
  350. FROM #tempTransactionR
  351. WHERE approvedDate BETWEEN @fdate AND @tDate
  352. IF ISNULL(@txnLimitCount,0) = 0 AND ((ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt)
  353. BEGIN
  354. SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
  355. <b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.(' + CAST((@sumTxnAmtUSD + @cAmtUSD) AS VARCHAR) + ' USD)'
  356. SET @shortMessage = CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+'limit exceeded for same receiver(r).'
  357. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
  358. RETURN
  359. END
  360. IF @txnLimitCount >0 AND (ISNULL(@txnCount,0)) >= @txnLimitCount
  361. BEGIN
  362. SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
  363. <b>'''+CAST(@period AS VARCHAR)+''' day no of transactions</b> Limit (<b>'+CAST(@txnCount AS VARCHAR)+' </b>) is exceeded.(' + CAST(@txnLimitCount as VARCHAr) + ' )'
  364. SET @shortMessage = CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' no of transactions limit exceeded for same receiver(r).'
  365. SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
  366. RETURN
  367. END
  368. SET @counterR = @counterR + 1
  369. END
  370. --Return success message if there is no complaince matched txn
  371. SELECT @errCode = 0, @message = 'Success', @ruleId = 0
  372. RETURN
  373. END
  374. END TRY
  375. BEGIN CATCH
  376. SET @message = ERROR_MESSAGE()
  377. SELECT @errCode = 1, @message = @message, @ruleId = ERROR_LINE()
  378. END CATCH