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.

299 lines
21 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_sendPayTable] 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 procEDURE [dbo].[proc_sendPayTable]
  9. @flag VARCHAR(20) = NULL
  10. ,@user VARCHAR(30) = NULL
  11. ,@rowId INT = NULL
  12. ,@country VARCHAR(30) = NULL
  13. ,@agent VARCHAR(100) = NULL
  14. ,@customerRegistration CHAR(1) = NULL
  15. ,@newCustomer CHAR(1) = NULL
  16. ,@collection CHAR(1) = NULL
  17. ,@id CHAR(1) = NULL
  18. ,@idIssueDate CHAR(1) = NULL
  19. ,@iDValidDate CHAR(1) = NULL
  20. ,@dob CHAR(1) = NULL
  21. ,@address CHAR(1) = NULL
  22. ,@city CHAR(1) = NULL
  23. ,@contact CHAR(1) = NULL
  24. ,@occupation CHAR(1) = NULL
  25. ,@company CHAR(1) = NULL
  26. ,@salaryRange CHAR(1) = NULL
  27. ,@purposeofRemittance CHAR(1) = NULL
  28. ,@sourceofFund CHAR(1) = NULL
  29. ,@rId CHAR(1) = NULL
  30. ,@rPlaceOfIssue CHAR(1) = NULL
  31. ,@raddress CHAR(1) = NULL
  32. ,@rcity CHAR(1) = NULL
  33. ,@rContact CHAR(1) = NULL
  34. ,@rRelationShip CHAR(1) = NULL
  35. ,@rDOB CHAR(1) = NULL
  36. ,@rIdValidDate CHAR(1) = NULL
  37. ,@nativeCountry CHAR(1) = NULL
  38. ,@tXNHistory CHAR(1) = NULL
  39. ,@opeType VARCHAR(4) = NULL
  40. ,@createdBy VARCHAR(30) = NULL
  41. ,@createdDate DATETIME = NULL
  42. ,@modifiedBy VARCHAR(30) = NULL
  43. ,@modifiedDate DATETIME = NULL
  44. ,@sortBy VARCHAR(50) = NULL
  45. ,@sortOrder VARCHAR(5) = NULL
  46. ,@pageSize INT = NULL
  47. ,@pageNumber INT = NULL
  48. AS
  49. BEGIN TRY
  50. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  51. DECLARE
  52. @sql VARCHAR(MAX)
  53. ,@table VARCHAR(MAX)
  54. ,@oldValue VARCHAR(MAX)
  55. ,@newValue VARCHAR(MAX)
  56. ,@module VARCHAR(10)
  57. ,@tableAlias VARCHAR(100)
  58. ,@logIdentifier VARCHAR(50)
  59. ,@logParamMod VARCHAR(100)
  60. ,@logParamMain VARCHAR(100)
  61. ,@select_field_list VARCHAR(MAX)
  62. ,@extra_field_list VARCHAR(MAX)
  63. ,@sql_filter VARCHAR(MAX) = ''
  64. ,@modType VARCHAR(6)
  65. SELECT
  66. @logIdentifier = 'rowId'
  67. ,@tableAlias = ' Send Pay Table'
  68. IF @flag ='s'
  69. BEGIN
  70. IF @sortBy IS NULL
  71. SET @sortBy ='rowId'
  72. SET @table = '
  73. (
  74. SELECT
  75. rowId = rowId,
  76. country = cm.countryName ,
  77. agentName = ISNULL(agentName,''All'') ,
  78. id = id,
  79. idIssueDate = idIssueDate,
  80. salaryRange = salaryRange,
  81. opeType = opeType,
  82. createdBy = spt.createdBy,
  83. createdDate = spt.createdDate
  84. FROM sendPayTable spt WITH(NOLOCK)
  85. LEFT JOIN agentMaster am WITH(NOLOCK) ON am.agentId = spt.agent
  86. LEFT JOIN countryMaster cm WITH(NOLOCK) ON cm.countryId = spt.country
  87. WHERE ISNULL(spt.isDeleted,''N'') = ''N''
  88. ) x'
  89. SET @sql_filter =''
  90. IF @country IS NOT NULL
  91. SET @sql_filter =@sql_filter + ' AND country Like ''%'+@country+'%'''
  92. IF @agent IS NOT NULL
  93. SET @sql_filter =@sql_filter + ' AND agentName Like ''%'+@agent+'%'''
  94. IF @opeType IS NOT NULL
  95. SET @sql_filter =@sql_filter + ' AND opeType = '''+@opeType+''''
  96. SET @select_field_list ='
  97. rowId
  98. ,country
  99. ,agentName
  100. ,id
  101. ,idIssueDate
  102. ,salaryRange
  103. ,opeType
  104. ,createdBy
  105. ,createdDate'
  106. EXEC dbo.proc_paging
  107. @table
  108. ,@sql_filter
  109. ,@select_field_list
  110. ,@extra_field_list
  111. ,@sortBy
  112. ,@sortOrder
  113. ,@pageSize
  114. ,@pageNumber
  115. END
  116. ELSE IF @flag = 'a'
  117. BEGIN
  118. SELECT spt.*, am.agentId, am.agentName, cm.countryId, cm.countryName FROM sendPayTable spt WITH(NOLOCK)
  119. LEFT JOIN agentMaster am WITH(NOLOCK) ON am.agentId = spt.agent
  120. LEFT JOIN countryMaster cm WITH(NOLOCK) ON cm.countryId = spt.country
  121. WHERE rowId = @rowId AND opeType = @opeType
  122. RETURN
  123. END
  124. ELSE IF @flag ='i'
  125. BEGIN
  126. IF EXISTS (SELECT 'X' FROM sendPayTable WHERE country = @country AND opeType = @opeType AND ISNULL(isDeleted,'N') <> 'Y' and ISNULL(agent,1) = ISNULL(@agent,1) )
  127. BEGIN
  128. EXEC proc_errorHandler 1, 'Cannot insert duplicate data', @rowId
  129. RETURN
  130. END
  131. BEGIN TRANSACTION
  132. INSERT INTO sendPayTable(
  133. country, agent, customerRegistration, newCustomer, collection, id, idIssueDate, iDValidDate, dob, address, city, contact,occupation
  134. ,company, salaryRange, purposeofRemittance,sourceofFund, rId, rPlaceOfIssue, raddress, rcity, rContact, rRelationShip, nativeCountry
  135. ,tXNHistory, opeType, createdBy, createdDate, modifiedBy ,modifiedDate ,rDOB,rIdValidDate)
  136. SELECT @country, @agent, @customerRegistration, @newCustomer, @collection, @id, @idIssueDate, @iDValidDate, @dob, @address, @city, @contact, @occupation
  137. ,@company, @salaryRange, @purposeofRemittance, @sourceofFund, @rId, @rPlaceOfIssue, @raddress, @rcity, @rContact, @rRelationShip, @nativeCountry
  138. ,@tXNHistory, @opeType, @user, GETDATE(), @modifiedBy, @modifiedDate ,@rDOB,@rIdValidDate
  139. SET @modType = 'Insert'
  140. SET @rowId = @@IDENTITY
  141. EXEC [dbo].proc_GetColumnToRow @tableAlias, @logIdentifier, @rowId, @newValue OUTPUT
  142. INSERT INTO #msg(errorCode, msg, id)
  143. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @rowId, @user, @oldValue, @newValue
  144. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  145. BEGIN
  146. IF @@TRANCOUNT > 0
  147. ROLLBACK TRANSACTION
  148. EXEC proc_errorHandler 1, 'Failed to update record.', @rowId
  149. RETURN
  150. END
  151. IF @@TRANCOUNT > 0
  152. COMMIT TRANSACTION
  153. EXEC proc_errorHandler 0, 'Record has been added successfully.', @rowId
  154. RETURN
  155. END
  156. ELSE IF @flag = 'u'
  157. BEGIN
  158. BEGIN TRANSACTION
  159. UPDATE sendPayTable SET
  160. country = @country
  161. ,agent = @agent
  162. ,customerRegistration = @customerRegistration
  163. ,newCustomer = @newCustomer
  164. ,collection = @collection
  165. ,id = @id
  166. ,idIssueDate = @idIssueDate
  167. ,iDValidDate = @iDValidDate
  168. ,dob = @dob
  169. ,address = @address
  170. ,city = @city
  171. ,contact = @contact
  172. ,occupation = @occupation
  173. ,company = @company
  174. ,salaryRange = @salaryRange
  175. ,purposeofRemittance = @purposeofRemittance
  176. ,sourceofFund = @sourceofFund
  177. ,rId = @rId
  178. ,rPlaceOfIssue = @rPlaceOfIssue
  179. ,raddress = @raddress
  180. ,rcity = @rcity
  181. ,rContact = @rContact
  182. ,rRelationShip = @rRelationShip
  183. ,rDOB = @rDOB
  184. ,rIdValidDate = @rIdValidDate
  185. ,nativeCountry = @nativeCountry
  186. ,tXNHistory = @tXNHistory
  187. ,opeType = @opeType
  188. ,modifiedBy = @user
  189. ,modifiedDate = GETDATE()
  190. WHERE rowId = @rowId
  191. SET @modType = 'Update'
  192. EXEC [dbo].proc_GetColumnToRow @tableAlias, @logIdentifier, @rowId, @newValue OUTPUT
  193. INSERT INTO #msg(errorCode, msg, id)
  194. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @rowId, @user, @oldValue, @newValue
  195. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  196. BEGIN
  197. IF @@TRANCOUNT > 0
  198. ROLLBACK TRANSACTION
  199. EXEC proc_errorHandler 1, 'Failed to update record.', @rowId
  200. RETURN
  201. END
  202. IF @@TRANCOUNT > 0
  203. COMMIT TRANSACTION
  204. EXEC proc_errorHandler 0, 'Record has been updated successfully.', @rowId
  205. RETURN
  206. END
  207. ELSE IF @flag ='copy'
  208. BEGIN
  209. IF EXISTS (SELECT 'X' FROM sendPayTable WHERE country = @country AND opeType = @opeType AND ISNULL(isDeleted,'N') <> 'Y' and ISNULL(agent,1) = ISNULL(@agent,1) )
  210. BEGIN
  211. EXEC proc_errorHandler 1, 'Cannot insert duplicate data', @rowId
  212. RETURN
  213. END
  214. BEGIN TRANSACTION
  215. INSERT INTO sendPayTable(
  216. country, agent, customerRegistration, newCustomer, collection, id, idIssueDate, iDValidDate, dob, address, city, contact,occupation
  217. ,company, salaryRange, purposeofRemittance,sourceofFund, rId, rPlaceOfIssue, raddress, rcity, rContact, rRelationShip, nativeCountry
  218. ,tXNHistory, opeType, createdBy, createdDate, modifiedBy ,modifiedDate ,rDOB,rIdValidDate)
  219. SELECT @country, @agent, @customerRegistration, @newCustomer, @collection, @id, @idIssueDate, @iDValidDate, @dob, @address, @city, @contact, @occupation
  220. ,@company, @salaryRange, @purposeofRemittance, @sourceofFund, @rId, @rPlaceOfIssue, @raddress, @rcity, @rContact, @rRelationShip, @nativeCountry
  221. ,@tXNHistory, @opeType, @user, GETDATE(), @modifiedBy, @modifiedDate ,@rDOB,@rIdValidDate
  222. SET @modType = 'Insert'
  223. SET @rowId = @@IDENTITY
  224. EXEC [dbo].proc_GetColumnToRow @tableAlias, @logIdentifier, @rowId, @newValue OUTPUT
  225. INSERT INTO #msg(errorCode, msg, id)
  226. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @rowId, @user, @oldValue, @newValue
  227. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  228. BEGIN
  229. IF @@TRANCOUNT > 0
  230. ROLLBACK TRANSACTION
  231. EXEC proc_errorHandler 1, 'Failed to update record.', @rowId
  232. RETURN
  233. END
  234. IF @@TRANCOUNT > 0
  235. COMMIT TRANSACTION
  236. EXEC proc_errorHandler 0, 'Record has been copied successfully.', @rowId
  237. RETURN
  238. END
  239. ELSE IF @flag ='d'
  240. BEGIN
  241. BEGIN TRANSACTION
  242. UPDATE sendPayTable SET
  243. isDeleted = 'Y'
  244. ,modifiedBy = @user
  245. ,modifiedDate = GETDATE()
  246. WHERE rowId = @rowId
  247. SET @modType = 'Delete'
  248. EXEC [dbo].proc_GetColumnToRow @tableAlias, @logIdentifier, @rowId, @newValue OUTPUT
  249. INSERT INTO #msg(errorCode, msg, id)
  250. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @rowId, @user, @oldValue, @newValue
  251. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  252. BEGIN
  253. IF @@TRANCOUNT > 0
  254. ROLLBACK TRANSACTION
  255. EXEC proc_errorHandler 1, 'Failed to delete record.', @rowId
  256. RETURN
  257. END
  258. IF @@TRANCOUNT > 0
  259. COMMIT TRANSACTION
  260. EXEC proc_errorHandler 0, 'Record has been deleted successfully.', @rowId
  261. RETURN
  262. END
  263. END TRY
  264. BEGIN CATCH
  265. IF @@TRANCOUNT > 0
  266. ROLLBACK TRANSACTION
  267. DECLARE @errorMessage VARCHAR(MAX)
  268. SET @errorMessage = ERROR_MESSAGE()
  269. EXEC proc_errorHandler 1, @errorMessage, @rowId
  270. END CATCH
  271. GO