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.

327 lines
25 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentSecurityRegionalReport] 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_agentSecurityRegionalReport]
  9. (
  10. @flag VARCHAR(10),
  11. @user VARCHAR(30),
  12. @zoneName VARCHAR(50) =NULL,
  13. @districtName VARCHAR(50) =NULL,
  14. @locationId VARCHAR(10) =NULL,
  15. @agentId VARCHAR(10) =NULL,
  16. @securityType VARCHAR(50) =NULL,
  17. @isExpiry VARCHAR(50) =NULL,
  18. @groupBy VARCHAR(50) =NULL,
  19. @date VARCHAR(50) =NULL
  20. )
  21. AS
  22. SET NOCOUNT ON
  23. BEGIN
  24. DECLARE @sql VARCHAR(max),@sqlGroupBy VARCHAR(MAX)
  25. SET @date=@date+' 23:59:59'
  26. IF @flag = 'rpt' --Security Type = All, Group By = Agent wise
  27. BEGIN
  28. IF @groupBy = 'aw' AND @securityType IS NULL
  29. BEGIN
  30. SET @sql = '
  31. SELECT
  32. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  33. [Zone] = am.agentState,
  34. [District] = am.agentDistrict,
  35. [Agent Name] = am.agentName,
  36. [Location] = al.districtName,
  37. [Bank Guarantee] = SUM(bg.amount),
  38. [Fixed Deposit] = SUM(fd.amount),
  39. [Cash Security] = SUM(cs.cashDeposit),
  40. [Mortgage] = SUM(m.valuationAmount)
  41. FROM dbo.agentMaster am WITH(NOLOCK)
  42. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  43. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  44. LEFT JOIN dbo.bankGuarantee bg WITH(NOLOCK) ON am.agentid = bg.agentId
  45. LEFT JOIN dbo.mortgage m WITH(NOLOCK) ON am.agentid = m.agentId
  46. LEFT JOIN dbo.fixedDeposit fd WITH(NOLOCK) ON am.agentId = fd.agentId
  47. LEFT JOIN dbo.cashSecurity cs WITH(NOLOCK) ON am.agentid = cs.agentId
  48. WHERE
  49. am.isSettlingAgent = ''Y''
  50. AND isNULL(zp.isDeleted,''N'')=''N''
  51. AND (agentType = 2903 or agentType =2904)
  52. and am.parentId <> 5576
  53. and am.agentCountry =''Nepal''
  54. AND ISNULL(am.agentBlock,''U'') = ''U''
  55. AND ISNULL(am.isDeleted,''N'') <> ''Y''
  56. AND (bg.bgId IS NOT NULL
  57. OR m.mortgageId IS NOT NULL
  58. OR cs.csId IS NOT NULL
  59. OR fd.fdId IS NOT NULL)
  60. '
  61. IF @date IS NOT NULL
  62. SET @sql=@sql+' AND (bg.expiryDate<='''+@date+''' OR fd.expiryDate<='''+@date+''')'
  63. SET @sqlGroupBy = ' GROUP BY am.agentId,am.agentName,am.agentState,am.agentDistrict,al.districtName
  64. ORDER BY am.agentState,am.agentDistrict,al.districtName,am.agentName'
  65. END
  66. IF @groupBy = 'aw' AND @securityType = 'bg'
  67. BEGIN
  68. SET @sql = '
  69. SELECT
  70. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  71. [Zone] = am.agentState,
  72. [District] = am.agentDistrict,
  73. [Agent Name] = am.agentName,
  74. [Location] = al.districtName,
  75. [Guarantee No.] = bg.guaranteeNo,
  76. [Amount] = bg.amount,
  77. [Bank Name] = bg.bankName,
  78. [Issue Date] = convert(varchar,bg.issuedDate,101),
  79. [Expiry Date] = convert(varchar,bg.expiryDate,101),
  80. [Follow Up Date] = convert(varchar,bg.followUpDate,101),
  81. [Created By] = bg.createdBy,
  82. [Created Date] = bg.createdDate
  83. FROM dbo.agentMaster am WITH(NOLOCK)
  84. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  85. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  86. INNER JOIN dbo.bankGuarantee bg WITH(NOLOCK) ON am.agentid = bg.agentId
  87. WHERE am.isSettlingAgent = ''Y''
  88. AND isNULL(zp.isDeleted,''N'')=''N''
  89. AND (agentType = 2903 or agentType =2904)
  90. and am.agentCountry =''Nepal''
  91. and am.parentId <> 5576'
  92. IF @date IS NOT NULL
  93. SET @sql=@sql+' AND bg.expiryDate<='''+@date+''''
  94. SET @sqlGroupBy = ''
  95. END
  96. IF @groupBy = 'aw' AND @securityType = 'cs'
  97. BEGIN
  98. SET @sql = '
  99. SELECT
  100. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  101. [Zone] = am.agentState,
  102. [District] = am.agentDistrict,
  103. [Agent Name] = am.agentName,
  104. [Location] = al.districtName,
  105. [Deposit A/C No.] = cs.depositAcNo,
  106. [Amount] = cs.cashDeposit,
  107. [Deposit Date] = convert(varchar,cs.depositedDate,101),
  108. [Bank Name] = cs.bankName,
  109. [Created By] = cs.createdBy,
  110. [Created Date] = cs.createdDate
  111. FROM dbo.agentMaster am WITH(NOLOCK)
  112. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  113. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  114. INNER JOIN dbo.cashSecurity cs WITH(NOLOCK) ON am.agentid = cs.agentId
  115. WHERE am.isSettlingAgent = ''Y''
  116. AND isNULL(zp.isDeleted,''N'')=''N''
  117. AND (agentType = 2903 or agentType =2904)
  118. and am.agentCountry =''Nepal''
  119. and am.parentId <> 5576 '
  120. SET @sqlGroupBy = ''
  121. END
  122. IF @groupBy = 'aw' AND @securityType = 'fd'
  123. BEGIN
  124. SET @sql = '
  125. SELECT
  126. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  127. [Zone] = am.agentState,
  128. [District] = am.agentDistrict,
  129. [Agent Name] = am.agentName,
  130. [Location] = al.districtName,
  131. [Bank Name] = fd.bankName,
  132. [Fixed Deposit No.] = fd.fixedDepositNo,
  133. [Amount] = fd.amount,
  134. [Issued Date] = convert(varchar,fd.issuedDate,101),
  135. [Expiry Date] = convert(varchar,fd.expiryDate,101),
  136. [Follow Up Date] = convert(varchar,fd.followUpDate,101),
  137. [Created By] = fd.createdBy,
  138. [Created Date] = fd.createdDate
  139. FROM dbo.agentMaster am WITH(NOLOCK)
  140. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  141. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  142. INNER JOIN dbo.fixedDeposit fd WITH(NOLOCK) ON am.agentid = fd.agentId
  143. WHERE am.isSettlingAgent = ''Y''
  144. AND isNULL(zp.isDeleted,''N'')=''N''
  145. AND (agentType = 2903 or agentType =2904)
  146. and am.agentCountry =''Nepal''
  147. and am.parentId <> 5576'
  148. IF @date IS NOT NULL
  149. SET @sql=@sql+' AND fd.expiryDate<='''+@date+''''
  150. SET @sqlGroupBy = ''
  151. END
  152. IF @groupBy = 'aw' AND @securityType = 'mo'
  153. BEGIN
  154. SET @sql = '
  155. SELECT
  156. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  157. [Zone] = am.agentState,
  158. [District] = am.agentDistrict,
  159. [Agent Name] = am.agentName,
  160. [Location] = al.districtName,
  161. [Reg. Office] = mo.regOffice,
  162. [Amount] = mo.valuationAmount,
  163. [Mortgage Reg. No.] = mo.mortgageRegNo,
  164. [Valuator] = mo.valuator,
  165. [Valuation Date] = convert(varchar,mo.valuationDate,101),
  166. [Property Type] = mo.propertyType,
  167. [Plot No.] = mo.plotNo,
  168. [Owner] = mo.owner,
  169. [Country] = mo.country,
  170. [Zone] = mo.state,
  171. [City] = mo.city,
  172. [Zip] = mo.zip,
  173. [Address] = mo.address,
  174. [Created By] = mo.createdBy,
  175. [Created Date] = mo.createdDate
  176. FROM dbo.agentMaster am WITH(NOLOCK)
  177. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  178. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  179. INNER JOIN dbo.mortgage mo WITH(NOLOCK) ON am.agentid = mo.agentId
  180. WHERE am.isSettlingAgent = ''Y''
  181. AND isNULL(zp.isDeleted,''N'')=''N''
  182. AND (agentType = 2903 or agentType =2904)
  183. and am.agentCountry =''Nepal''
  184. and am.parentId <> 5576'
  185. SET @sqlGroupBy = ''
  186. END
  187. IF @groupBy = 'aw' AND @securityType = 'na'
  188. BEGIN
  189. SET @sql = '
  190. SELECT
  191. [S.N.] = row_number()over(order by am.agentState,am.agentDistrict,al.districtName,am.agentName),
  192. [Zone] = am.agentState,
  193. [District] = am.agentDistrict,
  194. [Agent Name] = am.agentName,
  195. [Location] = al.districtName
  196. FROM dbo.agentMaster am WITH(NOLOCK)
  197. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  198. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  199. LEFT JOIN dbo.bankGuarantee bg WITH(NOLOCK) ON am.agentid = bg.agentId
  200. LEFT JOIN dbo.mortgage m WITH(NOLOCK) ON am.agentid = m.agentId
  201. LEFT JOIN dbo.fixedDeposit fd WITH(NOLOCK) ON am.agentId = fd.agentId
  202. LEFT JOIN dbo.cashSecurity cs WITH(NOLOCK) ON am.agentid = cs.agentId
  203. WHERE am.isSettlingAgent = ''Y''
  204. AND ISNULL(am.agentBlock,''U'') = ''U''
  205. AND ISNULL(am.isDeleted,''N'') <> ''Y''
  206. and am.parentId <> 5576
  207. AND isNULL(zp.isDeleted,''N'')=''N''
  208. AND (agentType = 2903 or agentType =2904)
  209. and am.agentCountry =''Nepal''
  210. AND (bg.bgId IS NULL
  211. AND m.mortgageId IS NULL
  212. AND cs.csId IS NULL
  213. AND fd.fdId IS NULL)
  214. '
  215. IF @date IS NOT NULL
  216. SET @sql=@sql+' AND (bg.expiryDate<='''+@date+''' OR fd.expiryDate<='''+@date+''')'
  217. SET @sqlGroupBy = ''
  218. END
  219. IF @zoneName IS NOT NULL
  220. SET @sql = @sql +' AND am.agentState = '''+@zoneName+''''
  221. IF @districtName IS NOT NULL
  222. SET @sql = @sql +' AND am.agentDistrict = '''+@districtName+''''
  223. IF @locationId IS NOT NULL
  224. SET @sql = @sql +' AND am.agentLocation = '''+@locationId+''''
  225. IF @agentId IS NOT NULL
  226. SET @sql = @sql +' AND am.agentId = '''+@agentId+''''
  227. IF @user IS NOT NULL
  228. SET @sql = @sql +' AND zp.userName = '''+@user+''''
  229. SET @sql = @sql + @sqlGroupBy
  230. EXEC(@sql)
  231. PRINT(@sql)
  232. IF @groupBy = 'summary'
  233. BEGIN
  234. DECLARE @tbl TABLE (securityType varchar(50),cnt INT,sflag VARCHAR(50))
  235. DECLARE @na INT
  236. SELECT @na = ISNULL(COUNT('X'),0)
  237. FROM dbo.agentMaster am WITH(NOLOCK)
  238. INNER JOIN userZoneMapping zp WITH(NOLOCK) ON am.agentState = zp.zoneName
  239. LEFT JOIN dbo.api_districtList al WITH(NOLOCK) ON am.agentLocation = al.districtCode
  240. LEFT JOIN dbo.bankGuarantee bg WITH(NOLOCK) ON am.agentid = bg.agentId
  241. LEFT JOIN dbo.mortgage m WITH(NOLOCK) ON am.agentid = m.agentId
  242. LEFT JOIN dbo.fixedDeposit fd WITH(NOLOCK) ON am.agentId = fd.agentId
  243. LEFT JOIN dbo.cashSecurity cs WITH(NOLOCK) ON am.agentid = cs.agentId
  244. WHERE
  245. am.isSettlingAgent = 'Y'
  246. AND (agentType = 2903 or agentType =2904)
  247. and am.agentCountry ='Nepal'
  248. and am.parentId <> 5576
  249. AND isNULL(zp.isDeleted,'N')='N'
  250. AND ISNULL(am.agentBlock,'U') = 'U'
  251. AND ISNULL(am.isDeleted,'N') <> 'Y'
  252. AND (bg.bgId IS NULL
  253. AND m.mortgageId IS NULL
  254. AND cs.csId IS NULL
  255. AND fd.fdId IS NULL)
  256. AND (bg.expiryDate<=isnull(@date,bg.expiryDate) OR fd.expiryDate<=isnull(@date,fd.expiryDate))
  257. INSERT INTO @tbl(securityType,cnt,sflag)
  258. SELECT 'Bank Guarantee',COUNT('x'),'bg' FROM dbo.bankGuarantee where expiryDate<=isnull(@date,expiryDate)
  259. INSERT INTO @tbl(securityType,cnt,sflag)
  260. SELECT 'Mortgage',COUNT('x'),'mo' FROM dbo.mortgage
  261. INSERT INTO @tbl(securityType,cnt,sflag)
  262. SELECT 'Fixed Deposit', COUNT('x'),'fd' FROM dbo.fixedDeposit where expiryDate<=isnull(@date,expiryDate)
  263. INSERT INTO @tbl(securityType,cnt,sflag)
  264. SELECT 'Cash Security', COUNT('x'),'cs' FROM dbo.cashSecurity
  265. INSERT INTO @tbl(securityType,cnt,sflag)
  266. SELECT 'Not Available', @na,'na'
  267. SELECT
  268. [S.N.] = ROW_NUMBER()OVER(ORDER BY securityType),
  269. [Security Type] = securityType,
  270. [Total Count] = cnt
  271. FROM @tbl WHERE sflag = ISNULL(@securityType,sflag)
  272. END
  273. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  274. SELECT 'Zone' head, ISNULL(@zoneName,'All') value UNION ALL
  275. SELECT 'District' head, ISNULL(@districtName,'All') value UNION ALL
  276. SELECT 'Location' head, CASE WHEN @locationId IS NULL THEN 'All' ELSE
  277. (SELECT districtName FROM dbo.api_districtList dl WITH(NOLOCK) WHERE dl.districtCode = @locationId) END UNION ALL
  278. SELECT 'Agent' head, CASE WHEN @agentId IS NULL THEN 'All' ELSE
  279. (SELECT agentName FROM dbo.agentMaster am WITH(NOLOCK) WHERE am.agentId = @agentId) END UNION ALL
  280. SELECT 'Security Type' head, CASE WHEN @securityType IS NULL THEN 'All'
  281. WHEN @securityType = 'bg' THEN 'Bank Guarantee'
  282. WHEN @securityType = 'cs' THEN 'Cash Security'
  283. WHEN @securityType = 'mo' THEN 'Mortgage'
  284. WHEN @securityType = 'fd' THEN 'Fixed Deposit'
  285. WHEN @securityType = 'na' THEN 'Not Available' END UNION ALL
  286. SELECT 'Group By' head, CASE WHEN @groupBy = 'aw' THEN 'Agent Wise'
  287. WHEN @groupBy = 'zw' THEN 'Zone Wise'
  288. WHEN @groupBy = 'dw' THEN 'District Wise'
  289. WHEN @groupBy = 'summary' THEN 'Summary' END
  290. SELECT 'Agent Credit Security Report' title
  291. END
  292. END
  293. GO