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.

504 lines
24 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_moneyGram] 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_moneyGram]
  9. @flag VARCHAR(50) = NULL
  10. ,@user VARCHAR(30) = NULL
  11. ,@id INT = NULL
  12. ,@agent VARCHAR(200) = NULL
  13. ,@controlNo VARCHAR(50) = NULL
  14. ,@recFullName VARCHAR(200) = NULL
  15. ,@sendFullName VARCHAR(200) = NULL
  16. ,@recContactNo VARCHAR(200) = NULL
  17. ,@amount MONEY = NULL
  18. ,@date DATETIME = NULL
  19. ,@location INT = NULL
  20. ,@address VARCHAR(MAX) = NULL
  21. ,@sessionId VARCHAR(100) = NULL
  22. ,@sortBy VARCHAR(50) = NULL
  23. ,@sortOrder VARCHAR(5) = NULL
  24. ,@pageSize INT = NULL
  25. ,@pageNumber INT = NULL
  26. AS
  27. SET NOCOUNT ON;
  28. SET XACT_ABORT ON;
  29. BEGIN TRY
  30. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  31. DECLARE
  32. @sql VARCHAR(MAX)
  33. ,@oldValue VARCHAR(MAX)
  34. ,@newValue VARCHAR(MAX)
  35. ,@module VARCHAR(10)
  36. ,@tableAlias VARCHAR(100)
  37. ,@logIdentifier VARCHAR(50)
  38. ,@logParamMod VARCHAR(100)
  39. ,@logParamMain VARCHAR(100)
  40. ,@table VARCHAR(MAX)
  41. ,@select_field_list VARCHAR(MAX)
  42. ,@extra_field_list VARCHAR(MAX)
  43. ,@sql_filter VARCHAR(MAX)
  44. ,@modType VARCHAR(6)
  45. ,@ApprovedFunctionId INT
  46. SELECT
  47. @ApprovedFunctionId = 20181230
  48. ,@logIdentifier = 'id'
  49. ,@logParamMain = 'moneyGram'
  50. ,@logParamMod = 'moneyGramMod'
  51. ,@module = '20'
  52. ,@tableAlias = 'Money Gram'
  53. IF @flag='SS'--POPULATING AGENT LIST AS LOCATION
  54. BEGIN
  55. select agentId,agentName from
  56. (
  57. select agentId,agentName from agentMaster where agentType =2903 and actAsBranch='y'
  58. union all
  59. select agentId,agentName from agentMaster where agentType=2904
  60. )a order by agentName
  61. END
  62. ELSE IF @flag IN ('s')
  63. BEGIN
  64. SET @table = '(
  65. SELECT
  66. id = ISNULL(mode.id, main.id)
  67. ,agent = ISNULL(mode.agent, main.agent)
  68. ,controlNo = ISNULL(mode.controlNo, main.controlNo)
  69. ,recFullName = ISNULL(mode.recFullName, main.recFullName)
  70. ,sendFullName = ISNULL(mode.sendFullName, main.sendFullName)
  71. ,recContactNo = ISNULL(mode.recContactNo, main.recContactNo)
  72. ,amount = ISNULL(mode.amount, main.amount)
  73. ,tranDate = ISNULL(mode.tranDate, main.tranDate)
  74. ,location = ISNULL(mode.location, main.location)
  75. ,address = ISNULL(mode.address, main.address)
  76. ,main.createdBy
  77. ,main.createdDate
  78. ,modifiedDate = CASE WHEN main.approvedBy IS NULL THEN main.createdDate ELSE ISNULL(mode.createdDate, main.modifiedDate) END
  79. ,modifiedBy = CASE WHEN main.approvedBy IS NULL THEN main.createdBy ELSE ISNULL(mode.createdBy, main.modifiedBy) END
  80. ,hasChanged = CASE WHEN (main.approvedBy IS NULL) OR
  81. (mode.id IS NOT NULL)
  82. THEN ''Y'' ELSE ''N'' END
  83. FROM moneyGram main WITH(NOLOCK)
  84. LEFT JOIN moneyGramMod mode ON main.id = mode.id AND mode.approvedBy IS NULL
  85. AND (
  86. mode.createdBy = ''' + @user + '''
  87. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  88. )
  89. WHERE
  90. ISNULL(mode.sessionId,main.sessionId)=''' + @sessionId + '''
  91. AND ISNULL(mode.createdBy,main.createdBy)=''' + @user + '''
  92. AND ISNULL(main.isDeleted, ''N'') <> ''Y''
  93. AND (
  94. main.approvedBy IS NOT NULL
  95. OR main.createdBy = ''' + @user + '''
  96. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  97. )
  98. ) '
  99. --PRINT (@table)
  100. END
  101. IF @flag = 'i'
  102. BEGIN
  103. if exists(select * from moneyGram where controlNo=@controlNo and isnull(status,'Requested')<>'Rejected')
  104. begin
  105. EXEC proc_errorHandler 1, 'Already Added For This Control Number!', @controlNo
  106. return;
  107. end
  108. if exists(select * from moneyGram where controlNo='MG'+@controlNo and isnull(status,'Requested')<>'Rejected')
  109. begin
  110. EXEC proc_errorHandler 1, 'Already Added For This Control Number!', @controlNo
  111. return;
  112. end
  113. INSERT INTO moneyGram (
  114. agent
  115. ,controlNo
  116. ,recFullName
  117. ,sendFullName
  118. ,recContactNo
  119. ,amount
  120. ,tranDate
  121. ,location
  122. ,address
  123. ,sessionId
  124. ,createdBy
  125. ,createdDate
  126. )
  127. SELECT
  128. @agent
  129. ,@controlNo
  130. ,@recFullName
  131. ,@sendFullName
  132. ,@recContactNo
  133. ,@amount
  134. ,@date
  135. ,@location
  136. ,@address
  137. ,@sessionId
  138. ,@user
  139. ,GETDATE()
  140. SET @id = SCOPE_IDENTITY()
  141. IF @@TRANCOUNT > 0
  142. COMMIT TRANSACTION
  143. EXEC proc_errorHandler 0, 'Record has been added successfully.', @id
  144. END
  145. ELSE IF @flag = 'a'
  146. BEGIN
  147. IF EXISTS (
  148. SELECT 'X' FROM moneyGramMod WITH(NOLOCK)
  149. WHERE id = @id AND createdBy = @user AND approvedBy IS NULL
  150. )
  151. BEGIN
  152. SELECT
  153. id
  154. ,agent
  155. ,controlNo
  156. ,recFullName
  157. ,sendFullName
  158. ,recContactNo
  159. ,dbo.ShowDecimalExceptComma(amount) amount
  160. ,convert(varchar,tranDate,107) tranDate
  161. ,location
  162. ,address
  163. ,sessionId
  164. ,createdBy
  165. ,createdDate
  166. ,modifiedBy
  167. ,modifiedDate
  168. ,isDeleted
  169. FROM moneyGramMod mode WITH(NOLOCK)
  170. WHERE mode.id= @id AND mode.approvedBy IS NULL
  171. END
  172. ELSE
  173. BEGIN
  174. SELECT
  175. id
  176. ,agent
  177. ,controlNo
  178. ,recFullName
  179. ,sendFullName
  180. ,recContactNo
  181. ,dbo.ShowDecimalExceptComma(amount) amount
  182. ,convert(varchar,tranDate,107) tranDate
  183. ,location
  184. ,address
  185. ,sessionId
  186. ,createdBy
  187. ,createdDate
  188. ,modifiedBy
  189. ,modifiedDate
  190. ,isDeleted
  191. FROM moneyGram WITH(NOLOCK) WHERE id = @id
  192. END
  193. END
  194. ELSE IF @flag='VIEW'
  195. BEGIN
  196. SELECT
  197. id
  198. ,agent
  199. ,controlNo
  200. ,recFullName
  201. ,sendFullName
  202. ,recContactNo
  203. ,dbo.ShowDecimal(amount) amount
  204. ,convert(varchar,tranDate,107) tranDate
  205. ,b.agentName location
  206. ,address
  207. ,sessionId
  208. ,a.createdBy
  209. ,convert(varchar,a.createdDate,107) createdDate
  210. ,a.modifiedBy
  211. ,convert(varchar,a.modifiedDate,107) modifiedDate
  212. ,a.isDeleted
  213. ,a.status
  214. ,a.approvedBy
  215. ,convert(varchar,a.approvedDate,107) approvedDate
  216. FROM moneyGram a WITH(NOLOCK) inner join agentMaster b WITH(NOLOCK) on a.location=b.agentId
  217. WHERE id = @id
  218. END
  219. ELSE IF @flag = 'u'
  220. BEGIN
  221. UPDATE moneyGram SET
  222. agent = @agent
  223. ,controlNo = @controlNo
  224. ,recFullName = @recFullName
  225. ,sendFullName = @sendFullName
  226. ,recContactNo = @recContactNo
  227. ,amount=@amount
  228. ,tranDate=@date
  229. ,location=@location
  230. ,address=@address
  231. ,sessionId=@sessionId
  232. ,modifiedBy = @user
  233. ,modifiedDate = GETDATE()
  234. WHERE id = @id
  235. IF @@TRANCOUNT > 0
  236. COMMIT TRANSACTION
  237. EXEC proc_errorHandler 0, 'Record updated successfully.', @id
  238. END
  239. ELSE IF @flag = 'd'
  240. BEGIN
  241. DELETE FROM moneyGram WHERE ID=@id
  242. EXEC proc_errorHandler 0, 'Record deleted successfully.', @id
  243. END
  244. ELSE IF @flag = 'finalSave'
  245. BEGIN
  246. UPDATE moneyGram SET sessionId='',status='Requested',controlNo='MG'+controlNo
  247. WHERE createdBy=@user and sessionId=@sessionId
  248. EXEC proc_errorHandler 0, 'Final Saved successfully.', @id
  249. END
  250. ELSE IF @flag = 's'
  251. BEGIN
  252. IF @sortBy IS NULL
  253. SET @sortBy = 'id'
  254. IF @sortOrder IS NULL
  255. SET @sortOrder = 'ASC'
  256. SET @table = '(
  257. SELECT
  258. main.id
  259. ,main.agent
  260. ,main.controlNo
  261. ,main.recFullName
  262. ,main.sendFullName
  263. ,main.recContactNo
  264. ,main.amount
  265. ,main.tranDate
  266. ,location=am.agentName
  267. ,main.address
  268. ,main.createdBy
  269. ,main.createdDate
  270. ,main.modifiedBy
  271. ,haschanged
  272. FROM agentMaster am
  273. INNER JOIN ' + @table + ' main ON am.agentId = main.location
  274. ) x
  275. '
  276. SET @sql_filter = ''
  277. SET @select_field_list ='
  278. id
  279. ,agent
  280. ,controlNo
  281. ,recFullName
  282. ,sendFullName
  283. ,recContactNo
  284. ,amount
  285. ,tranDate
  286. ,location
  287. ,address
  288. ,createdBy
  289. ,createdDate
  290. ,modifiedBy
  291. ,haschanged
  292. '
  293. EXEC dbo.proc_paging
  294. @table
  295. ,@sql_filter
  296. ,@select_field_list
  297. ,@extra_field_list
  298. ,@sortBy
  299. ,@sortOrder
  300. ,@pageSize
  301. ,@pageNumber
  302. END
  303. ELSE IF @flag = 'reject'
  304. BEGIN
  305. update moneyGram set approvedBy=@user,approvedDate=GETDATE(),status='Rejected' WHERE id = @id
  306. IF @@TRANCOUNT > 0
  307. COMMIT TRANSACTION
  308. EXEC proc_errorHandler 0, 'Changes rejected successfully.', @id
  309. END
  310. --alter table moneyGram add status varchar(50)
  311. ELSE IF @flag = 'approve'
  312. BEGIN
  313. UPDATE moneyGram SET
  314. approvedBy = @user
  315. ,approvedDate= GETDATE()
  316. ,sessionId=''
  317. ,status='Approved'
  318. WHERE id = @id
  319. /*
  320. remitTran updates here...
  321. */
  322. IF @@TRANCOUNT > 0
  323. COMMIT TRANSACTION
  324. EXEC proc_errorHandler 0, 'Changes approved successfully.', @id
  325. END
  326. ELSE IF @flag IN ('appS')
  327. BEGIN
  328. SET @table = '(
  329. SELECT
  330. id = main.id
  331. ,agent = main.agent
  332. ,controlNo = main.controlNo
  333. ,recFullName = main.recFullName
  334. ,sendFullName = main.sendFullName
  335. ,recContactNo = main.recContactNo
  336. ,amount = main.amount
  337. ,tranDate = main.tranDate
  338. ,location = main.location
  339. ,address = main.address
  340. ,status=main.status
  341. ,main.createdBy
  342. ,main.createdDate
  343. ,modifiedBy=isnull(main.modifiedBy,main.createdBy)
  344. ,approvedBy = main.approvedBy
  345. ,approvedDate = main.approvedDate
  346. ,hasChanged = CASE WHEN (main.approvedBy IS NULL) and (main.sessionId='''')
  347. THEN ''Y'' ELSE ''N'' END
  348. FROM moneyGram main WITH(NOLOCK)
  349. WHERE
  350. ISNULL(main.isDeleted, ''N'') <> ''Y''
  351. AND main.sessionId=''''
  352. AND (
  353. main.approvedBy IS NOT NULL
  354. OR main.createdBy = ''' + @user + '''
  355. OR ''Y'' = dbo.FNAHasRight(''' + @user + ''',' + CAST(@ApprovedFunctionId AS VARCHAR) + ')
  356. )
  357. ) '
  358. IF @sortBy IS NULL
  359. SET @sortBy = 'id'
  360. IF @sortOrder IS NULL
  361. SET @sortOrder = 'ASC'
  362. SET @table = '
  363. (SELECT
  364. main.id
  365. ,main.agent
  366. ,main.controlNo
  367. ,main.recFullName
  368. ,main.sendFullName
  369. ,main.recContactNo
  370. ,main.amount
  371. ,main.tranDate
  372. ,location=am.agentName
  373. ,main.address
  374. ,main.status
  375. ,main.createdBy
  376. ,main.createdDate
  377. ,main.modifiedBy
  378. ,main.approvedBy
  379. ,main.approvedDate
  380. ,haschanged
  381. FROM agentMaster am
  382. INNER JOIN ' + @table + ' main ON am.agentId = main.location)y
  383. '
  384. --select @table
  385. --return;
  386. SET @sql_filter = ''
  387. IF @controlNo IS NOT NULL
  388. SET @sql_filter = @sql_filter + ' AND controlNo = ''' + CAST(@controlNo AS VARCHAR)+ ''''
  389. IF @recFullName IS NOT NULL
  390. SET @sql_filter = @sql_filter + ' AND recFullName LIKE ''' + @recFullName + '%'''
  391. IF @sendFullName IS NOT NULL
  392. SET @sql_filter = @sql_filter + ' AND sendFullName LIKE ''' + @sendFullName + '%'''
  393. IF @location IS NOT NULL
  394. SET @sql_filter = @sql_filter + ' AND agent like ''' + @location + '%'''
  395. SET @select_field_list ='
  396. id
  397. ,agent
  398. ,controlNo
  399. ,recFullName
  400. ,sendFullName
  401. ,recContactNo
  402. ,amount
  403. ,tranDate
  404. ,location
  405. ,address
  406. ,status
  407. ,createdBy
  408. ,createdDate
  409. ,modifiedBy
  410. ,approvedBy
  411. ,approvedDate
  412. ,haschanged
  413. '
  414. EXEC dbo.proc_paging
  415. @table
  416. ,@sql_filter
  417. ,@select_field_list
  418. ,@extra_field_list
  419. ,@sortBy
  420. ,@sortOrder
  421. ,@pageSize
  422. ,@pageNumber
  423. END
  424. END TRY
  425. BEGIN CATCH
  426. IF @@TRANCOUNT > 0
  427. ROLLBACK TRANSACTION
  428. DECLARE @errorMessage VARCHAR(MAX)
  429. SET @errorMessage = ERROR_MESSAGE()
  430. EXEC proc_errorHandler 1, @errorMessage, @id
  431. END CATCH
  432. GO