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.

527 lines
25 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_crossExchangeRate] 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. EXEC proc_crossExchangeRate
  10. @ssAgent = 53
  11. ,@sCountry = 3
  12. ,@sAgent = 59
  13. ,@sBranch = 78
  14. ,@rsAgent = 3
  15. ,@rCountry = 1
  16. ,@rAgent = 9
  17. ,@rBranch = 19
  18. ,@listType = 'd'
  19. SELECT * FROM seRate
  20. */
  21. CREATE proc [dbo].[proc_crossExchangeRate]
  22. @user VARCHAR(30)
  23. ,@ssAgent INT = NULL
  24. ,@sCountry INT = NULL
  25. ,@sAgent INT = NULL
  26. ,@sBranch INT = NULL
  27. ,@rsAgent INT = NULL
  28. ,@rCountry INT = NULL
  29. ,@rAgent INT = NULL
  30. ,@rBranch INT = NULL
  31. ,@listType CHAR(1) = NULL
  32. /* 'd': no level
  33. 'a': agentLevel
  34. 'c': countryLevel
  35. 'b': branchLevel
  36. */
  37. AS
  38. SET NOCOUNT ON
  39. BEGIN TRY
  40. DECLARE @agentList TABLE (
  41. Id INT IDENTITY(1, 1)
  42. ,sCountry INT
  43. ,sAgent INT
  44. ,sBranch INT
  45. ,rCountry INT
  46. ,rAgent INT
  47. ,rBranch INT
  48. )
  49. DECLARE @agentList2 TABLE (
  50. Id INT IDENTITY(1, 1)
  51. ,sCountry INT
  52. ,sAgent INT
  53. ,sBranch INT
  54. ,rCountry INT
  55. ,rAgent INT
  56. ,rBranch INT
  57. ,sending INT
  58. ,receiving INT
  59. ,sType CHAR(1)
  60. ,rType CHAR(1)
  61. ,collCurr INT
  62. ,pCurr INT
  63. )
  64. IF @listType = 'd'
  65. BEGIN
  66. INSERT @agentList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch)
  67. SELECT @sCountry, @sAgent, @sBranch, @rCountry, @rAgent, @rBranch
  68. END
  69. ELSE IF @listType = 'c'
  70. BEGIN
  71. IF @sCountry IS NULL
  72. BEGIN
  73. EXEC proc_errorHandler 1, 'You must define sending country.', NULL
  74. RETURN
  75. END
  76. IF @rsAgent IS NULL
  77. BEGIN
  78. EXEC proc_errorHandler 1, 'You must define receiving super agent.', NULL
  79. RETURN
  80. END
  81. INSERT @agentList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch)
  82. SELECT
  83. DISTINCT
  84. @sCountry, @sAgent, @sBranch, am.agentCountryId, NULL, NULL
  85. FROM agentMaster am WITH(NOLOCK) WHERE parentId = @rsAgent
  86. END
  87. ELSE IF @listType = 'a'
  88. BEGIN
  89. IF @sCountry IS NULL
  90. BEGIN
  91. EXEC proc_errorHandler 1, 'You must define sending country.', NULL
  92. RETURN
  93. END
  94. IF @rsAgent IS NULL
  95. BEGIN
  96. EXEC proc_errorHandler 1, 'You must define receiving super agent.', NULL
  97. RETURN
  98. END
  99. IF @rCountry IS NOT NULL AND @rsAgent IS NOT NULL
  100. BEGIN
  101. INSERT @agentList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch)
  102. SELECT
  103. DISTINCT
  104. @sCountry, @sAgent, @sBranch, am.agentCountryId, am.agentId, NULL
  105. FROM agentMaster am WITH(NOLOCK) WHERE agentCountryId = @rCountry AND parentId = @rsAgent
  106. END
  107. ELSE IF @rsAgent IS NOT NULL
  108. BEGIN
  109. INSERT @agentList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch)
  110. SELECT
  111. DISTINCT
  112. @sCountry, @sAgent, @sBranch, am.agentCountryId, am.agentId, NULL
  113. FROM agentMaster am WITH(NOLOCK) WHERE parentId = @rsAgent
  114. END
  115. END
  116. ELSE IF @listType = 'b'
  117. BEGIN
  118. IF @sCountry IS NULL
  119. BEGIN
  120. EXEC proc_errorHandler 1, 'You must define sending country.', NULL
  121. RETURN
  122. END
  123. IF @rAgent IS NULL
  124. BEGIN
  125. EXEC proc_errorHandler 1, 'You must define receiving agent.', NULL
  126. RETURN
  127. END
  128. INSERT @agentList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch)
  129. SELECT
  130. DISTINCT
  131. @sCountry, @sAgent, @sBranch, am.agentCountryId, am.parentId, am.agentId
  132. FROM agentMaster am WITH(NOLOCK) WHERE parentId = @rAgent
  133. END
  134. DECLARE @Id INT, @max INT
  135. SELECT
  136. @max = MAX(Id)
  137. FROM @agentList
  138. SET @Id = 1
  139. WHILE @Id <= @max
  140. BEGIN
  141. SELECT
  142. @sCountry = sCountry
  143. ,@sAgent = sAgent
  144. ,@sBranch = sBranch
  145. ,@rCountry = rCountry
  146. ,@rAgent = rAgent
  147. ,@rBranch = rBranch
  148. FROM @agentList WHERE Id = @Id
  149. IF @sAgent IS NOT NULL AND @rAgent IS NOT NULL
  150. BEGIN
  151. INSERT @agentList2 (
  152. sCountry
  153. ,sAgent
  154. ,sBranch
  155. ,rCountry
  156. ,rAgent
  157. ,rBranch
  158. ,sending
  159. ,receiving
  160. ,sType
  161. ,rType
  162. ,collCurr
  163. ,pCurr
  164. )
  165. SELECT
  166. DISTINCT
  167. @sCountry
  168. ,@sAgent
  169. ,@sBranch
  170. ,@rCountry
  171. ,@rAgent
  172. ,@rBranch
  173. ,COALESCE(@sBranch, @sAgent, @sCountry)
  174. ,COALESCE(@rBranch, @rAgent, @rCountry)
  175. ,CASE WHEN @sBranch IS NOT NULL THEN 'B' WHEN @sAgent IS NOT NULL THEN 'A' WHEN @sCountry IS NOT NULL THEN 'C' END
  176. ,CASE WHEN @rBranch IS NOT NULL THEN 'B' WHEN @rAgent IS NOT NULL THEN 'A' WHEN @rCountry IS NOT NULL THEN 'C' END
  177. ,curr.collCurr
  178. ,curr.pCurr
  179. FROM @agentList am
  180. INNER JOIN (
  181. SELECT
  182. --DISTINCT
  183. sAgent = @sAgent
  184. ,rAgent = @rAgent
  185. ,collCurr = x.currencyId
  186. ,pCurr = y.currencyId
  187. FROM (
  188. SELECT
  189. currencyId
  190. FROM agentCurrency WHERE agentId = @sAgent AND (spFlag = 5200 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  191. ) x
  192. CROSS JOIN (
  193. SELECT
  194. currencyId
  195. FROM agentCurrency WHERE agentId = @rAgent AND (spFlag = 5201 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  196. ) y
  197. ) curr ON am.sAgent = curr.sAgent
  198. END
  199. ELSE IF @sAgent IS NOT NULL AND @rCountry IS NOT NULL
  200. BEGIN
  201. INSERT @agentList2 (
  202. sCountry
  203. ,sAgent
  204. ,sBranch
  205. ,rCountry
  206. ,rAgent
  207. ,rBranch
  208. ,sending
  209. ,receiving
  210. ,sType
  211. ,rType
  212. ,collCurr
  213. ,pCurr
  214. )
  215. SELECT
  216. DISTINCT
  217. @sCountry
  218. ,@sAgent
  219. ,@sBranch
  220. ,@rCountry
  221. ,@rAgent
  222. ,@rBranch
  223. ,COALESCE(@sBranch, @sAgent, @sCountry)
  224. ,COALESCE(@rBranch, @rAgent, @rCountry)
  225. ,CASE WHEN @sBranch IS NOT NULL THEN 'B' WHEN @sAgent IS NOT NULL THEN 'A' WHEN @sCountry IS NOT NULL THEN 'C' END
  226. ,CASE WHEN @rBranch IS NOT NULL THEN 'B' WHEN @rAgent IS NOT NULL THEN 'A' WHEN @rCountry IS NOT NULL THEN 'C' END
  227. ,curr.collCurr
  228. ,curr.pCurr
  229. FROM @agentList am
  230. INNER JOIN (
  231. SELECT
  232. --DISTINCT
  233. sAgent = @sAgent
  234. ,rCountry = @rCountry
  235. ,collCurr = x.currencyId
  236. ,pCurr = y.currencyId
  237. FROM (
  238. SELECT
  239. currencyId
  240. FROM agentCurrency WHERE agentId = @sAgent AND (spFlag = 5200 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  241. ) x
  242. CROSS JOIN (
  243. SELECT
  244. currencyId
  245. FROM countryCurrency WHERE countryId = @rCountry AND (spFlag = 5201 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  246. ) y
  247. ) curr ON am.sAgent = curr.sAgent
  248. END
  249. ELSE IF @sCountry IS NOT NULL AND @rAgent IS NOT NULL
  250. BEGIN
  251. INSERT @agentList2 (
  252. sCountry
  253. ,sAgent
  254. ,sBranch
  255. ,rCountry
  256. ,rAgent
  257. ,rBranch
  258. ,sending
  259. ,receiving
  260. ,sType
  261. ,rType
  262. ,collCurr
  263. ,pCurr
  264. )
  265. SELECT
  266. DISTINCT
  267. @sCountry
  268. ,@sAgent
  269. ,@sBranch
  270. ,@rCountry
  271. ,@rAgent
  272. ,@rBranch
  273. ,COALESCE(@sBranch, @sAgent, @sCountry)
  274. ,COALESCE(@rBranch, @rAgent, @rCountry)
  275. ,CASE WHEN @sBranch IS NOT NULL THEN 'B' WHEN @sAgent IS NOT NULL THEN 'A' WHEN @sCountry IS NOT NULL THEN 'C' END
  276. ,CASE WHEN @rBranch IS NOT NULL THEN 'B' WHEN @rAgent IS NOT NULL THEN 'A' WHEN @rCountry IS NOT NULL THEN 'C' END
  277. ,curr.collCurr
  278. ,curr.pCurr
  279. FROM @agentList am
  280. INNER JOIN (
  281. SELECT
  282. --DISTINCT
  283. sCountry = @sCountry
  284. ,rAgent = @rAgent
  285. ,collCurr = x.currencyId
  286. ,pCurr = y.currencyId
  287. FROM (
  288. SELECT
  289. currencyId
  290. FROM countryCurrency WHERE countryId = @sCountry AND (spFlag = 5201 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  291. ) x
  292. CROSS JOIN (
  293. SELECT
  294. currencyId
  295. FROM agentCurrency WHERE agentId = @rAgent AND (spFlag = 5200 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  296. ) y
  297. ) curr ON am.sCountry = curr.sCountry
  298. END
  299. ELSE IF @sCountry IS NOT NULL AND @rCountry IS NOT NULL
  300. BEGIN
  301. INSERT @agentList2 (
  302. sCountry
  303. ,sAgent
  304. ,sBranch
  305. ,rCountry
  306. ,rAgent
  307. ,rBranch
  308. ,sending
  309. ,receiving
  310. ,sType
  311. ,rType
  312. ,collCurr
  313. ,pCurr
  314. )
  315. SELECT
  316. DISTINCT
  317. @sCountry
  318. ,@sAgent
  319. ,@sBranch
  320. ,@rCountry
  321. ,@rAgent
  322. ,@rBranch
  323. ,COALESCE(@sBranch, @sAgent, @sCountry)
  324. ,COALESCE(@rBranch, @rAgent, @rCountry)
  325. ,CASE WHEN @sBranch IS NOT NULL THEN 'B' WHEN @sAgent IS NOT NULL THEN 'A' WHEN @sCountry IS NOT NULL THEN 'C' END
  326. ,CASE WHEN @rBranch IS NOT NULL THEN 'B' WHEN @rAgent IS NOT NULL THEN 'A' WHEN @rCountry IS NOT NULL THEN 'C' END
  327. ,curr.collCurr
  328. ,curr.pCurr
  329. FROM @agentList am
  330. INNER JOIN (
  331. SELECT
  332. --DISTINCT
  333. sCountry = @sCountry
  334. ,rCountry = @rCountry
  335. ,collCurr = x.currencyId
  336. ,pCurr = y.currencyId
  337. FROM (
  338. SELECT
  339. currencyId
  340. FROM countryCurrency WHERE countryId = @sCountry AND (spFlag = 5201 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  341. ) x
  342. CROSS JOIN (
  343. SELECT
  344. currencyId
  345. FROM countryCurrency WHERE countryId = @rCountry AND (spFlag = 5201 OR spFlag IS NULL) AND ISNULL(isDeleted, 'N') = 'N'
  346. ) y
  347. ) curr ON am.sCountry = curr.sCountry
  348. END
  349. SET @Id = @Id + 1
  350. END
  351. DECLARE @eList TABLE (
  352. sCountry INT
  353. ,sAgent INT
  354. ,sBranch INT
  355. ,rCountry INT
  356. ,rAgent INT
  357. ,rBranch INT
  358. ,sCost MONEY
  359. ,sMargin MONEY
  360. ,sAgentMargin MONEY
  361. ,sNet MONEY
  362. ,rCost MONEY
  363. ,rMargin MONEY
  364. ,rAgentMargin MONEY
  365. ,rNet MONEY
  366. ,crossRate MONEY
  367. ,collCurr INT
  368. ,payCurr INT
  369. )
  370. DECLARE
  371. @sending INT
  372. ,@receiving INT
  373. ,@sType CHAR(1)
  374. ,@rType CHAR(1)
  375. ,@collCurr INT
  376. ,@pCurr INT
  377. ,@pCost MONEY
  378. ,@pMargin MONEY
  379. ,@pAgentMargin MONEY
  380. ,@pVe MONEY
  381. ,@pNe MONEY
  382. ,@sCost MONEY
  383. ,@sMargin MONEY
  384. ,@sAgentMargin MONEY
  385. ,@sVe MONEY
  386. ,@sNe MONEY
  387. ,@crossRate MONEY
  388. SELECT
  389. @max = MAX(Id)
  390. FROM @agentList2
  391. SET @Id = 1
  392. WHILE @Id <= @max
  393. BEGIN
  394. SELECT
  395. @sCountry = sCountry
  396. ,@sAgent = sAgent
  397. ,@sBranch = sBranch
  398. ,@rCountry = rCountry
  399. ,@rAgent = rAgent
  400. ,@rBranch = rBranch
  401. ,@sending = sending
  402. ,@receiving = receiving
  403. ,@collCurr = collCurr
  404. ,@pCurr = pCurr
  405. ,@sType = sType
  406. ,@rType = rType
  407. FROM @agentList2 WHERE Id = @Id
  408. SELECT
  409. @pCost = pCost
  410. ,@pMargin = pMargin
  411. ,@pAgentMargin = pAgentMargin
  412. ,@pVe = pVe
  413. ,@pNe = pNe
  414. ,@sCost = sCost
  415. ,@sMargin = sMargin
  416. ,@sAgentMargin = sAgentMargin
  417. ,@sVe = sVe
  418. ,@sNe = sNe
  419. ,@crossRate = crossRate
  420. FROM [dbo].FNAGetEchangeRate(@ssAgent, @sending, @rsAgent, @receiving, @collCurr, @pCurr, @sType, @rType, 'N', @user)
  421. INSERT INTO @eList(sCountry, sAgent, sBranch, rCountry, rAgent, rBranch
  422. ,sCost, sMargin, sAgentMargin, sNet, rCost, rMargin
  423. ,rAgentMargin, rNet, crossRate, collCurr, payCurr
  424. )
  425. SELECT @sCountry, @sAgent, @sBranch, @rCountry, @rAgent, @rBranch
  426. ,@sCost, @sMargin, @sAgentMargin, (@sCost + @sMargin + @sAgentMargin), @pCost, @pMargin
  427. ,@pAgentMargin, (@pCost - @pMargin - @pAgentMargin), @crossRate, @collCurr, @pCurr
  428. SET @Id = @Id + 1
  429. END
  430. SELECT
  431. el.sCountry
  432. ,sCountryName = sc.countryName + ISNULL('' + sa.agentName, '') + ISNULL('' + sb.agentName, '') --+ '(' + coll.currencyCode + ')'
  433. ,el.sAgent
  434. ,sAgentName = sa.agentName
  435. ,el.sBranch
  436. ,sBranchName = sb.agentName
  437. ,el.rCountry
  438. ,rCountryName = rc.countryName + ISNULL('' + ra.agentName, '') + ISNULL('' + rb.agentName, '') --+ '(' + pay.currencyCode + ')'
  439. ,el.rAgent
  440. ,sAgentName = ra.agentName
  441. ,el.rBranch
  442. ,rBranchName = rb.agentName
  443. ,el.sCost
  444. ,el.sMargin
  445. ,el.sAgentMargin
  446. ,el.sNet
  447. ,el.rCost
  448. ,el.rMargin
  449. ,el.rAgentMargin
  450. ,el.rNet
  451. ,el.crossRate
  452. ,el.collCurr
  453. ,el.payCurr
  454. ,collCurrName = coll.currencyCode
  455. ,payCurrName = pay.currencyCode
  456. FROM @eList el
  457. LEFT JOIN countryMaster sc WITH(NOLOCK) ON sc.countryId = el.sCountry
  458. LEFT JOIN agentMaster sa WITH(NOLOCK) ON el.sAgent = sa.agentId
  459. LEFT JOIN agentMaster sb WITH(NOLOCK) ON el.sBranch = sb.agentId
  460. LEFT JOIN countryMaster rc WITH(NOLOCK) ON rc.countryId = el.rCountry
  461. LEFT JOIN agentMaster ra WITH(NOLOCK) ON el.rAgent = ra.agentId
  462. LEFT JOIN agentMaster rb WITH(NOLOCK) ON el.rBranch = rb.agentId
  463. LEFT JOIN currencyMaster coll WITH(NOLOCK) ON el.collCurr = coll.currencyId
  464. LEFT JOIN currencyMaster pay WITH(NOLOCK) ON el.payCurr = pay.currencyId
  465. END TRY
  466. BEGIN CATCH
  467. DECLARE @errMsg VARCHAR(500)
  468. SET @errMsg = ERROR_MESSAGE() + ' : ' + CAST(ERROR_LINE() AS VARCHAR(50))
  469. EXEC proc_errorHandler 1, @errMsg, NULL
  470. END CATCH
  471. GO