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.

330 lines
18 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentProfileUpdate] 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_agentProfileUpdate]
  9. @flag varchar(50) = NULL
  10. ,@user varchar(50) = NULL
  11. ,@agentId VARCHAR(30) = NULL
  12. ,@authorizePerson VARCHAR(100) = NULL
  13. ,@contactPerson VARCHAR(100) = NULL
  14. ,@agentPhone1 VARCHAR(50) = NULL
  15. ,@agentPhone2 VARCHAR(50) = NULL
  16. ,@agentMobile1 VARCHAR(50) = NULL
  17. ,@agentMobile2 VARCHAR(50) = NULL
  18. ,@agentFax1 VARCHAR(50) = NULL
  19. ,@agentFax2 VARCHAR(50) = NULL
  20. ,@address1 VARCHAR(50) = NULL
  21. ,@address2 VARCHAR(50) = NULL
  22. ,@agentEmail1 VARCHAR(100) = NULL
  23. ,@agentEmail2 VARCHAR(100) = NULL
  24. ,@latitude VARCHAR(100) = NULL
  25. ,@longitude VARCHAR(100) = NULL
  26. ,@sortBy VARCHAR(50) = NULL
  27. ,@sortOrder VARCHAR(5) = NULL
  28. ,@pageSize INT = NULL
  29. ,@pageNumber INT = NULL
  30. ,@agentName VARCHAR(200) = NULL
  31. ,@id INT = NULL
  32. ,@rptType CHAR(2) = NULL
  33. ,@fromDate VARCHAR(20) = NULL
  34. ,@toDate VARCHAR(20) = NULL
  35. AS
  36. SET NOCOUNT ON
  37. SET XACT_ABORT ON
  38. BEGIN TRY
  39. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), membershipId INT)
  40. DECLARE
  41. @sql VARCHAR(MAX)
  42. ,@oldValue VARCHAR(MAX)
  43. ,@newValue VARCHAR(MAX)
  44. ,@module VARCHAR(10)
  45. ,@tableAlias VARCHAR(100)
  46. ,@logIdentifier VARCHAR(50)
  47. ,@logParamMod VARCHAR(100)
  48. ,@logParamMain VARCHAR(100)
  49. ,@table VARCHAR(MAX)
  50. ,@select_field_list VARCHAR(MAX)
  51. ,@extra_field_list VARCHAR(MAX)
  52. ,@sql_filter VARCHAR(MAX)
  53. ,@modType VARCHAR(6)
  54. ,@errorMsg VARCHAR(MAX)
  55. IF @flag = 'i'
  56. BEGIN
  57. IF EXISTS(SELECT 'x' FROM agentProfileUpdate WITH(NOLOCK) WHERE agentid = @agentId AND approvedDate IS NOT NULL)
  58. BEGIN
  59. EXEC proc_errorHandler 1, 'Your profile has been already approved.', NULL
  60. RETURN
  61. END
  62. IF NOT EXISTS(SELECT 'x' FROM agentProfileUpdate WITH(NOLOCK) WHERE agentid = @agentId)
  63. BEGIN
  64. INSERT INTO agentProfileUpdate(
  65. agentid
  66. ,authorizePerson
  67. ,contactPerson
  68. ,Phone1
  69. ,Phone2
  70. ,Mobile1
  71. ,Mobile2
  72. ,Fax1
  73. ,Fax2
  74. ,address1
  75. ,address2
  76. ,Email1
  77. ,Email2
  78. ,latitude
  79. ,longitude
  80. ,createdBy
  81. ,createdDate
  82. )
  83. SELECT
  84. @agentId
  85. ,@authorizePerson
  86. ,@contactPerson
  87. ,@agentPhone1
  88. ,@agentPhone2
  89. ,@agentMobile1
  90. ,@agentMobile2
  91. ,@agentFax1
  92. ,@agentFax2
  93. ,@address1
  94. ,@address2
  95. ,@agentEmail1
  96. ,@agentEmail2
  97. ,@latitude
  98. ,@longitude
  99. ,@user
  100. ,GETDATE()
  101. EXEC proc_errorHandler 0, 'Thank you very much, Your profile has been updated successfully.', NULL
  102. RETURN
  103. END
  104. ELSE
  105. BEGIN
  106. UPDATE agentProfileUpdate SET
  107. authorizePerson = @authorizePerson
  108. ,contactPerson = @contactPerson
  109. ,phone1 = @agentPhone1
  110. ,phone2 = @agentPhone2
  111. ,mobile1 = @agentMobile1
  112. ,mobile2 = @agentMobile2
  113. ,fax1 = @agentFax1
  114. ,fax2 = @agentFax2
  115. ,email1 = @agentEmail1
  116. ,email2 = @agentEmail2
  117. ,address1 = @address1
  118. ,address2 = @address2
  119. ,latitude = @latitude
  120. ,longitude = @longitude
  121. ,modifiedBy = @user
  122. ,modifiedDate = GETDATE()
  123. WHERE agentid = @agentId
  124. EXEC proc_errorHandler 0, 'Profile has been updated successfully.', NULL
  125. RETURN
  126. END
  127. END
  128. IF @flag ='a'
  129. BEGIN
  130. select * from agentProfileUpdate where agentid = @agentId
  131. END
  132. IF @flag ='s'
  133. BEGIN
  134. IF @sortBy IS NULL
  135. SET @sortBy = 'id'
  136. IF @sortOrder IS NULL
  137. SET @sortOrder = 'ASC'
  138. SET @table = '(
  139. SELECT
  140. id,
  141. p.agentId,
  142. am.agentName,
  143. p.authorizePerson,
  144. p.phone1,
  145. p.mobile1,
  146. p.email1,
  147. p.address1,
  148. p.createdBy,
  149. p.createdDate
  150. FROM agentProfileUpdate p WITH(NOLOCK) INNER JOIN agentMaster am with(nolock) on p.agentid = am.agentId
  151. WHERE p.approvedDate IS NULL
  152. ) x'
  153. SET @sql_filter = ''
  154. IF @agentName is not null
  155. SET @sql_filter = @sql_filter + ' AND agentName LIKE ''%' + @agentName + '%'''
  156. SET @select_field_list ='
  157. id,
  158. agentId,
  159. agentName,
  160. authorizePerson,
  161. phone1,
  162. mobile1,
  163. email1,
  164. address1,
  165. createdBy,
  166. createdDate '
  167. EXEC dbo.proc_paging
  168. @table
  169. ,@sql_filter
  170. ,@select_field_list
  171. ,@extra_field_list
  172. ,@sortBy
  173. ,@sortOrder
  174. ,@pageSize
  175. ,@pageNumber
  176. END
  177. IF @flag ='approve'
  178. BEGIN
  179. if not exists(select 'x'
  180. from agentProfileUpdate with(nolock) where id = @id and approvedDate is null)
  181. BEGIN
  182. EXEC proc_errorHandler 1, 'Record not found.', NULL
  183. RETURN
  184. END
  185. --alter table agentMaster add latitude VARCHAR(100),longitude VARCHAR(100),agentAddress2 VARCHAR(MAX)
  186. update main set
  187. contactPerson1 = mode.authorizePerson,
  188. contactPerson2 = mode.contactperson,
  189. agentPhone1 = mode.phone1,
  190. agentPhone2 = mode.phone2,
  191. agentMobile1 = mode.mobile1,
  192. agentMobile2 = mode.mobile2,
  193. agentFax1 = mode.fax1,
  194. agentFax2 = mode.fax2,
  195. agentEmail1 = mode.email1,
  196. agentEmail2 = mode.email2,
  197. agentAddress = mode.address1,
  198. agentAddress2 = mode.address2,
  199. latitude = mode.latitude,
  200. longitude = mode.longitude,
  201. modifiedby = mode.createdby,
  202. modifiedDate = mode.createdDate,
  203. approvedBy = @user,
  204. approvedDate = GETDATE()
  205. FROM agentMaster main
  206. INNER JOIN agentProfileUpdate mode ON main.agentId = mode.agentId
  207. WHERE mode.id = @id
  208. UPDATE agentProfileUpdate SET approvedBy = @user, approvedDate = GETDATE() WHERE id = @id
  209. EXEC proc_errorHandler 0, 'Record has been approved successfully.', @id
  210. END
  211. IF @flag = 'check-update'
  212. BEGIN
  213. DECLARE @hasRight CHAR(1)
  214. SET @hasRight = DBO.FNAHasRight(@user,'40133800')
  215. IF @hasRight ='Y'
  216. BEGIN
  217. IF EXISTS(SELECT 'x' FROM agentProfileUpdate WITH(NOLOCK) WHERE agentId = @agentId)
  218. BEGIN
  219. SELECT @agentId agentId
  220. RETURN;
  221. END
  222. ELSE
  223. BEGIN
  224. DECLARE @date DATETIME
  225. SELECT @date = approvedDate FROM agentMaster am WITH(NOLOCK) WHERE agentId = @agentId
  226. IF @date > '2015-07-01'
  227. BEGIN
  228. SELECT @agentId agentId
  229. RETURN;
  230. END
  231. SELECT 'NULL' agentId
  232. RETURN;
  233. END
  234. END
  235. ELSE
  236. BEGIN
  237. SELECT '1001' agentId
  238. RETURN;
  239. END
  240. END
  241. IF @flag = 'rpt'
  242. BEGIN
  243. if @rptType <> 'nu'
  244. BEGIN
  245. SET @sql='SELECT
  246. [Agent Id] = p.agentId,
  247. [Agent Name] = am.agentName,
  248. [Authorized Person] = p.authorizePerson,
  249. [Phone1] = p.phone1,
  250. [Mobile1] = p.mobile1,
  251. [Email1] = p.email1,
  252. [Address] = p.address1,
  253. [Created By] = p.createdBy,
  254. [Created Date] = p.createdDate,
  255. [Approved By] = p.approvedBy,
  256. [Approved Date] = p.approvedDate
  257. FROM agentProfileUpdate p WITH(NOLOCK) INNER JOIN agentMaster am with(nolock) on p.agentid = am.agentId
  258. WHERE P.createdDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  259. IF @agentId IS NOT NULL
  260. SET @sql = @sql+ ' AND p.agentId = '''+@agentId+''''
  261. IF @rptType = 'up'
  262. SET @sql = @sql+ ' AND p.approvedDate is null'
  263. IF @rptType = 'ua'
  264. SET @sql = @sql+ ' AND p.approvedDate is not null'
  265. EXEC(@SQL)
  266. END
  267. ELSE
  268. BEGIN
  269. SELECT agentId INTO #TEMP
  270. FROM agentMaster a WITH(NOLOCK)
  271. WHERE agentCountry = 'Nepal'
  272. AND ((actAsBranch = 'Y' and agentType = 2903) OR agentType = 2904)
  273. AND ISNULL(a.isDeleted, 'N') = 'N'
  274. AND ISNULL(a.isActive, 'N') = 'Y'
  275. DELETE FROM #TEMP
  276. FROM #TEMP T
  277. INNER JOIN agentProfileUpdate L WITH(NOLOCK) ON T.agentId = L.agentId
  278. SELECT
  279. [Agent Id] = am.agentId
  280. ,[Agent Name] = am.agentName
  281. ,[Zone] = agentState
  282. ,[District] = agentDistrict
  283. ,[Address] = agentAddress
  284. ,[Phone] = ISNULL(agentPhone1,agentMobile1)
  285. FROM #TEMP A INNER JOIN agentMaster am with(nolock) on A.agentId = am.agentId
  286. order by am.agentName
  287. END
  288. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  289. SELECT 'Date Range' head, 'From '+CONVERT(VARCHAR,@fromDate,101)+' to '+CONVERT(VARCHAR,@toDate,101) value UNION ALL
  290. SELECT 'Agent',case when @agentId is null then 'All' else (select agentName from agentMaster with(nolock) where agentId =@agentId) end UNION ALL
  291. SELECT 'Report Type',case when @rptType='nu' then 'Not Updated'
  292. when @rptType='up' then 'Updated but pending'
  293. when @rptType='ua' then 'Updated but approved' end
  294. SELECT 'Agent Profile Update Report' title
  295. END
  296. END TRY
  297. BEGIN CATCH
  298. IF @@TRANCOUNT > 0
  299. ROLLBACK TRANSACTION
  300. DECLARE @errorMessage VARCHAR(MAX)
  301. SET @errorMessage = ERROR_MESSAGE()
  302. EXEC proc_errorHandler 1, @errorMessage, @agentId
  303. END CATCH
  304. GO