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.

509 lines
15 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_topUpLimit] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_topUpLimit]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_topUpLimit] 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_topUpLimit]
  12. @flag VARCHAR(50) = NULL
  13. ,@user VARCHAR(30) = NULL
  14. ,@tulId VARCHAR(30) = NULL
  15. ,@userId INT = NULL
  16. ,@currency INT = NULL
  17. ,@limitPerDay MONEY = NULL
  18. ,@perTopUpLimit MONEY = NULL
  19. ,@maxCreditLimitForAgent MONEY = NULL
  20. ,@userName VARCHAR(50) = NULL
  21. ,@sortBy VARCHAR(50) = NULL
  22. ,@sortOrder VARCHAR(5) = NULL
  23. ,@pageSize INT = NULL
  24. ,@pageNumber INT = NULL
  25. ,@haschanged VARCHAR(20) = NULL
  26. ,@hasLimit VARCHAR(20) = NULL
  27. AS
  28. SET NOCOUNT ON
  29. SET XACT_ABORT ON
  30. BEGIN TRY
  31. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  32. DECLARE
  33. @sql VARCHAR(MAX)
  34. ,@oldValue VARCHAR(MAX)
  35. ,@newValue VARCHAR(MAX)
  36. ,@module VARCHAR(10)
  37. ,@tableAlias VARCHAR(100)
  38. ,@logIdentifier VARCHAR(50)
  39. ,@logParamMod VARCHAR(100)
  40. ,@logParamMain VARCHAR(100)
  41. ,@table VARCHAR(MAX)
  42. ,@select_field_list VARCHAR(MAX)
  43. ,@extra_field_list VARCHAR(MAX)
  44. ,@sql_filter VARCHAR(MAX)
  45. ,@functionId INT
  46. ,@id VARCHAR(10)
  47. ,@modType VARCHAR(6)
  48. ,@ApprovedFunctionId INT
  49. SELECT
  50. @functionId = 20181300
  51. ,@logIdentifier = 'tulId'
  52. ,@logParamMain = 'topUpLimit'
  53. ,@logParamMod = 'topUpLimitMod'
  54. ,@module = '20'
  55. ,@tableAlias = 'User Top-Up Limit'
  56. ,@ApprovedFunctionId = 20181330
  57. DECLARE @maxLimit MONEY = 0
  58. ,@agentCountry INT
  59. IF @flag = 'cla' --Credit Limit Authority
  60. BEGIN
  61. SELECT @userId = userId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user AND ISNULL(isDeleted, 'N') = 'N'
  62. SELECT
  63. limitPerDay = ISNULL(limitPerDay, 0)
  64. ,perTopUpLimit = ISNULL(perTopUpLimit, 0)
  65. ,currency = cm.currencyCode
  66. FROM topUpLimit tul WITH(NOLOCK)
  67. INNER JOIN currencyMaster cm WITH(NOLOCK) ON tul.currency = cm.currencyId
  68. WHERE userId = @userId
  69. AND tul.currency = 1
  70. AND ISNULL(tul.isDeleted, 'N') = 'N'
  71. SELECT
  72. limitPerDay = ISNULL(limitPerDay, 0)
  73. ,perTopUpLimit = ISNULL(perTopUpLimit, 0)
  74. ,currency = cm.currencyCode
  75. FROM topUpLimitInt tul WITH(NOLOCK)
  76. INNER JOIN currencyMaster cm WITH(NOLOCK) ON tul.currency = cm.currencyId
  77. WHERE userId = @userId
  78. AND tul.currency = 2
  79. AND ISNULL(tul.isDeleted, 'N') = 'N'
  80. END
  81. IF @flag IN ('s')
  82. BEGIN
  83. SET @table = '(
  84. SELECT
  85. tulId = ISNULL(mode.tulId, main.tulId)
  86. ,userId = ISNULL(mode.userId,main.userId)
  87. ,limitPerDay = ISNULL(mode.limitPerDay,main.limitPerDay)
  88. ,currency = ISNULL(mode.currency,main.currency)
  89. ,perTopUpLimit = ISNULL(mode.perTopUpLimit,main.perTopUpLimit)
  90. ,maxCreditLimitForAgent = ISNULL(mode.maxCreditLimitForAgent,main.maxCreditLimitForAgent)
  91. ,main.createdBy
  92. ,main.createdDate
  93. ,modifiedDate = CASE WHEN main.approvedBy IS NULL THEN main.createdDate ELSE mode.createdDate END
  94. ,modifiedBy = CASE WHEN main.approvedBy IS NULL THEN main.createdBy ELSE mode.createdBy END
  95. ,hasChanged = CASE WHEN (main.approvedBy IS NULL) OR
  96. (mode.tulId IS NOT NULL)
  97. THEN ''Y'' ELSE ''N'' END
  98. ,hasLimit = CASE WHEN main.userId IS not NULL
  99. THEN ''Y'' ELSE ''N'' END
  100. FROM topUpLimit main WITH(NOLOCK)
  101. LEFT JOIN topUpLimitMod mode ON main.tulId = mode.tulId
  102. AND (
  103. mode.createdBy = ''' + @user + '''
  104. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  105. )
  106. WHERE ISNULL(main.isDeleted, ''N'') <> ''Y''
  107. AND (
  108. main.approvedBy IS NOT NULL
  109. OR main.createdBy = ''' + @user + '''
  110. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  111. )
  112. --AND NOT(ISNULL(mode.modType, '''') = ''D'' AND mode.createdBy = ''' + @user + ''')
  113. ) '
  114. END
  115. IF @flag = 'i'
  116. BEGIN
  117. IF(@perTopUpLimit > @limitPerDay)
  118. BEGIN
  119. EXEC proc_errorHandler 1, 'Per Top Up Limit exceeded the Total Limit Amount Per day', @id
  120. RETURN
  121. END
  122. IF EXISTS(SELECT 'X' FROM topUpLimit WHERE userId = @userId AND currency = @currency AND ISNULL(isDeleted, 'N') = 'N')
  123. BEGIN
  124. EXEC proc_errorHandler 1, 'Record already exists', NULL
  125. RETURN
  126. END
  127. BEGIN TRANSACTION
  128. --alter table topuplimit add maxCreditLimitForAgent money
  129. --alter table topUpLimitMod add maxCreditLimitForAgent money
  130. INSERT INTO topUpLimit (
  131. userId
  132. ,limitPerDay
  133. ,currency
  134. ,perTopUpLimit
  135. ,createdBy
  136. ,createdDate
  137. ,maxCreditLimitForAgent
  138. )
  139. SELECT
  140. @userId
  141. ,@limitPerDay
  142. ,@currency
  143. ,@perTopUpLimit
  144. ,@user
  145. ,GETDATE()
  146. ,@maxCreditLimitForAgent
  147. SET @id = SCOPE_IDENTITY()
  148. COMMIT TRANSACTION
  149. EXEC proc_errorHandler 0, 'Record has been added successfully.', @tulId
  150. END
  151. ELSE IF @flag = 'a'
  152. BEGIN
  153. IF EXISTS (SELECT 'X' FROM topUpLimitMod WITH(NOLOCK) WHERE tulId = @tulId AND createdBy = @user)
  154. BEGIN
  155. SELECT
  156. *
  157. FROM topUpLimitMod WITH(NOLOCK) WHERE tulId= @tulId
  158. END
  159. ELSE
  160. BEGIN
  161. SELECT
  162. *
  163. FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId
  164. END
  165. END
  166. ELSE IF @flag = 'u'
  167. BEGIN
  168. IF EXISTS (SELECT 'X' FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId AND ( createdBy <> @user AND approvedBy IS NULL))
  169. BEGIN
  170. EXEC proc_errorHandler 1, '<center>You can not modify this record. <br /> You are trying to perform an illegal operation.</center>', @tulId
  171. RETURN
  172. END
  173. IF EXISTS (SELECT 'X' FROM topUpLimitMod WITH(NOLOCK) WHERE tulId = @tulId AND createdBy<> @user)
  174. BEGIN
  175. EXEC proc_errorHandler 1, '<center>You can not modify this record. <br /> You are trying to perform an illegal operation.</center>', @tulId
  176. RETURN
  177. END
  178. IF(@perTopUpLimit > @limitPerDay)
  179. BEGIN
  180. EXEC proc_errorHandler 1, 'Per Top Up Limit exceeded the Total Limit Amount Per day', @id
  181. RETURN
  182. END
  183. BEGIN TRANSACTION
  184. IF EXISTS (SELECT 'X' FROM topUpLimit WHERE approvedBy IS NULL AND tulId = @tulId AND createdBy = @user)
  185. BEGIN
  186. UPDATE topUpLimit SET
  187. userId = @userId
  188. ,limitPerDay = @limitPerDay
  189. ,currency = @currency
  190. ,perTopUpLimit = @perTopUpLimit
  191. ,maxCreditLimitForAgent = @maxCreditLimitForAgent
  192. WHERE tulId = @tulId
  193. END
  194. ELSE
  195. BEGIN
  196. DELETE FROM topUpLimitMod WHERE tulId = @tulId
  197. INSERT INTO topUpLimitMod(
  198. tulId
  199. ,userId
  200. ,limitPerDay
  201. ,currency
  202. ,perTopUpLimit
  203. ,createdBy
  204. ,createdDate
  205. ,modType
  206. ,maxCreditLimitForAgent
  207. )
  208. SELECT
  209. @tulId
  210. ,@userId
  211. ,@limitPerDay
  212. ,@currency
  213. ,@perTopUpLimit
  214. ,@user
  215. ,GETDATE()
  216. ,'U'
  217. ,@maxCreditLimitForAgent
  218. SET @modType = 'update'
  219. END
  220. COMMIT TRANSACTION
  221. EXEC proc_errorHandler 0, 'Record updated successfully.', @tulId
  222. END
  223. ELSE IF @flag = 'd'
  224. BEGIN
  225. IF EXISTS (SELECT 'X' FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId AND (createdBy <> @user AND approvedBy IS NULL))
  226. BEGIN
  227. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @tulId
  228. RETURN
  229. END
  230. IF EXISTS (SELECT 'X' FROM topUpLimitMod WITH(NOLOCK) WHERE tulId = @tulId and createdBy <> @user)
  231. BEGIN
  232. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @tulId
  233. RETURN
  234. END
  235. BEGIN TRANSACTION
  236. IF EXISTS (SELECT 'X' FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId AND approvedBy IS NULL AND createdBy = @user)
  237. BEGIN
  238. DELETE FROM topUpLimit WHERE tulId = @tulId
  239. END
  240. ELSE
  241. BEGIN
  242. INSERT INTO topUpLimitMod(
  243. tulId
  244. ,userId
  245. ,limitPerDay
  246. ,currency
  247. ,perTopUpLimit
  248. ,createdBy
  249. ,createdDate
  250. ,modType
  251. ,maxCreditLimitForAgent
  252. )
  253. SELECT
  254. @tulId
  255. ,@userId
  256. ,@limitPerDay
  257. ,@currency
  258. ,@perTopUpLimit
  259. ,@user
  260. ,GETDATE()
  261. ,'D'
  262. ,@maxCreditLimitForAgent
  263. SET @modType = 'delete'
  264. END
  265. COMMIT TRANSACTION
  266. EXEC proc_errorHandler 0, 'Record deleted successfully', @tulId
  267. END
  268. ELSE IF @flag = 's'
  269. BEGIN
  270. IF @sortBy IS NULL
  271. SET @sortBy = 'tulId'
  272. IF @sortOrder IS NULL
  273. SET @sortOrder = 'ASC'
  274. SET @table = '(
  275. SELECT
  276. main.tulId
  277. ,au.userId
  278. ,name = ISNULL(au.firstName, '''') + ISNULL( '' '' + au.middleName, '''')+ ISNULL( '' '' + au.lastName, '''')
  279. ,au.userName
  280. ,limitPerDay = ISNULL(CAST(main.limitPerDay AS VARCHAR), ''N/A'')
  281. ,main.currency
  282. ,currencyName = ISNULL(curr.currencyCode, ''N/A'')
  283. ,perTopUpLimit = ISNULL(CAST(main.perTopUpLimit AS VARCHAR), ''N/A'')
  284. ,maxCreditLimitForAgent
  285. ,main.haschanged
  286. ,main.createdBy
  287. ,main.createdDate
  288. ,main.modifiedBy
  289. ,hasLimit = isnull(main.hasLimit,''N'')
  290. FROM applicationUsers au
  291. LEFT JOIN ' + @table + ' main ON au.userId = main.userId
  292. LEFT JOIN currencyMaster curr WITH(NOLOCK) ON main.currency = curr.currencyId
  293. WHERE au.agentId = dbo.FNAGetHOAgentId() and isnull(au.isActive,''Y'') = ''Y'' and isnull(au.isDeleted,''N'') = ''N''
  294. ) x'
  295. SET @sql_filter = ''
  296. PRINT (@table)
  297. IF @userName IS NOT NULL
  298. SET @sql_filter = @sql_filter + ' AND ISNULL(userName, '''') LIKE ''%' + @userName + '%'''
  299. IF @userId IS NOT NULL
  300. SET @sql_filter = @sql_filter + ' AND ISNULL(userId, '''') = ''' + CAST(@userId AS VARCHAR) + ''''
  301. IF @haschanged IS NOT NULL
  302. SET @sql_filter = @sql_filter + ' AND haschanged = ''' + CAST(@haschanged AS VARCHAR) + ''''
  303. IF @hasLimit IS NOT NULL
  304. SET @sql_filter = @sql_filter + ' AND hasLimit = ''' + CAST(@hasLimit AS VARCHAR) + ''''
  305. SET @select_field_list ='
  306. tulId
  307. ,userId
  308. ,name
  309. ,userName
  310. ,limitPerDay
  311. ,currency
  312. ,currencyName
  313. ,perTopUpLimit
  314. ,maxCreditLimitForAgent
  315. ,haschanged
  316. ,createdBy
  317. ,createdDate
  318. ,modifiedBy
  319. ,hasLimit
  320. '
  321. EXEC dbo.proc_paging
  322. @table
  323. ,@sql_filter
  324. ,@select_field_list
  325. ,@extra_field_list
  326. ,@sortBy
  327. ,@sortOrder
  328. ,@pageSize
  329. ,@pageNumber
  330. END
  331. ELSE IF @flag = 'reject'
  332. BEGIN
  333. IF NOT EXISTS (SELECT 'X' FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId and approvedBy IS NULL)
  334. AND
  335. NOT EXISTS (SELECT 'X' FROM topUpLimitMod WITH(NOLOCK) WHERE tulId = @tulId)
  336. BEGIN
  337. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @tulId
  338. RETURN
  339. END
  340. IF EXISTS (SELECT 'X' FROM topUpLimit WHERE approvedBy IS NULL AND tulId = @tulId)
  341. BEGIN --New record
  342. BEGIN TRANSACTION
  343. SET @modType = 'Reject'
  344. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @tulId, @oldValue OUTPUT
  345. INSERT INTO #msg(errorCode, msg, id)
  346. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @tulId, @user, @oldValue, @newValue
  347. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  348. BEGIN
  349. IF @@TRANCOUNT > 0
  350. ROLLBACK TRANSACTION
  351. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @tulId
  352. RETURN
  353. END
  354. DELETE FROM topUpLimit WHERE tulId= @tulId
  355. IF @@TRANCOUNT > 0
  356. COMMIT TRANSACTION
  357. END
  358. ELSE
  359. BEGIN
  360. BEGIN TRANSACTION
  361. SET @modType = 'Reject'
  362. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @tulId, @oldValue OUTPUT
  363. INSERT INTO #msg(errorCode, msg, id)
  364. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @tulId, @user, @oldValue, @newValue
  365. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  366. BEGIN
  367. IF @@TRANCOUNT > 0
  368. ROLLBACK TRANSACTION
  369. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @tulId
  370. RETURN
  371. END
  372. DELETE FROM topUpLimitMod WHERE tulId = @tulId
  373. IF @@TRANCOUNT > 0
  374. COMMIT TRANSACTION
  375. END
  376. EXEC proc_errorHandler 0, 'Changes rejected successfully.', @tulId
  377. END
  378. ELSE IF @flag = 'approve'
  379. BEGIN
  380. IF NOT EXISTS (SELECT 'X' FROM topUpLimit WITH(NOLOCK) WHERE tulId = @tulId AND approvedBy IS NULL)
  381. AND
  382. NOT EXISTS (SELECT 'X' FROM topUpLimitMod WITH(NOLOCK) WHERE tulId = @tulId)
  383. BEGIN
  384. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @tulId
  385. RETURN
  386. END
  387. BEGIN TRANSACTION
  388. IF EXISTS (SELECT 'X' FROM topUpLimit WHERE approvedBy IS NULL AND tulId = @tulId )
  389. SET @modType = 'I'
  390. ELSE
  391. SELECT @modType = modType FROM topUpLimitMod WHERE tulId = @tulId
  392. IF @modType = 'I'
  393. BEGIN --New record
  394. UPDATE topUpLimit SET
  395. isActive = 'Y'
  396. ,approvedBy = @user
  397. ,approvedDate= GETDATE()
  398. WHERE tulId = @tulId
  399. UPDATE au SET
  400. au.balance = main.limitPerDay
  401. FROM applicationUsers au
  402. INNER JOIN topUpLimit main ON au.userId = main.userId
  403. WHERE main.tulId = @tulId
  404. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @tulId, @newValue OUTPUT
  405. END
  406. ELSE IF @modType = 'U'
  407. BEGIN
  408. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @tulId, @oldValue OUTPUT
  409. UPDATE main SET
  410. main.userId = mode.userId
  411. ,main.limitPerDay = mode.limitPerDay
  412. ,main.currency = mode.currency
  413. ,main.perTopUpLimit = mode.perTopUpLimit
  414. ,main.modifiedDate = GETDATE()
  415. ,main.modifiedBy = @user
  416. ,main.maxCreditLimitForAgent = mode.maxCreditLimitForAgent
  417. FROM topUpLimit main
  418. INNER JOIN topUpLimitMod mode ON mode.tulId = main.tulId
  419. WHERE mode.tulId = @tulId
  420. UPDATE au SET
  421. au.balance = mode.limitPerDay
  422. FROM applicationUsers au
  423. INNER JOIN topUpLimitMod mode ON au.userId = mode.userId
  424. WHERE mode.tulId = @tulId
  425. EXEC [dbo].proc_GetColumnToRow 'topUpLimit', 'tulId', @tulId, @newValue OUTPUT
  426. END
  427. ELSE IF @modType = 'D'
  428. BEGIN
  429. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @tulId, @oldValue OUTPUT
  430. UPDATE topUpLimit SET
  431. isDeleted = 'Y'
  432. ,isActive = 'N'
  433. ,modifiedDate = GETDATE()
  434. ,modifiedBy = @user
  435. WHERE tulId = @tulId
  436. UPDATE au SET
  437. au.balance = NULL
  438. FROM applicationUsers au
  439. INNER JOIN topUpLimit main ON au.userId = main.userId
  440. WHERE main.tulId = @tulId
  441. END
  442. DELETE FROM topUpLimitMod WHERE tulId = @tulId
  443. INSERT INTO #msg(errorCode, msg, id)
  444. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @tulId, @user, @oldValue, @newValue
  445. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  446. BEGIN
  447. IF @@TRANCOUNT > 0
  448. ROLLBACK TRANSACTION
  449. EXEC proc_errorHandler 1, 'Could not approve the changes.', @tulId
  450. RETURN
  451. END
  452. IF @@TRANCOUNT > 0
  453. COMMIT TRANSACTION
  454. EXEC proc_errorHandler 0, 'Changes approved successfully.', @tulId
  455. END
  456. END TRY
  457. BEGIN CATCH
  458. IF @@TRANCOUNT > 0
  459. ROLLBACK TRANSACTION
  460. DECLARE @errorMessage VARCHAR(MAX)
  461. SET @errorMessage = ERROR_MESSAGE()
  462. EXEC proc_errorHandler 1, @errorMessage, @tulId
  463. END CATCH
  464. GO