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.

161 lines
7.0 KiB

1 year ago
  1. USE FastMoneyPro_Remit
  2. GO
  3. --EXEC proc_RegulatoryReport @DATE='2019-09-20'
  4. ALTER proc proc_RegulatoryReport
  5. @DATE DATETIME
  6. AS
  7. SET NOCOUNT ON;
  8. SET ARITHABORT ON;
  9. DECLARE @YEARSTART VARCHAR(10),@YEAREND DATETIME,@PREVYEAR VARCHAR(10)
  10. SELECT @PREVYEAR =CAST(YEAR(@DATE)-1 AS VARCHAR)+'-01-01',@YEARSTART = CAST(YEAR(@DATE) AS VARCHAR)+'-01-01',@YEAREND = @DATE +' 23:59:59'
  11. --drop table #TEMP
  12. select sbranch [Handling branch]
  13. ,[TransferDel] = '1',r.cancelApprovedDate
  14. ,r.id [SerialNo],1 [formOfRemit]
  15. ,r.createdDate [remitDT]
  16. ,collCurr,tAmt [RemittanceAmt],sCurrCostRate+ISNULL(sCurrHoMargin,0) [USDExRate]
  17. ,[RemitUSDAmt] = CAST(tAmt/(sCurrCostRate+ISNULL(sCurrHoMargin,0)) AS DECIMAL(16,2))
  18. ,serviceCharge,senderName = left(senderName,60)
  19. ,[SenderVerificationType] = s.idType
  20. ,[SenderVerificationNum] = s.idNumber
  21. ,[customerUniqueno] = s.customerId
  22. ,receiverName = LEFT(r.receiverName,60),CM.countryCode [BeneficiaryCountry]
  23. ,c.countryCode,s.customerId
  24. INTO #TEMP
  25. from remitTran r(nolock)
  26. INNER join tranSenders s (nolock) on s.tranId = r.Id
  27. LEFT JOIN countryMaster C(NOLOCK) ON C.countryName = S.nativeCountry
  28. LEFT JOIN countryMaster CM (NOLOCK) ON CM.COUNTRYNAME = R.pCountry
  29. where 1=1 AND tranType <> 'R'
  30. AND (r.createdDate between @DATE and @DATE+' 23:59:59' OR r.cancelApprovedDate between @DATE and @DATE+' 23:59:59')
  31. --and s.customerId = 38700
  32. ALTER TABLE #TEMP ADD [zipcode] VARCHAR(20),[SumUSDAmt] DECIMAL(16,2)
  33. DELETE FROM #TEMP WHERE FORMAT([remitDT],'yyyyMMdd') = FORMAT(ISNULL(cancelApprovedDate,'2000-01-01'),'yyyyMMdd')
  34. UPDATE #TEMP SET cancelApprovedDate = NULL WHERE FORMAT(cancelApprovedDate,'yyyyMMdd') > @DATE
  35. UPDATE t set t.[zipcode] = 'A' + RIGHT('000000'+CAST(m.swiftCode AS VARCHAR(6)),6) FROM #TEMP T
  36. INNER JOIN agentMaster m(nolock) on m.agentId = t.[Handling branch]
  37. ---- TO GET SUM OF USDAMT
  38. --SELECT [SumUSDAmt] = ROUND(SUM(tAmt/(sCurrCostRate+ISNULL(sCurrHoMargin,0))),2),s.customerId
  39. --INTO #PREVIOUSYEAR_SUM
  40. --FROM #TEMP T
  41. --INNER JOIN tranSenders s (NOLOCK) ON s.customerId = t.customerId
  42. --INNER JOIN remitTran r(NOLOCK) ON r.id = s.tranId
  43. --WHERE t.cancelApprovedDate BETWEEN @DATE and @YEAREND
  44. --AND t.remitDT BETWEEN @PREVYEAR AND @YEARSTART
  45. --and r.createdDate BETWEEN @PREVYEAR AND @YEARSTART
  46. --AND R.tranStatus <>'CANCEL'
  47. ----and s.customerId = 30278
  48. --GROUP BY s.customerId
  49. --select * from #TEMP
  50. --select * from #PREVIOUSYEAR_SUM
  51. --drop table #PREVIOUSYEAR_SUM
  52. --drop table #THISYEAR_SUM
  53. --select * from #PREVIOUSYEAR_SUM
  54. --select * from #TEMP where customerId = 30278
  55. ---- ONLY FOR THIS YEAR
  56. SELECT ROUND(sum(tAmt/(sCurrCostRate+ISNULL(sCurrHoMargin,0))),2) SumUSDAmt,s.customerId--,r.createdDate,r.cancelApprovedDate,r.id
  57. INTO #THISYEAR_SUM
  58. FROM remitTran r(NOLOCK)
  59. INNER JOIN tranSenders s (NOLOCK) ON s.tranId = r.Id
  60. WHERE r.createdDate BETWEEN @YEARSTART and @YEAREND
  61. --AND R.tranStatus <>'CANCEL'
  62. --and s.customerId = 30278
  63. GROUP BY s.customerId
  64. SELECT ROUND(sum(tAmt/(sCurrCostRate+ISNULL(sCurrHoMargin,0))),2) SumUSDAmt,s.customerId--,r.createdDate,r.cancelApprovedDate,r.id
  65. INTO #THISYEAR_SUMCANCEL
  66. FROM remitTran r(NOLOCK)
  67. INNER JOIN tranSenders s (NOLOCK) ON s.tranId = r.Id
  68. WHERE r.cancelApprovedDate BETWEEN @YEARSTART and @YEAREND
  69. AND r.createdDate BETWEEN @YEARSTART and @YEAREND
  70. AND R.tranStatus ='CANCEL'
  71. --and s.customerId = 30278
  72. GROUP BY s.customerId
  73. UPDATE T SET T.SumUSDAmt=T.SumUSDAmt-C.SumUSDAmt FROM #THISYEAR_SUM T
  74. INNER JOIN #THISYEAR_SUMCANCEL C ON C.customerId=T.customerId
  75. --SELECT ROUND(sum(tAmt/(sCurrCostRate+ISNULL(sCurrHoMargin,0))),2) SumUSDAmt,s.customerId--,r.createdDate,r.cancelApprovedDate,r.id
  76. --INTO #THISYEAR_SUM
  77. --FROM remitTran r(NOLOCK)
  78. --INNER JOIN tranSenders s (NOLOCK) ON s.tranId = r.Id
  79. --INNER JOIN (
  80. --select distinct customerId from #TEMP where remitDT BETWEEN @YEARSTART and @YEAREND
  81. --)t ON t.customerId = s.customerId --and t.SerialNo=s.tranId
  82. --WHERE r.createdDate BETWEEN @YEARSTART and @YEAREND
  83. ----AND R.tranStatus <>'CANCEL'
  84. ----and s.customerId = 30278
  85. --GROUP BY s.customer
  86. --select * from #THISYEAR_SUM
  87. --UPDATE T SET T.SumUSDAmt = P.SumUSDAmt FROM #TEMP T
  88. --INNER JOIN #PREVIOUSYEAR_SUM P ON P.customerId = T.customerId
  89. --WHERE T.cancelApprovedDate BETWEEN @YEARSTART AND @YEAREND
  90. --AND T.remitDT BETWEEN @PREVYEAR AND @YEARSTART
  91. UPDATE T SET T.SumUSDAmt = P.SumUSDAmt FROM #TEMP T
  92. INNER JOIN #THISYEAR_SUM P ON P.customerId = T.customerId
  93. WHERE T.remitDT BETWEEN @YEARSTART AND @YEAREND
  94. AND T.SumUSDAmt IS NULL
  95. ----IF TOTAL USD AMOUNT IS NULL MEANS TOTAL SEND - CANCEL =0
  96. UPDATE #TEMP SET SumUSDAmt = 0 WHERE SumUSDAmt IS NULL
  97. --DELETE FROM #TEMPSUM_USD WHERE FORMAT(createdDate,'yyyyMMdd') = FORMAT(ISNULL(cancelApprovedDate,'2000-01-01'),'yyyyMMdd')
  98. ---- CHECK WHETHER FIRST TIME TXN CUSTOMER EXIST OR NOT
  99. SELECT MIN(createdDate) createdDate,MIN(TRANID) TRANID,S.customerId
  100. INTO #TEMPCUSTOMER FROM remitTran(NOLOCK) r
  101. INNER JOIN tranSenders s (NOLOCK) ON s.tranId = r.id
  102. WHERE R.createdDate BETWEEN DATEADD(DAY,-5,@DATE) AND DATEADD(DAY,1,@DATE)
  103. GROUP BY S.customerId
  104. IF EXISTS( SELECT TOP 1 'A' FROM #TEMP C LEFT JOIN TBL_BOK_ReportingCustomerDetail T(NOLOCK) ON T.customerId = C.customerId WHERE T.customerId IS NULL)
  105. BEGIN
  106. DELETE C FROM #TEMPCUSTOMER C
  107. INNER JOIN TBL_BOK_ReportingCustomerDetail T ON T.customerId=C.customerId
  108. INSERT INTO TBL_BOK_ReportingCustomerDetail(customerId,firstTranId,firstTranDate,idType,idNumber,createdDate,createdBy)
  109. select DISTINCT C.customerId,T.TRANID,C.createdDate,T.idType,T.idNumber,GETDATE(),'system' FROM #TEMPCUSTOMER C
  110. INNER JOIN tranSenders(NOLOCK) T ON T.tranId=C.tranId
  111. ORDER BY C.customerId,TRANID
  112. END
  113. ---- update customer id type and number base on customer first time txn
  114. UPDATE C SET C.SenderVerificationNum = T.idNumber,C.SenderVerificationType = T.idType
  115. FROM #TEMP C
  116. INNER JOIN TBL_BOK_ReportingCustomerDetail T ON T.customerId=C.customerId
  117. SELECT [Handling branch],[zipcode]
  118. ,[TransferDel] = CASE WHEN FORMAT(ISNULL(cancelApprovedDate,'2200-01-01'),'yyyyMMdd') >FORMAT(CAST(@DATE AS DATE),'yyyyMMdd') THEN '1' ELSE '2' END
  119. ,[SerialNo],[formOfRemit]
  120. ,[remitDT] = FORMAT([remitDT],'yyyyMMdd')
  121. ,[remitTime] = 'H' + RIGHT('000000'+CAST(FORMAT(remitDT,'HHmmss') AS VARCHAR(6)),6)
  122. ,collCurr,[RemittanceAmt],[USDExRate],[RemitUSDAmt]
  123. ,serviceCharge,senderName
  124. ,[SenderVerificationType] = CASE
  125. WHEN LEN(REPLACE(SenderVerificationNum, '-', '')) <> '13' THEN '99'
  126. WHEN LEFT(RIGHT(REPLACE(SenderVerificationNum, '-', ''), 7), 1) = '0' OR customerUniqueno = 98526 THEN 'P02'
  127. WHEN LEFT(RIGHT(REPLACE(SenderVerificationNum, '-', ''), 7), 1) IN ('1', '2', '3', '4') THEN 'P01'
  128. WHEN
  129. LEFT(RIGHT(REPLACE(SenderVerificationNum, '-', ''), 7), 1) IN ('5', '6') THEN 'P04'
  130. ELSE '99'
  131. END
  132. ,[SenderVerificationNum] = replace(SenderVerificationNum,'-','')
  133. ,[SenderCountry] = CASE WHEN SenderVerificationType IN ('Alien Registration Card','National ID') AND LEFT(RIGHT(REPLACE(SenderVerificationNum, '-', ''), 7), 1) IN ('1', '2', '3', '4') THEN 'KR' ELSE countryCode END
  134. ,[SumUSDAmt],[customerUniqueno],receiverName,[BeneficiaryCountry]
  135. FROM(
  136. SELECT * FROM #TEMP
  137. )X
  138. ORDER BY [customerUniqueno],[SerialNo], [TransferDel]