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.

499 lines
28 KiB

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