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.

874 lines
26 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_creditLimitInt] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_creditLimitInt]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_creditLimitInt] 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_creditLimitInt]
  12. @flag VARCHAR(50) = NULL
  13. ,@user VARCHAR(30) = NULL
  14. ,@crLimitId VARCHAR(30) = NULL
  15. ,@agentId INT = NULL
  16. ,@parentAgentId INT = NULL
  17. ,@currency VARCHAR(3) = NULL
  18. ,@limitAmt MONEY = NULL
  19. ,@perTopUpAmt MONEY = NULL
  20. ,@maxLimitAmt MONEY = NULL
  21. ,@expiryDate DATETIME = NULL
  22. ,@todaysAddedMaxLimit MONEY = NULL
  23. ,@agentName VARCHAR(100) = NULL
  24. ,@agentCountry VARCHAR(100) = NULL
  25. ,@agentDistrict VARCHAR(100) = NULL
  26. ,@agentLocation VARCHAR(100) = NULL
  27. ,@createdBy VARCHAR(50) = NULL
  28. ,@approvedBy VARCHAR(50) = NULL
  29. ,@currencyCode VARCHAR(50) = NULL
  30. ,@haschanged CHAR(1) = NULL
  31. ,@sortBy VARCHAR(50) = NULL
  32. ,@sortOrder VARCHAR(5) = NULL
  33. ,@pageSize INT = NULL
  34. ,@pageNumber INT = NULL
  35. AS
  36. SET NOCOUNT ON
  37. SET XACT_ABORT ON
  38. BEGIN TRY
  39. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  40. DECLARE
  41. @sql VARCHAR(MAX)
  42. ,@oldValue VARCHAR(MAX)
  43. ,@newValue VARCHAR(MAX)
  44. ,@module VARCHAR(10)
  45. ,@tableAlias VARCHAR(100)
  46. ,@logIdentifier VARCHAR(50)
  47. ,@logParamMod VARCHAR(100)
  48. ,@logParamMain VARCHAR(100)
  49. ,@table VARCHAR(MAX)
  50. ,@select_field_list VARCHAR(MAX)
  51. ,@extra_field_list VARCHAR(MAX)
  52. ,@sql_filter VARCHAR(MAX)
  53. ,@id VARCHAR(10)
  54. ,@modType VARCHAR(6)
  55. ,@ApprovedFunctionId INT
  56. SELECT
  57. @ApprovedFunctionId =30011020
  58. ,@logIdentifier = 'crLimitId'
  59. ,@logParamMain = 'creditLimitInt'
  60. ,@logParamMod = 'creditLimitIntHistory'
  61. ,@module = '20'
  62. ,@tableAlias = 'Credit Limit'
  63. DECLARE
  64. @gl_code INT = 1
  65. ,@acctNum VARCHAR(30)
  66. ,@acBalCurrId INT
  67. ,@acBalCurr VARCHAR(3)
  68. ,@todaysSentCount INT
  69. ,@todaysSentAmount MONEY
  70. ,@todaysPaidCount INT
  71. ,@todaysPaidAmount MONEY
  72. ,@todaysCancelledCount INT
  73. ,@todaysCancelledAmount MONEY
  74. CREATE TABLE #agentId(agentId INT)
  75. IF @flag = 'detail'
  76. BEGIN
  77. DECLARE @todaysDate VARCHAR(50)
  78. SET @todaysDate = CONVERT(VARCHAR, GETDATE(), 101)
  79. --SELECT @acctNum = acct_num, @acBalCurrId = ac_currency FROM ac_master WHERE gl_code = @gl_code AND agent_id = @agentId
  80. SELECT @acBalCurr = currencyCode FROM currencyMaster WITH(NOLOCK) WHERE currencyId = @acBalCurrId
  81. SELECT @todaysSentCount = COUNT(id), @todaysSentAmount = SUM(tAmt) FROM remitTran WITH(NOLOCK) WHERE (sBranch = @agentId OR sAgent = @agentId) AND approvedDate BETWEEN @todaysDate AND @todaysDate + ' 23:59:59'
  82. SELECT @todaysPaidCount = COUNT(id), @todaysPaidAmount = SUM(pAmt) FROM remitTran WITH(NOLOCK) WHERE (pBranch = @agentId OR pAgent = @agentId) AND paidDate BETWEEN @todaysDate AND @todaysDate + ' 23:59:59'
  83. SELECT @todaysCancelledCount = COUNT(id), @todaysCancelledAmount = SUM(tAmt) FROM remitTran WITH(NOLOCK) WHERE (sBranch = @agentId OR sAgent = @agentId) AND cancelApprovedDate BETWEEN @todaysDate AND @todaysDate + ' 23:59:59'
  84. SELECT
  85. currentBalance = ISNULL(dbo.FNAGetACBal(@acctNum), 0)
  86. ,currentAvailable = ISNULL(dbo.FNAGetLimitBal(@agentId), 0)
  87. ,acBalCurr = @acBalCurr
  88. ,todaysSentCount = ISNULL(@todaysSentCount, 0)
  89. ,todaysSentAmount = ISNULL(@todaysSentAmount, 0)
  90. ,sentAmountCurr = 'NPR'
  91. ,todaysPaidCount = ISNULL(@todaysPaidCount, 0)
  92. ,todaysPaidAmount = ISNULL(@todaysPaidAmount, 0)
  93. ,paidAmountCurr = 'NPR'
  94. ,todaysCancelledCount = ISNULL(@todaysCancelledCount, 0)
  95. ,todaysCancelledAmount = ISNULL(@todaysCancelledAmount, 0)
  96. ,cancelledAmountCurr = 'NPR'
  97. END
  98. ELSE IF @flag = 'i'
  99. BEGIN
  100. IF (@limitAmt > @maxLimitAmt)
  101. BEGIN
  102. EXEC proc_errorHandler 1, 'Limit Amount defined greater than Max Limit Amt', @crLimitId
  103. RETURN
  104. END
  105. IF NOT EXISTS(SELECT 'X' FROM agentMaster WHERE agentId = @agentId AND ISNULL(isActive, 'N') = 'Y')
  106. BEGIN
  107. EXEC proc_errorHandler 1, 'Agent is not active', @agentId
  108. RETURN
  109. END
  110. IF EXISTS(SELECT 'A' FROM dbo.creditLimitInt (NOLOCK ) WHERE agentId = @agentId)
  111. BEGIN
  112. SELECT '1','Limit has already been setup for this agent',NULL
  113. RETURN
  114. END
  115. BEGIN TRANSACTION
  116. INSERT INTO creditLimitInt (
  117. agentId
  118. ,currency
  119. ,limitAmt
  120. ,perTopUpAmt
  121. ,maxLimitAmt
  122. ,expiryDate
  123. ,todaysAddedMaxLimit
  124. ,createdBy
  125. ,createdDate
  126. )
  127. SELECT
  128. @agentId
  129. ,@currency
  130. ,@limitAmt
  131. ,@perTopUpAmt
  132. ,@maxLimitAmt
  133. ,@expiryDate
  134. ,@todaysAddedMaxLimit
  135. ,@user
  136. ,GETDATE()
  137. SET @crLimitId = SCOPE_IDENTITY()
  138. INSERT INTO creditLimitIntHistory(
  139. crLimitId
  140. ,agentId
  141. ,currency
  142. ,limitAmt
  143. ,perTopUpAmt
  144. ,maxLimitAmt
  145. ,expiryDate
  146. ,createdBy
  147. ,createdDate
  148. ,modType
  149. ,status
  150. )
  151. SELECT
  152. @crLimitId
  153. ,@agentId
  154. ,@currency
  155. ,@limitAmt
  156. ,@perTopUpAmt
  157. ,@maxLimitAmt
  158. ,@expiryDate
  159. ,@user
  160. ,GETDATE()
  161. ,'I'
  162. ,'Requested'
  163. IF @@TRANCOUNT > 0
  164. COMMIT TRANSACTION
  165. EXEC proc_errorHandler 0, 'Record has been added successfully.', @crLimitId
  166. END
  167. ELSE IF @flag = 'a'
  168. BEGIN
  169. IF EXISTS (
  170. SELECT 'X' FROM creditLimitIntHistory WITH(NOLOCK)
  171. WHERE crLimitId = @crLimitId AND createdBy = @user AND approvedBy IS NULL
  172. )
  173. BEGIN
  174. SELECT
  175. mode.*
  176. ,CONVERT(VARCHAR,mode.expiryDate,101) expiryDate1
  177. FROM creditLimitIntHistory mode WITH(NOLOCK)
  178. INNER JOIN creditLimitInt main WITH(NOLOCK) ON mode.crLimitId = main.crLimitId
  179. WHERE mode.crLimitId= @crLimitId AND mode.approvedBy IS NULL
  180. END
  181. ELSE
  182. BEGIN
  183. SELECT
  184. *
  185. ,CONVERT(VARCHAR,expiryDate,101) expiryDate1
  186. FROM creditLimitInt WITH(NOLOCK) WHERE crLimitId = @crLimitId
  187. END
  188. END
  189. ELSE IF @flag = 'u'
  190. BEGIN
  191. IF EXISTS (
  192. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  193. WHERE crLimitId = @crLimitId AND ( createdBy <> @user AND approvedBy IS NULL)
  194. )
  195. BEGIN
  196. EXEC proc_errorHandler 1, 'You can not modify this record. Previous modification has not been approved yet1.', @crLimitId
  197. RETURN
  198. END
  199. IF EXISTS (
  200. SELECT 'X' FROM creditLimitIntHistory WITH(NOLOCK)
  201. WHERE crLimitId = @crLimitId AND (createdBy<> @user AND approvedBy IS NULL)
  202. )
  203. BEGIN
  204. EXEC proc_errorHandler 1, 'You can not modify this record. Previous modification has not been approved yet2.', @crLimitId
  205. RETURN
  206. END
  207. IF (@limitAmt > @maxLimitAmt)
  208. BEGIN
  209. EXEC proc_errorHandler 1, 'Limit Amount defined greater than Max Limit Amt', @crLimitId
  210. RETURN
  211. END
  212. BEGIN TRANSACTION
  213. IF EXISTS (SELECT 'X' FROM creditLimitInt WHERE approvedBy IS NULL AND crLimitId = @crLimitId)
  214. BEGIN
  215. UPDATE
  216. creditLimitInt SET
  217. agentId = @agentId
  218. ,currency = @currency
  219. ,limitAmt = @limitAmt
  220. ,perTopUpAmt = @perTopUpAmt
  221. ,maxLimitAmt = @maxLimitAmt
  222. ,expiryDate = @expiryDate
  223. ,todaysAddedMaxLimit = @todaysAddedMaxLimit
  224. ,modifiedBy = @user
  225. ,modifiedDate = GETDATE()
  226. WHERE crLimitId = @crLimitId
  227. UPDATE creditLimitIntHistory SET
  228. agentId = @agentId
  229. ,currency = @currency
  230. ,limitAmt = @limitAmt
  231. ,perTopUpAmt = @perTopUpAmt
  232. ,maxLimitAmt = @maxLimitAmt
  233. ,expiryDate = @expiryDate
  234. WHERE crLimitId = @crLimitId
  235. END
  236. ELSE
  237. BEGIN
  238. DELETE FROM creditLimitIntHistory WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  239. INSERT INTO creditLimitIntHistory(
  240. crLimitId
  241. ,agentId
  242. ,currency
  243. ,limitAmt
  244. ,perTopUpAmt
  245. ,maxLimitAmt
  246. ,expiryDate
  247. ,createdBy
  248. ,createdDate
  249. ,modType
  250. ,status
  251. )
  252. SELECT
  253. @crLimitId
  254. ,@agentId
  255. ,@currency
  256. ,@limitAmt
  257. ,@perTopUpAmt
  258. ,@maxLimitAmt
  259. ,@expiryDate
  260. ,@user
  261. ,GETDATE()
  262. ,'U'
  263. ,'Requested'
  264. END
  265. IF @@TRANCOUNT > 0
  266. COMMIT TRANSACTION
  267. EXEC proc_errorHandler 0, 'Record updated successfully.', @crLimitId
  268. END
  269. ELSE IF @flag = 'd'
  270. BEGIN
  271. IF EXISTS (
  272. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  273. WHERE crLimitId = @crLimitId AND (createdBy <> @user AND approvedBy IS NULL)
  274. )
  275. BEGIN
  276. EXEC proc_errorHandler 1, 'You can not delete this record. Previous modification has not been approved yet.', @crLimitId
  277. RETURN
  278. END
  279. IF EXISTS (
  280. SELECT 'X' FROM creditLimitIntHistory WITH(NOLOCK)
  281. WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  282. )
  283. BEGIN
  284. EXEC proc_errorHandler 1, 'You can not delete this record. Previous modification has not been approved yet.', @crLimitId
  285. RETURN
  286. END
  287. SELECT @agentId = agentId FROM creditLimitInt WHERE crLimitId = @crLimitId
  288. IF EXISTS(SELECT 'X' FROM creditLimitInt WITH(NOLOCK) WHERE crLimitId = @crLimitId AND approvedBy IS NULL AND createdBy = @user)
  289. BEGIN
  290. DELETE FROM creditLimitInt WHERE crLimitId = @crLimitId
  291. EXEC proc_errorHandler 0, 'Record deleted successfully.', @agentId
  292. RETURN
  293. END
  294. INSERT INTO creditLimitIntHistory(
  295. crLimitId
  296. ,agentId
  297. ,currency
  298. ,limitAmt
  299. ,perTopUpAmt
  300. ,maxLimitAmt
  301. ,expiryDate
  302. ,createdBy
  303. ,createdDate
  304. ,modType
  305. )
  306. SELECT
  307. crLimitId
  308. ,agentId
  309. ,currency
  310. ,limitAmt
  311. ,perTopUpAmt
  312. ,maxLimitAmt
  313. ,expiryDate
  314. ,@user
  315. ,GETDATE()
  316. ,'D'
  317. FROM creditLimitInt
  318. WHERE crLimitId = @crLimitId
  319. SET @modType = 'delete'
  320. EXEC proc_errorHandler 0, 'Record deleted successfully.', @agentId
  321. END
  322. ELSE IF @flag = 's'
  323. BEGIN
  324. DECLARE @hasRight CHAR(1)
  325. SET @hasRight = 'Y'
  326. IF @sortBy IS NULL
  327. SET @sortBy = 'agentName'
  328. IF @sortOrder IS NULL
  329. SET @sortOrder = 'ASC'
  330. SET @table = '(
  331. SELECT
  332. crLimitId = ISNULL(mode.crLimitId, main.crLimitId)
  333. ,agentId = ISNULL(mode.agentId, main.agentId)
  334. ,currency = ISNULL(mode.currency, main.currency)
  335. ,limitAmt = ISNULL(mode.limitAmt, main.limitAmt)
  336. ,perTopUpAmt = ISNULL(mode.perTopUpAmt, main.perTopUpAmt)
  337. ,maxLimitAmt = ISNULL(mode.maxLimitAmt, main.maxLimitAmt)
  338. ,expiryDate = ISNULL(mode.expiryDate, main.expiryDate)
  339. ,main.topUpTillYesterday
  340. ,main.topUpToday
  341. ,main.todaysSent
  342. ,main.todaysPaid
  343. ,main.todaysCancelled
  344. ,main.lienAmt
  345. ,main.createdBy
  346. ,main.createdDate
  347. ,modifiedDate = CASE WHEN main.approvedBy IS NULL THEN main.createdDate ELSE ISNULL(mode.createdDate, main.modifiedDate) END
  348. ,modifiedBy = CASE WHEN main.approvedBy IS NULL THEN main.createdBy ELSE ISNULL(mode.createdBy, main.modifiedBy) END
  349. ,hasChanged = CASE WHEN (main.approvedBy IS NULL) OR
  350. (mode.crLimitId IS NOT NULL)
  351. THEN ''Y'' ELSE ''N'' END
  352. FROM creditLimitInt main WITH(NOLOCK)
  353. LEFT JOIN creditLimitIntHistory mode ON main.crLimitId = mode.crLimitId AND mode.approvedBy IS NULL
  354. AND (
  355. mode.createdBy = ''' + @user + '''
  356. OR ''Y'' = ''' + @hasRight + '''
  357. )
  358. WHERE ISNULL(main.isDeleted, ''N'') <> ''Y''
  359. AND (
  360. main.approvedBy IS NOT NULL
  361. OR main.createdBy = ''' + @user + '''
  362. OR ''Y'' = ''' + @hasRight + '''
  363. )
  364. ) '
  365. print @table
  366. SET @table = '(
  367. SELECT
  368. main.crLimitId
  369. ,am.agentId
  370. ,am.agentName
  371. ,am.agentCountry
  372. ,am.agentCountryId
  373. ,main.currency
  374. ,limitAmt = ISNULL(CAST(main.limitAmt AS VARCHAR), ''N/A'')
  375. ,maxLimitAmt = ISNULL(CAST(main.maxLimitAmt AS VARCHAR), ''N/A'')
  376. ,perTopUpAmt = ISNULL(CAST(main.perTopUpAmt AS VARCHAR), ''N/A'')
  377. ,expiryDate = ISNULL(CAST(main.expiryDate AS VARCHAR), ''N/A'')
  378. ,main.createdBy
  379. ,main.createdDate
  380. ,main.modifiedBy
  381. ,haschanged
  382. FROM agentMaster am WITH(NOLOCK)
  383. LEFT JOIN ' + @table + ' main ON am.agentId = main.agentId
  384. WHERE am.isSettlingAgent = ''Y'' and am.parentId = '+cast(@parentAgentId as varchar)+'
  385. --WHERE am.isSettlingAgent = ''N'' and am.parentId = '+cast(@parentAgentId as varchar)+'
  386. ) x
  387. '
  388. SET @sql_filter = ''
  389. IF @haschanged IS NOT NULL
  390. SET @sql_filter = @sql_filter + ' AND haschanged = ''' + CAST(@haschanged AS VARCHAR) + ''''
  391. IF @agentCountry IS NOT NULL
  392. SET @sql_filter = @sql_filter + ' AND ISNULL(agentCountry, '''') = ''' + @agentCountry + ''''
  393. IF @agentDistrict IS NOT NULL
  394. SET @sql_filter = @sql_filter + ' AND ISNULL(agentDistrict, '''') = ''' + @agentDistrict + ''''
  395. IF @agentName IS NOT NULL
  396. SET @sql_filter = @sql_filter + ' AND ISNULL(agentName, '''') LIKE ''%' + @agentName + '%'''
  397. IF @agentLocation IS NOT NULL
  398. SET @sql_filter = @sql_filter + ' AND ISNULL(agentLocation, '''') =''' + @agentLocation + ''''
  399. SET @select_field_list ='
  400. crLimitId
  401. ,agentId
  402. ,agentName
  403. ,agentCountry
  404. ,agentCountryId
  405. ,currency
  406. ,limitAmt
  407. ,perTopUpAmt
  408. ,maxLimitAmt
  409. ,expiryDate
  410. ,createdBy
  411. ,createdDate
  412. ,modifiedBy
  413. ,haschanged
  414. '
  415. print @sql
  416. EXEC dbo.proc_paging
  417. @table
  418. ,@sql_filter
  419. ,@select_field_list
  420. ,@extra_field_list
  421. ,@sortBy
  422. ,@sortOrder
  423. ,@pageSize
  424. ,@pageNumber
  425. END
  426. ELSE IF @flag = 'simpleGrid'
  427. BEGIN
  428. IF @sortBy IS NULL
  429. SET @sortBy = 'agentName'
  430. IF @sortOrder IS NULL
  431. SET @sortOrder = 'ASC'
  432. SET @table = '(
  433. SELECT
  434. crLimitId = ISNULL(mode.crLimitId, main.crLimitId)
  435. ,agentId = ISNULL(mode.agentId, main.agentId)
  436. ,currency = ISNULL(mode.currency, main.currency)
  437. ,limitAmt = ISNULL(mode.limitAmt, main.limitAmt)
  438. ,perTopUpAmt = ISNULL(mode.perTopUpAmt, main.perTopUpAmt)
  439. ,maxLimitAmt = ISNULL(mode.maxLimitAmt, main.maxLimitAmt)
  440. ,expiryDate = ISNULL(mode.expiryDate, main.expiryDate)
  441. ,main.topUpTillYesterday
  442. ,main.topUpToday
  443. ,main.todaysSent
  444. ,main.todaysPaid
  445. ,main.todaysCancelled
  446. ,main.lienAmt
  447. ,main.createdBy
  448. ,main.createdDate
  449. ,modifiedDate = CASE WHEN main.approvedBy IS NULL THEN main.createdDate ELSE ISNULL(mode.createdDate, main.modifiedDate) END
  450. ,modifiedBy = CASE WHEN main.approvedBy IS NULL THEN main.createdBy ELSE ISNULL(mode.createdBy, main.modifiedBy) END
  451. ,hasChanged = CASE WHEN (main.approvedBy IS NULL) OR
  452. (mode.crLimitId IS NOT NULL)
  453. THEN ''Y'' ELSE ''N'' END
  454. FROM creditLimitInt main WITH(NOLOCK)
  455. LEFT JOIN creditLimitIntHistory mode ON main.crLimitId = mode.crLimitId AND mode.approvedBy IS NULL
  456. AND (
  457. mode.createdBy = ''' + @user + '''
  458. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  459. )
  460. WHERE ISNULL(main.isDeleted, ''N'') <> ''Y''
  461. AND (
  462. main.approvedBy IS NOT NULL
  463. OR main.createdBy = ''' + @user + '''
  464. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  465. )
  466. ) '
  467. IF @user IN ('admin', 'imeadmin')
  468. BEGIN
  469. SET @table = '(
  470. SELECT
  471. main.crLimitId
  472. ,am.agentId
  473. ,am.agentName
  474. ,am.agentCountry
  475. ,am.agentDistrict
  476. ,am.agentLocation
  477. ,currency = ISNULL(cm.currencyCode, ''N/A'')
  478. ,limitAmt = ISNULL(CAST(main.limitAmt AS VARCHAR), ''N/A'')
  479. ,maxLimitAmt = ISNULL(CAST(main.maxLimitAmt AS VARCHAR), ''N/A'')
  480. ,perTopUpAmt = ISNULL(CAST(main.perTopUpAmt AS VARCHAR), ''N/A'')
  481. --,currentBalance = v.clr_bal_amt
  482. --,availableLimit = dbo.FNAGetLimitBal(am.agentId)
  483. --,availableLimit = main.limitamt + main.topUpTillYesterday + main.topUpToday - V.todaysSend + V.todaysPaid + V.todaysCancel + v.clr_bal_amt - main.lienAmt
  484. ,expiryDate = ISNULL(CAST(main.expiryDate AS VARCHAR), ''N/A'')
  485. --,limitToppedUp = ISNULL(main.topUpTillYesterday, 0) + ISNULL(main.topUpToday, 0)
  486. --,todaysSent = V.todaysSend
  487. --,todaysPaid = V.todaysPaid
  488. --,todaysCancelled = V.todaysCancel
  489. ,main.createdBy
  490. ,main.createdDate
  491. ,main.modifiedBy
  492. ,haschanged
  493. FROM agentMaster am
  494. --INNER JOIN [192.168.0.83].FastMoneyPro_account.dbo.vWAgentClrBal V ON V.map_code = am.mapCodeInt
  495. LEFT JOIN ' + @table + ' main ON am.agentId = main.agentId
  496. LEFT JOIN currencyMaster cm WITH(NOLOCK) ON main.currency = cm.currencyCode
  497. WHERE am.isSettlingAgent = ''Y''
  498. ) x
  499. '
  500. END
  501. ELSE
  502. BEGIN
  503. /*
  504. IF OBJECT_ID('tempdb..#agentId') IS NOT NULL
  505. DROP TABLE #agentId*/
  506. --CREATE TABLE #agentId(agentId INT)
  507. INSERT INTO #agentId
  508. SELECT agentId FROM agentMaster WHERE ISNULL(isDeleted, 'N') = 'N'
  509. DELETE FROM #agentId
  510. FROM #agentId ag
  511. INNER JOIN
  512. agentGroupMaping agm ON agm.agentId = ag.agentId
  513. WHERE agm.groupCat = '6900' AND ISNULL(agm.isDeleted, 'N') = 'N'
  514. INSERT INTO #agentId
  515. SELECT DISTINCT agm.agentId FROM userGroupMapping ugm
  516. INNER JOIN agentGroupMaping agm ON agm.groupDetail = ugm.groupDetail AND ISNULL(agm.isDeleted, 'N') = 'N' AND ISNULL(ugm.isDeleted, 'N') = 'N'
  517. WHERE ugm.userName = @user
  518. SET @table = '(
  519. SELECT
  520. main.crLimitId
  521. ,am.agentId
  522. ,am.agentName
  523. ,am.agentCountry
  524. ,am.agentDistrict
  525. ,am.agentLocation
  526. ,currency = ISNULL(cm.currencyCode, ''N/A'')
  527. ,limitAmt = ISNULL(CAST(main.limitAmt AS VARCHAR), ''N/A'')
  528. ,maxLimitAmt = ISNULL(CAST(main.maxLimitAmt AS VARCHAR), ''N/A'')
  529. ,perTopUpAmt = ISNULL(CAST(main.perTopUpAmt AS VARCHAR), ''N/A'')
  530. ,expiryDate = ISNULL(CAST(main.expiryDate AS VARCHAR), ''N/A'')
  531. ,main.createdBy
  532. ,main.createdDate
  533. ,main.modifiedBy
  534. ,haschanged
  535. FROM agentMaster am
  536. INNER JOIN
  537. (
  538. SELECT DISTINCT agentId FROM #agentId
  539. )ag ON am.agentId = ag.agentId
  540. --INNER JOIN [192.168.0.83].FastMoneyPro_account.dbo.vWAgentClrBal V ON V.map_code = am.mapCodeInt
  541. LEFT JOIN ' + @table + ' main ON am.agentId = main.agentId
  542. LEFT JOIN currencyMaster cm WITH(NOLOCK) ON main.currency = cm.currencyCode
  543. WHERE am.isSettlingAgent = ''Y''
  544. ) x
  545. '
  546. END
  547. SET @sql_filter = ''
  548. IF @haschanged IS NOT NULL
  549. SET @sql_filter = @sql_filter + ' AND haschanged = ''' + CAST(@haschanged AS VARCHAR) + ''''
  550. IF @agentCountry IS NOT NULL
  551. SET @sql_filter = @sql_filter + ' AND ISNULL(agentCountry, '''') = ''' + @agentCountry + ''''
  552. IF @agentDistrict IS NOT NULL
  553. SET @sql_filter = @sql_filter + ' AND ISNULL(agentDistrict, '''') = ''' + @agentDistrict + ''''
  554. IF @agentName IS NOT NULL
  555. SET @sql_filter = @sql_filter + ' AND ISNULL(agentName, '''') LIKE ''%' + @agentName + '%'''
  556. IF @agentLocation IS NOT NULL
  557. SET @sql_filter = @sql_filter + ' AND ISNULL(agentLocation, '''') =''' + @agentLocation + ''''
  558. SET @select_field_list ='
  559. crLimitId
  560. ,agentId
  561. ,agentName
  562. ,agentCountry
  563. ,agentDistrict
  564. ,agentLocation
  565. ,currency
  566. ,limitAmt
  567. ,perTopUpAmt
  568. ,maxLimitAmt
  569. ,expiryDate
  570. ,createdBy
  571. ,createdDate
  572. ,modifiedBy
  573. ,haschanged
  574. '
  575. EXEC dbo.proc_paging
  576. @table
  577. ,@sql_filter
  578. ,@select_field_list
  579. ,@extra_field_list
  580. ,@sortBy
  581. ,@sortOrder
  582. ,@pageSize
  583. ,@pageNumber
  584. END
  585. ELSE IF @flag='history'
  586. BEGIN
  587. IF @sortBy IS NULL
  588. SET @sortBy = 'approvedDate'
  589. IF @sortOrder IS NULL
  590. SET @sortOrder = 'Desc'
  591. SET @table = '(
  592. select
  593. ROW_NUMBER()OVER(ORDER BY a.rowid desc) sn
  594. ,b.currencyCode
  595. ,limitAmt
  596. ,perTopUpAmt
  597. ,maxLimitAmt
  598. ,expiryDate
  599. ,a.createdBy
  600. ,a.approvedBy
  601. ,a.approvedDate
  602. ,haschanged=''Y''
  603. ,modifiedBy=''''
  604. from creditLimitIntHistory a with(nolock) inner join currencyMaster b with(nolock)
  605. on a.currency=b.currencyId
  606. where agentId=''' + cast(@agentId as varchar) + '''
  607. ) x
  608. '
  609. SET @sql_filter = ''
  610. IF @createdBy IS NOT NULL
  611. SET @sql_filter = @sql_filter + ' AND ISNULL(createdBy, '''') LIKE ''%' + @currencyCode + '%'''
  612. IF @approvedBy IS NOT NULL
  613. SET @sql_filter = @sql_filter + ' AND ISNULL(approvedBy, '''') LIKE ''%' + @currencyCode + '%'''
  614. IF @currencyCode IS NOT NULL
  615. SET @sql_filter = @sql_filter + ' AND ISNULL(currencyCode, '''') LIKE ''%' + @currencyCode + '%'''
  616. SET @select_field_list ='
  617. sn
  618. ,currencyCode
  619. ,limitAmt
  620. ,perTopUpAmt
  621. ,maxLimitAmt
  622. ,expiryDate
  623. ,createdBy
  624. ,approvedBy
  625. ,approvedDate
  626. ,haschanged
  627. ,modifiedBy
  628. '
  629. EXEC dbo.proc_paging
  630. @table
  631. ,@sql_filter
  632. ,@select_field_list
  633. ,@extra_field_list
  634. ,@sortBy
  635. ,@sortOrder
  636. ,@pageSize
  637. ,@pageNumber
  638. END
  639. ELSE IF @flag = 'reject'
  640. BEGIN
  641. IF NOT EXISTS (
  642. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  643. WHERE crLimitId = @crLimitId
  644. )
  645. AND
  646. NOT EXISTS (
  647. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  648. WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  649. )
  650. BEGIN
  651. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @crLimitId
  652. RETURN
  653. END
  654. IF EXISTS (SELECT 'X' FROM creditLimitInt WHERE approvedBy IS NULL AND crLimitId = @crLimitId)
  655. BEGIN --New record
  656. BEGIN TRANSACTION
  657. SET @modType = 'Reject'
  658. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @crLimitId, @oldValue OUTPUT
  659. INSERT INTO #msg(errorCode, msg, id)
  660. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @crLimitId, @user, @oldValue, @newValue
  661. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  662. BEGIN
  663. IF @@TRANCOUNT > 0
  664. ROLLBACK TRANSACTION
  665. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @crLimitId
  666. RETURN
  667. END
  668. DELETE FROM creditLimitInt WHERE crLimitId = @crLimitId
  669. UPDATE creditLimitIntHistory SET status='Rejected',approvedBy=@user,approvedDate=GETDATE() where crLimitId = @crLimitId
  670. --update FROM creditLimitInt WHERE crLimitId = @crLimitId
  671. IF @@TRANCOUNT > 0
  672. COMMIT TRANSACTION
  673. END
  674. ELSE
  675. BEGIN
  676. BEGIN TRANSACTION
  677. SET @modType = 'Reject'
  678. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @crLimitId, @oldValue OUTPUT
  679. INSERT INTO #msg(errorCode, msg, id)
  680. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @crLimitId, @user, @oldValue, @newValue
  681. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  682. BEGIN
  683. IF @@TRANCOUNT > 0
  684. ROLLBACK TRANSACTION
  685. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @crLimitId
  686. RETURN
  687. END
  688. --DELETE FROM creditLimitIntHistory WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  689. UPDATE creditLimitIntHistory SET status='Rejected',approvedBy=@user,approvedDate=GETDATE() where crLimitId = @crLimitId
  690. IF @@TRANCOUNT > 0
  691. COMMIT TRANSACTION
  692. END
  693. EXEC proc_errorHandler 0, 'Changes rejected successfully.', @crLimitId
  694. END
  695. ELSE IF @flag = 'approve'
  696. BEGIN
  697. IF NOT EXISTS (
  698. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  699. WHERE crLimitId = @crLimitId
  700. )
  701. AND
  702. NOT EXISTS (
  703. SELECT 'X' FROM creditLimitInt WITH(NOLOCK)
  704. WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  705. )
  706. BEGIN
  707. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @crLimitId
  708. RETURN
  709. END
  710. BEGIN TRANSACTION
  711. IF EXISTS (SELECT 'X' FROM creditLimitInt WHERE approvedBy IS NULL AND crLimitId = @crLimitId )
  712. SET @modType = 'I'
  713. ELSE
  714. SELECT @modType = modType FROM creditLimitIntHistory WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  715. IF @modType = 'I'
  716. BEGIN --New record
  717. UPDATE creditLimitInt SET
  718. isActive = 'Y'
  719. ,approvedBy = @user
  720. ,approvedDate= GETDATE()
  721. WHERE crLimitId = @crLimitId
  722. UPDATE creditLimitIntHistory SET
  723. approvedBy = @user
  724. ,approvedDate= GETDATE()
  725. ,status='Approved'
  726. WHERE crLimitId = @crLimitId
  727. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @crLimitId, @newValue OUTPUT
  728. --END
  729. END
  730. ELSE IF @modType = 'U'
  731. BEGIN
  732. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @crLimitId, @oldValue OUTPUT
  733. UPDATE main SET
  734. main.agentId = mode.agentId
  735. ,main.currency = mode.currency
  736. ,main.limitAmt = mode.limitAmt
  737. ,main.perTopUpAmt = mode.perTopUpAmt
  738. ,main.maxLimitAmt = mode.maxLimitAmt
  739. ,main.expiryDate = mode.expiryDate
  740. ,main.modifiedDate = GETDATE()
  741. ,main.modifiedBy = @user
  742. FROM creditLimitInt main
  743. INNER JOIN creditLimitIntHistory mode ON mode.crLimitId = main.crLimitId
  744. WHERE mode.crLimitId = @crLimitId AND mode.approvedBy IS NULL
  745. UPDATE creditLimitIntHistory SET
  746. approvedBy = @user
  747. ,approvedDate= GETDATE()
  748. ,status='Approved'
  749. WHERE crLimitId = @crLimitId
  750. EXEC [dbo].proc_GetColumnToRow 'creditLimitInt', 'crLimitId', @crLimitId, @newValue OUTPUT
  751. END
  752. ELSE IF @modType = 'D'
  753. BEGIN
  754. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @crLimitId, @oldValue OUTPUT
  755. UPDATE creditLimitInt SET
  756. isDeleted = 'Y'
  757. ,modifiedDate = GETDATE()
  758. ,modifiedBy = @user
  759. WHERE crLimitId = @crLimitId
  760. END
  761. UPDATE creditLimitIntHistory SET
  762. approvedBy = @user
  763. ,approvedDate = GETDATE()
  764. WHERE crLimitId = @crLimitId AND approvedBy IS NULL
  765. INSERT INTO #msg(errorCode, msg, id)
  766. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @crLimitId, @user, @oldValue, @newValue
  767. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  768. BEGIN
  769. IF @@TRANCOUNT > 0
  770. ROLLBACK TRANSACTION
  771. EXEC proc_errorHandler 1, 'Could not approve the changes.', @crLimitId
  772. RETURN
  773. END
  774. IF @@TRANCOUNT > 0
  775. COMMIT TRANSACTION
  776. EXEC proc_errorHandler 0, 'Changes approved successfully.', @crLimitId
  777. END
  778. END TRY
  779. BEGIN CATCH
  780. IF @@TRANCOUNT > 0
  781. ROLLBACK TRANSACTION
  782. DECLARE @errorMessage VARCHAR(MAX)
  783. SET @errorMessage = ERROR_MESSAGE()
  784. EXEC proc_errorHandler 1, @errorMessage, @crLimitId
  785. END CATCH
  786. GO