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.

576 lines
50 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_customerEnrollmentRptV2] 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_customerEnrollmentRptV2](
  9. @flag VARCHAR(50)=NULL
  10. ,@user VARCHAR(30)=NULL
  11. ,@fromDate VARCHAR(30)=NULL
  12. ,@toDate VARCHAR(30)=NULL
  13. ,@sZone VARCHAR(30)=NULL
  14. ,@sAgent VARCHAR(10)=NULL
  15. ,@memberShipId VARCHAR(50)=NULL
  16. ,@ageGrp VARCHAR(50)= NULL
  17. ,@agentGrp VARCHAR(50)= NULL
  18. )AS
  19. BEGIN
  20. DECLARE @FilterList TABLE(head VARCHAR(200), value VARCHAR(100))
  21. IF OBJECT_ID('tempdb..#TEMP_CUSTOMER_TXN') IS NOT NULL
  22. DROP TABLE #TEMP_CUSTOMER_TXN
  23. CREATE TABLE #TEMP_CUSTOMER_TXN(membershipId VARCHAR(16), tranType CHAR(1))
  24. DECLARE
  25. @table VARCHAR(MAX) = NULL
  26. ,@gobalFilter VARCHAR(MAX) =' WHERE ISNULL(cm.isDeleted,''N'') <> ''Y'' AND cm.rejectedDate IS NULL and cm.isKyc is null '
  27. ,@sql VARCHAR(MAX) = NULL
  28. ,@url VARCHAR(MAX) = NULL
  29. IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
  30. BEGIN
  31. INSERT INTO @FilterList
  32. SELECT 'From Date',@fromDate
  33. INSERT INTO @FilterList
  34. SELECT 'To Date',@toDate
  35. SET @gobalFilter=@gobalFilter+' AND cm.createdDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  36. END
  37. IF @sZone IS NOT NULL
  38. BEGIN
  39. INSERT INTO @FilterList
  40. SELECT 'Sending Zone',@sZone
  41. SET @gobalFilter=@gobalFilter+' AND am.agentState ='''+@sZone+''''
  42. END
  43. IF @sAgent IS NOT NULL
  44. BEGIN
  45. INSERT INTO @FilterList
  46. SELECT 'Sending Agent',agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@sAgent
  47. SET @gobalFilter=@gobalFilter+' AND am.agentId ='''+@sAgent+''''
  48. END
  49. IF @agentGrp IS NOT NULL
  50. BEGIN
  51. INSERT INTO @FilterList
  52. SELECT 'Agent Group',detailTitle FROM dbo.staticDataValue WITH(NOLOCK) WHERE valueId=@agentGrp
  53. SET @gobalFilter=@gobalFilter+' AND am.agentGrp ='''+@agentGrp+''''
  54. END
  55. IF @memberShipId IS NOT NULL
  56. BEGIN
  57. INSERT INTO @FilterList
  58. SELECT 'Membership Id',@memberShipId
  59. SET @gobalFilter=@gobalFilter+' AND cm.memberShipId ='''+@memberShipId+''''
  60. END
  61. INSERT INTO @FilterList
  62. SELECT 'Report By',case when @flag ='sz' then 'ZONE WISE'
  63. when @flag ='sa' then 'AGENT WISE'
  64. when @flag ='c' then 'CARD WISE'
  65. when @flag ='b' then 'SLAB WISE'
  66. when @flag ='s' then 'DETAIL'
  67. when @flag ='age' then 'AGE WISE' end
  68. SET @sql = 'insert into #TEMP_CUSTOMER_TXN(membershipId,tranType)
  69. select
  70. distinct cm.membershipId ,tranType = ''P''
  71. from customerMaster cm with(nolock)
  72. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  73. inner join vwTranReceiversArchive rec with(nolock) on cm.membershipId = rec.membershipId and cm.customerId = rec.customerId
  74. '+@gobalFilter +' and cm.approvedDate is not null ;
  75. insert into #TEMP_CUSTOMER_TXN(membershipId,tranType)
  76. select
  77. distinct cm.membershipId ,tranType = ''S''
  78. from customerMaster cm with(nolock)
  79. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  80. inner join vwTranSendersArchive rec with(nolock) on cm.membershipId = rec.membershipId and cm.customerId = rec.customerId
  81. '+@gobalFilter +' and cm.approvedDate is not null
  82. '
  83. IF @flag='sz'
  84. BEGIN
  85. SET @url ='<a href="#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=inactive&fromDate='+@fromDate+'&toDate='+@toDate+'&user='+@user
  86. SET @gobalFilter=@gobalFilter+' GROUP BY am.agentState,cm.approvedDate,r.membershipId,s.membershipId,t.membershipId'
  87. exec(@sql)
  88. SET @table='
  89. SELECT
  90. [S.N.] = ROW_NUMBER() OVER(ORDER BY zone),
  91. [ZONE] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=s&fromDate='+ISNULL(@fromDate,'')+'&toDate='+ISNULL(@toDate,'')+'&sZone=''+zone+''&membershipId='+ISNULL(@membershipId,'')+'&sAgent='+ISNULL(@sAgent,'')+'")>''+zone+''</a>'',
  92. [Enrolled] = sum(tot),
  93. [Approved] = sum(appCusCount),
  94. [Pending] = sum(penCusCount),
  95. [Send Active] = sum(sendActive),
  96. [Paid Active] = sum(paidActive),
  97. [Total Active] = sum(totalActive),
  98. [Total Inactive] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=inactive&fromDate='+ISNULL(@fromDate,'')+'&toDate='+ISNULL(@toDate,'')+'&sZone=''+zone+''&membershipId='+ISNULL(@membershipId,'')+'&sAgent='+ISNULL(@sAgent,'')+'")>''+cast(sum(appCusCount) - sum(totalActive) as varchar)+''</a>''
  99. FROM
  100. (
  101. SELECT
  102. zone = am.agentState
  103. ,tot = count(''x'')
  104. ,appCusCount = case when cm.approvedDate is not null then count(''x'') else 0 end
  105. ,penCusCount = case when cm.approvedDate is null then count(''x'') else 0 end
  106. ,paidActive = case when r.membershipId is not null then count(''x'') else 0 end
  107. ,sendActive = case when s.membershipId is not null then count(''x'') else 0 end
  108. ,totalActive = case when t.membershipId is not null then count(''x'') else 0 end
  109. FROM customerMaster cm with(nolock)
  110. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  111. LEFT JOIN
  112. (
  113. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''P''
  114. )r on r.membershipId = cm.membershipId
  115. left join
  116. (
  117. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''S''
  118. )s on s.membershipId = cm.membershipId
  119. LEFT JOIN
  120. (
  121. select distinct membershipId FROM #TEMP_CUSTOMER_TXN
  122. )t on cm.membershipId = t.membershipId
  123. '+@gobalFilter+'
  124. )X GROUP BY zone'
  125. print @table
  126. EXEC (@table)
  127. END
  128. IF @flag='sa'
  129. BEGIN
  130. SET @gobalFilter=@gobalFilter+' GROUP BY am.agentState,am.agentId,am.agentName,cm.approvedDate,r.membershipId,s.membershipId,t.membershipId '
  131. exec(@sql)
  132. SET @table='
  133. SELECT
  134. [S.N.] = row_number()over(order by zone)
  135. ,[Zone] = zone
  136. ,[Agent] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=s&fromDate='+ISNULL(@fromDate,'')+'&toDate='+ISNULL(@toDate,'')+'&sZone='+ISNULL(@sZone,'')+'&membershipId='+ISNULL(@membershipId,'')+'&sAgent=''+cast(agentId as varchar(50))+''")>''+agent+''</a>''
  137. ,[No. of Customer] = sum(tot)
  138. ,[No. of Approved] = sum(appCusCount)
  139. ,[No. of Pending] = sum(PenCusCount)
  140. ,[Send Active] = sum(sendActive)
  141. ,[Paid Active] = sum(paidActive)
  142. ,[Total Active] = sum(totalActive)
  143. ,[Total Inactive] = sum(appCusCount) - sum(totalActive)
  144. FROM
  145. (
  146. SELECT
  147. agentId = am.agentId
  148. ,zone = am.agentState
  149. ,agent = am.agentName
  150. ,tot = count(''x'')
  151. ,appCusCount = case when cm.approvedDate is not null then count(''x'') else 0 end
  152. ,PenCusCount = case when cm.approvedDate is null then count(''x'') else 0 end
  153. ,paidActive = case when r.membershipId is not null then count(''x'') else 0 end
  154. ,sendActive = case when s.membershipId is not null then count(''x'') else 0 end
  155. ,totalActive = case when t.membershipId is not null then count(''x'') else 0 end
  156. FROM customerMaster cm with(nolock)
  157. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  158. LEFT JOIN
  159. (
  160. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''P''
  161. )r on r.membershipId = cm.membershipId
  162. left join
  163. (
  164. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''S''
  165. )s on s.membershipId = cm.membershipId
  166. LEFT JOIN
  167. (
  168. select distinct membershipId FROM #TEMP_CUSTOMER_TXN
  169. )t on cm.membershipId = t.membershipId
  170. '+@gobalFilter+'
  171. )X group by zone,agent,agentId ORDER BY agent ASC'
  172. print @table
  173. EXEC (@table)
  174. END
  175. IF @flag='c'
  176. BEGIN
  177. SET @table='
  178. SELECT
  179. [S.N.] = row_number()over(order by am.agentName )
  180. ,[Agent] = am.agentName
  181. ,[Membership ID] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=s&fromDate='+ISNULL(@fromDate,'')+'&toDate='+isnull(@toDate,'')+'&sZone='+ISNULL(@sZone,'')+'&membershipId=''+cm.membershipId+''&sAgent='+ISNULL(@sAgent,'')+'")>''+cm.membershipId+''</a>''
  182. ,[Send TXN] = ISNULL(sendTxn,0)
  183. ,[Paid TXN] = ISNULL(payTxn,0)
  184. ,[Total TXN] = ISNULL(sendTxn,0) + ISNULL(payTxn,0)
  185. FROM customerMaster cm with(nolock)
  186. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  187. LEFT JOIN
  188. (
  189. SELECT
  190. membershipId = cm.membershipId,
  191. sendTxn = count(''x'')
  192. FROM customerMaster cm with(nolock)
  193. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  194. INNER JOIN tranSenders sen with(nolock) on cm.customerId = sen.customerId and cm.membershipId = sen.membershipId
  195. '+@gobalFilter+' and cm.approvedDate is not null GROUP BY cm.membershipId
  196. )X ON X.membershipId = cm.membershipId
  197. LEFT JOIN
  198. (
  199. SELECT
  200. membershipId = cm.membershipId,
  201. payTxn = count(''x'')
  202. FROM customerMaster cm with(nolock)
  203. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  204. INNER JOIN tranReceivers rec with(nolock) on cm.customerId = rec.customerId and cm.membershipId = rec.membershipId
  205. '+@gobalFilter+' and cm.approvedDate is not null GROUP BY cm.membershipId
  206. )Y ON Y.membershipId = cm.membershipId
  207. '+@gobalFilter
  208. print @table
  209. EXEC (@table)
  210. END
  211. IF @flag='s'
  212. BEGIN
  213. SET @gobalFilter=@gobalFilter+' ORDER BY am.agentName ASC'
  214. SET @table='SELECT
  215. [S.N.] =row_number()over(order by am.agentName)
  216. ,[Membership Id] =cm.membershipId
  217. ,[Customer Name] =ISNULL('' '' + cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName, '''')
  218. ,[Country] =cm.tCountry
  219. ,[Zone] =cm.tZone
  220. ,[District] =cm.tDistrict
  221. ,[VDC\MNC] =cm.tMunicipality+'' ''+ISNULL(tWardNo,'''')
  222. ,[Place Of Issue] =cm.placeOfIssue
  223. ,[Mobile] =cm.mobile
  224. ,[Occupation] =ISNULL(sdv.detailTitle, cm.occupation)
  225. ,[Date Of Birth] =cm.dobEng
  226. ,[Created By] =cm.createdBy
  227. ,[Created Date] =cm.createdDate
  228. ,[Approved By] =cm.approvedBy
  229. ,[Approved Date] =cm.approvedDate
  230. ,[Email] =cm.email
  231. ,[Issuing Agent] =am.agentName
  232. FROM customerMaster cm with(nolock)
  233. LEFT JOIN staticDataValue sdv (NOLOCK) ON cm.occupation = CAST(sdv.valueId AS VARCHAR)
  234. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId'+@gobalFilter
  235. print @table
  236. EXEC (@table)
  237. END
  238. IF @flag='detail'
  239. BEGIN
  240. SET @table='SELECT
  241. [S.N.] = row_number() over(order by rt.sAgentName)
  242. ,[Agent Name] = rt.sAgentName
  243. ,[Control No] = ''<span class = "link" onclick ="ViewTranDetailByControlNo('''''' + dbo.fnadecryptstring(rt.controlNo) + '''''');">'' + dbo.fnadecryptstring(rt.controlNo) + ''</span>''
  244. ,[Total Amount] = dbo.ShowDecimal(rt.pAmt)
  245. ,[Total bonus Point] = rt.bonusPoint
  246. ,[Membership Id] = ''<a href="#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'Remit/Administration/CustomerSetup/Display.aspx?membershipId='' + cm.membershipId + '''''')">'' + cm.membershipId + ''</a>''
  247. ,[Sender Name] = rt.senderName
  248. ,[Receiver Name] = rt.receiverName
  249. ,[Pay Status] = rt.payStatus
  250. ,[TXN Date] = rt.createdDateLocal
  251. FROM remitTran rt WITH(NOLOCK)
  252. INNER JOIN transenders ts WITH(NOLOCK) ON rt.id=ts.tranId
  253. INNER JOIN customerMaster cm WITH(NOLOCK)ON ts.membershipId=cm.membershipId
  254. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  255. '+@gobalFilter
  256. PRINT @Table
  257. EXEC(@Table)
  258. END
  259. IF @flag = 'inactive'
  260. BEGIN
  261. IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL
  262. DROP TABLE #TEMP
  263. IF OBJECT_ID('tempdb..#temp_send') IS NOT NULL
  264. DROP TABLE #temp_send
  265. IF OBJECT_ID('tempdb..#temp_paid') IS NOT NULL
  266. DROP TABLE #temp_paid
  267. CREATE TABLE #TEMP(membershipId VARCHAR(16),agentName varchar(500))
  268. CREATE TABLE #temp_send(membershipId VARCHAR(16))
  269. CREATE TABLE #temp_paid(membershipId VARCHAR(16))
  270. SET @table = '
  271. insert into #TEMP(membershipId,agentName)
  272. select membershipId,am.agentName
  273. from customerMaster cm with(nolock)
  274. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  275. '+@gobalFilter+'
  276. and cm.approvedDate is not null
  277. insert into #temp_send
  278. select distinct cm.membershipId from customerMaster cm with(nolock)
  279. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  280. inner join tranSenders sen with(nolock) on cm.membershipId = sen.membershipId and sen.customerId = cm.customerId
  281. '+@gobalFilter+'
  282. and cm.approvedDate is not null
  283. insert into #temp_paid
  284. select distinct cm.membershipId from customerMaster cm with(nolock)
  285. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  286. inner join tranReceivers rec with(nolock) on cm.membershipId = rec.membershipId and rec.customerId = cm.customerId
  287. '+@gobalFilter+'
  288. and cm.approvedDate is not null
  289. DELETE FROM #TEMP
  290. FROM #TEMP t INNER JOIN
  291. (
  292. select membershipId from #temp_send
  293. )sen ON t.membershipId = sen.membershipId
  294. DELETE FROM #TEMP
  295. FROM #TEMP t INNER JOIN
  296. (
  297. select membershipId from #temp_paid
  298. )sen ON t.membershipId = sen.membershipId
  299. select
  300. [S.N.] =row_number()over(order by t.agentName)
  301. ,[Membership Id] = ''<a href="#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'Remit/Administration/CustomerSetup/Display.aspx?membershipId='' + cm.membershipId + '''''')">'' + cm.membershipId + ''</a>''
  302. ,[Customer Name] =ISNULL('' '' + cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName, '''')
  303. ,[Country] =cm.tCountry
  304. ,[Zone] =cm.tZone
  305. ,[District] =cm.tDistrict
  306. ,[VDC\MNC] =cm.tMunicipality+'' ''+ISNULL(tWardNo,'''')
  307. ,[Place Of Issue] =cm.placeOfIssue
  308. ,[Mobile] =cm.mobile
  309. ,[Occupation] =ISNULL(sdv.detailTitle, cm.occupation)
  310. ,[Date Of Birth] =cm.dobEng
  311. ,[Created By] =cm.createdBy
  312. ,[Created Date] =cm.createdDate
  313. ,[Approved By] =cm.approvedBy
  314. ,[Approved Date] =cm.approvedDate
  315. ,[Email] =cm.email
  316. ,[Issuing Agent] =t.agentName
  317. from customerMaster cm with(nolock)
  318. LEFT JOIN staticDataValue sdv (NOLOCK) ON cm.occupation = CAST(sdv.valueId AS VARCHAR)
  319. inner join #TEMP t on t.membershipId = cm.membershipId'
  320. PRINT(@table);
  321. EXEC(@table);
  322. END
  323. IF @flag='age'
  324. BEGIN
  325. CREATE TABLE #temp_table(ageGrp VARCHAR(50), membershipId VARCHAR(16),agentId int,
  326. approvedDate datetime,customerId INT,createdDate datetime,isDeleted char(1),rejectedDate datetime,isKyc char(1))
  327. INSERT into #temp_table(ageGrp,membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,isKyc)
  328. SELECT 'a',membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,cm.isKyc
  329. FROM dbo.customerMaster cm WITH(NOLOCK) WHERE DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 25
  330. AND cm.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  331. AND cm.isDeleted IS NULL AND cm.rejectedDate IS NULL and cm.isKyc is null
  332. UNION ALL
  333. SELECT 'b',membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,cm.isKyc
  334. FROM dbo.customerMaster cm WITH(NOLOCK) WHERE DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 25 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 30
  335. AND cm.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  336. AND cm.isDeleted IS NULL AND cm.rejectedDate IS NULL and cm.isKyc is null
  337. UNION ALL
  338. SELECT 'c',membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,cm.isKyc
  339. FROM dbo.customerMaster cm WITH(NOLOCK) WHERE DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 30 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 35
  340. AND cm.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  341. AND cm.isDeleted IS NULL AND cm.rejectedDate IS NULL and cm.isKyc is null
  342. UNION ALL
  343. SELECT 'd',membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,cm.isKyc
  344. FROM dbo.customerMaster cm WITH(NOLOCK) WHERE DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 35 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 40 and
  345. cm.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  346. AND cm.isDeleted IS NULL AND cm.rejectedDate IS NULL and cm.isKyc is null
  347. UNION all
  348. SELECT 'e',membershipId,agentId,approvedDate,customerId,createdDate,isDeleted,rejectedDate,cm.isKyc
  349. FROM dbo.customerMaster cm WITH(NOLOCK) WHERE DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 40 AND
  350. cm.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  351. AND cm.isDeleted IS NULL AND cm.rejectedDate IS NULL and cm.isKyc is null
  352. SET @sql = 'insert into #TEMP_CUSTOMER_TXN(membershipId,tranType)
  353. select
  354. distinct cm.membershipId ,tranType = ''p''
  355. from #temp_table cm with(nolock)
  356. INNER JOIN agentMaster am with(nolock) ON cm.agentId = am.agentId
  357. inner join tranReceivers rec with(nolock) on cm.membershipId = rec.membershipId and cm.customerId = rec.customerId
  358. '+@gobalFilter +' and cm.approvedDate is not null ;
  359. insert into #TEMP_CUSTOMER_TXN(membershipId,tranType)
  360. select
  361. distinct cm.membershipId ,tranType = ''s''
  362. from #temp_table cm with(nolock)
  363. INNER JOIN agentMaster am with(nolock) ON cm.agentId = am.agentId
  364. inner join tranSenders rec with(nolock) on cm.membershipId = rec.membershipId and cm.customerId = rec.customerId
  365. '+@gobalFilter +' and cm.approvedDate is not null'
  366. SET @gobalFilter=@gobalFilter+' GROUP BY cm.ageGrp,cm.approvedDate,r.membershipId,s.membershipId,t.membershipId'
  367. PRINT @sql
  368. exec(@sql)
  369. SET @table='
  370. SELECT
  371. [S.N.] = ROW_NUMBER() OVER(ORDER BY ageGrp),
  372. [Age Group] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=s-age&fromDate='+ISNULL(@fromDate,'')+'&toDate='+ISNULL(@toDate,'')+'&sZone='+ISNULL(@sZone,'')+'&membershipId='+ISNULL(@membershipId,'')+'&sAgent='+ISNULL(@sAgent,'')+'&ageGrp=''+ageGrp+''")>''
  373. + CASE WHEN ageGrp = ''a'' THEN ''Below 18 to 25''
  374. WHEN ageGrp = ''b'' THEN ''25 to 30''
  375. WHEN ageGrp = ''c'' THEN ''30 to 35''
  376. WHEN ageGrp = ''d'' THEN ''35 to 40''
  377. WHEN ageGrp = ''e'' THEN ''40 and above'' END +''</a>'',
  378. [Enrolled] = sum(tot),
  379. [Approved] = sum(appCusCount),
  380. [Pending] = sum(penCusCount),
  381. [Send Active] = sum(sendActive),
  382. [Paid Active] = sum(paidActive),
  383. [Total Active] = sum(totalActive),
  384. [Total Inactive] = ''<a href = "#" onclick=OpenInNewWindow("Reports.aspx?reportName=customerptenrollment&searchBy=inactive-age&fromDate='+ISNULL(@fromDate,'')+'&toDate='+ISNULL(@toDate,'')+'&sZone='+ISNULL(@sZone,'')+'&membershipId='+ISNULL(@membershipId,'')+'&sAgent='+ISNULL(@sAgent,'')+'&ageGrp=''+ageGrp+''")>''+cast(sum(appCusCount) - sum(totalActive) as varchar)+''</a>''
  385. FROM
  386. (
  387. SELECT
  388. ageGrp = cm.ageGrp
  389. ,tot = count(''x'')
  390. ,appCusCount = case when cm.approvedDate is not null then count(''x'') else 0 end
  391. ,penCusCount = case when cm.approvedDate is null then count(''x'') else 0 end
  392. ,paidActive = case when r.membershipId is not null then count(''x'') else 0 end
  393. ,sendActive = case when s.membershipId is not null then count(''x'') else 0 end
  394. ,totalActive = case when t.membershipId is not null then count(''x'') else 0 end
  395. FROM #temp_table cm with(nolock)
  396. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId
  397. LEFT JOIN
  398. (
  399. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''P''
  400. )r on r.membershipId = cm.membershipId
  401. left join
  402. (
  403. select membershipId FROM #TEMP_CUSTOMER_TXN WHERE tranType =''S''
  404. )s on s.membershipId = cm.membershipId
  405. LEFT JOIN
  406. (
  407. select distinct membershipId FROM #TEMP_CUSTOMER_TXN
  408. )t on cm.membershipId = t.membershipId
  409. '+@gobalFilter+'
  410. )X GROUP BY ageGrp'
  411. print @table
  412. EXEC (@table)
  413. END
  414. IF @flag='s-age'
  415. BEGIN
  416. if @ageGrp = 'a'
  417. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 25'
  418. if @ageGrp = 'b'
  419. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 25 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 30 '
  420. if @ageGrp = 'c'
  421. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 30 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 35'
  422. if @ageGrp = 'd'
  423. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 35 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 40'
  424. if @ageGrp = 'e'
  425. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 40'
  426. SET @gobalFilter=@gobalFilter+' ORDER BY am.agentName ASC'
  427. SET @table='SELECT
  428. [S.N.] =row_number()over(order by am.agentName)
  429. ,[Membership Id] =cm.membershipId
  430. ,[Customer Name] =ISNULL('' '' + cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName, '''')
  431. ,[Country] =cm.tCountry
  432. ,[Zone] =cm.tZone
  433. ,[District] =cm.tDistrict
  434. ,[VDC\MNC] =cm.tMunicipality+'' ''+ISNULL(tWardNo,'''')
  435. ,[Place Of Issue] =cm.placeOfIssue
  436. ,[Mobile] =cm.mobile
  437. ,[Occupation] =ISNULL(sdv.detailTitle, cm.occupation)
  438. ,[Age] =DATEDIFF(YEAR,cm.dobEng,GETDATE())
  439. ,[Date Of Birth] =cm.dobEng
  440. ,[Date Of Birth(B.S.)] =cm.dobNep
  441. ,[Created By] =cm.createdBy
  442. ,[Created Date] =cm.createdDate
  443. ,[Approved By] =cm.approvedBy
  444. ,[Approved Date] =cm.approvedDate
  445. ,[Email] =cm.email
  446. ,[Issuing Agent] =am.agentName
  447. FROM customerMaster cm with(nolock)
  448. LEFT JOIN staticDataValue sdv (NOLOCK) ON cm.occupation = CAST(sdv.valueId AS VARCHAR)
  449. INNER JOIN agentMaster am with(nolock) ON cm.agentId=am.agentId'+@gobalFilter
  450. print @table
  451. EXEC (@table)
  452. END
  453. IF @flag = 'inactive-age'
  454. BEGIN
  455. IF OBJECT_ID('tempdb..#TEMP_1') IS NOT NULL
  456. DROP TABLE #TEMP_1
  457. IF OBJECT_ID('tempdb..#temp_send_1') IS NOT NULL
  458. DROP TABLE #temp_send_1
  459. IF OBJECT_ID('tempdb..#temp_paid_1') IS NOT NULL
  460. DROP TABLE #temp_paid_1
  461. CREATE TABLE #TEMP_1(membershipId VARCHAR(16),agentName varchar(500))
  462. CREATE TABLE #temp_send_1(membershipId VARCHAR(16))
  463. CREATE TABLE #temp_paid_1(membershipId VARCHAR(16))
  464. if @ageGrp = 'a'
  465. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 25'
  466. if @ageGrp = 'b'
  467. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 25 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 30 '
  468. if @ageGrp = 'c'
  469. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 30 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 35 '
  470. if @ageGrp = 'd'
  471. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 35 and DATEDIFF(YEAR,cm.dobEng,GETDATE()) < 40'
  472. if @ageGrp = 'e'
  473. set @gobalFilter = @gobalFilter+' AND DATEDIFF(YEAR,cm.dobEng,GETDATE()) >= 40'
  474. SET @table = '
  475. insert into #TEMP_1(membershipId,agentName)
  476. select membershipId,am.agentName
  477. from customerMaster cm with(nolock)
  478. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  479. '+@gobalFilter+'
  480. and cm.approvedDate is not null
  481. insert into #temp_send_1
  482. select distinct cm.membershipId from customerMaster cm with(nolock)
  483. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  484. inner join tranSenders sen with(nolock) on cm.membershipId = sen.membershipId and sen.customerId = cm.customerId
  485. '+@gobalFilter+'
  486. and cm.approvedDate is not null
  487. insert into #temp_paid_1
  488. select distinct cm.membershipId from customerMaster cm with(nolock)
  489. inner join agentMaster am with(nolock) on cm.agentId = am.agentId
  490. inner join tranReceivers rec with(nolock) on cm.membershipId = rec.membershipId and rec.customerId = cm.customerId
  491. '+@gobalFilter+'
  492. and cm.approvedDate is not null
  493. DELETE FROM #TEMP_1
  494. FROM #TEMP_1 t INNER JOIN
  495. (
  496. select membershipId from #temp_send_1
  497. )sen ON t.membershipId = sen.membershipId
  498. DELETE FROM #TEMP_1
  499. FROM #TEMP_1 t INNER JOIN
  500. (
  501. select membershipId from #temp_paid_1
  502. )sen ON t.membershipId = sen.membershipId
  503. select
  504. [S.N.] =row_number()over(order by t.agentName)
  505. ,[Membership Id] = ''<a href="#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'Remit/Administration/CustomerSetup/Display.aspx?membershipId='' + cm.membershipId + '''''')">'' + cm.membershipId + ''</a>''
  506. ,[Customer Name] =ISNULL('' '' + cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName, '''')
  507. ,[Country] =cm.tCountry
  508. ,[Zone] =cm.tZone
  509. ,[District] =cm.tDistrict
  510. ,[VDC\MNC] =cm.tMunicipality+'' ''+ISNULL(tWardNo,'''')
  511. ,[Place Of Issue] =cm.placeOfIssue
  512. ,[Mobile] =cm.mobile
  513. ,[Occupation] =ISNULL(sdv.detailTitle, cm.occupation)
  514. ,[Date Of Birth] =cm.dobEng
  515. ,[Created By] =cm.createdBy
  516. ,[Created Date] =cm.createdDate
  517. ,[Approved By] =cm.approvedBy
  518. ,[Approved Date] =cm.approvedDate
  519. ,[Email] =cm.email
  520. ,[Issuing Agent] =t.agentName
  521. from customerMaster cm with(nolock)
  522. LEFT JOIN staticDataValue sdv (NOLOCK) ON cm.occupation = CAST(sdv.valueId AS VARCHAR)
  523. inner join #TEMP_1 t on t.membershipId = cm.membershipId'
  524. PRINT(@table);
  525. EXEC(@table);
  526. END
  527. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  528. SELECT * FROM @FilterList
  529. SELECT 'CUSTOMER ENROLLMENT REPORT' title
  530. END
  531. GO