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.

468 lines
31 KiB

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