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.

556 lines
18 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentCommissionRule] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_agentCommissionRule]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_agentCommissionRule] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. ALTER PROC [dbo].[proc_agentCommissionRule]
  12. @flag VARCHAR(50) = NULL
  13. ,@user VARCHAR(30) = NULL
  14. ,@id INT = NULL
  15. ,@agentId INT = NULL
  16. ,@ruleId INT = NULL
  17. ,@groupId INT = NULL
  18. ,@packageName VARCHAR(200) = NULL
  19. ,@ruleName VARCHAR(200) = NULL
  20. ,@ruleType VARCHAR(200) = NULL
  21. ,@groupName VARCHAR(200) = NULL
  22. ,@scMasterId INT = NULL
  23. ,@agentCountry VARCHAR(200) = NULL
  24. ,@agentName VARCHAR(200) = NULL
  25. ,@isSettlingAgent VARCHAR(10) = NULL
  26. ,@sortBy VARCHAR(50) = NULL
  27. ,@sortOrder VARCHAR(5) = NULL
  28. ,@pageSize INT = NULL
  29. ,@pageNumber INT = NULL
  30. AS
  31. SET NOCOUNT ON;
  32. SET XACT_ABORT ON;
  33. BEGIN TRY
  34. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  35. DECLARE @table AS VARCHAR(MAX),@sql_filter VARCHAR(MAX)
  36. ,@select_field_list VARCHAR(MAX)
  37. ,@extra_field_list VARCHAR(MAX)
  38. DECLARE @commissionRule TABLE(ruleId INT)
  39. DECLARE @commissionRuleNew TABLE(ruleId INT)
  40. DECLARE @found INT = 0
  41. DECLARE @ssAgent INT, @rsAgent INT,@sCountry INT, @rCountry INT,
  42. @sAgent INT, @sBranch INT, @sState INT, @sGroup INT, @rAgent INT, @rBranch INT,
  43. @rState INT, @rGroup INT, @tranType INT
  44. -- ### START COMMISSION PACKAGE ### ---
  45. IF @flag = 'i'
  46. BEGIN
  47. IF EXISTS(SELECT 'X' FROM agentCommissionRule WHERE agentId = @agentId AND ruleId = @ruleId)
  48. BEGIN
  49. EXEC proc_errorHandler 1, 'Already Added!', @user
  50. RETURN;
  51. END
  52. INSERT @commissionRule
  53. SELECT ruleId FROM agentCommissionRule WHERE agentId = @agentId
  54. SELECT
  55. @sAgent = sAgent
  56. ,@sBranch = sBranch
  57. ,@sState = sState
  58. ,@sGroup = sGroup
  59. ,@rAgent = rAgent
  60. ,@rBranch = rBranch
  61. ,@rState = rState
  62. ,@rGroup = rGroup
  63. ,@tranType = tranType
  64. FROM scMaster WITH(NOLOCK) WHERE scMasterId = @ruleId
  65. IF EXISTS(SELECT 'X' FROM scMaster WHERE
  66. ISNULL(sAgent, 0) = ISNULL(@sAgent, 0)
  67. AND ISNULL(sBranch, 0) = ISNULL(@sBranch, 0)
  68. AND ISNULL(sState, 0) = ISNULL(@sState, 0)
  69. AND ISNULL(sGroup, 0) = ISNULL(@sGroup, 0)
  70. AND ISNULL(rAgent, 0) = ISNULL(@rAgent, 0)
  71. AND ISNULL(rBranch, 0) = ISNULL(@rBranch, 0)
  72. AND ISNULL(rState, 0) = ISNULL(@rState, 0)
  73. AND ISNULL(rGroup, 0) = ISNULL(@rGroup, 0)
  74. AND ISNULL(tranType, 0) = ISNULL(@tranType, 0)
  75. AND ISNULL(isDeleted, 'N') = 'N'
  76. AND scMasterId IN (SELECT ruleId FROM @commissionRule))
  77. BEGIN
  78. EXEC proc_errorHandler 1, 'Commission Rule with this criteria has already been added', NULL
  79. RETURN
  80. END
  81. INSERT INTO agentCommissionRule(
  82. agentId
  83. ,ruleId
  84. ,isActive
  85. ,createdBy
  86. ,createdDate
  87. )
  88. SELECT
  89. @agentId
  90. ,@ruleId
  91. ,'Y'
  92. ,@user
  93. ,GETDATE()
  94. EXEC proc_errorHandler 0, 'Successfully Added!', @user
  95. END
  96. IF @flag='packList'
  97. BEGIN
  98. select distinct agentId,case when ruleType='ds' then rtrim(ltrim(dbo.FNAGetDataValue(agentId)))+' -Domestic'
  99. else rtrim(ltrim(dbo.FNAGetDataValue(agentId)))+' -International' end as packageName from agentCommissionRule
  100. where isActive='Y'
  101. END
  102. IF @flag = 'u'
  103. BEGIN
  104. IF EXISTS(SELECT 'X' FROM agentCommissionRule WHERE agentId = @agentId AND ruleId = @ruleId and id <> @id)
  105. BEGIN
  106. EXEC proc_errorHandler 1, 'Already Added!', @user
  107. RETURN
  108. END
  109. UPDATE agentCommissionRule SET
  110. agentId = @agentId
  111. ,ruleId = @ruleId
  112. WHERE ID = @ID
  113. EXEC proc_errorHandler 0, 'Successfully Updated!', @user
  114. END
  115. IF @flag = 'a'
  116. BEGIN
  117. SELECT * FROM agentCommissionRule WHERE id = @id
  118. END
  119. IF @flag = 'd'
  120. BEGIN
  121. IF EXISTS (
  122. SELECT 'X' FROM agentCommissionRule WITH(NOLOCK)
  123. WHERE id = @id AND (createdBy <> @user AND approvedBy IS NULL)
  124. )
  125. BEGIN
  126. EXEC proc_errorHandler 1, 'You can not delete this record. Previous modification has not been approved yet.', @id
  127. END
  128. IF EXISTS (
  129. SELECT 'X' FROM agentCommissionRuleHistory WITH(NOLOCK)
  130. WHERE id = @id AND (approvedBy IS NULL AND createdBy <> @user)
  131. )
  132. BEGIN
  133. EXEC proc_errorHandler 1, 'You can not delete this record. Previous modification has not been approved yet.', @id
  134. RETURN
  135. END
  136. IF EXISTS(SELECT 'X' FROM agentCommissionRule WITH(NOLOCK) WHERE id = @id AND approvedBy IS NULL AND createdBy = @user)
  137. BEGIN
  138. DELETE FROM agentCommissionRule WHERE id = @id AND approvedBy IS NULL
  139. DELETE FROM agentCommissionRuleHistory WHERE id = @id AND approvedBy IS NULL
  140. EXEC proc_errorHandler 0, 'Record deleted successfully.', @id
  141. RETURN
  142. END
  143. ELSE
  144. BEGIN
  145. DELETE FROM agentCommissionRuleHistory WHERE id = @id AND approvedBy IS NULL
  146. INSERT INTO agentCommissionRuleHistory(
  147. id
  148. ,agentId
  149. ,ruleId
  150. ,ruleType
  151. ,modType
  152. ,createdBy
  153. ,createdDate
  154. )
  155. SELECT
  156. id
  157. ,agentId
  158. ,ruleId
  159. ,ruleType
  160. ,'D'
  161. ,@user
  162. ,GETDATE()
  163. FROM agentCommissionRule
  164. WHERE id = @id
  165. END
  166. EXEC proc_errorHandler 0, 'Record deleted successfully.', @user
  167. END
  168. IF @flag = 'ic'
  169. BEGIN
  170. --International Service Charge List
  171. SELECT
  172. A.ID
  173. ,A.ruleId
  174. ,A.agentId
  175. ,ROW_NUMBER() OVER(order by A.ID) [S.N.]
  176. ,[Agent Name] = '<a href="CommissionView.aspx?agentId=' + CAST(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleType=sc">' + B.agentName + '</a>'
  177. ,[Rule Name] = '<a href="CommissionView.aspx?agentId=' + CAST(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleId=' + CAST(A.ruleId AS VARCHAR) + '&ruleType=sc">' + C.code+ '</a>'
  178. ,A.createdBy [Created By]
  179. ,CONVERT(VARCHAR,A.createdDate,101) [Created Date]
  180. FROM agentCommissionRule A WITH(NOLOCK)
  181. INNER JOIN agentMaster B WITH(NOLOCK) ON A.agentId = B.agentId
  182. INNER JOIN sscMaster C WITH(NOLOCK) ON C.sscMasterId = A.ruleId
  183. WHERE A.ruleType='sc'
  184. AND A.agentId = @agentId
  185. --International Pay Commission List
  186. SELECT
  187. A.ID
  188. ,A.ruleId
  189. ,A.agentId
  190. ,ROW_NUMBER() OVER(order by A.ID) [S.N.]
  191. ,[Agent Name] = '<a href="CommissionView.aspx?agentId=' + cast(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleType=cp">' + B.agentName + '</a>'
  192. ,[Rule Name] = '<a href="CommissionView.aspx?agentId=' + cast(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleId=' + CAST(A.ruleId AS VARCHAR) + '&ruleType=cp">' + C.code + '</a>'
  193. ,A.createdBy [Created By]
  194. ,CONVERT(VARCHAR,A.createdDate,101) [Created Date]
  195. FROM agentCommissionRule A WITH(NOLOCK)
  196. INNER JOIN agentMaster B WITH(NOLOCK) ON A.agentId = B.agentId
  197. INNER JOIN scPayMaster C WITH(NOLOCK) ON C.scPayMasterId = A.ruleId
  198. WHERE A.ruleType='cp'
  199. AND A.agentId = @agentId
  200. --International Send Commission List
  201. SELECT
  202. A.ID
  203. ,A.ruleId
  204. ,A.agentId
  205. ,ROW_NUMBER() OVER(order by A.ID) [S.N.]
  206. ,[Agent Name] = '<a href="CommissionView.aspx?agentId=' + cast(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleType=cs">' + B.agentName + '</a>'
  207. ,[Rule Name] = '<a href="CommissionView.aspx?agentId=' + cast(A.agentId as varchar) + '&agentName=' + B.agentName + '&ruleId=' + cast(A.ruleId as varchar) + '&ruleType=cs">' +C.code+ '</a>'
  208. ,A.createdBy [Created By]
  209. ,CONVERT(VARCHAR,A.createdDate,101) [Created Date]
  210. FROM agentCommissionRule A WITH(NOLOCK)
  211. INNER JOIN agentMaster B WITH(NOLOCK) ON A.agentId = B.agentId
  212. INNER JOIN scSendMaster C WITH(NOLOCK) ON C.scSendMasterId = A.ruleId
  213. WHERE A.ruleType='cs'
  214. AND A.agentId = @agentId
  215. END
  216. ELSE IF @flag = 'vc' --View Changes(Maker/Checker)
  217. BEGIN
  218. SELECT
  219. main.ruleId
  220. ,hasChanged = 'N'
  221. ,modType = ''
  222. FROM agentCommissionRule main
  223. LEFT JOIN agentCommissionRuleHistory mode ON main.ruleId = mode.ruleId AND main.agentId = mode.agentId
  224. WHERE main.agentId = @agentId AND main.ruleType = 'sc' AND main.approvedBy IS NOT NULL AND mode.approvedBy IS NULL AND ISNULL(mode.modType, '') <> 'D'
  225. UNION ALL
  226. SELECT
  227. ruleId
  228. ,hasChanged = 'Y'
  229. ,modType
  230. FROM agentCommissionRuleHistory WHERE agentId = @agentId AND ruleType = 'sc' AND approvedBy IS NULL
  231. SELECT
  232. main.ruleId
  233. ,hasChanged = 'N'
  234. ,modType = ''
  235. FROM agentCommissionRule main
  236. LEFT JOIN agentCommissionRuleHistory mode ON main.ruleId = mode.ruleId AND main.agentId = mode.agentId
  237. WHERE main.agentId = @agentId AND main.ruleType = 'cp' AND main.approvedBy IS NOT NULL AND mode.approvedBy IS NULL AND ISNULL(mode.modType, '') <> 'D'
  238. UNION ALL
  239. SELECT
  240. ruleId
  241. ,hasChanged = 'Y'
  242. ,modType
  243. FROM agentCommissionRuleHistory WHERE agentId = @agentId AND ruleType = 'cp' AND approvedBy IS NULL
  244. SELECT
  245. main.ruleId
  246. ,hasChanged = 'N'
  247. ,modType = ''
  248. FROM agentCommissionRule main
  249. LEFT JOIN agentCommissionRuleHistory mode ON main.ruleId = mode.ruleId AND main.agentId = mode.agentId
  250. WHERE main.agentId = @agentId AND main.ruleType = 'cs' AND main.approvedBy IS NOT NULL AND mode.approvedBy IS NULL AND ISNULL(mode.modType, '') <> 'D'
  251. UNION ALL
  252. SELECT
  253. ruleId
  254. ,hasChanged = 'Y'
  255. ,modType
  256. FROM agentCommissionRuleHistory WHERE agentId = @agentId AND ruleType = 'cs' AND approvedBy IS NULL
  257. END
  258. ELSE IF @flag = 'pal' --Package Audit Log
  259. BEGIN
  260. SELECT DISTINCT
  261. createdBy
  262. ,createdDate
  263. ,agentId
  264. FROM agentCommissionRuleHistory WITH(NOLOCK) WHERE agentId = @agentId AND approvedBy IS NULL
  265. END
  266. IF @flag='V'
  267. BEGIN
  268. if @ruleType='sc'
  269. begin
  270. SELECT
  271. DISTINCT
  272. code [Code]
  273. ,description [Desc]
  274. ,CM.countryName sCountry
  275. ,dbo.GetAgentNameFromId(ssAgent) ssAgent
  276. ,dbo.GetAgentNameFromId(sAgent) sAgent
  277. ,dbo.GetAgentNameFromId(sBranch) sBranch
  278. ,isnull(stTbl.stateCode,'Any') sState
  279. ,main.zip sZip
  280. ,isnull(dbo.[FNAGetDataValue](main.agentGroup),'Any') sGroup
  281. ,CM1.countryName rCountry
  282. ,dbo.GetAgentNameFromId(rsAgent) rsAgent
  283. ,dbo.GetAgentNameFromId(rAgent) rAgent
  284. ,dbo.GetAgentNameFromId(rBranch) rBranch
  285. ,isnull(stTbl1.stateCode,'Any') rState
  286. ,rZip
  287. ,isnull(dbo.[FNAGetDataValue](rAgentGroup),'Any') rGroup
  288. ,effectiveFrom = main.approvedDate
  289. ,effectiveTo
  290. ,baseCurrency baseCurrency
  291. ,tranType = isnull(scM.typeTitle ,'All')
  292. ,ve positiveDisc
  293. ,dbo.FNAGetDataValue(veType) positiveDiscType
  294. ,ne negativeDisc
  295. ,dbo.FNAGetDataValue(neType) negativeDiscType
  296. ,main.createdBy
  297. ,main.createdDate
  298. FROM sscMaster main WITH(NOLOCK)
  299. INNER JOIN agentCommissionRule comPck with(nolock) on main.sscMasterId=comPck.ruleId
  300. INNER JOIN countryMaster CM with(nolock) on CM.countryId=main.sCountry
  301. INNER JOIN countryMaster CM1 WITH(NOLOCK) ON CM1.countryId=main.rCountry
  302. LEFT JOIN countryStateMaster stTbl with(nolock) on stTbl.stateId=main.State
  303. LEFT JOIN countryStateMaster stTbl1 with(nolock) on stTbl1.stateId=main.rState
  304. LEFT JOIN serviceTypeMaster scM on scM.serviceTypeId = main.tranType
  305. WHERE main.sscMasterId = @scMasterId
  306. end
  307. IF @ruleType='cp'
  308. BEGIN
  309. SELECT
  310. distinct
  311. code [Code]
  312. ,description [Desc]
  313. ,dbo.GetAgentNameFromId(ssAgent) ssAgent
  314. ,dbo.GetAgentNameFromId(sAgent) sAgent
  315. ,dbo.GetAgentNameFromId(sBranch) sBranch
  316. ,isnull(stTbl.stateCode,'All State') sState
  317. ,ISNULL(cm.countryName,'All Coutry') sCountry
  318. ,ISNULL(main.zip,'') sZip
  319. ,isnull(dbo.[FNAGetDataValue](agentGroup),'Any') sGroup
  320. ,ISNULL(rcm.countryName,'All Coutry') rCountry
  321. ,dbo.GetAgentNameFromId(rsAgent) rsAgent
  322. ,dbo.GetAgentNameFromId(rAgent) rAgent
  323. ,dbo.GetAgentNameFromId(rBranch) rBranch
  324. ,isnull(stTbl1.stateCode,'All State') rState
  325. ,ISNULL(main.rZip,'') rZip
  326. ,isnull(dbo.[FNAGetDataValue](ragentGroup),'Any') rGroup
  327. ,tranType = isnull(scM.typeTitle ,'All')
  328. ,main.baseCurrency baseCurrency
  329. ,main.commissionCurrency commCurrency
  330. ,dbo.[FNAGetDataValue](commissionBase) commBase
  331. ,effectiveFrom = main.approvedDate
  332. ,main.effectiveTo
  333. from scPayMaster main with(nolock)
  334. inner join agentCommissionRule comPck with(nolock) on main.scPayMasterId=comPck.ruleId
  335. left join countryStateMaster stTbl with(nolock) on stTbl.stateId= main.[state]
  336. left join countryStateMaster stTbl1 with(nolock) on stTbl1.stateId= main.rState
  337. left join serviceTypeMaster scM on scM.serviceTypeId=main.tranType
  338. left join countryMaster cm with (nolock) on cm.countryId = main.sCountry
  339. left join countryMaster rcm with (nolock) on rcm.countryId = main.rCountry
  340. where main.scPayMasterId=@scMasterId
  341. end
  342. if @ruleType='cs'
  343. begin
  344. select
  345. distinct
  346. code [Code]
  347. ,description [Desc]
  348. ,dbo.GetAgentNameFromId(ssAgent) ssAgent
  349. ,dbo.GetAgentNameFromId(sAgent) sAgent
  350. ,dbo.GetAgentNameFromId(sBranch) sBranch
  351. ,isnull(stTbl.stateCode,'All State') sState
  352. ,ISNULL(cm.countryName,'All Coutry') sCountry
  353. ,ISNULL(main.zip,'') sZip
  354. ,isnull(dbo.[FNAGetDataValue](agentGroup),'Any') sGroup
  355. ,ISNULL(rcm.countryName,'All Coutry') rCountry
  356. ,dbo.GetAgentNameFromId(rsAgent) rsAgent
  357. ,dbo.GetAgentNameFromId(rAgent) rAgent
  358. ,dbo.GetAgentNameFromId(rBranch) rBranch
  359. ,isnull(stTbl1.stateCode,'All State') rState
  360. ,ISNULL(main.rZip,'') rZip
  361. ,isnull(dbo.[FNAGetDataValue](ragentGroup),'Any') rGroup
  362. ,tranType = isnull(scM.typeTitle ,'All')
  363. ,main.baseCurrency baseCurrency
  364. ,dbo.[FNAGetDataValue](commissionBase) commBase
  365. ,effectiveFrom = main.approvedDate
  366. ,main.effectiveTo
  367. from scSendMaster main with(nolock)
  368. inner join agentCommissionRule comPck with(nolock) on main.scSendMasterId=comPck.ruleId
  369. left join countryStateMaster stTbl with(nolock) on stTbl.stateId=main.[State]
  370. left join countryStateMaster stTbl1 with(nolock) on stTbl1.stateId=main.rState
  371. left join serviceTypeMaster scM on scM.serviceTypeId=main.tranType
  372. left join countryMaster cm with (nolock) on cm.countryId = sCountry
  373. left join countryMaster rcm with (nolock) on rcm.countryId = rCountry
  374. where main.scSendMasterId=@scMasterId
  375. end
  376. END
  377. IF @flag='V1'
  378. BEGIN
  379. if @ruleType='ds'
  380. begin
  381. SELECT
  382. scDetailId = main.scDetailId
  383. ,fromAmt = main.fromAmt
  384. ,toAmt = main.toAmt
  385. ,serviceChargePcnt = main.serviceChargePcnt
  386. ,serviceChargeMinAmt = main.serviceChargeMinAmt
  387. ,serviceChargeMaxAmt = main.serviceChargeMaxAmt
  388. ,sAgentCommPcnt = main.sAgentCommPcnt
  389. ,sAgentCommMinAmt = main.sAgentCommMinAmt
  390. ,sAgentCommMaxAmt = main.sAgentCommMinAmt
  391. ,ssAgentCommPcnt = main.ssAgentCommPcnt
  392. ,ssAgentCommMinAmt = main.ssAgentCommMinAmt
  393. ,ssAgentCommMaxAmt = main.ssAgentCommMinAmt
  394. ,pAgentCommPcnt = main.pAgentCommPcnt
  395. ,pAgentCommMinAmt = main.pAgentCommMinAmt
  396. ,pAgentCommMaxAmt = main.pAgentCommMaxAmt
  397. ,psAgentCommPcnt = main.psAgentCommPcnt
  398. ,psAgentCommMinAmt = main.psAgentCommMinAmt
  399. ,psAgentCommMaxAmt = main.psAgentCommMaxAmt
  400. ,bankCommPcnt = main.bankCommPcnt
  401. ,bankCommMinAmt = main.bankCommMinAmt
  402. ,bankCommMaxAmt = main.bankCommMaxAmt
  403. ,main.createdBy
  404. ,main.createdDate
  405. FROM scDetail main WITH(NOLOCK)
  406. WHERE main.scMasterId = @scMasterId AND ISNULL(main.isDeleted, 'N') <> 'Y'
  407. ORDER BY fromAmt
  408. end
  409. if @ruleType='sc'
  410. begin
  411. SELECT [From Amount] = ISNULL(mode.fromAmt, main.fromAmt)
  412. ,[To Amount] = ISNULL(mode.toAmt, main.toAmt)
  413. ,Percentage = ISNULL(mode.pcnt, main.pcnt)
  414. ,[Min Amount] = ISNULL(mode.minAmt, main.minAmt)
  415. ,[Max Amount] = ISNULL(mode.maxAmt, main.maxAmt)
  416. FROM sscDetail main WITH(NOLOCK)
  417. LEFT JOIN sscDetailHistory mode ON main.sscDetailId = mode.sscDetailId AND mode.approvedBy IS NULL
  418. WHERE main.sscMasterId = @scMasterId AND ISNULL(main.isDeleted, 'N') <> 'Y'
  419. ORDER BY [From Amount]
  420. end
  421. if @ruleType='cp'
  422. begin
  423. SELECT
  424. [From Amount] = main.fromAmt
  425. ,[To Amount] = main.toAmt
  426. ,[Percentage] = main.pcnt
  427. ,[Min Amount] = main.minAmt
  428. ,[Max Amount] = main.maxAmt
  429. FROM scPayDetail main WITH(NOLOCK)
  430. WHERE main.scPayMasterId = @scMasterId
  431. AND ISNULL(main.isDeleted, 'N') <> 'Y'
  432. ORDER BY [From Amount]
  433. end
  434. if @ruleType='cs'
  435. begin
  436. SELECT
  437. [From Amount] = main.fromAmt
  438. ,[To Amount] = main.toAmt
  439. ,[Percentage] = main.pcnt
  440. ,[Min Amount] = main.minAmt
  441. ,[Max Amount] = main.maxAmt
  442. FROM scSendDetail main WITH(NOLOCK)
  443. WHERE main.scSendMasterId = @scMasterId
  444. AND ISNULL(main.isDeleted, 'N') <> 'Y'
  445. ORDER BY [From Amount]
  446. end
  447. END
  448. IF @flag='s'
  449. BEGIN
  450. SET @table = '
  451. (
  452. SELECT
  453. parentId = am.parentId
  454. ,agentId = am.agentId
  455. ,agentCode = am.agentCode
  456. ,mapCodeInt = am.mapCodeInt
  457. ,agentName = am.agentName
  458. ,agentAddress = am.agentAddress
  459. ,agentCity = am.agentCity
  460. ,agentCountry = am.agentCountry
  461. ,isSettlingAgent = am.isSettlingAgent
  462. FROM agentMaster am WITH(NOLOCK)
  463. WHERE ISNULL(am.isDeleted, ''N'') <> ''Y''
  464. AND isnull(am.isActive,''Y'') = ''Y''
  465. AND am.agentType NOT IN (2905,2906)
  466. )x'
  467. IF @sortBy IS NULL
  468. SET @sortBy = 'agentId'
  469. IF @sortOrder IS NULL
  470. SET @sortOrder = 'ASC'
  471. SET @sql_filter = ''
  472. IF @agentId IS NOT NULL
  473. SET @sql_filter = @sql_filter + ' AND ISNULL(parentId, '''') = ''' + CAST(@agentId AS VARCHAR) + ''''
  474. IF @agentCountry IS NOT NULL
  475. SET @sql_filter = @sql_filter + ' AND ISNULL(agentCountry, '''') = ''' + CAST(@agentCountry AS VARCHAR) + ''''
  476. IF @agentName IS NOT NULL
  477. SET @sql_filter = @sql_filter + ' AND ISNULL(agentName, '''') LIKE ''%' + @agentName + '%'''
  478. IF @isSettlingAgent IS NOT NULL
  479. SET @sql_filter = @sql_filter + ' AND ISNULL(isSettlingAgent, ''N'') = ''' + @isSettlingAgent + ''''
  480. SET @select_field_list ='
  481. parentId
  482. ,agentId
  483. ,agentCode
  484. ,mapCodeInt
  485. ,agentName
  486. ,agentAddress
  487. ,agentCity
  488. ,agentCountry
  489. ,isSettlingAgent
  490. '
  491. EXEC dbo.proc_paging
  492. @table
  493. ,@sql_filter
  494. ,@select_field_list
  495. ,@extra_field_list
  496. ,@sortBy
  497. ,@sortOrder
  498. ,@pageSize
  499. ,@pageNumber
  500. END
  501. -- ### END COMMISSION VIEW ### ---
  502. END TRY
  503. BEGIN CATCH
  504. IF @@TRANCOUNT > 0
  505. ROLLBACK TRANSACTION
  506. DECLARE @errorMessage VARCHAR(MAX)
  507. SET @errorMessage = ERROR_MESSAGE()
  508. EXEC proc_errorHandler 1, @errorMessage, @user
  509. END CATCH